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')
142 i = (len + 1) / 41 - 1;
143 graft = xmalloc(sizeof(*graft) + 20 * i);
144 graft->nr_parent = i;
145 if (get_sha1_hex(buf, graft->sha1))
147 for (i = 40; i < len; i += 41) {
150 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
156 error("bad graft data: %s", buf);
161 static int read_graft_file(const char *graft_file)
163 FILE *fp = fopen(graft_file, "r");
167 while (fgets(buf, sizeof(buf), fp)) {
168 /* The format is just "Commit Parent1 Parent2 ...\n" */
169 int len = strlen(buf);
170 struct commit_graft *graft = read_graft_line(buf, len);
173 if (register_commit_graft(graft, 1))
174 error("duplicate graft data: %s", buf);
180 static void prepare_commit_graft(void)
182 static int commit_graft_prepared;
185 if (commit_graft_prepared)
187 graft_file = get_graft_file();
188 read_graft_file(graft_file);
189 /* make sure shallows are read */
190 is_repository_shallow();
191 commit_graft_prepared = 1;
194 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
197 prepare_commit_graft();
198 pos = commit_graft_pos(sha1);
201 return commit_graft[pos];
204 int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
207 for (i = 0; i < commit_graft_nr; i++)
208 if (commit_graft[i]->nr_parent < 0) {
210 sha1_to_hex(commit_graft[i]->sha1);
212 if (use_pack_protocol)
213 packet_buf_write(out, "shallow %s", hex);
215 strbuf_addstr(out, hex);
216 strbuf_addch(out, '\n');
222 int unregister_shallow(const unsigned char *sha1)
224 int pos = commit_graft_pos(sha1);
227 if (pos + 1 < commit_graft_nr)
228 memmove(commit_graft + pos, commit_graft + pos + 1,
229 sizeof(struct commit_graft *)
230 * (commit_graft_nr - pos - 1));
235 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
238 char *bufptr = buffer;
239 unsigned char parent[20];
240 struct commit_list **pptr;
241 struct commit_graft *graft;
243 if (item->object.parsed)
245 item->object.parsed = 1;
247 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
248 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
249 if (get_sha1_hex(bufptr + 5, parent) < 0)
250 return error("bad tree pointer in commit %s",
251 sha1_to_hex(item->object.sha1));
252 item->tree = lookup_tree(parent);
253 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
254 pptr = &item->parents;
256 graft = lookup_commit_graft(item->object.sha1);
257 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
258 struct commit *new_parent;
260 if (tail <= bufptr + 48 ||
261 get_sha1_hex(bufptr + 7, parent) ||
263 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
266 * The clone is shallow if nr_parent < 0, and we must
267 * not traverse its real parents even when we unhide them.
269 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
271 new_parent = lookup_commit(parent);
273 pptr = &commit_list_insert(new_parent, pptr)->next;
277 struct commit *new_parent;
278 for (i = 0; i < graft->nr_parent; i++) {
279 new_parent = lookup_commit(graft->parent[i]);
282 pptr = &commit_list_insert(new_parent, pptr)->next;
285 item->date = parse_commit_date(bufptr, tail);
290 int parse_commit(struct commit *item)
292 enum object_type type;
299 if (item->object.parsed)
301 buffer = read_sha1_file(item->object.sha1, &type, &size);
303 return error("Could not read %s",
304 sha1_to_hex(item->object.sha1));
305 if (type != OBJ_COMMIT) {
307 return error("Object %s not a commit",
308 sha1_to_hex(item->object.sha1));
310 ret = parse_commit_buffer(item, buffer, size);
311 if (save_commit_buffer && !ret) {
312 item->buffer = buffer;
319 int find_commit_subject(const char *commit_buffer, const char **subject)
322 const char *p = commit_buffer;
324 while (*p && (*p != '\n' || p[1] != '\n'))
328 for (eol = p; *eol && *eol != '\n'; eol++)
338 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
340 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
341 new_list->item = item;
342 new_list->next = *list_p;
347 unsigned commit_list_count(const struct commit_list *l)
350 for (; l; l = l->next )
355 void free_commit_list(struct commit_list *list)
358 struct commit_list *temp = list;
364 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
366 struct commit_list **pp = list;
367 struct commit_list *p;
368 while ((p = *pp) != NULL) {
369 if (p->item->date < item->date) {
374 return commit_list_insert(item, pp);
378 void sort_by_date(struct commit_list **list)
380 struct commit_list *ret = NULL;
382 insert_by_date((*list)->item, &ret);
383 *list = (*list)->next;
388 struct commit *pop_most_recent_commit(struct commit_list **list,
391 struct commit *ret = (*list)->item;
392 struct commit_list *parents = ret->parents;
393 struct commit_list *old = *list;
395 *list = (*list)->next;
399 struct commit *commit = parents->item;
400 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
401 commit->object.flags |= mark;
402 insert_by_date(commit, list);
404 parents = parents->next;
409 void clear_commit_marks(struct commit *commit, unsigned int mark)
412 struct commit_list *parents;
414 if (!(mark & commit->object.flags))
417 commit->object.flags &= ~mark;
419 parents = commit->parents;
423 while ((parents = parents->next))
424 clear_commit_marks(parents->item, mark);
426 commit = commit->parents->item;
430 struct commit *pop_commit(struct commit_list **stack)
432 struct commit_list *top = *stack;
433 struct commit *item = top ? top->item : NULL;
443 * Performs an in-place topological sort on the list supplied.
445 void sort_in_topological_order(struct commit_list ** list, int lifo)
447 struct commit_list *next, *orig = *list;
448 struct commit_list *work, **insert;
449 struct commit_list **pptr;
455 /* Mark them and clear the indegree */
456 for (next = orig; next; next = next->next) {
457 struct commit *commit = next->item;
458 commit->indegree = 1;
461 /* update the indegree */
462 for (next = orig; next; next = next->next) {
463 struct commit_list * parents = next->item->parents;
465 struct commit *parent = parents->item;
467 if (parent->indegree)
469 parents = parents->next;
476 * tips are nodes not reachable from any other node in the list
478 * the tips serve as a starting set for the work queue.
482 for (next = orig; next; next = next->next) {
483 struct commit *commit = next->item;
485 if (commit->indegree == 1)
486 insert = &commit_list_insert(commit, insert)->next;
489 /* process the list in topological order */
496 struct commit *commit;
497 struct commit_list *parents, *work_item;
500 work = work_item->next;
501 work_item->next = NULL;
503 commit = work_item->item;
504 for (parents = commit->parents; parents ; parents = parents->next) {
505 struct commit *parent=parents->item;
507 if (!parent->indegree)
511 * parents are only enqueued for emission
512 * when all their children have been emitted thereby
513 * guaranteeing topological order.
515 if (--parent->indegree == 1) {
517 insert_by_date(parent, &work);
519 commit_list_insert(parent, &work);
523 * work_item is a commit all of whose children
524 * have already been emitted. we can emit it now.
526 commit->indegree = 0;
528 pptr = &work_item->next;
532 /* merge-base stuff */
534 /* bits #0..15 in revision.h */
535 #define PARENT1 (1u<<16)
536 #define PARENT2 (1u<<17)
537 #define STALE (1u<<18)
538 #define RESULT (1u<<19)
540 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
542 static struct commit *interesting(struct commit_list *list)
545 struct commit *commit = list->item;
547 if (commit->object.flags & STALE)
554 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
556 struct commit_list *list = NULL;
557 struct commit_list *result = NULL;
560 for (i = 0; i < n; i++) {
563 * We do not mark this even with RESULT so we do not
564 * have to clean it up.
566 return commit_list_insert(one, &result);
569 if (parse_commit(one))
571 for (i = 0; i < n; i++) {
572 if (parse_commit(twos[i]))
576 one->object.flags |= PARENT1;
577 insert_by_date(one, &list);
578 for (i = 0; i < n; i++) {
579 twos[i]->object.flags |= PARENT2;
580 insert_by_date(twos[i], &list);
583 while (interesting(list)) {
584 struct commit *commit;
585 struct commit_list *parents;
586 struct commit_list *next;
594 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
595 if (flags == (PARENT1 | PARENT2)) {
596 if (!(commit->object.flags & RESULT)) {
597 commit->object.flags |= RESULT;
598 insert_by_date(commit, &result);
600 /* Mark parents of a found merge stale */
603 parents = commit->parents;
605 struct commit *p = parents->item;
606 parents = parents->next;
607 if ((p->object.flags & flags) == flags)
611 p->object.flags |= flags;
612 insert_by_date(p, &list);
616 /* Clean up the result to remove stale ones */
617 free_commit_list(list);
618 list = result; result = NULL;
620 struct commit_list *next = list->next;
621 if (!(list->item->object.flags & STALE))
622 insert_by_date(list->item, &result);
629 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
631 struct commit_list *i, *j, *k, *ret = NULL;
632 struct commit_list **pptr = &ret;
634 for (i = in; i; i = i->next) {
636 pptr = &commit_list_insert(i->item, pptr)->next;
638 struct commit_list *new = NULL, *end = NULL;
640 for (j = ret; j; j = j->next) {
641 struct commit_list *bases;
642 bases = get_merge_bases(i->item, j->item, 1);
647 for (k = bases; k; k = k->next)
656 struct commit_list *get_merge_bases_many(struct commit *one,
658 struct commit **twos,
661 struct commit_list *list;
662 struct commit **rslt;
663 struct commit_list *result;
666 result = merge_bases_many(one, n, twos);
667 for (i = 0; i < n; i++) {
671 if (!result || !result->next) {
673 clear_commit_marks(one, all_flags);
674 for (i = 0; i < n; i++)
675 clear_commit_marks(twos[i], all_flags);
680 /* There are more than one */
687 rslt = xcalloc(cnt, sizeof(*rslt));
688 for (list = result, i = 0; list; list = list->next)
689 rslt[i++] = list->item;
690 free_commit_list(result);
692 clear_commit_marks(one, all_flags);
693 for (i = 0; i < n; i++)
694 clear_commit_marks(twos[i], all_flags);
695 for (i = 0; i < cnt - 1; i++) {
696 for (j = i+1; j < cnt; j++) {
697 if (!rslt[i] || !rslt[j])
699 result = merge_bases_many(rslt[i], 1, &rslt[j]);
700 clear_commit_marks(rslt[i], all_flags);
701 clear_commit_marks(rslt[j], all_flags);
702 for (list = result; list; list = list->next) {
703 if (rslt[i] == list->item)
705 if (rslt[j] == list->item)
711 /* Surviving ones in rslt[] are the independent results */
713 for (i = 0; i < cnt; i++) {
715 insert_by_date(rslt[i], &result);
721 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
724 return get_merge_bases_many(one, 1, &two, cleanup);
727 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
731 while (with_commit) {
732 struct commit *other;
734 other = with_commit->item;
735 with_commit = with_commit->next;
736 if (in_merge_bases(other, &commit, 1))
742 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
744 struct commit_list *bases, *b;
748 bases = get_merge_bases(commit, *reference, 1);
751 for (b = bases; b; b = b->next) {
752 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
758 free_commit_list(bases);
762 struct commit_list *reduce_heads(struct commit_list *heads)
764 struct commit_list *p;
765 struct commit_list *result = NULL, **tail = &result;
766 struct commit **other;
767 size_t num_head, num_other;
772 /* Avoid unnecessary reallocations */
773 for (p = heads, num_head = 0; p; p = p->next)
775 other = xcalloc(sizeof(*other), num_head);
777 /* For each commit, see if it can be reached by others */
778 for (p = heads; p; p = p->next) {
779 struct commit_list *q, *base;
781 /* Do we already have this in the result? */
782 for (q = result; q; q = q->next)
783 if (p->item == q->item)
789 for (q = heads; q; q = q->next) {
790 if (p->item == q->item)
792 other[num_other++] = q->item;
795 base = get_merge_bases_many(p->item, num_other, other, 1);
799 * If p->item does not have anything common with other
800 * commits, there won't be any merge base. If it is
801 * reachable from some of the others, p->item will be
802 * the merge base. If its history is connected with
803 * others, but p->item is not reachable by others, we
804 * will get something other than p->item back.
806 if (!base || (base->item != p->item))
807 tail = &(commit_list_insert(p->item, tail)->next);
808 free_commit_list(base);
814 static const char commit_utf8_warn[] =
815 "Warning: commit message does not conform to UTF-8.\n"
816 "You may want to amend it after fixing the message, or set the config\n"
817 "variable i18n.commitencoding to the encoding your project uses.\n";
819 int commit_tree(const char *msg, unsigned char *tree,
820 struct commit_list *parents, unsigned char *ret,
824 int encoding_is_utf8;
825 struct strbuf buffer;
827 assert_sha1_type(tree, OBJ_TREE);
829 /* Not having i18n.commitencoding is the same as having utf-8 */
830 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
832 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
833 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
836 * NOTE! This ordering means that the same exact tree merged with a
837 * different order of parents will be a _different_ changeset even
838 * if everything else stays the same.
841 struct commit_list *next = parents->next;
842 strbuf_addf(&buffer, "parent %s\n",
843 sha1_to_hex(parents->item->object.sha1));
848 /* Person/date information */
850 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
851 strbuf_addf(&buffer, "author %s\n", author);
852 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
853 if (!encoding_is_utf8)
854 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
855 strbuf_addch(&buffer, '\n');
857 /* And add the comment */
858 strbuf_addstr(&buffer, msg);
860 /* And check the encoding */
861 if (encoding_is_utf8 && !is_utf8(buffer.buf))
862 fprintf(stderr, commit_utf8_warn);
864 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
865 strbuf_release(&buffer);