Commit | Line | Data |
---|---|---|
8f1d2e6f | 1 | #include "cache.h" |
961784ee | 2 | #include "tag.h" |
175785e5 | 3 | #include "commit.h" |
ed09aef0 | 4 | #include "pkt-line.h" |
52883fbd | 5 | #include "utf8.h" |
199c45bf JH |
6 | #include "diff.h" |
7 | #include "revision.h" | |
a97a7468 | 8 | #include "notes.h" |
175785e5 | 9 | |
60ab26de LT |
10 | int save_commit_buffer = 1; |
11 | ||
175785e5 DB |
12 | const char *commit_type = "commit"; |
13 | ||
f76412ed JH |
14 | static struct commit *check_commit(struct object *obj, |
15 | const unsigned char *sha1, | |
16 | int quiet) | |
961784ee | 17 | { |
1974632c | 18 | if (obj->type != OBJ_COMMIT) { |
f76412ed JH |
19 | if (!quiet) |
20 | error("Object %s is a %s, not a commit", | |
885a86ab | 21 | sha1_to_hex(sha1), typename(obj->type)); |
961784ee LT |
22 | return NULL; |
23 | } | |
24 | return (struct commit *) obj; | |
25 | } | |
26 | ||
f76412ed JH |
27 | struct commit *lookup_commit_reference_gently(const unsigned char *sha1, |
28 | int quiet) | |
961784ee | 29 | { |
9534f40b | 30 | struct object *obj = deref_tag(parse_object(sha1), NULL, 0); |
961784ee LT |
31 | |
32 | if (!obj) | |
33 | return NULL; | |
f76412ed JH |
34 | return check_commit(obj, sha1, quiet); |
35 | } | |
36 | ||
37 | struct commit *lookup_commit_reference(const unsigned char *sha1) | |
38 | { | |
39 | return lookup_commit_reference_gently(sha1, 0); | |
961784ee LT |
40 | } |
41 | ||
baf18fc2 NTND |
42 | struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name) |
43 | { | |
44 | struct commit *c = lookup_commit_reference(sha1); | |
45 | if (!c) | |
46 | die(_("could not parse %s"), ref_name); | |
47 | if (hashcmp(sha1, c->object.sha1)) { | |
48 | warning(_("%s %s is not a commit!"), | |
49 | ref_name, sha1_to_hex(sha1)); | |
50 | } | |
51 | return c; | |
52 | } | |
53 | ||
5d6ccf5c | 54 | struct commit *lookup_commit(const unsigned char *sha1) |
175785e5 DB |
55 | { |
56 | struct object *obj = lookup_object(sha1); | |
100c5f3b LT |
57 | if (!obj) |
58 | return create_object(sha1, OBJ_COMMIT, alloc_commit_node()); | |
d1af002d | 59 | if (!obj->type) |
1974632c | 60 | obj->type = OBJ_COMMIT; |
f76412ed | 61 | return check_commit(obj, sha1, 0); |
175785e5 DB |
62 | } |
63 | ||
a6fa5992 PN |
64 | struct commit *lookup_commit_reference_by_name(const char *name) |
65 | { | |
66 | unsigned char sha1[20]; | |
67 | struct commit *commit; | |
68 | ||
69 | if (get_sha1(name, sha1)) | |
70 | return NULL; | |
71 | commit = lookup_commit_reference(sha1); | |
72 | if (!commit || parse_commit(commit)) | |
73 | return NULL; | |
74 | return commit; | |
75 | } | |
76 | ||
0a617799 | 77 | static unsigned long parse_commit_date(const char *buf, const char *tail) |
175785e5 | 78 | { |
0a617799 | 79 | const char *dateptr; |
175785e5 | 80 | |
0a617799 MK |
81 | if (buf + 6 >= tail) |
82 | return 0; | |
175785e5 DB |
83 | if (memcmp(buf, "author", 6)) |
84 | return 0; | |
0a617799 | 85 | while (buf < tail && *buf++ != '\n') |
175785e5 | 86 | /* nada */; |
0a617799 MK |
87 | if (buf + 9 >= tail) |
88 | return 0; | |
175785e5 DB |
89 | if (memcmp(buf, "committer", 9)) |
90 | return 0; | |
0a617799 | 91 | while (buf < tail && *buf++ != '>') |
175785e5 | 92 | /* nada */; |
0a617799 MK |
93 | if (buf >= tail) |
94 | return 0; | |
95 | dateptr = buf; | |
96 | while (buf < tail && *buf++ != '\n') | |
97 | /* nada */; | |
98 | if (buf >= tail) | |
99 | return 0; | |
100 | /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */ | |
f2909743 | 101 | return strtoul(dateptr, NULL, 10); |
175785e5 DB |
102 | } |
103 | ||
5040f17e | 104 | static struct commit_graft **commit_graft; |
5da5c8f4 JH |
105 | static int commit_graft_alloc, commit_graft_nr; |
106 | ||
107 | static int commit_graft_pos(const unsigned char *sha1) | |
108 | { | |
109 | int lo, hi; | |
110 | lo = 0; | |
111 | hi = commit_graft_nr; | |
112 | while (lo < hi) { | |
113 | int mi = (lo + hi) / 2; | |
114 | struct commit_graft *graft = commit_graft[mi]; | |
a89fccd2 | 115 | int cmp = hashcmp(sha1, graft->sha1); |
5da5c8f4 JH |
116 | if (!cmp) |
117 | return mi; | |
118 | if (cmp < 0) | |
119 | hi = mi; | |
120 | else | |
121 | lo = mi + 1; | |
122 | } | |
123 | return -lo - 1; | |
124 | } | |
125 | ||
5040f17e JH |
126 | int register_commit_graft(struct commit_graft *graft, int ignore_dups) |
127 | { | |
128 | int pos = commit_graft_pos(graft->sha1); | |
a6080a0a | 129 | |
5040f17e JH |
130 | if (0 <= pos) { |
131 | if (ignore_dups) | |
132 | free(graft); | |
133 | else { | |
134 | free(commit_graft[pos]); | |
135 | commit_graft[pos] = graft; | |
136 | } | |
137 | return 1; | |
138 | } | |
139 | pos = -pos - 1; | |
140 | if (commit_graft_alloc <= ++commit_graft_nr) { | |
141 | commit_graft_alloc = alloc_nr(commit_graft_alloc); | |
142 | commit_graft = xrealloc(commit_graft, | |
143 | sizeof(*commit_graft) * | |
144 | commit_graft_alloc); | |
145 | } | |
146 | if (pos < commit_graft_nr) | |
147 | memmove(commit_graft + pos + 1, | |
148 | commit_graft + pos, | |
149 | (commit_graft_nr - pos - 1) * | |
150 | sizeof(*commit_graft)); | |
151 | commit_graft[pos] = graft; | |
152 | return 0; | |
153 | } | |
154 | ||
155 | struct commit_graft *read_graft_line(char *buf, int len) | |
156 | { | |
157 | /* The format is just "Commit Parent1 Parent2 ...\n" */ | |
158 | int i; | |
159 | struct commit_graft *graft = NULL; | |
160 | ||
fe0a3cb2 JH |
161 | while (len && isspace(buf[len-1])) |
162 | buf[--len] = '\0'; | |
360204c3 | 163 | if (buf[0] == '#' || buf[0] == '\0') |
5bc4ce58 | 164 | return NULL; |
df5d43be RT |
165 | if ((len + 1) % 41) |
166 | goto bad_graft_data; | |
5040f17e JH |
167 | i = (len + 1) / 41 - 1; |
168 | graft = xmalloc(sizeof(*graft) + 20 * i); | |
169 | graft->nr_parent = i; | |
170 | if (get_sha1_hex(buf, graft->sha1)) | |
171 | goto bad_graft_data; | |
172 | for (i = 40; i < len; i += 41) { | |
173 | if (buf[i] != ' ') | |
174 | goto bad_graft_data; | |
175 | if (get_sha1_hex(buf + i + 1, graft->parent[i/41])) | |
176 | goto bad_graft_data; | |
177 | } | |
178 | return graft; | |
df5d43be RT |
179 | |
180 | bad_graft_data: | |
181 | error("bad graft data: %s", buf); | |
182 | free(graft); | |
183 | return NULL; | |
5040f17e JH |
184 | } |
185 | ||
c5ae6439 | 186 | static int read_graft_file(const char *graft_file) |
5da5c8f4 | 187 | { |
5da5c8f4 JH |
188 | FILE *fp = fopen(graft_file, "r"); |
189 | char buf[1024]; | |
5040f17e JH |
190 | if (!fp) |
191 | return -1; | |
5da5c8f4 JH |
192 | while (fgets(buf, sizeof(buf), fp)) { |
193 | /* The format is just "Commit Parent1 Parent2 ...\n" */ | |
194 | int len = strlen(buf); | |
5040f17e | 195 | struct commit_graft *graft = read_graft_line(buf, len); |
5bc4ce58 JH |
196 | if (!graft) |
197 | continue; | |
5040f17e | 198 | if (register_commit_graft(graft, 1)) |
5da5c8f4 | 199 | error("duplicate graft data: %s", buf); |
5da5c8f4 JH |
200 | } |
201 | fclose(fp); | |
5040f17e JH |
202 | return 0; |
203 | } | |
204 | ||
205 | static void prepare_commit_graft(void) | |
206 | { | |
207 | static int commit_graft_prepared; | |
208 | char *graft_file; | |
209 | ||
210 | if (commit_graft_prepared) | |
211 | return; | |
212 | graft_file = get_graft_file(); | |
213 | read_graft_file(graft_file); | |
ed09aef0 JS |
214 | /* make sure shallows are read */ |
215 | is_repository_shallow(); | |
5040f17e | 216 | commit_graft_prepared = 1; |
5da5c8f4 JH |
217 | } |
218 | ||
45163382 | 219 | struct commit_graft *lookup_commit_graft(const unsigned char *sha1) |
5da5c8f4 JH |
220 | { |
221 | int pos; | |
5040f17e | 222 | prepare_commit_graft(); |
5da5c8f4 JH |
223 | pos = commit_graft_pos(sha1); |
224 | if (pos < 0) | |
225 | return NULL; | |
226 | return commit_graft[pos]; | |
227 | } | |
228 | ||
edace6f0 | 229 | int write_shallow_commits(struct strbuf *out, int use_pack_protocol) |
ed09aef0 JS |
230 | { |
231 | int i, count = 0; | |
232 | for (i = 0; i < commit_graft_nr; i++) | |
233 | if (commit_graft[i]->nr_parent < 0) { | |
234 | const char *hex = | |
235 | sha1_to_hex(commit_graft[i]->sha1); | |
236 | count++; | |
237 | if (use_pack_protocol) | |
edace6f0 | 238 | packet_buf_write(out, "shallow %s", hex); |
ed09aef0 | 239 | else { |
edace6f0 SP |
240 | strbuf_addstr(out, hex); |
241 | strbuf_addch(out, '\n'); | |
ed09aef0 JS |
242 | } |
243 | } | |
244 | return count; | |
245 | } | |
246 | ||
f53514bc JS |
247 | int unregister_shallow(const unsigned char *sha1) |
248 | { | |
249 | int pos = commit_graft_pos(sha1); | |
250 | if (pos < 0) | |
251 | return -1; | |
252 | if (pos + 1 < commit_graft_nr) | |
65d41d48 | 253 | memmove(commit_graft + pos, commit_graft + pos + 1, |
f53514bc JS |
254 | sizeof(struct commit_graft *) |
255 | * (commit_graft_nr - pos - 1)); | |
256 | commit_graft_nr--; | |
257 | return 0; | |
258 | } | |
259 | ||
cf7b1cad | 260 | int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size) |
175785e5 | 261 | { |
cf7b1cad NTND |
262 | const char *tail = buffer; |
263 | const char *bufptr = buffer; | |
175785e5 | 264 | unsigned char parent[20]; |
6c88be16 | 265 | struct commit_list **pptr; |
5da5c8f4 | 266 | struct commit_graft *graft; |
bd2c39f5 | 267 | |
175785e5 DB |
268 | if (item->object.parsed) |
269 | return 0; | |
270 | item->object.parsed = 1; | |
3b44f15a | 271 | tail += size; |
0a617799 | 272 | if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n') |
f7cc77d7 | 273 | return error("bogus commit object %s", sha1_to_hex(item->object.sha1)); |
0a617799 | 274 | if (get_sha1_hex(bufptr + 5, parent) < 0) |
bd2afde8 JH |
275 | return error("bad tree pointer in commit %s", |
276 | sha1_to_hex(item->object.sha1)); | |
175785e5 | 277 | item->tree = lookup_tree(parent); |
175785e5 | 278 | bufptr += 46; /* "tree " + "hex sha1" + "\n" */ |
6c88be16 | 279 | pptr = &item->parents; |
5da5c8f4 JH |
280 | |
281 | graft = lookup_commit_graft(item->object.sha1); | |
3b44f15a | 282 | while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) { |
f7cc77d7 LT |
283 | struct commit *new_parent; |
284 | ||
3b44f15a JH |
285 | if (tail <= bufptr + 48 || |
286 | get_sha1_hex(bufptr + 7, parent) || | |
287 | bufptr[47] != '\n') | |
f7cc77d7 | 288 | return error("bad parents in commit %s", sha1_to_hex(item->object.sha1)); |
5da5c8f4 | 289 | bufptr += 48; |
7f3140cd JS |
290 | /* |
291 | * The clone is shallow if nr_parent < 0, and we must | |
292 | * not traverse its real parents even when we unhide them. | |
293 | */ | |
294 | if (graft && (graft->nr_parent < 0 || grafts_replace_parents)) | |
5da5c8f4 | 295 | continue; |
f7cc77d7 | 296 | new_parent = lookup_commit(parent); |
39468def | 297 | if (new_parent) |
6c88be16 | 298 | pptr = &commit_list_insert(new_parent, pptr)->next; |
5da5c8f4 JH |
299 | } |
300 | if (graft) { | |
301 | int i; | |
302 | struct commit *new_parent; | |
303 | for (i = 0; i < graft->nr_parent; i++) { | |
304 | new_parent = lookup_commit(graft->parent[i]); | |
305 | if (!new_parent) | |
306 | continue; | |
307 | pptr = &commit_list_insert(new_parent, pptr)->next; | |
5da5c8f4 | 308 | } |
175785e5 | 309 | } |
0a617799 | 310 | item->date = parse_commit_date(bufptr, tail); |
27dedf0c | 311 | |
175785e5 DB |
312 | return 0; |
313 | } | |
314 | ||
bd2c39f5 NP |
315 | int parse_commit(struct commit *item) |
316 | { | |
21666f1a | 317 | enum object_type type; |
bd2c39f5 NP |
318 | void *buffer; |
319 | unsigned long size; | |
320 | int ret; | |
321 | ||
9786f68b MK |
322 | if (!item) |
323 | return -1; | |
bd2c39f5 NP |
324 | if (item->object.parsed) |
325 | return 0; | |
21666f1a | 326 | buffer = read_sha1_file(item->object.sha1, &type, &size); |
bd2c39f5 NP |
327 | if (!buffer) |
328 | return error("Could not read %s", | |
329 | sha1_to_hex(item->object.sha1)); | |
21666f1a | 330 | if (type != OBJ_COMMIT) { |
bd2c39f5 NP |
331 | free(buffer); |
332 | return error("Object %s not a commit", | |
333 | sha1_to_hex(item->object.sha1)); | |
334 | } | |
335 | ret = parse_commit_buffer(item, buffer, size); | |
60ab26de | 336 | if (save_commit_buffer && !ret) { |
3ff1fbbb LT |
337 | item->buffer = buffer; |
338 | return 0; | |
339 | } | |
bd2c39f5 NP |
340 | free(buffer); |
341 | return ret; | |
342 | } | |
343 | ||
11af2aae CC |
344 | int find_commit_subject(const char *commit_buffer, const char **subject) |
345 | { | |
346 | const char *eol; | |
347 | const char *p = commit_buffer; | |
348 | ||
349 | while (*p && (*p != '\n' || p[1] != '\n')) | |
350 | p++; | |
351 | if (*p) { | |
352 | p += 2; | |
353 | for (eol = p; *eol && *eol != '\n'; eol++) | |
354 | ; /* do nothing */ | |
355 | } else | |
356 | eol = p; | |
357 | ||
358 | *subject = p; | |
359 | ||
360 | return eol - p; | |
361 | } | |
362 | ||
ac5155ef | 363 | struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p) |
dd97f850 | 364 | { |
812666c8 | 365 | struct commit_list *new_list = xmalloc(sizeof(struct commit_list)); |
dd97f850 DB |
366 | new_list->item = item; |
367 | new_list->next = *list_p; | |
368 | *list_p = new_list; | |
ac5155ef | 369 | return new_list; |
dd97f850 DB |
370 | } |
371 | ||
65319475 MV |
372 | unsigned commit_list_count(const struct commit_list *l) |
373 | { | |
374 | unsigned c = 0; | |
375 | for (; l; l = l->next ) | |
376 | c++; | |
377 | return c; | |
378 | } | |
379 | ||
175785e5 DB |
380 | void free_commit_list(struct commit_list *list) |
381 | { | |
382 | while (list) { | |
383 | struct commit_list *temp = list; | |
384 | list = temp->next; | |
385 | free(temp); | |
386 | } | |
387 | } | |
dd97f850 | 388 | |
47e44ed1 | 389 | struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list) |
dd97f850 DB |
390 | { |
391 | struct commit_list **pp = list; | |
392 | struct commit_list *p; | |
393 | while ((p = *pp) != NULL) { | |
394 | if (p->item->date < item->date) { | |
395 | break; | |
396 | } | |
397 | pp = &p->next; | |
398 | } | |
f755494c | 399 | return commit_list_insert(item, pp); |
dd97f850 DB |
400 | } |
401 | ||
a6080a0a | 402 | |
47e44ed1 | 403 | void commit_list_sort_by_date(struct commit_list **list) |
dd97f850 DB |
404 | { |
405 | struct commit_list *ret = NULL; | |
406 | while (*list) { | |
47e44ed1 | 407 | commit_list_insert_by_date((*list)->item, &ret); |
dd97f850 DB |
408 | *list = (*list)->next; |
409 | } | |
410 | *list = ret; | |
411 | } | |
412 | ||
58e28af6 DB |
413 | struct commit *pop_most_recent_commit(struct commit_list **list, |
414 | unsigned int mark) | |
dd97f850 DB |
415 | { |
416 | struct commit *ret = (*list)->item; | |
417 | struct commit_list *parents = ret->parents; | |
418 | struct commit_list *old = *list; | |
419 | ||
420 | *list = (*list)->next; | |
421 | free(old); | |
422 | ||
423 | while (parents) { | |
4056c091 | 424 | struct commit *commit = parents->item; |
dec38c81 | 425 | if (!parse_commit(commit) && !(commit->object.flags & mark)) { |
58e28af6 | 426 | commit->object.flags |= mark; |
47e44ed1 | 427 | commit_list_insert_by_date(commit, list); |
4056c091 | 428 | } |
dd97f850 DB |
429 | parents = parents->next; |
430 | } | |
431 | return ret; | |
432 | } | |
e3bc7a3b | 433 | |
f8f9c73c JH |
434 | void clear_commit_marks(struct commit *commit, unsigned int mark) |
435 | { | |
60fcc2e6 JS |
436 | while (commit) { |
437 | struct commit_list *parents; | |
f8f9c73c | 438 | |
60fcc2e6 JS |
439 | if (!(mark & commit->object.flags)) |
440 | return; | |
58ecf5c1 | 441 | |
60fcc2e6 JS |
442 | commit->object.flags &= ~mark; |
443 | ||
444 | parents = commit->parents; | |
445 | if (!parents) | |
446 | return; | |
447 | ||
448 | while ((parents = parents->next)) | |
449 | clear_commit_marks(parents->item, mark); | |
450 | ||
451 | commit = commit->parents->item; | |
f8f9c73c JH |
452 | } |
453 | } | |
454 | ||
a3437b8c JS |
455 | struct commit *pop_commit(struct commit_list **stack) |
456 | { | |
457 | struct commit_list *top = *stack; | |
458 | struct commit *item = top ? top->item : NULL; | |
459 | ||
460 | if (top) { | |
461 | *stack = top->next; | |
462 | free(top); | |
463 | } | |
464 | return item; | |
465 | } | |
466 | ||
ab580ace JS |
467 | /* |
468 | * Performs an in-place topological sort on the list supplied. | |
469 | */ | |
4c8725f1 | 470 | void sort_in_topological_order(struct commit_list ** list, int lifo) |
6b6dcfc2 | 471 | { |
23c17d4a LT |
472 | struct commit_list *next, *orig = *list; |
473 | struct commit_list *work, **insert; | |
474 | struct commit_list **pptr; | |
a6080a0a | 475 | |
23c17d4a | 476 | if (!orig) |
7d6fb370 | 477 | return; |
23c17d4a LT |
478 | *list = NULL; |
479 | ||
480 | /* Mark them and clear the indegree */ | |
481 | for (next = orig; next; next = next->next) { | |
482 | struct commit *commit = next->item; | |
e358f3c3 | 483 | commit->indegree = 1; |
ab580ace | 484 | } |
23c17d4a | 485 | |
ab580ace | 486 | /* update the indegree */ |
23c17d4a | 487 | for (next = orig; next; next = next->next) { |
ab580ace JS |
488 | struct commit_list * parents = next->item->parents; |
489 | while (parents) { | |
23c17d4a | 490 | struct commit *parent = parents->item; |
6b6dcfc2 | 491 | |
e358f3c3 | 492 | if (parent->indegree) |
23c17d4a LT |
493 | parent->indegree++; |
494 | parents = parents->next; | |
ab580ace | 495 | } |
ab580ace | 496 | } |
23c17d4a | 497 | |
a6080a0a | 498 | /* |
674d1727 PH |
499 | * find the tips |
500 | * | |
501 | * tips are nodes not reachable from any other node in the list | |
502 | * | |
503 | * the tips serve as a starting set for the work queue. | |
504 | */ | |
23c17d4a | 505 | work = NULL; |
2ed02887 | 506 | insert = &work; |
23c17d4a LT |
507 | for (next = orig; next; next = next->next) { |
508 | struct commit *commit = next->item; | |
ab580ace | 509 | |
e358f3c3 | 510 | if (commit->indegree == 1) |
23c17d4a | 511 | insert = &commit_list_insert(commit, insert)->next; |
ab580ace | 512 | } |
4c8725f1 | 513 | |
ab580ace | 514 | /* process the list in topological order */ |
4c8725f1 | 515 | if (!lifo) |
47e44ed1 | 516 | commit_list_sort_by_date(&work); |
23c17d4a LT |
517 | |
518 | pptr = list; | |
519 | *list = NULL; | |
ab580ace | 520 | while (work) { |
23c17d4a LT |
521 | struct commit *commit; |
522 | struct commit_list *parents, *work_item; | |
ab580ace | 523 | |
23c17d4a LT |
524 | work_item = work; |
525 | work = work_item->next; | |
526 | work_item->next = NULL; | |
527 | ||
528 | commit = work_item->item; | |
529 | for (parents = commit->parents; parents ; parents = parents->next) { | |
530 | struct commit *parent=parents->item; | |
531 | ||
e358f3c3 | 532 | if (!parent->indegree) |
23c17d4a LT |
533 | continue; |
534 | ||
535 | /* | |
536 | * parents are only enqueued for emission | |
537 | * when all their children have been emitted thereby | |
538 | * guaranteeing topological order. | |
539 | */ | |
e358f3c3 | 540 | if (--parent->indegree == 1) { |
23c17d4a | 541 | if (!lifo) |
47e44ed1 | 542 | commit_list_insert_by_date(parent, &work); |
23c17d4a LT |
543 | else |
544 | commit_list_insert(parent, &work); | |
ab580ace | 545 | } |
ab580ace JS |
546 | } |
547 | /* | |
674d1727 PH |
548 | * work_item is a commit all of whose children |
549 | * have already been emitted. we can emit it now. | |
550 | */ | |
e358f3c3 | 551 | commit->indegree = 0; |
23c17d4a LT |
552 | *pptr = work_item; |
553 | pptr = &work_item->next; | |
ab580ace | 554 | } |
ab580ace | 555 | } |
7c6f8aaf | 556 | |
1295228d | 557 | /* merge-base stuff */ |
7c6f8aaf | 558 | |
577ed5c2 JH |
559 | /* bits #0..15 in revision.h */ |
560 | #define PARENT1 (1u<<16) | |
561 | #define PARENT2 (1u<<17) | |
562 | #define STALE (1u<<18) | |
563 | #define RESULT (1u<<19) | |
7c6f8aaf | 564 | |
1295228d JH |
565 | static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT); |
566 | ||
7c6f8aaf JS |
567 | static struct commit *interesting(struct commit_list *list) |
568 | { | |
569 | while (list) { | |
570 | struct commit *commit = list->item; | |
571 | list = list->next; | |
542ccefe | 572 | if (commit->object.flags & STALE) |
7c6f8aaf JS |
573 | continue; |
574 | return commit; | |
575 | } | |
576 | return NULL; | |
577 | } | |
578 | ||
6a938648 | 579 | static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos) |
7c6f8aaf JS |
580 | { |
581 | struct commit_list *list = NULL; | |
582 | struct commit_list *result = NULL; | |
6a938648 | 583 | int i; |
7c6f8aaf | 584 | |
6a938648 JH |
585 | for (i = 0; i < n; i++) { |
586 | if (one == twos[i]) | |
587 | /* | |
588 | * We do not mark this even with RESULT so we do not | |
589 | * have to clean it up. | |
590 | */ | |
591 | return commit_list_insert(one, &result); | |
592 | } | |
7c6f8aaf | 593 | |
172947e6 MK |
594 | if (parse_commit(one)) |
595 | return NULL; | |
6a938648 JH |
596 | for (i = 0; i < n; i++) { |
597 | if (parse_commit(twos[i])) | |
598 | return NULL; | |
599 | } | |
7c6f8aaf | 600 | |
f3249438 | 601 | one->object.flags |= PARENT1; |
47e44ed1 | 602 | commit_list_insert_by_date(one, &list); |
6a938648 JH |
603 | for (i = 0; i < n; i++) { |
604 | twos[i]->object.flags |= PARENT2; | |
47e44ed1 | 605 | commit_list_insert_by_date(twos[i], &list); |
6a938648 | 606 | } |
7c6f8aaf JS |
607 | |
608 | while (interesting(list)) { | |
f3249438 | 609 | struct commit *commit; |
7c6f8aaf | 610 | struct commit_list *parents; |
f203d696 | 611 | struct commit_list *next; |
f3249438 | 612 | int flags; |
7c6f8aaf | 613 | |
f3249438 | 614 | commit = list->item; |
f203d696 | 615 | next = list->next; |
f3249438 | 616 | free(list); |
f203d696 | 617 | list = next; |
7c6f8aaf | 618 | |
f3249438 JH |
619 | flags = commit->object.flags & (PARENT1 | PARENT2 | STALE); |
620 | if (flags == (PARENT1 | PARENT2)) { | |
621 | if (!(commit->object.flags & RESULT)) { | |
622 | commit->object.flags |= RESULT; | |
47e44ed1 | 623 | commit_list_insert_by_date(commit, &result); |
f3249438 | 624 | } |
542ccefe JH |
625 | /* Mark parents of a found merge stale */ |
626 | flags |= STALE; | |
7c6f8aaf JS |
627 | } |
628 | parents = commit->parents; | |
629 | while (parents) { | |
630 | struct commit *p = parents->item; | |
631 | parents = parents->next; | |
632 | if ((p->object.flags & flags) == flags) | |
633 | continue; | |
172947e6 MK |
634 | if (parse_commit(p)) |
635 | return NULL; | |
7c6f8aaf | 636 | p->object.flags |= flags; |
47e44ed1 | 637 | commit_list_insert_by_date(p, &list); |
7c6f8aaf JS |
638 | } |
639 | } | |
640 | ||
f3249438 | 641 | /* Clean up the result to remove stale ones */ |
1295228d | 642 | free_commit_list(list); |
f3249438 JH |
643 | list = result; result = NULL; |
644 | while (list) { | |
f203d696 | 645 | struct commit_list *next = list->next; |
f3249438 | 646 | if (!(list->item->object.flags & STALE)) |
47e44ed1 | 647 | commit_list_insert_by_date(list->item, &result); |
f3249438 | 648 | free(list); |
f203d696 | 649 | list = next; |
f3249438 JH |
650 | } |
651 | return result; | |
652 | } | |
7c6f8aaf | 653 | |
5240c9d7 MV |
654 | struct commit_list *get_octopus_merge_bases(struct commit_list *in) |
655 | { | |
656 | struct commit_list *i, *j, *k, *ret = NULL; | |
657 | struct commit_list **pptr = &ret; | |
658 | ||
659 | for (i = in; i; i = i->next) { | |
660 | if (!ret) | |
661 | pptr = &commit_list_insert(i->item, pptr)->next; | |
662 | else { | |
663 | struct commit_list *new = NULL, *end = NULL; | |
664 | ||
665 | for (j = ret; j; j = j->next) { | |
666 | struct commit_list *bases; | |
667 | bases = get_merge_bases(i->item, j->item, 1); | |
668 | if (!new) | |
669 | new = bases; | |
670 | else | |
671 | end->next = bases; | |
672 | for (k = bases; k; k = k->next) | |
673 | end = k; | |
674 | } | |
675 | ret = new; | |
676 | } | |
677 | } | |
678 | return ret; | |
679 | } | |
680 | ||
6a938648 JH |
681 | struct commit_list *get_merge_bases_many(struct commit *one, |
682 | int n, | |
683 | struct commit **twos, | |
684 | int cleanup) | |
f3249438 | 685 | { |
f3249438 JH |
686 | struct commit_list *list; |
687 | struct commit **rslt; | |
688 | struct commit_list *result; | |
689 | int cnt, i, j; | |
690 | ||
6a938648 JH |
691 | result = merge_bases_many(one, n, twos); |
692 | for (i = 0; i < n; i++) { | |
693 | if (one == twos[i]) | |
694 | return result; | |
695 | } | |
f3249438 JH |
696 | if (!result || !result->next) { |
697 | if (cleanup) { | |
698 | clear_commit_marks(one, all_flags); | |
6a938648 JH |
699 | for (i = 0; i < n; i++) |
700 | clear_commit_marks(twos[i], all_flags); | |
7c6f8aaf | 701 | } |
f3249438 | 702 | return result; |
7c6f8aaf JS |
703 | } |
704 | ||
f3249438 JH |
705 | /* There are more than one */ |
706 | cnt = 0; | |
707 | list = result; | |
708 | while (list) { | |
709 | list = list->next; | |
710 | cnt++; | |
711 | } | |
712 | rslt = xcalloc(cnt, sizeof(*rslt)); | |
713 | for (list = result, i = 0; list; list = list->next) | |
714 | rslt[i++] = list->item; | |
715 | free_commit_list(result); | |
716 | ||
717 | clear_commit_marks(one, all_flags); | |
6a938648 JH |
718 | for (i = 0; i < n; i++) |
719 | clear_commit_marks(twos[i], all_flags); | |
f3249438 JH |
720 | for (i = 0; i < cnt - 1; i++) { |
721 | for (j = i+1; j < cnt; j++) { | |
722 | if (!rslt[i] || !rslt[j]) | |
723 | continue; | |
6a938648 | 724 | result = merge_bases_many(rslt[i], 1, &rslt[j]); |
f3249438 JH |
725 | clear_commit_marks(rslt[i], all_flags); |
726 | clear_commit_marks(rslt[j], all_flags); | |
727 | for (list = result; list; list = list->next) { | |
728 | if (rslt[i] == list->item) | |
729 | rslt[i] = NULL; | |
730 | if (rslt[j] == list->item) | |
731 | rslt[j] = NULL; | |
732 | } | |
733 | } | |
58ecf5c1 | 734 | } |
7c6f8aaf | 735 | |
f3249438 JH |
736 | /* Surviving ones in rslt[] are the independent results */ |
737 | result = NULL; | |
738 | for (i = 0; i < cnt; i++) { | |
739 | if (rslt[i]) | |
47e44ed1 | 740 | commit_list_insert_by_date(rslt[i], &result); |
f3249438 JH |
741 | } |
742 | free(rslt); | |
7c6f8aaf JS |
743 | return result; |
744 | } | |
2ecd2bbc | 745 | |
6a938648 JH |
746 | struct commit_list *get_merge_bases(struct commit *one, struct commit *two, |
747 | int cleanup) | |
748 | { | |
749 | return get_merge_bases_many(one, 1, &two, cleanup); | |
750 | } | |
751 | ||
7fcdb36e JG |
752 | int is_descendant_of(struct commit *commit, struct commit_list *with_commit) |
753 | { | |
754 | if (!with_commit) | |
755 | return 1; | |
756 | while (with_commit) { | |
757 | struct commit *other; | |
758 | ||
759 | other = with_commit->item; | |
760 | with_commit = with_commit->next; | |
761 | if (in_merge_bases(other, &commit, 1)) | |
762 | return 1; | |
763 | } | |
764 | return 0; | |
765 | } | |
766 | ||
03840fc3 | 767 | int in_merge_bases(struct commit *commit, struct commit **reference, int num) |
2ecd2bbc JH |
768 | { |
769 | struct commit_list *bases, *b; | |
770 | int ret = 0; | |
771 | ||
03840fc3 JH |
772 | if (num == 1) |
773 | bases = get_merge_bases(commit, *reference, 1); | |
774 | else | |
775 | die("not yet"); | |
2ecd2bbc | 776 | for (b = bases; b; b = b->next) { |
03840fc3 | 777 | if (!hashcmp(commit->object.sha1, b->item->object.sha1)) { |
2ecd2bbc JH |
778 | ret = 1; |
779 | break; | |
780 | } | |
781 | } | |
782 | ||
783 | free_commit_list(bases); | |
784 | return ret; | |
785 | } | |
98cf9c3b JH |
786 | |
787 | struct commit_list *reduce_heads(struct commit_list *heads) | |
788 | { | |
789 | struct commit_list *p; | |
790 | struct commit_list *result = NULL, **tail = &result; | |
791 | struct commit **other; | |
792 | size_t num_head, num_other; | |
793 | ||
794 | if (!heads) | |
795 | return NULL; | |
796 | ||
797 | /* Avoid unnecessary reallocations */ | |
798 | for (p = heads, num_head = 0; p; p = p->next) | |
799 | num_head++; | |
800 | other = xcalloc(sizeof(*other), num_head); | |
801 | ||
802 | /* For each commit, see if it can be reached by others */ | |
803 | for (p = heads; p; p = p->next) { | |
804 | struct commit_list *q, *base; | |
805 | ||
711f6b29 JH |
806 | /* Do we already have this in the result? */ |
807 | for (q = result; q; q = q->next) | |
808 | if (p->item == q->item) | |
809 | break; | |
810 | if (q) | |
811 | continue; | |
812 | ||
98cf9c3b JH |
813 | num_other = 0; |
814 | for (q = heads; q; q = q->next) { | |
3d1dd472 | 815 | if (p->item == q->item) |
98cf9c3b JH |
816 | continue; |
817 | other[num_other++] = q->item; | |
818 | } | |
711f6b29 | 819 | if (num_other) |
98cf9c3b | 820 | base = get_merge_bases_many(p->item, num_other, other, 1); |
711f6b29 | 821 | else |
98cf9c3b JH |
822 | base = NULL; |
823 | /* | |
824 | * If p->item does not have anything common with other | |
825 | * commits, there won't be any merge base. If it is | |
826 | * reachable from some of the others, p->item will be | |
827 | * the merge base. If its history is connected with | |
828 | * others, but p->item is not reachable by others, we | |
829 | * will get something other than p->item back. | |
830 | */ | |
831 | if (!base || (base->item != p->item)) | |
832 | tail = &(commit_list_insert(p->item, tail)->next); | |
833 | free_commit_list(base); | |
834 | } | |
835 | free(other); | |
836 | return result; | |
837 | } | |
40d52ff7 JK |
838 | |
839 | static const char commit_utf8_warn[] = | |
840 | "Warning: commit message does not conform to UTF-8.\n" | |
841 | "You may want to amend it after fixing the message, or set the config\n" | |
842 | "variable i18n.commitencoding to the encoding your project uses.\n"; | |
843 | ||
844 | int commit_tree(const char *msg, unsigned char *tree, | |
845 | struct commit_list *parents, unsigned char *ret, | |
846 | const char *author) | |
847 | { | |
848 | int result; | |
849 | int encoding_is_utf8; | |
850 | struct strbuf buffer; | |
851 | ||
852 | assert_sha1_type(tree, OBJ_TREE); | |
853 | ||
854 | /* Not having i18n.commitencoding is the same as having utf-8 */ | |
855 | encoding_is_utf8 = is_encoding_utf8(git_commit_encoding); | |
856 | ||
857 | strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */ | |
858 | strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree)); | |
859 | ||
860 | /* | |
861 | * NOTE! This ordering means that the same exact tree merged with a | |
862 | * different order of parents will be a _different_ changeset even | |
863 | * if everything else stays the same. | |
864 | */ | |
865 | while (parents) { | |
866 | struct commit_list *next = parents->next; | |
867 | strbuf_addf(&buffer, "parent %s\n", | |
868 | sha1_to_hex(parents->item->object.sha1)); | |
869 | free(parents); | |
870 | parents = next; | |
871 | } | |
872 | ||
873 | /* Person/date information */ | |
874 | if (!author) | |
875 | author = git_author_info(IDENT_ERROR_ON_NO_NAME); | |
876 | strbuf_addf(&buffer, "author %s\n", author); | |
877 | strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME)); | |
878 | if (!encoding_is_utf8) | |
879 | strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding); | |
880 | strbuf_addch(&buffer, '\n'); | |
881 | ||
882 | /* And add the comment */ | |
883 | strbuf_addstr(&buffer, msg); | |
884 | ||
885 | /* And check the encoding */ | |
886 | if (encoding_is_utf8 && !is_utf8(buffer.buf)) | |
887 | fprintf(stderr, commit_utf8_warn); | |
888 | ||
889 | result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret); | |
890 | strbuf_release(&buffer); | |
891 | return result; | |
892 | } |