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_or_die(const unsigned char *sha1, const char *ref_name)
44 struct commit *c = lookup_commit_reference(sha1);
46 die(_("could not parse %s"), ref_name);
47 if (hashcmp(sha1, c->object.sha1)) {
48 warning(_("%s %s is not a commit!"),
49 ref_name, sha1_to_hex(sha1));
54 struct commit *lookup_commit(const unsigned char *sha1)
56 struct object *obj = lookup_object(sha1);
58 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
60 obj->type = OBJ_COMMIT;
61 return check_commit(obj, sha1, 0);
64 struct commit *lookup_commit_reference_by_name(const char *name)
66 unsigned char sha1[20];
67 struct commit *commit;
69 if (get_sha1(name, sha1))
71 commit = lookup_commit_reference(sha1);
72 if (!commit || parse_commit(commit))
77 static unsigned long parse_commit_date(const char *buf, const char *tail)
83 if (memcmp(buf, "author", 6))
85 while (buf < tail && *buf++ != '\n')
89 if (memcmp(buf, "committer", 9))
91 while (buf < tail && *buf++ != '>')
96 while (buf < tail && *buf++ != '\n')
100 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
101 return strtoul(dateptr, NULL, 10);
104 static struct commit_graft **commit_graft;
105 static int commit_graft_alloc, commit_graft_nr;
107 static int commit_graft_pos(const unsigned char *sha1)
111 hi = commit_graft_nr;
113 int mi = (lo + hi) / 2;
114 struct commit_graft *graft = commit_graft[mi];
115 int cmp = hashcmp(sha1, graft->sha1);
126 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
128 int pos = commit_graft_pos(graft->sha1);
134 free(commit_graft[pos]);
135 commit_graft[pos] = graft;
140 if (commit_graft_alloc <= ++commit_graft_nr) {
141 commit_graft_alloc = alloc_nr(commit_graft_alloc);
142 commit_graft = xrealloc(commit_graft,
143 sizeof(*commit_graft) *
146 if (pos < commit_graft_nr)
147 memmove(commit_graft + pos + 1,
149 (commit_graft_nr - pos - 1) *
150 sizeof(*commit_graft));
151 commit_graft[pos] = graft;
155 struct commit_graft *read_graft_line(char *buf, int len)
157 /* The format is just "Commit Parent1 Parent2 ...\n" */
159 struct commit_graft *graft = NULL;
161 while (len && isspace(buf[len-1]))
163 if (buf[0] == '#' || buf[0] == '\0')
167 i = (len + 1) / 41 - 1;
168 graft = xmalloc(sizeof(*graft) + 20 * i);
169 graft->nr_parent = i;
170 if (get_sha1_hex(buf, graft->sha1))
172 for (i = 40; i < len; i += 41) {
175 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
181 error("bad graft data: %s", buf);
186 static int read_graft_file(const char *graft_file)
188 FILE *fp = fopen(graft_file, "r");
192 while (fgets(buf, sizeof(buf), fp)) {
193 /* The format is just "Commit Parent1 Parent2 ...\n" */
194 int len = strlen(buf);
195 struct commit_graft *graft = read_graft_line(buf, len);
198 if (register_commit_graft(graft, 1))
199 error("duplicate graft data: %s", buf);
205 static void prepare_commit_graft(void)
207 static int commit_graft_prepared;
210 if (commit_graft_prepared)
212 graft_file = get_graft_file();
213 read_graft_file(graft_file);
214 /* make sure shallows are read */
215 is_repository_shallow();
216 commit_graft_prepared = 1;
219 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
222 prepare_commit_graft();
223 pos = commit_graft_pos(sha1);
226 return commit_graft[pos];
229 int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
232 for (i = 0; i < commit_graft_nr; i++)
233 if (commit_graft[i]->nr_parent < 0) {
235 sha1_to_hex(commit_graft[i]->sha1);
237 if (use_pack_protocol)
238 packet_buf_write(out, "shallow %s", hex);
240 strbuf_addstr(out, hex);
241 strbuf_addch(out, '\n');
247 int unregister_shallow(const unsigned char *sha1)
249 int pos = commit_graft_pos(sha1);
252 if (pos + 1 < commit_graft_nr)
253 memmove(commit_graft + pos, commit_graft + pos + 1,
254 sizeof(struct commit_graft *)
255 * (commit_graft_nr - pos - 1));
260 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
262 const char *tail = buffer;
263 const char *bufptr = buffer;
264 unsigned char parent[20];
265 struct commit_list **pptr;
266 struct commit_graft *graft;
268 if (item->object.parsed)
270 item->object.parsed = 1;
272 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
273 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
274 if (get_sha1_hex(bufptr + 5, parent) < 0)
275 return error("bad tree pointer in commit %s",
276 sha1_to_hex(item->object.sha1));
277 item->tree = lookup_tree(parent);
278 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
279 pptr = &item->parents;
281 graft = lookup_commit_graft(item->object.sha1);
282 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
283 struct commit *new_parent;
285 if (tail <= bufptr + 48 ||
286 get_sha1_hex(bufptr + 7, parent) ||
288 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
291 * The clone is shallow if nr_parent < 0, and we must
292 * not traverse its real parents even when we unhide them.
294 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
296 new_parent = lookup_commit(parent);
298 pptr = &commit_list_insert(new_parent, pptr)->next;
302 struct commit *new_parent;
303 for (i = 0; i < graft->nr_parent; i++) {
304 new_parent = lookup_commit(graft->parent[i]);
307 pptr = &commit_list_insert(new_parent, pptr)->next;
310 item->date = parse_commit_date(bufptr, tail);
315 int parse_commit(struct commit *item)
317 enum object_type type;
324 if (item->object.parsed)
326 buffer = read_sha1_file(item->object.sha1, &type, &size);
328 return error("Could not read %s",
329 sha1_to_hex(item->object.sha1));
330 if (type != OBJ_COMMIT) {
332 return error("Object %s not a commit",
333 sha1_to_hex(item->object.sha1));
335 ret = parse_commit_buffer(item, buffer, size);
336 if (save_commit_buffer && !ret) {
337 item->buffer = buffer;
344 int find_commit_subject(const char *commit_buffer, const char **subject)
347 const char *p = commit_buffer;
349 while (*p && (*p != '\n' || p[1] != '\n'))
353 for (eol = p; *eol && *eol != '\n'; eol++)
363 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
365 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
366 new_list->item = item;
367 new_list->next = *list_p;
372 unsigned commit_list_count(const struct commit_list *l)
375 for (; l; l = l->next )
380 void free_commit_list(struct commit_list *list)
383 struct commit_list *temp = list;
389 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
391 struct commit_list **pp = list;
392 struct commit_list *p;
393 while ((p = *pp) != NULL) {
394 if (p->item->date < item->date) {
399 return commit_list_insert(item, pp);
403 void commit_list_sort_by_date(struct commit_list **list)
405 struct commit_list *ret = NULL;
407 commit_list_insert_by_date((*list)->item, &ret);
408 *list = (*list)->next;
413 struct commit *pop_most_recent_commit(struct commit_list **list,
416 struct commit *ret = (*list)->item;
417 struct commit_list *parents = ret->parents;
418 struct commit_list *old = *list;
420 *list = (*list)->next;
424 struct commit *commit = parents->item;
425 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
426 commit->object.flags |= mark;
427 commit_list_insert_by_date(commit, list);
429 parents = parents->next;
434 void clear_commit_marks(struct commit *commit, unsigned int mark)
437 struct commit_list *parents;
439 if (!(mark & commit->object.flags))
442 commit->object.flags &= ~mark;
444 parents = commit->parents;
448 while ((parents = parents->next))
449 clear_commit_marks(parents->item, mark);
451 commit = commit->parents->item;
455 struct commit *pop_commit(struct commit_list **stack)
457 struct commit_list *top = *stack;
458 struct commit *item = top ? top->item : NULL;
468 * Performs an in-place topological sort on the list supplied.
470 void sort_in_topological_order(struct commit_list ** list, int lifo)
472 struct commit_list *next, *orig = *list;
473 struct commit_list *work, **insert;
474 struct commit_list **pptr;
480 /* Mark them and clear the indegree */
481 for (next = orig; next; next = next->next) {
482 struct commit *commit = next->item;
483 commit->indegree = 1;
486 /* update the indegree */
487 for (next = orig; next; next = next->next) {
488 struct commit_list * parents = next->item->parents;
490 struct commit *parent = parents->item;
492 if (parent->indegree)
494 parents = parents->next;
501 * tips are nodes not reachable from any other node in the list
503 * the tips serve as a starting set for the work queue.
507 for (next = orig; next; next = next->next) {
508 struct commit *commit = next->item;
510 if (commit->indegree == 1)
511 insert = &commit_list_insert(commit, insert)->next;
514 /* process the list in topological order */
516 commit_list_sort_by_date(&work);
521 struct commit *commit;
522 struct commit_list *parents, *work_item;
525 work = work_item->next;
526 work_item->next = NULL;
528 commit = work_item->item;
529 for (parents = commit->parents; parents ; parents = parents->next) {
530 struct commit *parent=parents->item;
532 if (!parent->indegree)
536 * parents are only enqueued for emission
537 * when all their children have been emitted thereby
538 * guaranteeing topological order.
540 if (--parent->indegree == 1) {
542 commit_list_insert_by_date(parent, &work);
544 commit_list_insert(parent, &work);
548 * work_item is a commit all of whose children
549 * have already been emitted. we can emit it now.
551 commit->indegree = 0;
553 pptr = &work_item->next;
557 /* merge-base stuff */
559 /* bits #0..15 in revision.h */
560 #define PARENT1 (1u<<16)
561 #define PARENT2 (1u<<17)
562 #define STALE (1u<<18)
563 #define RESULT (1u<<19)
565 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
567 static struct commit *interesting(struct commit_list *list)
570 struct commit *commit = list->item;
572 if (commit->object.flags & STALE)
579 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
581 struct commit_list *list = NULL;
582 struct commit_list *result = NULL;
585 for (i = 0; i < n; i++) {
588 * We do not mark this even with RESULT so we do not
589 * have to clean it up.
591 return commit_list_insert(one, &result);
594 if (parse_commit(one))
596 for (i = 0; i < n; i++) {
597 if (parse_commit(twos[i]))
601 one->object.flags |= PARENT1;
602 commit_list_insert_by_date(one, &list);
603 for (i = 0; i < n; i++) {
604 twos[i]->object.flags |= PARENT2;
605 commit_list_insert_by_date(twos[i], &list);
608 while (interesting(list)) {
609 struct commit *commit;
610 struct commit_list *parents;
611 struct commit_list *next;
619 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
620 if (flags == (PARENT1 | PARENT2)) {
621 if (!(commit->object.flags & RESULT)) {
622 commit->object.flags |= RESULT;
623 commit_list_insert_by_date(commit, &result);
625 /* Mark parents of a found merge stale */
628 parents = commit->parents;
630 struct commit *p = parents->item;
631 parents = parents->next;
632 if ((p->object.flags & flags) == flags)
636 p->object.flags |= flags;
637 commit_list_insert_by_date(p, &list);
641 /* Clean up the result to remove stale ones */
642 free_commit_list(list);
643 list = result; result = NULL;
645 struct commit_list *next = list->next;
646 if (!(list->item->object.flags & STALE))
647 commit_list_insert_by_date(list->item, &result);
654 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
656 struct commit_list *i, *j, *k, *ret = NULL;
657 struct commit_list **pptr = &ret;
659 for (i = in; i; i = i->next) {
661 pptr = &commit_list_insert(i->item, pptr)->next;
663 struct commit_list *new = NULL, *end = NULL;
665 for (j = ret; j; j = j->next) {
666 struct commit_list *bases;
667 bases = get_merge_bases(i->item, j->item, 1);
672 for (k = bases; k; k = k->next)
681 struct commit_list *get_merge_bases_many(struct commit *one,
683 struct commit **twos,
686 struct commit_list *list;
687 struct commit **rslt;
688 struct commit_list *result;
691 result = merge_bases_many(one, n, twos);
692 for (i = 0; i < n; i++) {
696 if (!result || !result->next) {
698 clear_commit_marks(one, all_flags);
699 for (i = 0; i < n; i++)
700 clear_commit_marks(twos[i], all_flags);
705 /* There are more than one */
712 rslt = xcalloc(cnt, sizeof(*rslt));
713 for (list = result, i = 0; list; list = list->next)
714 rslt[i++] = list->item;
715 free_commit_list(result);
717 clear_commit_marks(one, all_flags);
718 for (i = 0; i < n; i++)
719 clear_commit_marks(twos[i], all_flags);
720 for (i = 0; i < cnt - 1; i++) {
721 for (j = i+1; j < cnt; j++) {
722 if (!rslt[i] || !rslt[j])
724 result = merge_bases_many(rslt[i], 1, &rslt[j]);
725 clear_commit_marks(rslt[i], all_flags);
726 clear_commit_marks(rslt[j], all_flags);
727 for (list = result; list; list = list->next) {
728 if (rslt[i] == list->item)
730 if (rslt[j] == list->item)
736 /* Surviving ones in rslt[] are the independent results */
738 for (i = 0; i < cnt; i++) {
740 commit_list_insert_by_date(rslt[i], &result);
746 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
749 return get_merge_bases_many(one, 1, &two, cleanup);
752 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
756 while (with_commit) {
757 struct commit *other;
759 other = with_commit->item;
760 with_commit = with_commit->next;
761 if (in_merge_bases(other, &commit, 1))
767 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
769 struct commit_list *bases, *b;
773 bases = get_merge_bases(commit, *reference, 1);
776 for (b = bases; b; b = b->next) {
777 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
783 free_commit_list(bases);
787 struct commit_list *reduce_heads(struct commit_list *heads)
789 struct commit_list *p;
790 struct commit_list *result = NULL, **tail = &result;
791 struct commit **other;
792 size_t num_head, num_other;
797 /* Avoid unnecessary reallocations */
798 for (p = heads, num_head = 0; p; p = p->next)
800 other = xcalloc(sizeof(*other), num_head);
802 /* For each commit, see if it can be reached by others */
803 for (p = heads; p; p = p->next) {
804 struct commit_list *q, *base;
806 /* Do we already have this in the result? */
807 for (q = result; q; q = q->next)
808 if (p->item == q->item)
814 for (q = heads; q; q = q->next) {
815 if (p->item == q->item)
817 other[num_other++] = q->item;
820 base = get_merge_bases_many(p->item, num_other, other, 1);
824 * If p->item does not have anything common with other
825 * commits, there won't be any merge base. If it is
826 * reachable from some of the others, p->item will be
827 * the merge base. If its history is connected with
828 * others, but p->item is not reachable by others, we
829 * will get something other than p->item back.
831 if (!base || (base->item != p->item))
832 tail = &(commit_list_insert(p->item, tail)->next);
833 free_commit_list(base);
839 static const char commit_utf8_warn[] =
840 "Warning: commit message does not conform to UTF-8.\n"
841 "You may want to amend it after fixing the message, or set the config\n"
842 "variable i18n.commitencoding to the encoding your project uses.\n";
844 int commit_tree(const char *msg, unsigned char *tree,
845 struct commit_list *parents, unsigned char *ret,
849 int encoding_is_utf8;
850 struct strbuf buffer;
852 assert_sha1_type(tree, OBJ_TREE);
854 /* Not having i18n.commitencoding is the same as having utf-8 */
855 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
857 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
858 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
861 * NOTE! This ordering means that the same exact tree merged with a
862 * different order of parents will be a _different_ changeset even
863 * if everything else stays the same.
866 struct commit_list *next = parents->next;
867 strbuf_addf(&buffer, "parent %s\n",
868 sha1_to_hex(parents->item->object.sha1));
873 /* Person/date information */
875 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
876 strbuf_addf(&buffer, "author %s\n", author);
877 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
878 if (!encoding_is_utf8)
879 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
880 strbuf_addch(&buffer, '\n');
882 /* And add the comment */
883 strbuf_addstr(&buffer, msg);
885 /* And check the encoding */
886 if (encoding_is_utf8 && !is_utf8(buffer.buf))
887 fprintf(stderr, commit_utf8_warn);
889 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
890 strbuf_release(&buffer);