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 for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
232 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
233 ret = fn(commit_graft[i], cb_data);
237 int unregister_shallow(const unsigned char *sha1)
239 int pos = commit_graft_pos(sha1);
242 if (pos + 1 < commit_graft_nr)
243 memmove(commit_graft + pos, commit_graft + pos + 1,
244 sizeof(struct commit_graft *)
245 * (commit_graft_nr - pos - 1));
250 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
252 const char *tail = buffer;
253 const char *bufptr = buffer;
254 unsigned char parent[20];
255 struct commit_list **pptr;
256 struct commit_graft *graft;
258 if (item->object.parsed)
260 item->object.parsed = 1;
262 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
263 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
264 if (get_sha1_hex(bufptr + 5, parent) < 0)
265 return error("bad tree pointer in commit %s",
266 sha1_to_hex(item->object.sha1));
267 item->tree = lookup_tree(parent);
268 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
269 pptr = &item->parents;
271 graft = lookup_commit_graft(item->object.sha1);
272 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
273 struct commit *new_parent;
275 if (tail <= bufptr + 48 ||
276 get_sha1_hex(bufptr + 7, parent) ||
278 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
281 * The clone is shallow if nr_parent < 0, and we must
282 * not traverse its real parents even when we unhide them.
284 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
286 new_parent = lookup_commit(parent);
288 pptr = &commit_list_insert(new_parent, pptr)->next;
292 struct commit *new_parent;
293 for (i = 0; i < graft->nr_parent; i++) {
294 new_parent = lookup_commit(graft->parent[i]);
297 pptr = &commit_list_insert(new_parent, pptr)->next;
300 item->date = parse_commit_date(bufptr, tail);
305 int parse_commit(struct commit *item)
307 enum object_type type;
314 if (item->object.parsed)
316 buffer = read_sha1_file(item->object.sha1, &type, &size);
318 return error("Could not read %s",
319 sha1_to_hex(item->object.sha1));
320 if (type != OBJ_COMMIT) {
322 return error("Object %s not a commit",
323 sha1_to_hex(item->object.sha1));
325 ret = parse_commit_buffer(item, buffer, size);
326 if (save_commit_buffer && !ret) {
327 item->buffer = buffer;
334 int find_commit_subject(const char *commit_buffer, const char **subject)
337 const char *p = commit_buffer;
339 while (*p && (*p != '\n' || p[1] != '\n'))
343 for (eol = p; *eol && *eol != '\n'; eol++)
353 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
355 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
356 new_list->item = item;
357 new_list->next = *list_p;
362 unsigned commit_list_count(const struct commit_list *l)
365 for (; l; l = l->next )
370 void free_commit_list(struct commit_list *list)
373 struct commit_list *temp = list;
379 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
381 struct commit_list **pp = list;
382 struct commit_list *p;
383 while ((p = *pp) != NULL) {
384 if (p->item->date < item->date) {
389 return commit_list_insert(item, pp);
393 void commit_list_sort_by_date(struct commit_list **list)
395 struct commit_list *ret = NULL;
397 commit_list_insert_by_date((*list)->item, &ret);
398 *list = (*list)->next;
403 struct commit *pop_most_recent_commit(struct commit_list **list,
406 struct commit *ret = (*list)->item;
407 struct commit_list *parents = ret->parents;
408 struct commit_list *old = *list;
410 *list = (*list)->next;
414 struct commit *commit = parents->item;
415 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
416 commit->object.flags |= mark;
417 commit_list_insert_by_date(commit, list);
419 parents = parents->next;
424 void clear_commit_marks(struct commit *commit, unsigned int mark)
427 struct commit_list *parents;
429 if (!(mark & commit->object.flags))
432 commit->object.flags &= ~mark;
434 parents = commit->parents;
438 while ((parents = parents->next))
439 clear_commit_marks(parents->item, mark);
441 commit = commit->parents->item;
445 void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
447 struct object *object;
448 struct commit *commit;
451 for (i = 0; i < a->nr; i++) {
452 object = a->objects[i].item;
453 commit = lookup_commit_reference_gently(object->sha1, 1);
455 clear_commit_marks(commit, mark);
459 struct commit *pop_commit(struct commit_list **stack)
461 struct commit_list *top = *stack;
462 struct commit *item = top ? top->item : NULL;
472 * Performs an in-place topological sort on the list supplied.
474 void sort_in_topological_order(struct commit_list ** list, int lifo)
476 struct commit_list *next, *orig = *list;
477 struct commit_list *work, **insert;
478 struct commit_list **pptr;
484 /* Mark them and clear the indegree */
485 for (next = orig; next; next = next->next) {
486 struct commit *commit = next->item;
487 commit->indegree = 1;
490 /* update the indegree */
491 for (next = orig; next; next = next->next) {
492 struct commit_list * parents = next->item->parents;
494 struct commit *parent = parents->item;
496 if (parent->indegree)
498 parents = parents->next;
505 * tips are nodes not reachable from any other node in the list
507 * the tips serve as a starting set for the work queue.
511 for (next = orig; next; next = next->next) {
512 struct commit *commit = next->item;
514 if (commit->indegree == 1)
515 insert = &commit_list_insert(commit, insert)->next;
518 /* process the list in topological order */
520 commit_list_sort_by_date(&work);
525 struct commit *commit;
526 struct commit_list *parents, *work_item;
529 work = work_item->next;
530 work_item->next = NULL;
532 commit = work_item->item;
533 for (parents = commit->parents; parents ; parents = parents->next) {
534 struct commit *parent = parents->item;
536 if (!parent->indegree)
540 * parents are only enqueued for emission
541 * when all their children have been emitted thereby
542 * guaranteeing topological order.
544 if (--parent->indegree == 1) {
546 commit_list_insert_by_date(parent, &work);
548 commit_list_insert(parent, &work);
552 * work_item is a commit all of whose children
553 * have already been emitted. we can emit it now.
555 commit->indegree = 0;
557 pptr = &work_item->next;
561 /* merge-base stuff */
563 /* bits #0..15 in revision.h */
564 #define PARENT1 (1u<<16)
565 #define PARENT2 (1u<<17)
566 #define STALE (1u<<18)
567 #define RESULT (1u<<19)
569 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
571 static struct commit *interesting(struct commit_list *list)
574 struct commit *commit = list->item;
576 if (commit->object.flags & STALE)
583 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
585 struct commit_list *list = NULL;
586 struct commit_list *result = NULL;
589 for (i = 0; i < n; i++) {
592 * We do not mark this even with RESULT so we do not
593 * have to clean it up.
595 return commit_list_insert(one, &result);
598 if (parse_commit(one))
600 for (i = 0; i < n; i++) {
601 if (parse_commit(twos[i]))
605 one->object.flags |= PARENT1;
606 commit_list_insert_by_date(one, &list);
607 for (i = 0; i < n; i++) {
608 twos[i]->object.flags |= PARENT2;
609 commit_list_insert_by_date(twos[i], &list);
612 while (interesting(list)) {
613 struct commit *commit;
614 struct commit_list *parents;
615 struct commit_list *next;
623 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
624 if (flags == (PARENT1 | PARENT2)) {
625 if (!(commit->object.flags & RESULT)) {
626 commit->object.flags |= RESULT;
627 commit_list_insert_by_date(commit, &result);
629 /* Mark parents of a found merge stale */
632 parents = commit->parents;
634 struct commit *p = parents->item;
635 parents = parents->next;
636 if ((p->object.flags & flags) == flags)
640 p->object.flags |= flags;
641 commit_list_insert_by_date(p, &list);
645 /* Clean up the result to remove stale ones */
646 free_commit_list(list);
647 list = result; result = NULL;
649 struct commit_list *next = list->next;
650 if (!(list->item->object.flags & STALE))
651 commit_list_insert_by_date(list->item, &result);
658 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
660 struct commit_list *i, *j, *k, *ret = NULL;
661 struct commit_list **pptr = &ret;
663 for (i = in; i; i = i->next) {
665 pptr = &commit_list_insert(i->item, pptr)->next;
667 struct commit_list *new = NULL, *end = NULL;
669 for (j = ret; j; j = j->next) {
670 struct commit_list *bases;
671 bases = get_merge_bases(i->item, j->item, 1);
676 for (k = bases; k; k = k->next)
685 struct commit_list *get_merge_bases_many(struct commit *one,
687 struct commit **twos,
690 struct commit_list *list;
691 struct commit **rslt;
692 struct commit_list *result;
695 result = merge_bases_many(one, n, twos);
696 for (i = 0; i < n; i++) {
700 if (!result || !result->next) {
702 clear_commit_marks(one, all_flags);
703 for (i = 0; i < n; i++)
704 clear_commit_marks(twos[i], all_flags);
709 /* There are more than one */
716 rslt = xcalloc(cnt, sizeof(*rslt));
717 for (list = result, i = 0; list; list = list->next)
718 rslt[i++] = list->item;
719 free_commit_list(result);
721 clear_commit_marks(one, all_flags);
722 for (i = 0; i < n; i++)
723 clear_commit_marks(twos[i], all_flags);
724 for (i = 0; i < cnt - 1; i++) {
725 for (j = i+1; j < cnt; j++) {
726 if (!rslt[i] || !rslt[j])
728 result = merge_bases_many(rslt[i], 1, &rslt[j]);
729 clear_commit_marks(rslt[i], all_flags);
730 clear_commit_marks(rslt[j], all_flags);
731 for (list = result; list; list = list->next) {
732 if (rslt[i] == list->item)
734 if (rslt[j] == list->item)
740 /* Surviving ones in rslt[] are the independent results */
742 for (i = 0; i < cnt; i++) {
744 commit_list_insert_by_date(rslt[i], &result);
750 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
753 return get_merge_bases_many(one, 1, &two, cleanup);
756 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
760 while (with_commit) {
761 struct commit *other;
763 other = with_commit->item;
764 with_commit = with_commit->next;
765 if (in_merge_bases(other, &commit, 1))
771 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
773 struct commit_list *bases, *b;
777 bases = get_merge_bases(commit, *reference, 1);
780 for (b = bases; b; b = b->next) {
781 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
787 free_commit_list(bases);
791 struct commit_list *reduce_heads(struct commit_list *heads)
793 struct commit_list *p;
794 struct commit_list *result = NULL, **tail = &result;
795 struct commit **other;
796 size_t num_head, num_other;
801 /* Avoid unnecessary reallocations */
802 for (p = heads, num_head = 0; p; p = p->next)
804 other = xcalloc(sizeof(*other), num_head);
806 /* For each commit, see if it can be reached by others */
807 for (p = heads; p; p = p->next) {
808 struct commit_list *q, *base;
810 /* Do we already have this in the result? */
811 for (q = result; q; q = q->next)
812 if (p->item == q->item)
818 for (q = heads; q; q = q->next) {
819 if (p->item == q->item)
821 other[num_other++] = q->item;
824 base = get_merge_bases_many(p->item, num_other, other, 1);
828 * If p->item does not have anything common with other
829 * commits, there won't be any merge base. If it is
830 * reachable from some of the others, p->item will be
831 * the merge base. If its history is connected with
832 * others, but p->item is not reachable by others, we
833 * will get something other than p->item back.
835 if (!base || (base->item != p->item))
836 tail = &(commit_list_insert(p->item, tail)->next);
837 free_commit_list(base);
843 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
845 struct merge_remote_desc *desc;
846 struct commit_extra_header *mergetag;
848 unsigned long size, len;
849 enum object_type type;
851 desc = merge_remote_util(parent);
852 if (!desc || !desc->obj)
854 buf = read_sha1_file(desc->obj->sha1, &type, &size);
855 if (!buf || type != OBJ_TAG)
857 len = parse_signature(buf, size);
861 * We could verify this signature and either omit the tag when
862 * it does not validate, but the integrator may not have the
863 * public key of the signer of the tag he is merging, while a
864 * later auditor may have it while auditing, so let's not run
865 * verify-signed-buffer here for now...
867 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
868 * warn("warning: signed tag unverified.");
870 mergetag = xcalloc(1, sizeof(*mergetag));
871 mergetag->key = xstrdup("mergetag");
872 mergetag->value = buf;
873 mergetag->len = size;
876 *tail = &mergetag->next;
883 void append_merge_tag_headers(struct commit_list *parents,
884 struct commit_extra_header ***tail)
887 struct commit *parent = parents->item;
888 handle_signed_tag(parent, tail);
889 parents = parents->next;
893 static void add_extra_header(struct strbuf *buffer,
894 struct commit_extra_header *extra)
896 strbuf_addstr(buffer, extra->key);
898 strbuf_add_lines(buffer, " ", extra->value, extra->len);
900 strbuf_addch(buffer, '\n');
903 struct commit_extra_header *read_commit_extra_headers(struct commit *commit)
905 struct commit_extra_header *extra = NULL;
907 enum object_type type;
908 char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
909 if (buffer && type == OBJ_COMMIT)
910 extra = read_commit_extra_header_lines(buffer, size);
915 static inline int standard_header_field(const char *field, size_t len)
917 return ((len == 4 && !memcmp(field, "tree ", 5)) ||
918 (len == 6 && !memcmp(field, "parent ", 7)) ||
919 (len == 6 && !memcmp(field, "author ", 7)) ||
920 (len == 9 && !memcmp(field, "committer ", 10)) ||
921 (len == 8 && !memcmp(field, "encoding ", 9)));
924 struct commit_extra_header *read_commit_extra_header_lines(const char *buffer, size_t size)
926 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
927 const char *line, *next, *eof, *eob;
928 struct strbuf buf = STRBUF_INIT;
930 for (line = buffer, eob = line + size;
931 line < eob && *line != '\n';
933 next = memchr(line, '\n', eob - line);
934 next = next ? next + 1 : eob;
938 strbuf_add(&buf, line + 1, next - (line + 1));
942 it->value = strbuf_detach(&buf, &it->len);
946 eof = strchr(line, ' ');
950 if (standard_header_field(line, eof - line))
953 it = xcalloc(1, sizeof(*it));
954 it->key = xmemdupz(line, eof-line);
958 strbuf_add(&buf, eof + 1, next - (eof + 1));
961 it->value = strbuf_detach(&buf, &it->len);
965 void free_commit_extra_headers(struct commit_extra_header *extra)
968 struct commit_extra_header *next = extra->next;
976 int commit_tree(const char *msg, unsigned char *tree,
977 struct commit_list *parents, unsigned char *ret,
980 struct commit_extra_header *extra = NULL, **tail = &extra;
983 append_merge_tag_headers(parents, &tail);
984 result = commit_tree_extended(msg, tree, parents, ret, author, extra);
985 free_commit_extra_headers(extra);
989 static const char commit_utf8_warn[] =
990 "Warning: commit message does not conform to UTF-8.\n"
991 "You may want to amend it after fixing the message, or set the config\n"
992 "variable i18n.commitencoding to the encoding your project uses.\n";
994 int commit_tree_extended(const char *msg, unsigned char *tree,
995 struct commit_list *parents, unsigned char *ret,
996 const char *author, struct commit_extra_header *extra)
999 int encoding_is_utf8;
1000 struct strbuf buffer;
1002 assert_sha1_type(tree, OBJ_TREE);
1004 /* Not having i18n.commitencoding is the same as having utf-8 */
1005 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1007 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1008 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1011 * NOTE! This ordering means that the same exact tree merged with a
1012 * different order of parents will be a _different_ changeset even
1013 * if everything else stays the same.
1016 struct commit_list *next = parents->next;
1017 struct commit *parent = parents->item;
1019 strbuf_addf(&buffer, "parent %s\n",
1020 sha1_to_hex(parent->object.sha1));
1025 /* Person/date information */
1027 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
1028 strbuf_addf(&buffer, "author %s\n", author);
1029 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
1030 if (!encoding_is_utf8)
1031 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1034 add_extra_header(&buffer, extra);
1035 extra = extra->next;
1037 strbuf_addch(&buffer, '\n');
1039 /* And add the comment */
1040 strbuf_addstr(&buffer, msg);
1042 /* And check the encoding */
1043 if (encoding_is_utf8 && !is_utf8(buffer.buf))
1044 fprintf(stderr, commit_utf8_warn);
1046 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1047 strbuf_release(&buffer);
1051 struct commit *get_merge_parent(const char *name)
1054 struct commit *commit;
1055 unsigned char sha1[20];
1056 if (get_sha1(name, sha1))
1058 obj = parse_object(sha1);
1059 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1060 if (commit && !commit->util) {
1061 struct merge_remote_desc *desc;
1062 desc = xmalloc(sizeof(*desc));
1064 desc->name = strdup(name);
1065 commit->util = desc;