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