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')
153 if ((len + 1) % 41) {
155 error("bad graft data: %s", buf);
159 i = (len + 1) / 41 - 1;
160 graft = xmalloc(sizeof(*graft) + 20 * i);
161 graft->nr_parent = i;
162 if (get_sha1_hex(buf, graft->sha1))
164 for (i = 40; i < len; i += 41) {
167 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
173 static int read_graft_file(const char *graft_file)
175 FILE *fp = fopen(graft_file, "r");
179 while (fgets(buf, sizeof(buf), fp)) {
180 /* The format is just "Commit Parent1 Parent2 ...\n" */
181 int len = strlen(buf);
182 struct commit_graft *graft = read_graft_line(buf, len);
185 if (register_commit_graft(graft, 1))
186 error("duplicate graft data: %s", buf);
192 static void prepare_commit_graft(void)
194 static int commit_graft_prepared;
197 if (commit_graft_prepared)
199 graft_file = get_graft_file();
200 read_graft_file(graft_file);
201 /* make sure shallows are read */
202 is_repository_shallow();
203 commit_graft_prepared = 1;
206 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
209 prepare_commit_graft();
210 pos = commit_graft_pos(sha1);
213 return commit_graft[pos];
216 int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
219 for (i = 0; i < commit_graft_nr; i++)
220 if (commit_graft[i]->nr_parent < 0) {
222 sha1_to_hex(commit_graft[i]->sha1);
224 if (use_pack_protocol)
225 packet_buf_write(out, "shallow %s", hex);
227 strbuf_addstr(out, hex);
228 strbuf_addch(out, '\n');
234 int unregister_shallow(const unsigned char *sha1)
236 int pos = commit_graft_pos(sha1);
239 if (pos + 1 < commit_graft_nr)
240 memmove(commit_graft + pos, commit_graft + pos + 1,
241 sizeof(struct commit_graft *)
242 * (commit_graft_nr - pos - 1));
247 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
250 char *bufptr = buffer;
251 unsigned char parent[20];
252 struct commit_list **pptr;
253 struct commit_graft *graft;
255 if (item->object.parsed)
257 item->object.parsed = 1;
259 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
260 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
261 if (get_sha1_hex(bufptr + 5, parent) < 0)
262 return error("bad tree pointer in commit %s",
263 sha1_to_hex(item->object.sha1));
264 item->tree = lookup_tree(parent);
265 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
266 pptr = &item->parents;
268 graft = lookup_commit_graft(item->object.sha1);
269 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
270 struct commit *new_parent;
272 if (tail <= bufptr + 48 ||
273 get_sha1_hex(bufptr + 7, parent) ||
275 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
278 * The clone is shallow if nr_parent < 0, and we must
279 * not traverse its real parents even when we unhide them.
281 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
283 new_parent = lookup_commit(parent);
285 pptr = &commit_list_insert(new_parent, pptr)->next;
289 struct commit *new_parent;
290 for (i = 0; i < graft->nr_parent; i++) {
291 new_parent = lookup_commit(graft->parent[i]);
294 pptr = &commit_list_insert(new_parent, pptr)->next;
297 item->date = parse_commit_date(bufptr, tail);
302 int parse_commit(struct commit *item)
304 enum object_type type;
311 if (item->object.parsed)
313 buffer = read_sha1_file(item->object.sha1, &type, &size);
315 return error("Could not read %s",
316 sha1_to_hex(item->object.sha1));
317 if (type != OBJ_COMMIT) {
319 return error("Object %s not a commit",
320 sha1_to_hex(item->object.sha1));
322 ret = parse_commit_buffer(item, buffer, size);
323 if (save_commit_buffer && !ret) {
324 item->buffer = buffer;
331 int find_commit_subject(const char *commit_buffer, const char **subject)
334 const char *p = commit_buffer;
336 while (*p && (*p != '\n' || p[1] != '\n'))
340 for (eol = p; *eol && *eol != '\n'; eol++)
350 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
352 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
353 new_list->item = item;
354 new_list->next = *list_p;
359 unsigned commit_list_count(const struct commit_list *l)
362 for (; l; l = l->next )
367 void free_commit_list(struct commit_list *list)
370 struct commit_list *temp = list;
376 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
378 struct commit_list **pp = list;
379 struct commit_list *p;
380 while ((p = *pp) != NULL) {
381 if (p->item->date < item->date) {
386 return commit_list_insert(item, pp);
390 void sort_by_date(struct commit_list **list)
392 struct commit_list *ret = NULL;
394 insert_by_date((*list)->item, &ret);
395 *list = (*list)->next;
400 struct commit *pop_most_recent_commit(struct commit_list **list,
403 struct commit *ret = (*list)->item;
404 struct commit_list *parents = ret->parents;
405 struct commit_list *old = *list;
407 *list = (*list)->next;
411 struct commit *commit = parents->item;
412 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
413 commit->object.flags |= mark;
414 insert_by_date(commit, list);
416 parents = parents->next;
421 void clear_commit_marks(struct commit *commit, unsigned int mark)
424 struct commit_list *parents;
426 if (!(mark & commit->object.flags))
429 commit->object.flags &= ~mark;
431 parents = commit->parents;
435 while ((parents = parents->next))
436 clear_commit_marks(parents->item, mark);
438 commit = commit->parents->item;
442 struct commit *pop_commit(struct commit_list **stack)
444 struct commit_list *top = *stack;
445 struct commit *item = top ? top->item : NULL;
455 * Performs an in-place topological sort on the list supplied.
457 void sort_in_topological_order(struct commit_list ** list, int lifo)
459 struct commit_list *next, *orig = *list;
460 struct commit_list *work, **insert;
461 struct commit_list **pptr;
467 /* Mark them and clear the indegree */
468 for (next = orig; next; next = next->next) {
469 struct commit *commit = next->item;
470 commit->indegree = 1;
473 /* update the indegree */
474 for (next = orig; next; next = next->next) {
475 struct commit_list * parents = next->item->parents;
477 struct commit *parent = parents->item;
479 if (parent->indegree)
481 parents = parents->next;
488 * tips are nodes not reachable from any other node in the list
490 * the tips serve as a starting set for the work queue.
494 for (next = orig; next; next = next->next) {
495 struct commit *commit = next->item;
497 if (commit->indegree == 1)
498 insert = &commit_list_insert(commit, insert)->next;
501 /* process the list in topological order */
508 struct commit *commit;
509 struct commit_list *parents, *work_item;
512 work = work_item->next;
513 work_item->next = NULL;
515 commit = work_item->item;
516 for (parents = commit->parents; parents ; parents = parents->next) {
517 struct commit *parent=parents->item;
519 if (!parent->indegree)
523 * parents are only enqueued for emission
524 * when all their children have been emitted thereby
525 * guaranteeing topological order.
527 if (--parent->indegree == 1) {
529 insert_by_date(parent, &work);
531 commit_list_insert(parent, &work);
535 * work_item is a commit all of whose children
536 * have already been emitted. we can emit it now.
538 commit->indegree = 0;
540 pptr = &work_item->next;
544 /* merge-base stuff */
546 /* bits #0..15 in revision.h */
547 #define PARENT1 (1u<<16)
548 #define PARENT2 (1u<<17)
549 #define STALE (1u<<18)
550 #define RESULT (1u<<19)
552 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
554 static struct commit *interesting(struct commit_list *list)
557 struct commit *commit = list->item;
559 if (commit->object.flags & STALE)
566 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
568 struct commit_list *list = NULL;
569 struct commit_list *result = NULL;
572 for (i = 0; i < n; i++) {
575 * We do not mark this even with RESULT so we do not
576 * have to clean it up.
578 return commit_list_insert(one, &result);
581 if (parse_commit(one))
583 for (i = 0; i < n; i++) {
584 if (parse_commit(twos[i]))
588 one->object.flags |= PARENT1;
589 insert_by_date(one, &list);
590 for (i = 0; i < n; i++) {
591 twos[i]->object.flags |= PARENT2;
592 insert_by_date(twos[i], &list);
595 while (interesting(list)) {
596 struct commit *commit;
597 struct commit_list *parents;
598 struct commit_list *next;
606 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
607 if (flags == (PARENT1 | PARENT2)) {
608 if (!(commit->object.flags & RESULT)) {
609 commit->object.flags |= RESULT;
610 insert_by_date(commit, &result);
612 /* Mark parents of a found merge stale */
615 parents = commit->parents;
617 struct commit *p = parents->item;
618 parents = parents->next;
619 if ((p->object.flags & flags) == flags)
623 p->object.flags |= flags;
624 insert_by_date(p, &list);
628 /* Clean up the result to remove stale ones */
629 free_commit_list(list);
630 list = result; result = NULL;
632 struct commit_list *next = list->next;
633 if (!(list->item->object.flags & STALE))
634 insert_by_date(list->item, &result);
641 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
643 struct commit_list *i, *j, *k, *ret = NULL;
644 struct commit_list **pptr = &ret;
646 for (i = in; i; i = i->next) {
648 pptr = &commit_list_insert(i->item, pptr)->next;
650 struct commit_list *new = NULL, *end = NULL;
652 for (j = ret; j; j = j->next) {
653 struct commit_list *bases;
654 bases = get_merge_bases(i->item, j->item, 1);
659 for (k = bases; k; k = k->next)
668 struct commit_list *get_merge_bases_many(struct commit *one,
670 struct commit **twos,
673 struct commit_list *list;
674 struct commit **rslt;
675 struct commit_list *result;
678 result = merge_bases_many(one, n, twos);
679 for (i = 0; i < n; i++) {
683 if (!result || !result->next) {
685 clear_commit_marks(one, all_flags);
686 for (i = 0; i < n; i++)
687 clear_commit_marks(twos[i], all_flags);
692 /* There are more than one */
699 rslt = xcalloc(cnt, sizeof(*rslt));
700 for (list = result, i = 0; list; list = list->next)
701 rslt[i++] = list->item;
702 free_commit_list(result);
704 clear_commit_marks(one, all_flags);
705 for (i = 0; i < n; i++)
706 clear_commit_marks(twos[i], all_flags);
707 for (i = 0; i < cnt - 1; i++) {
708 for (j = i+1; j < cnt; j++) {
709 if (!rslt[i] || !rslt[j])
711 result = merge_bases_many(rslt[i], 1, &rslt[j]);
712 clear_commit_marks(rslt[i], all_flags);
713 clear_commit_marks(rslt[j], all_flags);
714 for (list = result; list; list = list->next) {
715 if (rslt[i] == list->item)
717 if (rslt[j] == list->item)
723 /* Surviving ones in rslt[] are the independent results */
725 for (i = 0; i < cnt; i++) {
727 insert_by_date(rslt[i], &result);
733 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
736 return get_merge_bases_many(one, 1, &two, cleanup);
739 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
743 while (with_commit) {
744 struct commit *other;
746 other = with_commit->item;
747 with_commit = with_commit->next;
748 if (in_merge_bases(other, &commit, 1))
754 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
756 struct commit_list *bases, *b;
760 bases = get_merge_bases(commit, *reference, 1);
763 for (b = bases; b; b = b->next) {
764 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
770 free_commit_list(bases);
774 struct commit_list *reduce_heads(struct commit_list *heads)
776 struct commit_list *p;
777 struct commit_list *result = NULL, **tail = &result;
778 struct commit **other;
779 size_t num_head, num_other;
784 /* Avoid unnecessary reallocations */
785 for (p = heads, num_head = 0; p; p = p->next)
787 other = xcalloc(sizeof(*other), num_head);
789 /* For each commit, see if it can be reached by others */
790 for (p = heads; p; p = p->next) {
791 struct commit_list *q, *base;
793 /* Do we already have this in the result? */
794 for (q = result; q; q = q->next)
795 if (p->item == q->item)
801 for (q = heads; q; q = q->next) {
802 if (p->item == q->item)
804 other[num_other++] = q->item;
807 base = get_merge_bases_many(p->item, num_other, other, 1);
811 * If p->item does not have anything common with other
812 * commits, there won't be any merge base. If it is
813 * reachable from some of the others, p->item will be
814 * the merge base. If its history is connected with
815 * others, but p->item is not reachable by others, we
816 * will get something other than p->item back.
818 if (!base || (base->item != p->item))
819 tail = &(commit_list_insert(p->item, tail)->next);
820 free_commit_list(base);
826 static const char commit_utf8_warn[] =
827 "Warning: commit message does not conform to UTF-8.\n"
828 "You may want to amend it after fixing the message, or set the config\n"
829 "variable i18n.commitencoding to the encoding your project uses.\n";
831 int commit_tree(const char *msg, unsigned char *tree,
832 struct commit_list *parents, unsigned char *ret,
836 int encoding_is_utf8;
837 struct strbuf buffer;
839 assert_sha1_type(tree, OBJ_TREE);
841 /* Not having i18n.commitencoding is the same as having utf-8 */
842 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
844 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
845 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
848 * NOTE! This ordering means that the same exact tree merged with a
849 * different order of parents will be a _different_ changeset even
850 * if everything else stays the same.
853 struct commit_list *next = parents->next;
854 strbuf_addf(&buffer, "parent %s\n",
855 sha1_to_hex(parents->item->object.sha1));
860 /* Person/date information */
862 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
863 strbuf_addf(&buffer, "author %s\n", author);
864 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
865 if (!encoding_is_utf8)
866 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
867 strbuf_addch(&buffer, '\n');
869 /* And add the comment */
870 strbuf_addstr(&buffer, msg);
872 /* And check the encoding */
873 if (encoding_is_utf8 && !is_utf8(buffer.buf))
874 fprintf(stderr, commit_utf8_warn);
876 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
877 strbuf_release(&buffer);