9 int save_commit_buffer = 1;
11 const char *commit_type = "commit";
13 static struct commit *check_commit(struct object *obj,
14 const unsigned char *sha1,
17 if (obj->type != OBJ_COMMIT) {
19 error("Object %s is a %s, not a commit",
20 sha1_to_hex(sha1), typename(obj->type));
23 return (struct commit *) obj;
26 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
29 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
33 return check_commit(obj, sha1, quiet);
36 struct commit *lookup_commit_reference(const unsigned char *sha1)
38 return lookup_commit_reference_gently(sha1, 0);
41 struct commit *lookup_commit(const unsigned char *sha1)
43 struct object *obj = lookup_object(sha1);
45 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
47 obj->type = OBJ_COMMIT;
48 return check_commit(obj, sha1, 0);
51 static unsigned long parse_commit_date(const char *buf, const char *tail)
58 if (memcmp(buf, "author", 6))
60 while (buf < tail && *buf++ != '\n')
64 if (memcmp(buf, "committer", 9))
66 while (buf < tail && *buf++ != '>')
71 while (buf < tail && *buf++ != '\n')
75 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
76 date = strtoul(dateptr, NULL, 10);
77 if (date == ULONG_MAX)
82 static struct commit_graft **commit_graft;
83 static int commit_graft_alloc, commit_graft_nr;
85 static int commit_graft_pos(const unsigned char *sha1)
91 int mi = (lo + hi) / 2;
92 struct commit_graft *graft = commit_graft[mi];
93 int cmp = hashcmp(sha1, graft->sha1);
104 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
106 int pos = commit_graft_pos(graft->sha1);
112 free(commit_graft[pos]);
113 commit_graft[pos] = graft;
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) *
124 if (pos < commit_graft_nr)
125 memmove(commit_graft + pos + 1,
127 (commit_graft_nr - pos - 1) *
128 sizeof(*commit_graft));
129 commit_graft[pos] = graft;
133 struct commit_graft *read_graft_line(char *buf, int len)
135 /* The format is just "Commit Parent1 Parent2 ...\n" */
137 struct commit_graft *graft = NULL;
139 if (buf[len-1] == '\n')
141 if (buf[0] == '#' || buf[0] == '\0')
143 if ((len + 1) % 41) {
145 error("bad graft data: %s", buf);
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))
154 for (i = 40; i < len; i += 41) {
157 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
163 static int read_graft_file(const char *graft_file)
165 FILE *fp = fopen(graft_file, "r");
169 while (fgets(buf, sizeof(buf), fp)) {
170 /* The format is just "Commit Parent1 Parent2 ...\n" */
171 int len = strlen(buf);
172 struct commit_graft *graft = read_graft_line(buf, len);
175 if (register_commit_graft(graft, 1))
176 error("duplicate graft data: %s", buf);
182 static void prepare_commit_graft(void)
184 static int commit_graft_prepared;
187 if (commit_graft_prepared)
189 graft_file = get_graft_file();
190 read_graft_file(graft_file);
191 /* make sure shallows are read */
192 is_repository_shallow();
193 commit_graft_prepared = 1;
196 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
199 prepare_commit_graft();
200 pos = commit_graft_pos(sha1);
203 return commit_graft[pos];
206 int write_shallow_commits(int fd, int use_pack_protocol)
209 for (i = 0; i < commit_graft_nr; i++)
210 if (commit_graft[i]->nr_parent < 0) {
212 sha1_to_hex(commit_graft[i]->sha1);
214 if (use_pack_protocol)
215 packet_write(fd, "shallow %s", hex);
217 if (write_in_full(fd, hex, 40) != 40)
219 if (write_in_full(fd, "\n", 1) != 1)
226 int unregister_shallow(const unsigned char *sha1)
228 int pos = commit_graft_pos(sha1);
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));
239 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
242 char *bufptr = buffer;
243 unsigned char parent[20];
244 struct commit_list **pptr;
245 struct commit_graft *graft;
247 if (item->object.parsed)
249 item->object.parsed = 1;
251 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
252 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
253 if (get_sha1_hex(bufptr + 5, parent) < 0)
254 return error("bad tree pointer in commit %s",
255 sha1_to_hex(item->object.sha1));
256 item->tree = lookup_tree(parent);
257 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
258 pptr = &item->parents;
260 graft = lookup_commit_graft(item->object.sha1);
261 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
262 struct commit *new_parent;
264 if (tail <= bufptr + 48 ||
265 get_sha1_hex(bufptr + 7, parent) ||
267 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
270 * The clone is shallow if nr_parent < 0, and we must
271 * not traverse its real parents even when we unhide them.
273 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
275 new_parent = lookup_commit(parent);
277 pptr = &commit_list_insert(new_parent, pptr)->next;
281 struct commit *new_parent;
282 for (i = 0; i < graft->nr_parent; i++) {
283 new_parent = lookup_commit(graft->parent[i]);
286 pptr = &commit_list_insert(new_parent, pptr)->next;
289 item->date = parse_commit_date(bufptr, tail);
294 int parse_commit(struct commit *item)
296 enum object_type type;
303 if (item->object.parsed)
305 buffer = read_sha1_file(item->object.sha1, &type, &size);
307 return error("Could not read %s",
308 sha1_to_hex(item->object.sha1));
309 if (type != OBJ_COMMIT) {
311 return error("Object %s not a commit",
312 sha1_to_hex(item->object.sha1));
314 ret = parse_commit_buffer(item, buffer, size);
315 if (save_commit_buffer && !ret) {
316 item->buffer = buffer;
323 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
325 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
326 new_list->item = item;
327 new_list->next = *list_p;
332 unsigned commit_list_count(const struct commit_list *l)
335 for (; l; l = l->next )
340 void free_commit_list(struct commit_list *list)
343 struct commit_list *temp = list;
349 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
351 struct commit_list **pp = list;
352 struct commit_list *p;
353 while ((p = *pp) != NULL) {
354 if (p->item->date < item->date) {
359 return commit_list_insert(item, pp);
363 void sort_by_date(struct commit_list **list)
365 struct commit_list *ret = NULL;
367 insert_by_date((*list)->item, &ret);
368 *list = (*list)->next;
373 struct commit *pop_most_recent_commit(struct commit_list **list,
376 struct commit *ret = (*list)->item;
377 struct commit_list *parents = ret->parents;
378 struct commit_list *old = *list;
380 *list = (*list)->next;
384 struct commit *commit = parents->item;
385 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
386 commit->object.flags |= mark;
387 insert_by_date(commit, list);
389 parents = parents->next;
394 void clear_commit_marks(struct commit *commit, unsigned int mark)
397 struct commit_list *parents;
399 if (!(mark & commit->object.flags))
402 commit->object.flags &= ~mark;
404 parents = commit->parents;
408 while ((parents = parents->next))
409 clear_commit_marks(parents->item, mark);
411 commit = commit->parents->item;
415 struct commit *pop_commit(struct commit_list **stack)
417 struct commit_list *top = *stack;
418 struct commit *item = top ? top->item : NULL;
428 * Performs an in-place topological sort on the list supplied.
430 void sort_in_topological_order(struct commit_list ** list, int lifo)
432 struct commit_list *next, *orig = *list;
433 struct commit_list *work, **insert;
434 struct commit_list **pptr;
440 /* Mark them and clear the indegree */
441 for (next = orig; next; next = next->next) {
442 struct commit *commit = next->item;
443 commit->indegree = 1;
446 /* update the indegree */
447 for (next = orig; next; next = next->next) {
448 struct commit_list * parents = next->item->parents;
450 struct commit *parent = parents->item;
452 if (parent->indegree)
454 parents = parents->next;
461 * tips are nodes not reachable from any other node in the list
463 * the tips serve as a starting set for the work queue.
467 for (next = orig; next; next = next->next) {
468 struct commit *commit = next->item;
470 if (commit->indegree == 1)
471 insert = &commit_list_insert(commit, insert)->next;
474 /* process the list in topological order */
481 struct commit *commit;
482 struct commit_list *parents, *work_item;
485 work = work_item->next;
486 work_item->next = NULL;
488 commit = work_item->item;
489 for (parents = commit->parents; parents ; parents = parents->next) {
490 struct commit *parent=parents->item;
492 if (!parent->indegree)
496 * parents are only enqueued for emission
497 * when all their children have been emitted thereby
498 * guaranteeing topological order.
500 if (--parent->indegree == 1) {
502 insert_by_date(parent, &work);
504 commit_list_insert(parent, &work);
508 * work_item is a commit all of whose children
509 * have already been emitted. we can emit it now.
511 commit->indegree = 0;
513 pptr = &work_item->next;
517 /* merge-base stuff */
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)
525 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
527 static struct commit *interesting(struct commit_list *list)
530 struct commit *commit = list->item;
532 if (commit->object.flags & STALE)
539 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
541 struct commit_list *list = NULL;
542 struct commit_list *result = NULL;
545 for (i = 0; i < n; i++) {
548 * We do not mark this even with RESULT so we do not
549 * have to clean it up.
551 return commit_list_insert(one, &result);
554 if (parse_commit(one))
556 for (i = 0; i < n; i++) {
557 if (parse_commit(twos[i]))
561 one->object.flags |= PARENT1;
562 insert_by_date(one, &list);
563 for (i = 0; i < n; i++) {
564 twos[i]->object.flags |= PARENT2;
565 insert_by_date(twos[i], &list);
568 while (interesting(list)) {
569 struct commit *commit;
570 struct commit_list *parents;
571 struct commit_list *n;
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);
585 /* Mark parents of a found merge stale */
588 parents = commit->parents;
590 struct commit *p = parents->item;
591 parents = parents->next;
592 if ((p->object.flags & flags) == flags)
596 p->object.flags |= flags;
597 insert_by_date(p, &list);
601 /* Clean up the result to remove stale ones */
602 free_commit_list(list);
603 list = result; result = NULL;
605 struct commit_list *n = list->next;
606 if (!(list->item->object.flags & STALE))
607 insert_by_date(list->item, &result);
614 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
616 struct commit_list *i, *j, *k, *ret = NULL;
617 struct commit_list **pptr = &ret;
619 for (i = in; i; i = i->next) {
621 pptr = &commit_list_insert(i->item, pptr)->next;
623 struct commit_list *new = NULL, *end = NULL;
625 for (j = ret; j; j = j->next) {
626 struct commit_list *bases;
627 bases = get_merge_bases(i->item, j->item, 1);
632 for (k = bases; k; k = k->next)
641 struct commit_list *get_merge_bases_many(struct commit *one,
643 struct commit **twos,
646 struct commit_list *list;
647 struct commit **rslt;
648 struct commit_list *result;
651 result = merge_bases_many(one, n, twos);
652 for (i = 0; i < n; i++) {
656 if (!result || !result->next) {
658 clear_commit_marks(one, all_flags);
659 for (i = 0; i < n; i++)
660 clear_commit_marks(twos[i], all_flags);
665 /* There are more than one */
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);
677 clear_commit_marks(one, all_flags);
678 for (i = 0; i < n; i++)
679 clear_commit_marks(twos[i], all_flags);
680 for (i = 0; i < cnt - 1; i++) {
681 for (j = i+1; j < cnt; j++) {
682 if (!rslt[i] || !rslt[j])
684 result = merge_bases_many(rslt[i], 1, &rslt[j]);
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)
690 if (rslt[j] == list->item)
696 /* Surviving ones in rslt[] are the independent results */
698 for (i = 0; i < cnt; i++) {
700 insert_by_date(rslt[i], &result);
706 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
709 return get_merge_bases_many(one, 1, &two, cleanup);
712 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
716 while (with_commit) {
717 struct commit *other;
719 other = with_commit->item;
720 with_commit = with_commit->next;
721 if (in_merge_bases(other, &commit, 1))
727 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
729 struct commit_list *bases, *b;
733 bases = get_merge_bases(commit, *reference, 1);
736 for (b = bases; b; b = b->next) {
737 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
743 free_commit_list(bases);
747 struct commit_list *reduce_heads(struct commit_list *heads)
749 struct commit_list *p;
750 struct commit_list *result = NULL, **tail = &result;
751 struct commit **other;
752 size_t num_head, num_other;
757 /* Avoid unnecessary reallocations */
758 for (p = heads, num_head = 0; p; p = p->next)
760 other = xcalloc(sizeof(*other), num_head);
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;
766 /* Do we already have this in the result? */
767 for (q = result; q; q = q->next)
768 if (p->item == q->item)
774 for (q = heads; q; q = q->next) {
775 if (p->item == q->item)
777 other[num_other++] = q->item;
780 base = get_merge_bases_many(p->item, num_other, other, 1);
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.
791 if (!base || (base->item != p->item))
792 tail = &(commit_list_insert(p->item, tail)->next);
793 free_commit_list(base);