10 int save_commit_buffer = 1;
12 const char *commit_type = "commit";
14 static struct commit *check_commit(struct object *obj,
15 const unsigned char *sha1,
18 if (obj->type != OBJ_COMMIT) {
20 error("Object %s is a %s, not a commit",
21 sha1_to_hex(sha1), typename(obj->type));
24 return (struct commit *) obj;
27 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
30 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
34 return check_commit(obj, sha1, quiet);
37 struct commit *lookup_commit_reference(const unsigned char *sha1)
39 return lookup_commit_reference_gently(sha1, 0);
42 struct commit *lookup_commit(const unsigned char *sha1)
44 struct object *obj = lookup_object(sha1);
46 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
48 obj->type = OBJ_COMMIT;
49 return check_commit(obj, sha1, 0);
52 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 return strtoul(dateptr, NULL, 10);
79 static struct commit_graft **commit_graft;
80 static int commit_graft_alloc, commit_graft_nr;
82 static int commit_graft_pos(const unsigned char *sha1)
88 int mi = (lo + hi) / 2;
89 struct commit_graft *graft = commit_graft[mi];
90 int cmp = hashcmp(sha1, graft->sha1);
101 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
103 int pos = commit_graft_pos(graft->sha1);
109 free(commit_graft[pos]);
110 commit_graft[pos] = graft;
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) *
121 if (pos < commit_graft_nr)
122 memmove(commit_graft + pos + 1,
124 (commit_graft_nr - pos - 1) *
125 sizeof(*commit_graft));
126 commit_graft[pos] = graft;
130 struct commit_graft *read_graft_line(char *buf, int len)
132 /* The format is just "Commit Parent1 Parent2 ...\n" */
134 struct commit_graft *graft = NULL;
136 while (len && isspace(buf[len-1]))
138 if (buf[0] == '#' || buf[0] == '\0')
140 if ((len + 1) % 41) {
142 error("bad graft data: %s", buf);
146 i = (len + 1) / 41 - 1;
147 graft = xmalloc(sizeof(*graft) + 20 * i);
148 graft->nr_parent = i;
149 if (get_sha1_hex(buf, graft->sha1))
151 for (i = 40; i < len; i += 41) {
154 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
160 static int read_graft_file(const char *graft_file)
162 FILE *fp = fopen(graft_file, "r");
166 while (fgets(buf, sizeof(buf), fp)) {
167 /* The format is just "Commit Parent1 Parent2 ...\n" */
168 int len = strlen(buf);
169 struct commit_graft *graft = read_graft_line(buf, len);
172 if (register_commit_graft(graft, 1))
173 error("duplicate graft data: %s", buf);
179 static void prepare_commit_graft(void)
181 static int commit_graft_prepared;
184 if (commit_graft_prepared)
186 graft_file = get_graft_file();
187 read_graft_file(graft_file);
188 /* make sure shallows are read */
189 is_repository_shallow();
190 commit_graft_prepared = 1;
193 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
196 prepare_commit_graft();
197 pos = commit_graft_pos(sha1);
200 return commit_graft[pos];
203 int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
206 for (i = 0; i < commit_graft_nr; i++)
207 if (commit_graft[i]->nr_parent < 0) {
209 sha1_to_hex(commit_graft[i]->sha1);
211 if (use_pack_protocol)
212 packet_buf_write(out, "shallow %s", hex);
214 strbuf_addstr(out, hex);
215 strbuf_addch(out, '\n');
221 int unregister_shallow(const unsigned char *sha1)
223 int pos = commit_graft_pos(sha1);
226 if (pos + 1 < commit_graft_nr)
227 memmove(commit_graft + pos, commit_graft + pos + 1,
228 sizeof(struct commit_graft *)
229 * (commit_graft_nr - pos - 1));
234 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
237 char *bufptr = buffer;
238 unsigned char parent[20];
239 struct commit_list **pptr;
240 struct commit_graft *graft;
242 if (item->object.parsed)
244 item->object.parsed = 1;
246 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
247 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
248 if (get_sha1_hex(bufptr + 5, parent) < 0)
249 return error("bad tree pointer in commit %s",
250 sha1_to_hex(item->object.sha1));
251 item->tree = lookup_tree(parent);
252 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
253 pptr = &item->parents;
255 graft = lookup_commit_graft(item->object.sha1);
256 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
257 struct commit *new_parent;
259 if (tail <= bufptr + 48 ||
260 get_sha1_hex(bufptr + 7, parent) ||
262 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
265 * The clone is shallow if nr_parent < 0, and we must
266 * not traverse its real parents even when we unhide them.
268 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
270 new_parent = lookup_commit(parent);
272 pptr = &commit_list_insert(new_parent, pptr)->next;
276 struct commit *new_parent;
277 for (i = 0; i < graft->nr_parent; i++) {
278 new_parent = lookup_commit(graft->parent[i]);
281 pptr = &commit_list_insert(new_parent, pptr)->next;
284 item->date = parse_commit_date(bufptr, tail);
289 int parse_commit(struct commit *item)
291 enum object_type type;
298 if (item->object.parsed)
300 buffer = read_sha1_file(item->object.sha1, &type, &size);
302 return error("Could not read %s",
303 sha1_to_hex(item->object.sha1));
304 if (type != OBJ_COMMIT) {
306 return error("Object %s not a commit",
307 sha1_to_hex(item->object.sha1));
309 ret = parse_commit_buffer(item, buffer, size);
310 if (save_commit_buffer && !ret) {
311 item->buffer = buffer;
318 int find_commit_subject(const char *commit_buffer, const char **subject)
321 const char *p = commit_buffer;
323 while (*p && (*p != '\n' || p[1] != '\n'))
327 for (eol = p; *eol && *eol != '\n'; eol++)
337 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
339 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
340 new_list->item = item;
341 new_list->next = *list_p;
346 unsigned commit_list_count(const struct commit_list *l)
349 for (; l; l = l->next )
354 void free_commit_list(struct commit_list *list)
357 struct commit_list *temp = list;
363 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
365 struct commit_list **pp = list;
366 struct commit_list *p;
367 while ((p = *pp) != NULL) {
368 if (p->item->date < item->date) {
373 return commit_list_insert(item, pp);
377 void sort_by_date(struct commit_list **list)
379 struct commit_list *ret = NULL;
381 insert_by_date((*list)->item, &ret);
382 *list = (*list)->next;
387 struct commit *pop_most_recent_commit(struct commit_list **list,
390 struct commit *ret = (*list)->item;
391 struct commit_list *parents = ret->parents;
392 struct commit_list *old = *list;
394 *list = (*list)->next;
398 struct commit *commit = parents->item;
399 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
400 commit->object.flags |= mark;
401 insert_by_date(commit, list);
403 parents = parents->next;
408 void clear_commit_marks(struct commit *commit, unsigned int mark)
411 struct commit_list *parents;
413 if (!(mark & commit->object.flags))
416 commit->object.flags &= ~mark;
418 parents = commit->parents;
422 while ((parents = parents->next))
423 clear_commit_marks(parents->item, mark);
425 commit = commit->parents->item;
429 struct commit *pop_commit(struct commit_list **stack)
431 struct commit_list *top = *stack;
432 struct commit *item = top ? top->item : NULL;
442 * Performs an in-place topological sort on the list supplied.
444 void sort_in_topological_order(struct commit_list ** list, int lifo)
446 struct commit_list *next, *orig = *list;
447 struct commit_list *work, **insert;
448 struct commit_list **pptr;
454 /* Mark them and clear the indegree */
455 for (next = orig; next; next = next->next) {
456 struct commit *commit = next->item;
457 commit->indegree = 1;
460 /* update the indegree */
461 for (next = orig; next; next = next->next) {
462 struct commit_list * parents = next->item->parents;
464 struct commit *parent = parents->item;
466 if (parent->indegree)
468 parents = parents->next;
475 * tips are nodes not reachable from any other node in the list
477 * the tips serve as a starting set for the work queue.
481 for (next = orig; next; next = next->next) {
482 struct commit *commit = next->item;
484 if (commit->indegree == 1)
485 insert = &commit_list_insert(commit, insert)->next;
488 /* process the list in topological order */
495 struct commit *commit;
496 struct commit_list *parents, *work_item;
499 work = work_item->next;
500 work_item->next = NULL;
502 commit = work_item->item;
503 for (parents = commit->parents; parents ; parents = parents->next) {
504 struct commit *parent=parents->item;
506 if (!parent->indegree)
510 * parents are only enqueued for emission
511 * when all their children have been emitted thereby
512 * guaranteeing topological order.
514 if (--parent->indegree == 1) {
516 insert_by_date(parent, &work);
518 commit_list_insert(parent, &work);
522 * work_item is a commit all of whose children
523 * have already been emitted. we can emit it now.
525 commit->indegree = 0;
527 pptr = &work_item->next;
531 /* merge-base stuff */
533 /* bits #0..15 in revision.h */
534 #define PARENT1 (1u<<16)
535 #define PARENT2 (1u<<17)
536 #define STALE (1u<<18)
537 #define RESULT (1u<<19)
539 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
541 static struct commit *interesting(struct commit_list *list)
544 struct commit *commit = list->item;
546 if (commit->object.flags & STALE)
553 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
555 struct commit_list *list = NULL;
556 struct commit_list *result = NULL;
559 for (i = 0; i < n; i++) {
562 * We do not mark this even with RESULT so we do not
563 * have to clean it up.
565 return commit_list_insert(one, &result);
568 if (parse_commit(one))
570 for (i = 0; i < n; i++) {
571 if (parse_commit(twos[i]))
575 one->object.flags |= PARENT1;
576 insert_by_date(one, &list);
577 for (i = 0; i < n; i++) {
578 twos[i]->object.flags |= PARENT2;
579 insert_by_date(twos[i], &list);
582 while (interesting(list)) {
583 struct commit *commit;
584 struct commit_list *parents;
585 struct commit_list *next;
593 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
594 if (flags == (PARENT1 | PARENT2)) {
595 if (!(commit->object.flags & RESULT)) {
596 commit->object.flags |= RESULT;
597 insert_by_date(commit, &result);
599 /* Mark parents of a found merge stale */
602 parents = commit->parents;
604 struct commit *p = parents->item;
605 parents = parents->next;
606 if ((p->object.flags & flags) == flags)
610 p->object.flags |= flags;
611 insert_by_date(p, &list);
615 /* Clean up the result to remove stale ones */
616 free_commit_list(list);
617 list = result; result = NULL;
619 struct commit_list *next = list->next;
620 if (!(list->item->object.flags & STALE))
621 insert_by_date(list->item, &result);
628 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
630 struct commit_list *i, *j, *k, *ret = NULL;
631 struct commit_list **pptr = &ret;
633 for (i = in; i; i = i->next) {
635 pptr = &commit_list_insert(i->item, pptr)->next;
637 struct commit_list *new = NULL, *end = NULL;
639 for (j = ret; j; j = j->next) {
640 struct commit_list *bases;
641 bases = get_merge_bases(i->item, j->item, 1);
646 for (k = bases; k; k = k->next)
655 struct commit_list *get_merge_bases_many(struct commit *one,
657 struct commit **twos,
660 struct commit_list *list;
661 struct commit **rslt;
662 struct commit_list *result;
665 result = merge_bases_many(one, n, twos);
666 for (i = 0; i < n; i++) {
670 if (!result || !result->next) {
672 clear_commit_marks(one, all_flags);
673 for (i = 0; i < n; i++)
674 clear_commit_marks(twos[i], all_flags);
679 /* There are more than one */
686 rslt = xcalloc(cnt, sizeof(*rslt));
687 for (list = result, i = 0; list; list = list->next)
688 rslt[i++] = list->item;
689 free_commit_list(result);
691 clear_commit_marks(one, all_flags);
692 for (i = 0; i < n; i++)
693 clear_commit_marks(twos[i], all_flags);
694 for (i = 0; i < cnt - 1; i++) {
695 for (j = i+1; j < cnt; j++) {
696 if (!rslt[i] || !rslt[j])
698 result = merge_bases_many(rslt[i], 1, &rslt[j]);
699 clear_commit_marks(rslt[i], all_flags);
700 clear_commit_marks(rslt[j], all_flags);
701 for (list = result; list; list = list->next) {
702 if (rslt[i] == list->item)
704 if (rslt[j] == list->item)
710 /* Surviving ones in rslt[] are the independent results */
712 for (i = 0; i < cnt; i++) {
714 insert_by_date(rslt[i], &result);
720 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
723 return get_merge_bases_many(one, 1, &two, cleanup);
726 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
730 while (with_commit) {
731 struct commit *other;
733 other = with_commit->item;
734 with_commit = with_commit->next;
735 if (in_merge_bases(other, &commit, 1))
741 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
743 struct commit_list *bases, *b;
747 bases = get_merge_bases(commit, *reference, 1);
750 for (b = bases; b; b = b->next) {
751 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
757 free_commit_list(bases);
761 struct commit_list *reduce_heads(struct commit_list *heads)
763 struct commit_list *p;
764 struct commit_list *result = NULL, **tail = &result;
765 struct commit **other;
766 size_t num_head, num_other;
771 /* Avoid unnecessary reallocations */
772 for (p = heads, num_head = 0; p; p = p->next)
774 other = xcalloc(sizeof(*other), num_head);
776 /* For each commit, see if it can be reached by others */
777 for (p = heads; p; p = p->next) {
778 struct commit_list *q, *base;
780 /* Do we already have this in the result? */
781 for (q = result; q; q = q->next)
782 if (p->item == q->item)
788 for (q = heads; q; q = q->next) {
789 if (p->item == q->item)
791 other[num_other++] = q->item;
794 base = get_merge_bases_many(p->item, num_other, other, 1);
798 * If p->item does not have anything common with other
799 * commits, there won't be any merge base. If it is
800 * reachable from some of the others, p->item will be
801 * the merge base. If its history is connected with
802 * others, but p->item is not reachable by others, we
803 * will get something other than p->item back.
805 if (!base || (base->item != p->item))
806 tail = &(commit_list_insert(p->item, tail)->next);
807 free_commit_list(base);
813 static const char commit_utf8_warn[] =
814 "Warning: commit message does not conform to UTF-8.\n"
815 "You may want to amend it after fixing the message, or set the config\n"
816 "variable i18n.commitencoding to the encoding your project uses.\n";
818 int commit_tree(const char *msg, unsigned char *tree,
819 struct commit_list *parents, unsigned char *ret,
823 int encoding_is_utf8;
824 struct strbuf buffer;
826 assert_sha1_type(tree, OBJ_TREE);
828 /* Not having i18n.commitencoding is the same as having utf-8 */
829 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
831 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
832 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
835 * NOTE! This ordering means that the same exact tree merged with a
836 * different order of parents will be a _different_ changeset even
837 * if everything else stays the same.
840 struct commit_list *next = parents->next;
841 strbuf_addf(&buffer, "parent %s\n",
842 sha1_to_hex(parents->item->object.sha1));
847 /* Person/date information */
849 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
850 strbuf_addf(&buffer, "author %s\n", author);
851 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
852 if (!encoding_is_utf8)
853 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
854 strbuf_addch(&buffer, '\n');
856 /* And add the comment */
857 strbuf_addstr(&buffer, msg);
859 /* And check the encoding */
860 if (encoding_is_utf8 && !is_utf8(buffer.buf))
861 fprintf(stderr, commit_utf8_warn);
863 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
864 strbuf_release(&buffer);