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