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 struct commit *lookup_commit_reference_by_name(const char *name)
54 unsigned char sha1[20];
55 struct commit *commit;
57 if (get_sha1(name, sha1))
59 commit = lookup_commit_reference(sha1);
60 if (!commit || parse_commit(commit))
65 static unsigned long parse_commit_date(const char *buf, const char *tail)
71 if (memcmp(buf, "author", 6))
73 while (buf < tail && *buf++ != '\n')
77 if (memcmp(buf, "committer", 9))
79 while (buf < tail && *buf++ != '>')
84 while (buf < tail && *buf++ != '\n')
88 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
89 return strtoul(dateptr, NULL, 10);
92 static struct commit_graft **commit_graft;
93 static int commit_graft_alloc, commit_graft_nr;
95 static int commit_graft_pos(const unsigned char *sha1)
101 int mi = (lo + hi) / 2;
102 struct commit_graft *graft = commit_graft[mi];
103 int cmp = hashcmp(sha1, graft->sha1);
114 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
116 int pos = commit_graft_pos(graft->sha1);
122 free(commit_graft[pos]);
123 commit_graft[pos] = graft;
128 if (commit_graft_alloc <= ++commit_graft_nr) {
129 commit_graft_alloc = alloc_nr(commit_graft_alloc);
130 commit_graft = xrealloc(commit_graft,
131 sizeof(*commit_graft) *
134 if (pos < commit_graft_nr)
135 memmove(commit_graft + pos + 1,
137 (commit_graft_nr - pos - 1) *
138 sizeof(*commit_graft));
139 commit_graft[pos] = graft;
143 struct commit_graft *read_graft_line(char *buf, int len)
145 /* The format is just "Commit Parent1 Parent2 ...\n" */
147 struct commit_graft *graft = NULL;
149 while (len && isspace(buf[len-1]))
151 if (buf[0] == '#' || buf[0] == '\0')
155 i = (len + 1) / 41 - 1;
156 graft = xmalloc(sizeof(*graft) + 20 * i);
157 graft->nr_parent = i;
158 if (get_sha1_hex(buf, graft->sha1))
160 for (i = 40; i < len; i += 41) {
163 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
169 error("bad graft data: %s", buf);
174 static int read_graft_file(const char *graft_file)
176 FILE *fp = fopen(graft_file, "r");
180 while (fgets(buf, sizeof(buf), fp)) {
181 /* The format is just "Commit Parent1 Parent2 ...\n" */
182 int len = strlen(buf);
183 struct commit_graft *graft = read_graft_line(buf, len);
186 if (register_commit_graft(graft, 1))
187 error("duplicate graft data: %s", buf);
193 static void prepare_commit_graft(void)
195 static int commit_graft_prepared;
198 if (commit_graft_prepared)
200 graft_file = get_graft_file();
201 read_graft_file(graft_file);
202 /* make sure shallows are read */
203 is_repository_shallow();
204 commit_graft_prepared = 1;
207 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
210 prepare_commit_graft();
211 pos = commit_graft_pos(sha1);
214 return commit_graft[pos];
217 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
220 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
221 ret = fn(commit_graft[i], cb_data);
225 int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
228 for (i = 0; i < commit_graft_nr; i++)
229 if (commit_graft[i]->nr_parent < 0) {
231 sha1_to_hex(commit_graft[i]->sha1);
233 if (use_pack_protocol)
234 packet_buf_write(out, "shallow %s", hex);
236 strbuf_addstr(out, hex);
237 strbuf_addch(out, '\n');
243 int unregister_shallow(const unsigned char *sha1)
245 int pos = commit_graft_pos(sha1);
248 if (pos + 1 < commit_graft_nr)
249 memmove(commit_graft + pos, commit_graft + pos + 1,
250 sizeof(struct commit_graft *)
251 * (commit_graft_nr - pos - 1));
256 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
258 const char *tail = buffer;
259 const char *bufptr = buffer;
260 unsigned char parent[20];
261 struct commit_list **pptr;
262 struct commit_graft *graft;
264 if (item->object.parsed)
266 item->object.parsed = 1;
268 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
269 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
270 if (get_sha1_hex(bufptr + 5, parent) < 0)
271 return error("bad tree pointer in commit %s",
272 sha1_to_hex(item->object.sha1));
273 item->tree = lookup_tree(parent);
274 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
275 pptr = &item->parents;
277 graft = lookup_commit_graft(item->object.sha1);
278 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
279 struct commit *new_parent;
281 if (tail <= bufptr + 48 ||
282 get_sha1_hex(bufptr + 7, parent) ||
284 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
287 * The clone is shallow if nr_parent < 0, and we must
288 * not traverse its real parents even when we unhide them.
290 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
292 new_parent = lookup_commit(parent);
294 pptr = &commit_list_insert(new_parent, pptr)->next;
298 struct commit *new_parent;
299 for (i = 0; i < graft->nr_parent; i++) {
300 new_parent = lookup_commit(graft->parent[i]);
303 pptr = &commit_list_insert(new_parent, pptr)->next;
306 item->date = parse_commit_date(bufptr, tail);
311 int parse_commit(struct commit *item)
313 enum object_type type;
320 if (item->object.parsed)
322 buffer = read_sha1_file(item->object.sha1, &type, &size);
324 return error("Could not read %s",
325 sha1_to_hex(item->object.sha1));
326 if (type != OBJ_COMMIT) {
328 return error("Object %s not a commit",
329 sha1_to_hex(item->object.sha1));
331 ret = parse_commit_buffer(item, buffer, size);
332 if (save_commit_buffer && !ret) {
333 item->buffer = buffer;
340 int find_commit_subject(const char *commit_buffer, const char **subject)
343 const char *p = commit_buffer;
345 while (*p && (*p != '\n' || p[1] != '\n'))
349 for (eol = p; *eol && *eol != '\n'; eol++)
359 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
361 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
362 new_list->item = item;
363 new_list->next = *list_p;
368 unsigned commit_list_count(const struct commit_list *l)
371 for (; l; l = l->next )
376 void free_commit_list(struct commit_list *list)
379 struct commit_list *temp = list;
385 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
387 struct commit_list **pp = list;
388 struct commit_list *p;
389 while ((p = *pp) != NULL) {
390 if (p->item->date < item->date) {
395 return commit_list_insert(item, pp);
399 void commit_list_sort_by_date(struct commit_list **list)
401 struct commit_list *ret = NULL;
403 commit_list_insert_by_date((*list)->item, &ret);
404 *list = (*list)->next;
409 struct commit *pop_most_recent_commit(struct commit_list **list,
412 struct commit *ret = (*list)->item;
413 struct commit_list *parents = ret->parents;
414 struct commit_list *old = *list;
416 *list = (*list)->next;
420 struct commit *commit = parents->item;
421 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
422 commit->object.flags |= mark;
423 commit_list_insert_by_date(commit, list);
425 parents = parents->next;
430 void clear_commit_marks(struct commit *commit, unsigned int mark)
433 struct commit_list *parents;
435 if (!(mark & commit->object.flags))
438 commit->object.flags &= ~mark;
440 parents = commit->parents;
444 while ((parents = parents->next))
445 clear_commit_marks(parents->item, mark);
447 commit = commit->parents->item;
451 struct commit *pop_commit(struct commit_list **stack)
453 struct commit_list *top = *stack;
454 struct commit *item = top ? top->item : NULL;
464 * Performs an in-place topological sort on the list supplied.
466 void sort_in_topological_order(struct commit_list ** list, int lifo)
468 struct commit_list *next, *orig = *list;
469 struct commit_list *work, **insert;
470 struct commit_list **pptr;
476 /* Mark them and clear the indegree */
477 for (next = orig; next; next = next->next) {
478 struct commit *commit = next->item;
479 commit->indegree = 1;
482 /* update the indegree */
483 for (next = orig; next; next = next->next) {
484 struct commit_list * parents = next->item->parents;
486 struct commit *parent = parents->item;
488 if (parent->indegree)
490 parents = parents->next;
497 * tips are nodes not reachable from any other node in the list
499 * the tips serve as a starting set for the work queue.
503 for (next = orig; next; next = next->next) {
504 struct commit *commit = next->item;
506 if (commit->indegree == 1)
507 insert = &commit_list_insert(commit, insert)->next;
510 /* process the list in topological order */
512 commit_list_sort_by_date(&work);
517 struct commit *commit;
518 struct commit_list *parents, *work_item;
521 work = work_item->next;
522 work_item->next = NULL;
524 commit = work_item->item;
525 for (parents = commit->parents; parents ; parents = parents->next) {
526 struct commit *parent=parents->item;
528 if (!parent->indegree)
532 * parents are only enqueued for emission
533 * when all their children have been emitted thereby
534 * guaranteeing topological order.
536 if (--parent->indegree == 1) {
538 commit_list_insert_by_date(parent, &work);
540 commit_list_insert(parent, &work);
544 * work_item is a commit all of whose children
545 * have already been emitted. we can emit it now.
547 commit->indegree = 0;
549 pptr = &work_item->next;
553 /* merge-base stuff */
555 /* bits #0..15 in revision.h */
556 #define PARENT1 (1u<<16)
557 #define PARENT2 (1u<<17)
558 #define STALE (1u<<18)
559 #define RESULT (1u<<19)
561 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
563 static struct commit *interesting(struct commit_list *list)
566 struct commit *commit = list->item;
568 if (commit->object.flags & STALE)
575 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
577 struct commit_list *list = NULL;
578 struct commit_list *result = NULL;
581 for (i = 0; i < n; i++) {
584 * We do not mark this even with RESULT so we do not
585 * have to clean it up.
587 return commit_list_insert(one, &result);
590 if (parse_commit(one))
592 for (i = 0; i < n; i++) {
593 if (parse_commit(twos[i]))
597 one->object.flags |= PARENT1;
598 commit_list_insert_by_date(one, &list);
599 for (i = 0; i < n; i++) {
600 twos[i]->object.flags |= PARENT2;
601 commit_list_insert_by_date(twos[i], &list);
604 while (interesting(list)) {
605 struct commit *commit;
606 struct commit_list *parents;
607 struct commit_list *next;
615 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
616 if (flags == (PARENT1 | PARENT2)) {
617 if (!(commit->object.flags & RESULT)) {
618 commit->object.flags |= RESULT;
619 commit_list_insert_by_date(commit, &result);
621 /* Mark parents of a found merge stale */
624 parents = commit->parents;
626 struct commit *p = parents->item;
627 parents = parents->next;
628 if ((p->object.flags & flags) == flags)
632 p->object.flags |= flags;
633 commit_list_insert_by_date(p, &list);
637 /* Clean up the result to remove stale ones */
638 free_commit_list(list);
639 list = result; result = NULL;
641 struct commit_list *next = list->next;
642 if (!(list->item->object.flags & STALE))
643 commit_list_insert_by_date(list->item, &result);
650 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
652 struct commit_list *i, *j, *k, *ret = NULL;
653 struct commit_list **pptr = &ret;
655 for (i = in; i; i = i->next) {
657 pptr = &commit_list_insert(i->item, pptr)->next;
659 struct commit_list *new = NULL, *end = NULL;
661 for (j = ret; j; j = j->next) {
662 struct commit_list *bases;
663 bases = get_merge_bases(i->item, j->item, 1);
668 for (k = bases; k; k = k->next)
677 struct commit_list *get_merge_bases_many(struct commit *one,
679 struct commit **twos,
682 struct commit_list *list;
683 struct commit **rslt;
684 struct commit_list *result;
687 result = merge_bases_many(one, n, twos);
688 for (i = 0; i < n; i++) {
692 if (!result || !result->next) {
694 clear_commit_marks(one, all_flags);
695 for (i = 0; i < n; i++)
696 clear_commit_marks(twos[i], all_flags);
701 /* There are more than one */
708 rslt = xcalloc(cnt, sizeof(*rslt));
709 for (list = result, i = 0; list; list = list->next)
710 rslt[i++] = list->item;
711 free_commit_list(result);
713 clear_commit_marks(one, all_flags);
714 for (i = 0; i < n; i++)
715 clear_commit_marks(twos[i], all_flags);
716 for (i = 0; i < cnt - 1; i++) {
717 for (j = i+1; j < cnt; j++) {
718 if (!rslt[i] || !rslt[j])
720 result = merge_bases_many(rslt[i], 1, &rslt[j]);
721 clear_commit_marks(rslt[i], all_flags);
722 clear_commit_marks(rslt[j], all_flags);
723 for (list = result; list; list = list->next) {
724 if (rslt[i] == list->item)
726 if (rslt[j] == list->item)
732 /* Surviving ones in rslt[] are the independent results */
734 for (i = 0; i < cnt; i++) {
736 commit_list_insert_by_date(rslt[i], &result);
742 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
745 return get_merge_bases_many(one, 1, &two, cleanup);
748 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
752 while (with_commit) {
753 struct commit *other;
755 other = with_commit->item;
756 with_commit = with_commit->next;
757 if (in_merge_bases(other, &commit, 1))
763 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
765 struct commit_list *bases, *b;
769 bases = get_merge_bases(commit, *reference, 1);
772 for (b = bases; b; b = b->next) {
773 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
779 free_commit_list(bases);
783 struct commit_list *reduce_heads(struct commit_list *heads)
785 struct commit_list *p;
786 struct commit_list *result = NULL, **tail = &result;
787 struct commit **other;
788 size_t num_head, num_other;
793 /* Avoid unnecessary reallocations */
794 for (p = heads, num_head = 0; p; p = p->next)
796 other = xcalloc(sizeof(*other), num_head);
798 /* For each commit, see if it can be reached by others */
799 for (p = heads; p; p = p->next) {
800 struct commit_list *q, *base;
802 /* Do we already have this in the result? */
803 for (q = result; q; q = q->next)
804 if (p->item == q->item)
810 for (q = heads; q; q = q->next) {
811 if (p->item == q->item)
813 other[num_other++] = q->item;
816 base = get_merge_bases_many(p->item, num_other, other, 1);
820 * If p->item does not have anything common with other
821 * commits, there won't be any merge base. If it is
822 * reachable from some of the others, p->item will be
823 * the merge base. If its history is connected with
824 * others, but p->item is not reachable by others, we
825 * will get something other than p->item back.
827 if (!base || (base->item != p->item))
828 tail = &(commit_list_insert(p->item, tail)->next);
829 free_commit_list(base);
835 static const char commit_utf8_warn[] =
836 "Warning: commit message does not conform to UTF-8.\n"
837 "You may want to amend it after fixing the message, or set the config\n"
838 "variable i18n.commitencoding to the encoding your project uses.\n";
840 int commit_tree(const char *msg, unsigned char *tree,
841 struct commit_list *parents, unsigned char *ret,
845 int encoding_is_utf8;
846 struct strbuf buffer;
848 assert_sha1_type(tree, OBJ_TREE);
850 /* Not having i18n.commitencoding is the same as having utf-8 */
851 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
853 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
854 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
857 * NOTE! This ordering means that the same exact tree merged with a
858 * different order of parents will be a _different_ changeset even
859 * if everything else stays the same.
862 struct commit_list *next = parents->next;
863 strbuf_addf(&buffer, "parent %s\n",
864 sha1_to_hex(parents->item->object.sha1));
869 /* Person/date information */
871 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
872 strbuf_addf(&buffer, "author %s\n", author);
873 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
874 if (!encoding_is_utf8)
875 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
876 strbuf_addch(&buffer, '\n');
878 /* And add the comment */
879 strbuf_addstr(&buffer, msg);
881 /* And check the encoding */
882 if (encoding_is_utf8 && !is_utf8(buffer.buf))
883 fprintf(stderr, commit_utf8_warn);
885 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
886 strbuf_release(&buffer);