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