4 #include "commit-graph.h"
10 #include "gpg-interface.h"
11 #include "mergesort.h"
12 #include "commit-slab.h"
13 #include "prio-queue.h"
14 #include "sha1-lookup.h"
15 #include "wt-status.h"
18 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
20 int save_commit_buffer = 1;
22 const char *commit_type = "commit";
24 struct commit *lookup_commit_reference_gently(const struct object_id *oid,
27 struct object *obj = deref_tag(parse_object(oid), NULL, 0);
31 return object_as_type(obj, OBJ_COMMIT, quiet);
34 struct commit *lookup_commit_reference(const struct object_id *oid)
36 return lookup_commit_reference_gently(oid, 0);
39 struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name)
41 struct commit *c = lookup_commit_reference(oid);
43 die(_("could not parse %s"), ref_name);
44 if (oidcmp(oid, &c->object.oid)) {
45 warning(_("%s %s is not a commit!"),
46 ref_name, oid_to_hex(oid));
51 struct commit *lookup_commit(const struct object_id *oid)
53 struct object *obj = lookup_object(oid->hash);
55 return create_object(oid->hash, alloc_commit_node());
56 return object_as_type(obj, OBJ_COMMIT, 0);
59 struct commit *lookup_commit_reference_by_name(const char *name)
62 struct commit *commit;
64 if (get_oid_committish(name, &oid))
66 commit = lookup_commit_reference(&oid);
67 if (parse_commit(commit))
72 static timestamp_t parse_commit_date(const char *buf, const char *tail)
78 if (memcmp(buf, "author", 6))
80 while (buf < tail && *buf++ != '\n')
84 if (memcmp(buf, "committer", 9))
86 while (buf < tail && *buf++ != '>')
91 while (buf < tail && *buf++ != '\n')
95 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
96 return parse_timestamp(dateptr, NULL, 10);
99 static struct commit_graft **commit_graft;
100 static int commit_graft_alloc, commit_graft_nr;
102 static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
104 struct commit_graft **commit_graft_table = table;
105 return commit_graft_table[index]->oid.hash;
108 static int commit_graft_pos(const unsigned char *sha1)
110 return sha1_pos(sha1, commit_graft, commit_graft_nr,
111 commit_graft_sha1_access);
114 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
116 int pos = commit_graft_pos(graft->oid.hash);
122 free(commit_graft[pos]);
123 commit_graft[pos] = graft;
128 ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc);
130 if (pos < commit_graft_nr)
131 MOVE_ARRAY(commit_graft + pos + 1, commit_graft + pos,
132 commit_graft_nr - pos - 1);
133 commit_graft[pos] = graft;
137 struct commit_graft *read_graft_line(struct strbuf *line)
139 /* The format is just "Commit Parent1 Parent2 ...\n" */
141 const char *tail = NULL;
142 struct commit_graft *graft = NULL;
143 struct object_id dummy_oid, *oid;
146 if (!line->len || line->buf[0] == '#')
149 * phase 0 verifies line, counts hashes in line and allocates graft
150 * phase 1 fills graft
152 for (phase = 0; phase < 2; phase++) {
153 oid = graft ? &graft->oid : &dummy_oid;
154 if (parse_oid_hex(line->buf, oid, &tail))
156 for (i = 0; *tail != '\0'; i++) {
157 oid = graft ? &graft->parent[i] : &dummy_oid;
158 if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
162 graft = xmalloc(st_add(sizeof(*graft),
163 st_mult(sizeof(struct object_id), i)));
164 graft->nr_parent = i;
170 error("bad graft data: %s", line->buf);
175 static int read_graft_file(const char *graft_file)
177 FILE *fp = fopen_or_warn(graft_file, "r");
178 struct strbuf buf = STRBUF_INIT;
181 if (advice_graft_file_deprecated)
182 advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
183 "and will be removed in a future Git version.\n"
185 "Please use \"git replace --convert-graft-file\"\n"
186 "to convert the grafts into replace refs.\n"
188 "Turn this message off by running\n"
189 "\"git config advice.graftFileDeprecated false\""));
190 while (!strbuf_getwholeline(&buf, fp, '\n')) {
191 /* The format is just "Commit Parent1 Parent2 ...\n" */
192 struct commit_graft *graft = read_graft_line(&buf);
195 if (register_commit_graft(graft, 1))
196 error("duplicate graft data: %s", buf.buf);
199 strbuf_release(&buf);
203 static void prepare_commit_graft(void)
205 static int commit_graft_prepared;
208 if (commit_graft_prepared)
210 if (!startup_info->have_repository)
213 graft_file = get_graft_file();
214 read_graft_file(graft_file);
215 /* make sure shallows are read */
216 is_repository_shallow();
217 commit_graft_prepared = 1;
220 struct commit_graft *lookup_commit_graft(const struct object_id *oid)
223 prepare_commit_graft();
224 pos = commit_graft_pos(oid->hash);
227 return commit_graft[pos];
230 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
233 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
234 ret = fn(commit_graft[i], cb_data);
238 int unregister_shallow(const struct object_id *oid)
240 int pos = commit_graft_pos(oid->hash);
243 if (pos + 1 < commit_graft_nr)
244 MOVE_ARRAY(commit_graft + pos, commit_graft + pos + 1,
245 commit_graft_nr - pos - 1);
250 struct commit_buffer {
254 define_commit_slab(buffer_slab, struct commit_buffer);
255 static struct buffer_slab buffer_slab = COMMIT_SLAB_INIT(1, buffer_slab);
257 void set_commit_buffer(struct commit *commit, void *buffer, unsigned long size)
259 struct commit_buffer *v = buffer_slab_at(&buffer_slab, commit);
264 const void *get_cached_commit_buffer(const struct commit *commit, unsigned long *sizep)
266 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
277 const void *get_commit_buffer(const struct commit *commit, unsigned long *sizep)
279 const void *ret = get_cached_commit_buffer(commit, sizep);
281 enum object_type type;
283 ret = read_object_file(&commit->object.oid, &type, &size);
285 die("cannot read commit object %s",
286 oid_to_hex(&commit->object.oid));
287 if (type != OBJ_COMMIT)
288 die("expected commit for %s, got %s",
289 oid_to_hex(&commit->object.oid), type_name(type));
296 void unuse_commit_buffer(const struct commit *commit, const void *buffer)
298 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
299 if (!(v && v->buffer == buffer))
300 free((void *)buffer);
303 void free_commit_buffer(struct commit *commit)
305 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
307 FREE_AND_NULL(v->buffer);
312 struct tree *get_commit_tree(const struct commit *commit)
314 if (commit->maybe_tree || !commit->object.parsed)
315 return commit->maybe_tree;
317 if (commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
318 BUG("commit has NULL tree, but was not loaded from commit-graph");
320 return get_commit_tree_in_graph(commit);
323 struct object_id *get_commit_tree_oid(const struct commit *commit)
325 return &get_commit_tree(commit)->object.oid;
328 const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
330 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
347 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size, int check_graph)
349 const char *tail = buffer;
350 const char *bufptr = buffer;
351 struct object_id parent;
352 struct commit_list **pptr;
353 struct commit_graft *graft;
354 const int tree_entry_len = GIT_SHA1_HEXSZ + 5;
355 const int parent_entry_len = GIT_SHA1_HEXSZ + 7;
357 if (item->object.parsed)
359 item->object.parsed = 1;
361 if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
362 bufptr[tree_entry_len] != '\n')
363 return error("bogus commit object %s", oid_to_hex(&item->object.oid));
364 if (get_oid_hex(bufptr + 5, &parent) < 0)
365 return error("bad tree pointer in commit %s",
366 oid_to_hex(&item->object.oid));
367 item->maybe_tree = lookup_tree(&parent);
368 bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
369 pptr = &item->parents;
371 graft = lookup_commit_graft(&item->object.oid);
372 while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
373 struct commit *new_parent;
375 if (tail <= bufptr + parent_entry_len + 1 ||
376 get_oid_hex(bufptr + 7, &parent) ||
377 bufptr[parent_entry_len] != '\n')
378 return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
379 bufptr += parent_entry_len + 1;
381 * The clone is shallow if nr_parent < 0, and we must
382 * not traverse its real parents even when we unhide them.
384 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
386 new_parent = lookup_commit(&parent);
388 pptr = &commit_list_insert(new_parent, pptr)->next;
392 struct commit *new_parent;
393 for (i = 0; i < graft->nr_parent; i++) {
394 new_parent = lookup_commit(&graft->parent[i]);
397 pptr = &commit_list_insert(new_parent, pptr)->next;
400 item->date = parse_commit_date(bufptr, tail);
403 load_commit_graph_info(item);
408 int parse_commit_gently(struct commit *item, int quiet_on_missing)
410 enum object_type type;
417 if (item->object.parsed)
419 if (parse_commit_in_graph(item))
421 buffer = read_object_file(&item->object.oid, &type, &size);
423 return quiet_on_missing ? -1 :
424 error("Could not read %s",
425 oid_to_hex(&item->object.oid));
426 if (type != OBJ_COMMIT) {
428 return error("Object %s not a commit",
429 oid_to_hex(&item->object.oid));
431 ret = parse_commit_buffer(item, buffer, size, 0);
432 if (save_commit_buffer && !ret) {
433 set_commit_buffer(item, buffer, size);
440 void parse_commit_or_die(struct commit *item)
442 if (parse_commit(item))
443 die("unable to parse commit %s",
444 item ? oid_to_hex(&item->object.oid) : "(null)");
447 int find_commit_subject(const char *commit_buffer, const char **subject)
450 const char *p = commit_buffer;
452 while (*p && (*p != '\n' || p[1] != '\n'))
455 p = skip_blank_lines(p + 2);
456 eol = strchrnul(p, '\n');
465 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
467 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
468 new_list->item = item;
469 new_list->next = *list_p;
474 unsigned commit_list_count(const struct commit_list *l)
477 for (; l; l = l->next )
482 struct commit_list *copy_commit_list(struct commit_list *list)
484 struct commit_list *head = NULL;
485 struct commit_list **pp = &head;
487 pp = commit_list_append(list->item, pp);
493 void free_commit_list(struct commit_list *list)
499 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
501 struct commit_list **pp = list;
502 struct commit_list *p;
503 while ((p = *pp) != NULL) {
504 if (p->item->date < item->date) {
509 return commit_list_insert(item, pp);
512 static int commit_list_compare_by_date(const void *a, const void *b)
514 timestamp_t a_date = ((const struct commit_list *)a)->item->date;
515 timestamp_t b_date = ((const struct commit_list *)b)->item->date;
523 static void *commit_list_get_next(const void *a)
525 return ((const struct commit_list *)a)->next;
528 static void commit_list_set_next(void *a, void *next)
530 ((struct commit_list *)a)->next = next;
533 void commit_list_sort_by_date(struct commit_list **list)
535 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
536 commit_list_compare_by_date);
539 struct commit *pop_most_recent_commit(struct commit_list **list,
542 struct commit *ret = pop_commit(list);
543 struct commit_list *parents = ret->parents;
546 struct commit *commit = parents->item;
547 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
548 commit->object.flags |= mark;
549 commit_list_insert_by_date(commit, list);
551 parents = parents->next;
556 static void clear_commit_marks_1(struct commit_list **plist,
557 struct commit *commit, unsigned int mark)
560 struct commit_list *parents;
562 if (!(mark & commit->object.flags))
565 commit->object.flags &= ~mark;
567 parents = commit->parents;
571 while ((parents = parents->next))
572 commit_list_insert(parents->item, plist);
574 commit = commit->parents->item;
578 void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
580 struct commit_list *list = NULL;
583 clear_commit_marks_1(&list, *commit, mark);
587 clear_commit_marks_1(&list, pop_commit(&list), mark);
590 void clear_commit_marks(struct commit *commit, unsigned int mark)
592 clear_commit_marks_many(1, &commit, mark);
595 struct commit *pop_commit(struct commit_list **stack)
597 struct commit_list *top = *stack;
598 struct commit *item = top ? top->item : NULL;
608 * Topological sort support
611 /* count number of children that have not been emitted */
612 define_commit_slab(indegree_slab, int);
614 /* record author-date for each commit object */
615 define_commit_slab(author_date_slab, unsigned long);
617 static void record_author_date(struct author_date_slab *author_date,
618 struct commit *commit)
620 const char *buffer = get_commit_buffer(commit, NULL);
621 struct ident_split ident;
622 const char *ident_line;
627 ident_line = find_commit_header(buffer, "author", &ident_len);
629 goto fail_exit; /* no author line */
630 if (split_ident_line(&ident, ident_line, ident_len) ||
631 !ident.date_begin || !ident.date_end)
632 goto fail_exit; /* malformed "author" line */
634 date = parse_timestamp(ident.date_begin, &date_end, 10);
635 if (date_end != ident.date_end)
636 goto fail_exit; /* malformed date */
637 *(author_date_slab_at(author_date, commit)) = date;
640 unuse_commit_buffer(commit, buffer);
643 static int compare_commits_by_author_date(const void *a_, const void *b_,
646 const struct commit *a = a_, *b = b_;
647 struct author_date_slab *author_date = cb_data;
648 timestamp_t a_date = *(author_date_slab_at(author_date, a));
649 timestamp_t b_date = *(author_date_slab_at(author_date, b));
651 /* newer commits with larger date first */
654 else if (a_date > b_date)
659 int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused)
661 const struct commit *a = a_, *b = b_;
663 /* newer commits first */
664 if (a->generation < b->generation)
666 else if (a->generation > b->generation)
669 /* use date as a heuristic when generations are equal */
670 if (a->date < b->date)
672 else if (a->date > b->date)
677 int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
679 const struct commit *a = a_, *b = b_;
680 /* newer commits with larger date first */
681 if (a->date < b->date)
683 else if (a->date > b->date)
689 * Performs an in-place topological sort on the list supplied.
691 void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
693 struct commit_list *next, *orig = *list;
694 struct commit_list **pptr;
695 struct indegree_slab indegree;
696 struct prio_queue queue;
697 struct commit *commit;
698 struct author_date_slab author_date;
704 init_indegree_slab(&indegree);
705 memset(&queue, '\0', sizeof(queue));
707 switch (sort_order) {
708 default: /* REV_SORT_IN_GRAPH_ORDER */
709 queue.compare = NULL;
711 case REV_SORT_BY_COMMIT_DATE:
712 queue.compare = compare_commits_by_commit_date;
714 case REV_SORT_BY_AUTHOR_DATE:
715 init_author_date_slab(&author_date);
716 queue.compare = compare_commits_by_author_date;
717 queue.cb_data = &author_date;
721 /* Mark them and clear the indegree */
722 for (next = orig; next; next = next->next) {
723 struct commit *commit = next->item;
724 *(indegree_slab_at(&indegree, commit)) = 1;
725 /* also record the author dates, if needed */
726 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
727 record_author_date(&author_date, commit);
730 /* update the indegree */
731 for (next = orig; next; next = next->next) {
732 struct commit_list *parents = next->item->parents;
734 struct commit *parent = parents->item;
735 int *pi = indegree_slab_at(&indegree, parent);
739 parents = parents->next;
746 * tips are nodes not reachable from any other node in the list
748 * the tips serve as a starting set for the work queue.
750 for (next = orig; next; next = next->next) {
751 struct commit *commit = next->item;
753 if (*(indegree_slab_at(&indegree, commit)) == 1)
754 prio_queue_put(&queue, commit);
758 * This is unfortunate; the initial tips need to be shown
759 * in the order given from the revision traversal machinery.
761 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
762 prio_queue_reverse(&queue);
764 /* We no longer need the commit list */
765 free_commit_list(orig);
769 while ((commit = prio_queue_get(&queue)) != NULL) {
770 struct commit_list *parents;
772 for (parents = commit->parents; parents ; parents = parents->next) {
773 struct commit *parent = parents->item;
774 int *pi = indegree_slab_at(&indegree, parent);
780 * parents are only enqueued for emission
781 * when all their children have been emitted thereby
782 * guaranteeing topological order.
785 prio_queue_put(&queue, parent);
788 * all children of commit have already been
789 * emitted. we can emit it now.
791 *(indegree_slab_at(&indegree, commit)) = 0;
793 pptr = &commit_list_insert(commit, pptr)->next;
796 clear_indegree_slab(&indegree);
797 clear_prio_queue(&queue);
798 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
799 clear_author_date_slab(&author_date);
802 /* merge-base stuff */
804 /* Remember to update object flag allocation in object.h */
805 #define PARENT1 (1u<<16)
806 #define PARENT2 (1u<<17)
807 #define STALE (1u<<18)
808 #define RESULT (1u<<19)
810 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
812 static int queue_has_nonstale(struct prio_queue *queue)
815 for (i = 0; i < queue->nr; i++) {
816 struct commit *commit = queue->array[i].data;
817 if (!(commit->object.flags & STALE))
823 /* all input commits in one and twos[] must have been parsed! */
824 static struct commit_list *paint_down_to_common(struct commit *one, int n,
825 struct commit **twos,
828 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
829 struct commit_list *result = NULL;
831 uint32_t last_gen = GENERATION_NUMBER_INFINITY;
833 one->object.flags |= PARENT1;
835 commit_list_append(one, &result);
838 prio_queue_put(&queue, one);
840 for (i = 0; i < n; i++) {
841 twos[i]->object.flags |= PARENT2;
842 prio_queue_put(&queue, twos[i]);
845 while (queue_has_nonstale(&queue)) {
846 struct commit *commit = prio_queue_get(&queue);
847 struct commit_list *parents;
850 if (commit->generation > last_gen)
851 BUG("bad generation skip %8x > %8x at %s",
852 commit->generation, last_gen,
853 oid_to_hex(&commit->object.oid));
854 last_gen = commit->generation;
856 if (commit->generation < min_generation)
859 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
860 if (flags == (PARENT1 | PARENT2)) {
861 if (!(commit->object.flags & RESULT)) {
862 commit->object.flags |= RESULT;
863 commit_list_insert_by_date(commit, &result);
865 /* Mark parents of a found merge stale */
868 parents = commit->parents;
870 struct commit *p = parents->item;
871 parents = parents->next;
872 if ((p->object.flags & flags) == flags)
876 p->object.flags |= flags;
877 prio_queue_put(&queue, p);
881 clear_prio_queue(&queue);
885 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
887 struct commit_list *list = NULL;
888 struct commit_list *result = NULL;
891 for (i = 0; i < n; i++) {
894 * We do not mark this even with RESULT so we do not
895 * have to clean it up.
897 return commit_list_insert(one, &result);
900 if (parse_commit(one))
902 for (i = 0; i < n; i++) {
903 if (parse_commit(twos[i]))
907 list = paint_down_to_common(one, n, twos, 0);
910 struct commit *commit = pop_commit(&list);
911 if (!(commit->object.flags & STALE))
912 commit_list_insert_by_date(commit, &result);
917 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
919 struct commit_list *i, *j, *k, *ret = NULL;
924 commit_list_insert(in->item, &ret);
926 for (i = in->next; i; i = i->next) {
927 struct commit_list *new_commits = NULL, *end = NULL;
929 for (j = ret; j; j = j->next) {
930 struct commit_list *bases;
931 bases = get_merge_bases(i->item, j->item);
936 for (k = bases; k; k = k->next)
944 static int remove_redundant(struct commit **array, int cnt)
947 * Some commit in the array may be an ancestor of
948 * another commit. Move such commit to the end of
949 * the array, and return the number of commits that
950 * are independent from each other.
952 struct commit **work;
953 unsigned char *redundant;
957 work = xcalloc(cnt, sizeof(*work));
958 redundant = xcalloc(cnt, 1);
959 ALLOC_ARRAY(filled_index, cnt - 1);
961 for (i = 0; i < cnt; i++)
962 parse_commit(array[i]);
963 for (i = 0; i < cnt; i++) {
964 struct commit_list *common;
965 uint32_t min_generation = array[i]->generation;
969 for (j = filled = 0; j < cnt; j++) {
970 if (i == j || redundant[j])
972 filled_index[filled] = j;
973 work[filled++] = array[j];
975 if (array[j]->generation < min_generation)
976 min_generation = array[j]->generation;
978 common = paint_down_to_common(array[i], filled, work,
980 if (array[i]->object.flags & PARENT2)
982 for (j = 0; j < filled; j++)
983 if (work[j]->object.flags & PARENT1)
984 redundant[filled_index[j]] = 1;
985 clear_commit_marks(array[i], all_flags);
986 clear_commit_marks_many(filled, work, all_flags);
987 free_commit_list(common);
990 /* Now collect the result */
991 COPY_ARRAY(work, array, cnt);
992 for (i = filled = 0; i < cnt; i++)
994 array[filled++] = work[i];
995 for (j = filled, i = 0; i < cnt; i++)
997 array[j++] = work[i];
1004 static struct commit_list *get_merge_bases_many_0(struct commit *one,
1006 struct commit **twos,
1009 struct commit_list *list;
1010 struct commit **rslt;
1011 struct commit_list *result;
1014 result = merge_bases_many(one, n, twos);
1015 for (i = 0; i < n; i++) {
1019 if (!result || !result->next) {
1021 clear_commit_marks(one, all_flags);
1022 clear_commit_marks_many(n, twos, all_flags);
1027 /* There are more than one */
1028 cnt = commit_list_count(result);
1029 rslt = xcalloc(cnt, sizeof(*rslt));
1030 for (list = result, i = 0; list; list = list->next)
1031 rslt[i++] = list->item;
1032 free_commit_list(result);
1034 clear_commit_marks(one, all_flags);
1035 clear_commit_marks_many(n, twos, all_flags);
1037 cnt = remove_redundant(rslt, cnt);
1039 for (i = 0; i < cnt; i++)
1040 commit_list_insert_by_date(rslt[i], &result);
1045 struct commit_list *get_merge_bases_many(struct commit *one,
1047 struct commit **twos)
1049 return get_merge_bases_many_0(one, n, twos, 1);
1052 struct commit_list *get_merge_bases_many_dirty(struct commit *one,
1054 struct commit **twos)
1056 return get_merge_bases_many_0(one, n, twos, 0);
1059 struct commit_list *get_merge_bases(struct commit *one, struct commit *two)
1061 return get_merge_bases_many_0(one, 1, &two, 1);
1065 * Is "commit" a descendant of one of the elements on the "with_commit" list?
1067 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
1071 while (with_commit) {
1072 struct commit *other;
1074 other = with_commit->item;
1075 with_commit = with_commit->next;
1076 if (in_merge_bases(other, commit))
1083 * Is "commit" an ancestor of one of the "references"?
1085 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
1087 struct commit_list *bases;
1089 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
1091 if (parse_commit(commit))
1093 for (i = 0; i < nr_reference; i++) {
1094 if (parse_commit(reference[i]))
1096 if (reference[i]->generation < min_generation)
1097 min_generation = reference[i]->generation;
1100 if (commit->generation > min_generation)
1103 bases = paint_down_to_common(commit, nr_reference, reference, commit->generation);
1104 if (commit->object.flags & PARENT2)
1106 clear_commit_marks(commit, all_flags);
1107 clear_commit_marks_many(nr_reference, reference, all_flags);
1108 free_commit_list(bases);
1113 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1115 int in_merge_bases(struct commit *commit, struct commit *reference)
1117 return in_merge_bases_many(commit, 1, &reference);
1120 struct commit_list *reduce_heads(struct commit_list *heads)
1122 struct commit_list *p;
1123 struct commit_list *result = NULL, **tail = &result;
1124 struct commit **array;
1131 for (p = heads; p; p = p->next)
1132 p->item->object.flags &= ~STALE;
1133 for (p = heads, num_head = 0; p; p = p->next) {
1134 if (p->item->object.flags & STALE)
1136 p->item->object.flags |= STALE;
1139 array = xcalloc(num_head, sizeof(*array));
1140 for (p = heads, i = 0; p; p = p->next) {
1141 if (p->item->object.flags & STALE) {
1142 array[i++] = p->item;
1143 p->item->object.flags &= ~STALE;
1146 num_head = remove_redundant(array, num_head);
1147 for (i = 0; i < num_head; i++)
1148 tail = &commit_list_insert(array[i], tail)->next;
1153 void reduce_heads_replace(struct commit_list **heads)
1155 struct commit_list *result = reduce_heads(*heads);
1156 free_commit_list(*heads);
1160 static const char gpg_sig_header[] = "gpgsig";
1161 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
1163 static int do_sign_commit(struct strbuf *buf, const char *keyid)
1165 struct strbuf sig = STRBUF_INIT;
1166 int inspos, copypos;
1169 /* find the end of the header */
1170 eoh = strstr(buf->buf, "\n\n");
1174 inspos = eoh - buf->buf + 1;
1176 if (!keyid || !*keyid)
1177 keyid = get_signing_key();
1178 if (sign_buffer(buf, &sig, keyid)) {
1179 strbuf_release(&sig);
1183 for (copypos = 0; sig.buf[copypos]; ) {
1184 const char *bol = sig.buf + copypos;
1185 const char *eol = strchrnul(bol, '\n');
1186 int len = (eol - bol) + !!*eol;
1189 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1190 inspos += gpg_sig_header_len;
1192 strbuf_insert(buf, inspos++, " ", 1);
1193 strbuf_insert(buf, inspos, bol, len);
1197 strbuf_release(&sig);
1201 int parse_signed_commit(const struct commit *commit,
1202 struct strbuf *payload, struct strbuf *signature)
1206 const char *buffer = get_commit_buffer(commit, &size);
1207 int in_signature, saw_signature = -1;
1208 const char *line, *tail;
1211 tail = buffer + size;
1214 while (line < tail) {
1215 const char *sig = NULL;
1216 const char *next = memchr(line, '\n', tail - line);
1218 next = next ? next + 1 : tail;
1219 if (in_signature && line[0] == ' ')
1221 else if (starts_with(line, gpg_sig_header) &&
1222 line[gpg_sig_header_len] == ' ')
1223 sig = line + gpg_sig_header_len + 1;
1225 strbuf_add(signature, sig, next - sig);
1230 /* dump the whole remainder of the buffer */
1232 strbuf_add(payload, line, next - line);
1237 unuse_commit_buffer(commit, buffer);
1238 return saw_signature;
1241 int remove_signature(struct strbuf *buf)
1243 const char *line = buf->buf;
1244 const char *tail = buf->buf + buf->len;
1245 int in_signature = 0;
1246 const char *sig_start = NULL;
1247 const char *sig_end = NULL;
1249 while (line < tail) {
1250 const char *next = memchr(line, '\n', tail - line);
1251 next = next ? next + 1 : tail;
1253 if (in_signature && line[0] == ' ')
1255 else if (starts_with(line, gpg_sig_header) &&
1256 line[gpg_sig_header_len] == ' ') {
1262 /* dump the whole remainder of the buffer */
1270 strbuf_remove(buf, sig_start - buf->buf, sig_end - sig_start);
1272 return sig_start != NULL;
1275 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1277 struct merge_remote_desc *desc;
1278 struct commit_extra_header *mergetag;
1280 unsigned long size, len;
1281 enum object_type type;
1283 desc = merge_remote_util(parent);
1284 if (!desc || !desc->obj)
1286 buf = read_object_file(&desc->obj->oid, &type, &size);
1287 if (!buf || type != OBJ_TAG)
1289 len = parse_signature(buf, size);
1293 * We could verify this signature and either omit the tag when
1294 * it does not validate, but the integrator may not have the
1295 * public key of the signer of the tag he is merging, while a
1296 * later auditor may have it while auditing, so let's not run
1297 * verify-signed-buffer here for now...
1299 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1300 * warn("warning: signed tag unverified.");
1302 mergetag = xcalloc(1, sizeof(*mergetag));
1303 mergetag->key = xstrdup("mergetag");
1304 mergetag->value = buf;
1305 mergetag->len = size;
1308 *tail = &mergetag->next;
1315 int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
1317 struct strbuf payload = STRBUF_INIT;
1318 struct strbuf signature = STRBUF_INIT;
1323 if (parse_signed_commit(commit, &payload, &signature) <= 0)
1325 ret = check_signature(payload.buf, payload.len, signature.buf,
1326 signature.len, sigc);
1329 strbuf_release(&payload);
1330 strbuf_release(&signature);
1337 void append_merge_tag_headers(struct commit_list *parents,
1338 struct commit_extra_header ***tail)
1341 struct commit *parent = parents->item;
1342 handle_signed_tag(parent, tail);
1343 parents = parents->next;
1347 static void add_extra_header(struct strbuf *buffer,
1348 struct commit_extra_header *extra)
1350 strbuf_addstr(buffer, extra->key);
1352 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1354 strbuf_addch(buffer, '\n');
1357 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1358 const char **exclude)
1360 struct commit_extra_header *extra = NULL;
1362 const char *buffer = get_commit_buffer(commit, &size);
1363 extra = read_commit_extra_header_lines(buffer, size, exclude);
1364 unuse_commit_buffer(commit, buffer);
1368 int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
1370 struct commit_extra_header *extra, *to_free;
1373 to_free = read_commit_extra_headers(commit, NULL);
1374 for (extra = to_free; !res && extra; extra = extra->next) {
1375 if (strcmp(extra->key, "mergetag"))
1376 continue; /* not a merge tag */
1377 res = fn(commit, extra, data);
1379 free_commit_extra_headers(to_free);
1383 static inline int standard_header_field(const char *field, size_t len)
1385 return ((len == 4 && !memcmp(field, "tree", 4)) ||
1386 (len == 6 && !memcmp(field, "parent", 6)) ||
1387 (len == 6 && !memcmp(field, "author", 6)) ||
1388 (len == 9 && !memcmp(field, "committer", 9)) ||
1389 (len == 8 && !memcmp(field, "encoding", 8)));
1392 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1398 size_t xlen = strlen(*exclude);
1399 if (len == xlen && !memcmp(field, *exclude, xlen))
1406 static struct commit_extra_header *read_commit_extra_header_lines(
1407 const char *buffer, size_t size,
1408 const char **exclude)
1410 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1411 const char *line, *next, *eof, *eob;
1412 struct strbuf buf = STRBUF_INIT;
1414 for (line = buffer, eob = line + size;
1415 line < eob && *line != '\n';
1417 next = memchr(line, '\n', eob - line);
1418 next = next ? next + 1 : eob;
1422 strbuf_add(&buf, line + 1, next - (line + 1));
1426 it->value = strbuf_detach(&buf, &it->len);
1430 eof = memchr(line, ' ', next - line);
1433 else if (standard_header_field(line, eof - line) ||
1434 excluded_header_field(line, eof - line, exclude))
1437 it = xcalloc(1, sizeof(*it));
1438 it->key = xmemdupz(line, eof-line);
1442 strbuf_add(&buf, eof + 1, next - (eof + 1));
1445 it->value = strbuf_detach(&buf, &it->len);
1449 void free_commit_extra_headers(struct commit_extra_header *extra)
1452 struct commit_extra_header *next = extra->next;
1460 int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1461 struct commit_list *parents, struct object_id *ret,
1462 const char *author, const char *sign_commit)
1464 struct commit_extra_header *extra = NULL, **tail = &extra;
1467 append_merge_tag_headers(parents, &tail);
1468 result = commit_tree_extended(msg, msg_len, tree, parents, ret,
1469 author, sign_commit, extra);
1470 free_commit_extra_headers(extra);
1474 static int find_invalid_utf8(const char *buf, int len)
1477 static const unsigned int max_codepoint[] = {
1478 0x7f, 0x7ff, 0xffff, 0x10ffff
1482 unsigned char c = *buf++;
1483 int bytes, bad_offset;
1484 unsigned int codepoint;
1485 unsigned int min_val, max_val;
1490 /* Simple US-ASCII? No worries. */
1494 bad_offset = offset-1;
1497 * Count how many more high bits set: that's how
1498 * many more bytes this sequence should have.
1507 * Must be between 1 and 3 more bytes. Longer sequences result in
1508 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1510 if (bytes < 1 || 3 < bytes)
1513 /* Do we *have* that many bytes? */
1518 * Place the encoded bits at the bottom of the value and compute the
1521 codepoint = (c & 0x7f) >> bytes;
1522 min_val = max_codepoint[bytes-1] + 1;
1523 max_val = max_codepoint[bytes];
1528 /* And verify that they are good continuation bytes */
1531 codepoint |= *buf & 0x3f;
1532 if ((*buf++ & 0xc0) != 0x80)
1536 /* Reject codepoints that are out of range for the sequence length. */
1537 if (codepoint < min_val || codepoint > max_val)
1539 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1540 if ((codepoint & 0x1ff800) == 0xd800)
1542 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1543 if ((codepoint & 0xfffe) == 0xfffe)
1545 /* So are anything in the range U+FDD0..U+FDEF. */
1546 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
1553 * This verifies that the buffer is in proper utf8 format.
1555 * If it isn't, it assumes any non-utf8 characters are Latin1,
1556 * and does the conversion.
1558 static int verify_utf8(struct strbuf *buf)
1566 unsigned char replace[2];
1568 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1574 strbuf_remove(buf, pos, 1);
1576 /* We know 'c' must be in the range 128-255 */
1577 replace[0] = 0xc0 + (c >> 6);
1578 replace[1] = 0x80 + (c & 0x3f);
1579 strbuf_insert(buf, pos, replace, 2);
1584 static const char commit_utf8_warn[] =
1585 N_("Warning: commit message did not conform to UTF-8.\n"
1586 "You may want to amend it after fixing the message, or set the config\n"
1587 "variable i18n.commitencoding to the encoding your project uses.\n");
1589 int commit_tree_extended(const char *msg, size_t msg_len,
1590 const struct object_id *tree,
1591 struct commit_list *parents, struct object_id *ret,
1592 const char *author, const char *sign_commit,
1593 struct commit_extra_header *extra)
1596 int encoding_is_utf8;
1597 struct strbuf buffer;
1599 assert_oid_type(tree, OBJ_TREE);
1601 if (memchr(msg, '\0', msg_len))
1602 return error("a NUL byte in commit log message not allowed.");
1604 /* Not having i18n.commitencoding is the same as having utf-8 */
1605 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1607 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1608 strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree));
1611 * NOTE! This ordering means that the same exact tree merged with a
1612 * different order of parents will be a _different_ changeset even
1613 * if everything else stays the same.
1616 struct commit *parent = pop_commit(&parents);
1617 strbuf_addf(&buffer, "parent %s\n",
1618 oid_to_hex(&parent->object.oid));
1621 /* Person/date information */
1623 author = git_author_info(IDENT_STRICT);
1624 strbuf_addf(&buffer, "author %s\n", author);
1625 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1626 if (!encoding_is_utf8)
1627 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1630 add_extra_header(&buffer, extra);
1631 extra = extra->next;
1633 strbuf_addch(&buffer, '\n');
1635 /* And add the comment */
1636 strbuf_add(&buffer, msg, msg_len);
1638 /* And check the encoding */
1639 if (encoding_is_utf8 && !verify_utf8(&buffer))
1640 fprintf(stderr, _(commit_utf8_warn));
1642 if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
1647 result = write_object_file(buffer.buf, buffer.len, commit_type, ret);
1649 strbuf_release(&buffer);
1653 define_commit_slab(merge_desc_slab, struct merge_remote_desc *);
1654 static struct merge_desc_slab merge_desc_slab = COMMIT_SLAB_INIT(1, merge_desc_slab);
1656 struct merge_remote_desc *merge_remote_util(struct commit *commit)
1658 return *merge_desc_slab_at(&merge_desc_slab, commit);
1661 void set_merge_remote_desc(struct commit *commit,
1662 const char *name, struct object *obj)
1664 struct merge_remote_desc *desc;
1665 FLEX_ALLOC_STR(desc, name, name);
1667 *merge_desc_slab_at(&merge_desc_slab, commit) = desc;
1670 struct commit *get_merge_parent(const char *name)
1673 struct commit *commit;
1674 struct object_id oid;
1675 if (get_oid(name, &oid))
1677 obj = parse_object(&oid);
1678 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1679 if (commit && !merge_remote_util(commit))
1680 set_merge_remote_desc(commit, name, obj);
1685 * Append a commit to the end of the commit_list.
1687 * next starts by pointing to the variable that holds the head of an
1688 * empty commit_list, and is updated to point to the "next" field of
1689 * the last item on the list as new commits are appended.
1693 * struct commit_list *list;
1694 * struct commit_list **next = &list;
1696 * next = commit_list_append(c1, next);
1697 * next = commit_list_append(c2, next);
1698 * assert(commit_list_count(list) == 2);
1701 struct commit_list **commit_list_append(struct commit *commit,
1702 struct commit_list **next)
1704 struct commit_list *new_commit = xmalloc(sizeof(struct commit_list));
1705 new_commit->item = commit;
1707 new_commit->next = NULL;
1708 return &new_commit->next;
1711 const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1713 int key_len = strlen(key);
1714 const char *line = msg;
1717 const char *eol = strchrnul(line, '\n');
1722 if (eol - line > key_len &&
1723 !strncmp(line, key, key_len) &&
1724 line[key_len] == ' ') {
1725 *out_len = eol - line - key_len - 1;
1726 return line + key_len + 1;
1728 line = *eol ? eol + 1 : NULL;
1734 * Inspect the given string and determine the true "end" of the log message, in
1735 * order to find where to put a new Signed-off-by: line. Ignored are
1736 * trailing comment lines and blank lines. To support "git commit -s
1737 * --amend" on an existing commit, we also ignore "Conflicts:". To
1738 * support "git commit -v", we truncate at cut lines.
1740 * Returns the number of bytes from the tail to ignore, to be fed as
1741 * the second parameter to append_signoff().
1743 int ignore_non_trailer(const char *buf, size_t len)
1747 int in_old_conflicts_block = 0;
1748 size_t cutoff = wt_status_locate_end(buf, len);
1750 while (bol < cutoff) {
1751 const char *next_line = memchr(buf + bol, '\n', len - bol);
1754 next_line = buf + len;
1758 if (buf[bol] == comment_line_char || buf[bol] == '\n') {
1759 /* is this the first of the run of comments? */
1762 /* otherwise, it is just continuing */
1763 } else if (starts_with(buf + bol, "Conflicts:\n")) {
1764 in_old_conflicts_block = 1;
1767 } else if (in_old_conflicts_block && buf[bol] == '\t') {
1768 ; /* a pathname in the conflicts block */
1770 /* the previous was not trailing comment */
1772 in_old_conflicts_block = 0;
1774 bol = next_line - buf;
1776 return boc ? len - boc : len - cutoff;