4 #include "commit-graph.h"
5 #include "repository.h"
6 #include "object-store.h"
13 #include "gpg-interface.h"
14 #include "mergesort.h"
15 #include "commit-slab.h"
16 #include "prio-queue.h"
17 #include "sha1-lookup.h"
18 #include "wt-status.h"
21 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
23 int save_commit_buffer = 1;
25 const char *commit_type = "commit";
27 struct commit *lookup_commit_reference_gently(const struct object_id *oid,
30 struct object *obj = deref_tag(parse_object(oid), NULL, 0);
34 return object_as_type(obj, OBJ_COMMIT, quiet);
37 struct commit *lookup_commit_reference(const struct object_id *oid)
39 return lookup_commit_reference_gently(oid, 0);
42 struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name)
44 struct commit *c = lookup_commit_reference(oid);
46 die(_("could not parse %s"), ref_name);
47 if (oidcmp(oid, &c->object.oid)) {
48 warning(_("%s %s is not a commit!"),
49 ref_name, oid_to_hex(oid));
54 struct commit *lookup_commit(const struct object_id *oid)
56 struct object *obj = lookup_object(oid->hash);
58 return create_object(the_repository, oid->hash,
59 alloc_commit_node(the_repository));
60 return object_as_type(obj, OBJ_COMMIT, 0);
63 struct commit *lookup_commit_reference_by_name(const char *name)
66 struct commit *commit;
68 if (get_oid_committish(name, &oid))
70 commit = lookup_commit_reference(&oid);
71 if (parse_commit(commit))
76 static timestamp_t parse_commit_date(const char *buf, const char *tail)
82 if (memcmp(buf, "author", 6))
84 while (buf < tail && *buf++ != '\n')
88 if (memcmp(buf, "committer", 9))
90 while (buf < tail && *buf++ != '>')
95 while (buf < tail && *buf++ != '\n')
99 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
100 return parse_timestamp(dateptr, NULL, 10);
103 static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
105 struct commit_graft **commit_graft_table = table;
106 return commit_graft_table[index]->oid.hash;
109 static int commit_graft_pos(struct repository *r, const unsigned char *sha1)
111 return sha1_pos(sha1, r->parsed_objects->grafts,
112 r->parsed_objects->grafts_nr,
113 commit_graft_sha1_access);
116 int register_commit_graft(struct repository *r, struct commit_graft *graft,
119 int pos = commit_graft_pos(r, graft->oid.hash);
125 free(r->parsed_objects->grafts[pos]);
126 r->parsed_objects->grafts[pos] = graft;
131 ALLOC_GROW(r->parsed_objects->grafts,
132 r->parsed_objects->grafts_nr + 1,
133 r->parsed_objects->grafts_alloc);
134 r->parsed_objects->grafts_nr++;
135 if (pos < r->parsed_objects->grafts_nr)
136 memmove(r->parsed_objects->grafts + pos + 1,
137 r->parsed_objects->grafts + pos,
138 (r->parsed_objects->grafts_nr - pos - 1) *
139 sizeof(*r->parsed_objects->grafts));
140 r->parsed_objects->grafts[pos] = graft;
144 struct commit_graft *read_graft_line(struct strbuf *line)
146 /* The format is just "Commit Parent1 Parent2 ...\n" */
148 const char *tail = NULL;
149 struct commit_graft *graft = NULL;
150 struct object_id dummy_oid, *oid;
153 if (!line->len || line->buf[0] == '#')
156 * phase 0 verifies line, counts hashes in line and allocates graft
157 * phase 1 fills graft
159 for (phase = 0; phase < 2; phase++) {
160 oid = graft ? &graft->oid : &dummy_oid;
161 if (parse_oid_hex(line->buf, oid, &tail))
163 for (i = 0; *tail != '\0'; i++) {
164 oid = graft ? &graft->parent[i] : &dummy_oid;
165 if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
169 graft = xmalloc(st_add(sizeof(*graft),
170 st_mult(sizeof(struct object_id), i)));
171 graft->nr_parent = i;
177 error("bad graft data: %s", line->buf);
182 static int read_graft_file(struct repository *r, const char *graft_file)
184 FILE *fp = fopen_or_warn(graft_file, "r");
185 struct strbuf buf = STRBUF_INIT;
188 if (advice_graft_file_deprecated)
189 advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
190 "and will be removed in a future Git version.\n"
192 "Please use \"git replace --convert-graft-file\"\n"
193 "to convert the grafts into replace refs.\n"
195 "Turn this message off by running\n"
196 "\"git config advice.graftFileDeprecated false\""));
197 while (!strbuf_getwholeline(&buf, fp, '\n')) {
198 /* The format is just "Commit Parent1 Parent2 ...\n" */
199 struct commit_graft *graft = read_graft_line(&buf);
202 if (register_commit_graft(r, graft, 1))
203 error("duplicate graft data: %s", buf.buf);
206 strbuf_release(&buf);
210 static void prepare_commit_graft(struct repository *r)
214 if (r->parsed_objects->commit_graft_prepared)
216 if (!startup_info->have_repository)
219 graft_file = get_graft_file(r);
220 read_graft_file(r, graft_file);
221 /* make sure shallows are read */
222 is_repository_shallow(r);
223 r->parsed_objects->commit_graft_prepared = 1;
226 struct commit_graft *lookup_commit_graft(struct repository *r, const struct object_id *oid)
229 prepare_commit_graft(r);
230 pos = commit_graft_pos(r, oid->hash);
233 return r->parsed_objects->grafts[pos];
236 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
239 for (i = ret = 0; i < the_repository->parsed_objects->grafts_nr && !ret; i++)
240 ret = fn(the_repository->parsed_objects->grafts[i], cb_data);
244 int unregister_shallow(const struct object_id *oid)
246 int pos = commit_graft_pos(the_repository, oid->hash);
249 if (pos + 1 < the_repository->parsed_objects->grafts_nr)
250 MOVE_ARRAY(the_repository->parsed_objects->grafts + pos,
251 the_repository->parsed_objects->grafts + pos + 1,
252 the_repository->parsed_objects->grafts_nr - pos - 1);
253 the_repository->parsed_objects->grafts_nr--;
257 struct commit_buffer {
261 define_commit_slab(buffer_slab, struct commit_buffer);
262 static struct buffer_slab buffer_slab = COMMIT_SLAB_INIT(1, buffer_slab);
264 void set_commit_buffer(struct commit *commit, void *buffer, unsigned long size)
266 struct commit_buffer *v = buffer_slab_at(&buffer_slab, commit);
271 const void *get_cached_commit_buffer(const struct commit *commit, unsigned long *sizep)
273 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
284 const void *get_commit_buffer(const struct commit *commit, unsigned long *sizep)
286 const void *ret = get_cached_commit_buffer(commit, sizep);
288 enum object_type type;
290 ret = read_object_file(&commit->object.oid, &type, &size);
292 die("cannot read commit object %s",
293 oid_to_hex(&commit->object.oid));
294 if (type != OBJ_COMMIT)
295 die("expected commit for %s, got %s",
296 oid_to_hex(&commit->object.oid), type_name(type));
303 void unuse_commit_buffer(const struct commit *commit, const void *buffer)
305 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
306 if (!(v && v->buffer == buffer))
307 free((void *)buffer);
310 void free_commit_buffer(struct commit *commit)
312 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
314 FREE_AND_NULL(v->buffer);
319 struct tree *get_commit_tree(const struct commit *commit)
321 if (commit->maybe_tree || !commit->object.parsed)
322 return commit->maybe_tree;
324 if (commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
325 BUG("commit has NULL tree, but was not loaded from commit-graph");
327 return get_commit_tree_in_graph(commit);
330 struct object_id *get_commit_tree_oid(const struct commit *commit)
332 return &get_commit_tree(commit)->object.oid;
335 void release_commit_memory(struct commit *c)
337 c->maybe_tree = NULL;
339 free_commit_buffer(c);
340 free_commit_list(c->parents);
341 /* TODO: what about commit->util? */
343 c->object.parsed = 0;
346 const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
348 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
365 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size, int check_graph)
367 const char *tail = buffer;
368 const char *bufptr = buffer;
369 struct object_id parent;
370 struct commit_list **pptr;
371 struct commit_graft *graft;
372 const int tree_entry_len = GIT_SHA1_HEXSZ + 5;
373 const int parent_entry_len = GIT_SHA1_HEXSZ + 7;
375 if (item->object.parsed)
377 item->object.parsed = 1;
379 if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
380 bufptr[tree_entry_len] != '\n')
381 return error("bogus commit object %s", oid_to_hex(&item->object.oid));
382 if (get_oid_hex(bufptr + 5, &parent) < 0)
383 return error("bad tree pointer in commit %s",
384 oid_to_hex(&item->object.oid));
385 item->maybe_tree = lookup_tree(&parent);
386 bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
387 pptr = &item->parents;
389 graft = lookup_commit_graft(the_repository, &item->object.oid);
390 while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
391 struct commit *new_parent;
393 if (tail <= bufptr + parent_entry_len + 1 ||
394 get_oid_hex(bufptr + 7, &parent) ||
395 bufptr[parent_entry_len] != '\n')
396 return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
397 bufptr += parent_entry_len + 1;
399 * The clone is shallow if nr_parent < 0, and we must
400 * not traverse its real parents even when we unhide them.
402 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
404 new_parent = lookup_commit(&parent);
406 pptr = &commit_list_insert(new_parent, pptr)->next;
410 struct commit *new_parent;
411 for (i = 0; i < graft->nr_parent; i++) {
412 new_parent = lookup_commit(&graft->parent[i]);
415 pptr = &commit_list_insert(new_parent, pptr)->next;
418 item->date = parse_commit_date(bufptr, tail);
421 load_commit_graph_info(item);
426 int parse_commit_gently(struct commit *item, int quiet_on_missing)
428 enum object_type type;
435 if (item->object.parsed)
437 if (parse_commit_in_graph(item))
439 buffer = read_object_file(&item->object.oid, &type, &size);
441 return quiet_on_missing ? -1 :
442 error("Could not read %s",
443 oid_to_hex(&item->object.oid));
444 if (type != OBJ_COMMIT) {
446 return error("Object %s not a commit",
447 oid_to_hex(&item->object.oid));
449 ret = parse_commit_buffer(item, buffer, size, 0);
450 if (save_commit_buffer && !ret) {
451 set_commit_buffer(item, buffer, size);
458 void parse_commit_or_die(struct commit *item)
460 if (parse_commit(item))
461 die("unable to parse commit %s",
462 item ? oid_to_hex(&item->object.oid) : "(null)");
465 int find_commit_subject(const char *commit_buffer, const char **subject)
468 const char *p = commit_buffer;
470 while (*p && (*p != '\n' || p[1] != '\n'))
473 p = skip_blank_lines(p + 2);
474 eol = strchrnul(p, '\n');
483 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
485 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
486 new_list->item = item;
487 new_list->next = *list_p;
492 unsigned commit_list_count(const struct commit_list *l)
495 for (; l; l = l->next )
500 struct commit_list *copy_commit_list(struct commit_list *list)
502 struct commit_list *head = NULL;
503 struct commit_list **pp = &head;
505 pp = commit_list_append(list->item, pp);
511 void free_commit_list(struct commit_list *list)
517 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
519 struct commit_list **pp = list;
520 struct commit_list *p;
521 while ((p = *pp) != NULL) {
522 if (p->item->date < item->date) {
527 return commit_list_insert(item, pp);
530 static int commit_list_compare_by_date(const void *a, const void *b)
532 timestamp_t a_date = ((const struct commit_list *)a)->item->date;
533 timestamp_t b_date = ((const struct commit_list *)b)->item->date;
541 static void *commit_list_get_next(const void *a)
543 return ((const struct commit_list *)a)->next;
546 static void commit_list_set_next(void *a, void *next)
548 ((struct commit_list *)a)->next = next;
551 void commit_list_sort_by_date(struct commit_list **list)
553 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
554 commit_list_compare_by_date);
557 struct commit *pop_most_recent_commit(struct commit_list **list,
560 struct commit *ret = pop_commit(list);
561 struct commit_list *parents = ret->parents;
564 struct commit *commit = parents->item;
565 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
566 commit->object.flags |= mark;
567 commit_list_insert_by_date(commit, list);
569 parents = parents->next;
574 static void clear_commit_marks_1(struct commit_list **plist,
575 struct commit *commit, unsigned int mark)
578 struct commit_list *parents;
580 if (!(mark & commit->object.flags))
583 commit->object.flags &= ~mark;
585 parents = commit->parents;
589 while ((parents = parents->next))
590 commit_list_insert(parents->item, plist);
592 commit = commit->parents->item;
596 void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
598 struct commit_list *list = NULL;
601 clear_commit_marks_1(&list, *commit, mark);
605 clear_commit_marks_1(&list, pop_commit(&list), mark);
608 void clear_commit_marks(struct commit *commit, unsigned int mark)
610 clear_commit_marks_many(1, &commit, mark);
613 struct commit *pop_commit(struct commit_list **stack)
615 struct commit_list *top = *stack;
616 struct commit *item = top ? top->item : NULL;
626 * Topological sort support
629 /* count number of children that have not been emitted */
630 define_commit_slab(indegree_slab, int);
632 /* record author-date for each commit object */
633 define_commit_slab(author_date_slab, unsigned long);
635 static void record_author_date(struct author_date_slab *author_date,
636 struct commit *commit)
638 const char *buffer = get_commit_buffer(commit, NULL);
639 struct ident_split ident;
640 const char *ident_line;
645 ident_line = find_commit_header(buffer, "author", &ident_len);
647 goto fail_exit; /* no author line */
648 if (split_ident_line(&ident, ident_line, ident_len) ||
649 !ident.date_begin || !ident.date_end)
650 goto fail_exit; /* malformed "author" line */
652 date = parse_timestamp(ident.date_begin, &date_end, 10);
653 if (date_end != ident.date_end)
654 goto fail_exit; /* malformed date */
655 *(author_date_slab_at(author_date, commit)) = date;
658 unuse_commit_buffer(commit, buffer);
661 static int compare_commits_by_author_date(const void *a_, const void *b_,
664 const struct commit *a = a_, *b = b_;
665 struct author_date_slab *author_date = cb_data;
666 timestamp_t a_date = *(author_date_slab_at(author_date, a));
667 timestamp_t b_date = *(author_date_slab_at(author_date, b));
669 /* newer commits with larger date first */
672 else if (a_date > b_date)
677 int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused)
679 const struct commit *a = a_, *b = b_;
681 /* newer commits first */
682 if (a->generation < b->generation)
684 else if (a->generation > b->generation)
687 /* use date as a heuristic when generations are equal */
688 if (a->date < b->date)
690 else if (a->date > b->date)
695 int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
697 const struct commit *a = a_, *b = b_;
698 /* newer commits with larger date first */
699 if (a->date < b->date)
701 else if (a->date > b->date)
707 * Performs an in-place topological sort on the list supplied.
709 void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
711 struct commit_list *next, *orig = *list;
712 struct commit_list **pptr;
713 struct indegree_slab indegree;
714 struct prio_queue queue;
715 struct commit *commit;
716 struct author_date_slab author_date;
722 init_indegree_slab(&indegree);
723 memset(&queue, '\0', sizeof(queue));
725 switch (sort_order) {
726 default: /* REV_SORT_IN_GRAPH_ORDER */
727 queue.compare = NULL;
729 case REV_SORT_BY_COMMIT_DATE:
730 queue.compare = compare_commits_by_commit_date;
732 case REV_SORT_BY_AUTHOR_DATE:
733 init_author_date_slab(&author_date);
734 queue.compare = compare_commits_by_author_date;
735 queue.cb_data = &author_date;
739 /* Mark them and clear the indegree */
740 for (next = orig; next; next = next->next) {
741 struct commit *commit = next->item;
742 *(indegree_slab_at(&indegree, commit)) = 1;
743 /* also record the author dates, if needed */
744 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
745 record_author_date(&author_date, commit);
748 /* update the indegree */
749 for (next = orig; next; next = next->next) {
750 struct commit_list *parents = next->item->parents;
752 struct commit *parent = parents->item;
753 int *pi = indegree_slab_at(&indegree, parent);
757 parents = parents->next;
764 * tips are nodes not reachable from any other node in the list
766 * the tips serve as a starting set for the work queue.
768 for (next = orig; next; next = next->next) {
769 struct commit *commit = next->item;
771 if (*(indegree_slab_at(&indegree, commit)) == 1)
772 prio_queue_put(&queue, commit);
776 * This is unfortunate; the initial tips need to be shown
777 * in the order given from the revision traversal machinery.
779 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
780 prio_queue_reverse(&queue);
782 /* We no longer need the commit list */
783 free_commit_list(orig);
787 while ((commit = prio_queue_get(&queue)) != NULL) {
788 struct commit_list *parents;
790 for (parents = commit->parents; parents ; parents = parents->next) {
791 struct commit *parent = parents->item;
792 int *pi = indegree_slab_at(&indegree, parent);
798 * parents are only enqueued for emission
799 * when all their children have been emitted thereby
800 * guaranteeing topological order.
803 prio_queue_put(&queue, parent);
806 * all children of commit have already been
807 * emitted. we can emit it now.
809 *(indegree_slab_at(&indegree, commit)) = 0;
811 pptr = &commit_list_insert(commit, pptr)->next;
814 clear_indegree_slab(&indegree);
815 clear_prio_queue(&queue);
816 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
817 clear_author_date_slab(&author_date);
820 /* merge-base stuff */
822 /* Remember to update object flag allocation in object.h */
823 #define PARENT1 (1u<<16)
824 #define PARENT2 (1u<<17)
825 #define STALE (1u<<18)
826 #define RESULT (1u<<19)
828 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
830 static int queue_has_nonstale(struct prio_queue *queue)
833 for (i = 0; i < queue->nr; i++) {
834 struct commit *commit = queue->array[i].data;
835 if (!(commit->object.flags & STALE))
841 /* all input commits in one and twos[] must have been parsed! */
842 static struct commit_list *paint_down_to_common(struct commit *one, int n,
843 struct commit **twos,
846 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
847 struct commit_list *result = NULL;
849 uint32_t last_gen = GENERATION_NUMBER_INFINITY;
851 one->object.flags |= PARENT1;
853 commit_list_append(one, &result);
856 prio_queue_put(&queue, one);
858 for (i = 0; i < n; i++) {
859 twos[i]->object.flags |= PARENT2;
860 prio_queue_put(&queue, twos[i]);
863 while (queue_has_nonstale(&queue)) {
864 struct commit *commit = prio_queue_get(&queue);
865 struct commit_list *parents;
868 if (commit->generation > last_gen)
869 BUG("bad generation skip %8x > %8x at %s",
870 commit->generation, last_gen,
871 oid_to_hex(&commit->object.oid));
872 last_gen = commit->generation;
874 if (commit->generation < min_generation)
877 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
878 if (flags == (PARENT1 | PARENT2)) {
879 if (!(commit->object.flags & RESULT)) {
880 commit->object.flags |= RESULT;
881 commit_list_insert_by_date(commit, &result);
883 /* Mark parents of a found merge stale */
886 parents = commit->parents;
888 struct commit *p = parents->item;
889 parents = parents->next;
890 if ((p->object.flags & flags) == flags)
894 p->object.flags |= flags;
895 prio_queue_put(&queue, p);
899 clear_prio_queue(&queue);
903 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
905 struct commit_list *list = NULL;
906 struct commit_list *result = NULL;
909 for (i = 0; i < n; i++) {
912 * We do not mark this even with RESULT so we do not
913 * have to clean it up.
915 return commit_list_insert(one, &result);
918 if (parse_commit(one))
920 for (i = 0; i < n; i++) {
921 if (parse_commit(twos[i]))
925 list = paint_down_to_common(one, n, twos, 0);
928 struct commit *commit = pop_commit(&list);
929 if (!(commit->object.flags & STALE))
930 commit_list_insert_by_date(commit, &result);
935 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
937 struct commit_list *i, *j, *k, *ret = NULL;
942 commit_list_insert(in->item, &ret);
944 for (i = in->next; i; i = i->next) {
945 struct commit_list *new_commits = NULL, *end = NULL;
947 for (j = ret; j; j = j->next) {
948 struct commit_list *bases;
949 bases = get_merge_bases(i->item, j->item);
954 for (k = bases; k; k = k->next)
962 static int remove_redundant(struct commit **array, int cnt)
965 * Some commit in the array may be an ancestor of
966 * another commit. Move such commit to the end of
967 * the array, and return the number of commits that
968 * are independent from each other.
970 struct commit **work;
971 unsigned char *redundant;
975 work = xcalloc(cnt, sizeof(*work));
976 redundant = xcalloc(cnt, 1);
977 ALLOC_ARRAY(filled_index, cnt - 1);
979 for (i = 0; i < cnt; i++)
980 parse_commit(array[i]);
981 for (i = 0; i < cnt; i++) {
982 struct commit_list *common;
983 uint32_t min_generation = array[i]->generation;
987 for (j = filled = 0; j < cnt; j++) {
988 if (i == j || redundant[j])
990 filled_index[filled] = j;
991 work[filled++] = array[j];
993 if (array[j]->generation < min_generation)
994 min_generation = array[j]->generation;
996 common = paint_down_to_common(array[i], filled, work,
998 if (array[i]->object.flags & PARENT2)
1000 for (j = 0; j < filled; j++)
1001 if (work[j]->object.flags & PARENT1)
1002 redundant[filled_index[j]] = 1;
1003 clear_commit_marks(array[i], all_flags);
1004 clear_commit_marks_many(filled, work, all_flags);
1005 free_commit_list(common);
1008 /* Now collect the result */
1009 COPY_ARRAY(work, array, cnt);
1010 for (i = filled = 0; i < cnt; i++)
1012 array[filled++] = work[i];
1013 for (j = filled, i = 0; i < cnt; i++)
1015 array[j++] = work[i];
1022 static struct commit_list *get_merge_bases_many_0(struct commit *one,
1024 struct commit **twos,
1027 struct commit_list *list;
1028 struct commit **rslt;
1029 struct commit_list *result;
1032 result = merge_bases_many(one, n, twos);
1033 for (i = 0; i < n; i++) {
1037 if (!result || !result->next) {
1039 clear_commit_marks(one, all_flags);
1040 clear_commit_marks_many(n, twos, all_flags);
1045 /* There are more than one */
1046 cnt = commit_list_count(result);
1047 rslt = xcalloc(cnt, sizeof(*rslt));
1048 for (list = result, i = 0; list; list = list->next)
1049 rslt[i++] = list->item;
1050 free_commit_list(result);
1052 clear_commit_marks(one, all_flags);
1053 clear_commit_marks_many(n, twos, all_flags);
1055 cnt = remove_redundant(rslt, cnt);
1057 for (i = 0; i < cnt; i++)
1058 commit_list_insert_by_date(rslt[i], &result);
1063 struct commit_list *get_merge_bases_many(struct commit *one,
1065 struct commit **twos)
1067 return get_merge_bases_many_0(one, n, twos, 1);
1070 struct commit_list *get_merge_bases_many_dirty(struct commit *one,
1072 struct commit **twos)
1074 return get_merge_bases_many_0(one, n, twos, 0);
1077 struct commit_list *get_merge_bases(struct commit *one, struct commit *two)
1079 return get_merge_bases_many_0(one, 1, &two, 1);
1083 * Is "commit" a descendant of one of the elements on the "with_commit" list?
1085 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
1089 while (with_commit) {
1090 struct commit *other;
1092 other = with_commit->item;
1093 with_commit = with_commit->next;
1094 if (in_merge_bases(other, commit))
1101 * Is "commit" an ancestor of one of the "references"?
1103 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
1105 struct commit_list *bases;
1107 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
1109 if (parse_commit(commit))
1111 for (i = 0; i < nr_reference; i++) {
1112 if (parse_commit(reference[i]))
1114 if (reference[i]->generation < min_generation)
1115 min_generation = reference[i]->generation;
1118 if (commit->generation > min_generation)
1121 bases = paint_down_to_common(commit, nr_reference, reference, commit->generation);
1122 if (commit->object.flags & PARENT2)
1124 clear_commit_marks(commit, all_flags);
1125 clear_commit_marks_many(nr_reference, reference, all_flags);
1126 free_commit_list(bases);
1131 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1133 int in_merge_bases(struct commit *commit, struct commit *reference)
1135 return in_merge_bases_many(commit, 1, &reference);
1138 struct commit_list *reduce_heads(struct commit_list *heads)
1140 struct commit_list *p;
1141 struct commit_list *result = NULL, **tail = &result;
1142 struct commit **array;
1149 for (p = heads; p; p = p->next)
1150 p->item->object.flags &= ~STALE;
1151 for (p = heads, num_head = 0; p; p = p->next) {
1152 if (p->item->object.flags & STALE)
1154 p->item->object.flags |= STALE;
1157 array = xcalloc(num_head, sizeof(*array));
1158 for (p = heads, i = 0; p; p = p->next) {
1159 if (p->item->object.flags & STALE) {
1160 array[i++] = p->item;
1161 p->item->object.flags &= ~STALE;
1164 num_head = remove_redundant(array, num_head);
1165 for (i = 0; i < num_head; i++)
1166 tail = &commit_list_insert(array[i], tail)->next;
1171 void reduce_heads_replace(struct commit_list **heads)
1173 struct commit_list *result = reduce_heads(*heads);
1174 free_commit_list(*heads);
1178 static const char gpg_sig_header[] = "gpgsig";
1179 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
1181 static int do_sign_commit(struct strbuf *buf, const char *keyid)
1183 struct strbuf sig = STRBUF_INIT;
1184 int inspos, copypos;
1187 /* find the end of the header */
1188 eoh = strstr(buf->buf, "\n\n");
1192 inspos = eoh - buf->buf + 1;
1194 if (!keyid || !*keyid)
1195 keyid = get_signing_key();
1196 if (sign_buffer(buf, &sig, keyid)) {
1197 strbuf_release(&sig);
1201 for (copypos = 0; sig.buf[copypos]; ) {
1202 const char *bol = sig.buf + copypos;
1203 const char *eol = strchrnul(bol, '\n');
1204 int len = (eol - bol) + !!*eol;
1207 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1208 inspos += gpg_sig_header_len;
1210 strbuf_insert(buf, inspos++, " ", 1);
1211 strbuf_insert(buf, inspos, bol, len);
1215 strbuf_release(&sig);
1219 int parse_signed_commit(const struct commit *commit,
1220 struct strbuf *payload, struct strbuf *signature)
1224 const char *buffer = get_commit_buffer(commit, &size);
1225 int in_signature, saw_signature = -1;
1226 const char *line, *tail;
1229 tail = buffer + size;
1232 while (line < tail) {
1233 const char *sig = NULL;
1234 const char *next = memchr(line, '\n', tail - line);
1236 next = next ? next + 1 : tail;
1237 if (in_signature && line[0] == ' ')
1239 else if (starts_with(line, gpg_sig_header) &&
1240 line[gpg_sig_header_len] == ' ')
1241 sig = line + gpg_sig_header_len + 1;
1243 strbuf_add(signature, sig, next - sig);
1248 /* dump the whole remainder of the buffer */
1250 strbuf_add(payload, line, next - line);
1255 unuse_commit_buffer(commit, buffer);
1256 return saw_signature;
1259 int remove_signature(struct strbuf *buf)
1261 const char *line = buf->buf;
1262 const char *tail = buf->buf + buf->len;
1263 int in_signature = 0;
1264 const char *sig_start = NULL;
1265 const char *sig_end = NULL;
1267 while (line < tail) {
1268 const char *next = memchr(line, '\n', tail - line);
1269 next = next ? next + 1 : tail;
1271 if (in_signature && line[0] == ' ')
1273 else if (starts_with(line, gpg_sig_header) &&
1274 line[gpg_sig_header_len] == ' ') {
1280 /* dump the whole remainder of the buffer */
1288 strbuf_remove(buf, sig_start - buf->buf, sig_end - sig_start);
1290 return sig_start != NULL;
1293 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1295 struct merge_remote_desc *desc;
1296 struct commit_extra_header *mergetag;
1298 unsigned long size, len;
1299 enum object_type type;
1301 desc = merge_remote_util(parent);
1302 if (!desc || !desc->obj)
1304 buf = read_object_file(&desc->obj->oid, &type, &size);
1305 if (!buf || type != OBJ_TAG)
1307 len = parse_signature(buf, size);
1311 * We could verify this signature and either omit the tag when
1312 * it does not validate, but the integrator may not have the
1313 * public key of the signer of the tag he is merging, while a
1314 * later auditor may have it while auditing, so let's not run
1315 * verify-signed-buffer here for now...
1317 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1318 * warn("warning: signed tag unverified.");
1320 mergetag = xcalloc(1, sizeof(*mergetag));
1321 mergetag->key = xstrdup("mergetag");
1322 mergetag->value = buf;
1323 mergetag->len = size;
1326 *tail = &mergetag->next;
1333 int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
1335 struct strbuf payload = STRBUF_INIT;
1336 struct strbuf signature = STRBUF_INIT;
1341 if (parse_signed_commit(commit, &payload, &signature) <= 0)
1343 ret = check_signature(payload.buf, payload.len, signature.buf,
1344 signature.len, sigc);
1347 strbuf_release(&payload);
1348 strbuf_release(&signature);
1355 void append_merge_tag_headers(struct commit_list *parents,
1356 struct commit_extra_header ***tail)
1359 struct commit *parent = parents->item;
1360 handle_signed_tag(parent, tail);
1361 parents = parents->next;
1365 static void add_extra_header(struct strbuf *buffer,
1366 struct commit_extra_header *extra)
1368 strbuf_addstr(buffer, extra->key);
1370 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1372 strbuf_addch(buffer, '\n');
1375 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1376 const char **exclude)
1378 struct commit_extra_header *extra = NULL;
1380 const char *buffer = get_commit_buffer(commit, &size);
1381 extra = read_commit_extra_header_lines(buffer, size, exclude);
1382 unuse_commit_buffer(commit, buffer);
1386 int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
1388 struct commit_extra_header *extra, *to_free;
1391 to_free = read_commit_extra_headers(commit, NULL);
1392 for (extra = to_free; !res && extra; extra = extra->next) {
1393 if (strcmp(extra->key, "mergetag"))
1394 continue; /* not a merge tag */
1395 res = fn(commit, extra, data);
1397 free_commit_extra_headers(to_free);
1401 static inline int standard_header_field(const char *field, size_t len)
1403 return ((len == 4 && !memcmp(field, "tree", 4)) ||
1404 (len == 6 && !memcmp(field, "parent", 6)) ||
1405 (len == 6 && !memcmp(field, "author", 6)) ||
1406 (len == 9 && !memcmp(field, "committer", 9)) ||
1407 (len == 8 && !memcmp(field, "encoding", 8)));
1410 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1416 size_t xlen = strlen(*exclude);
1417 if (len == xlen && !memcmp(field, *exclude, xlen))
1424 static struct commit_extra_header *read_commit_extra_header_lines(
1425 const char *buffer, size_t size,
1426 const char **exclude)
1428 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1429 const char *line, *next, *eof, *eob;
1430 struct strbuf buf = STRBUF_INIT;
1432 for (line = buffer, eob = line + size;
1433 line < eob && *line != '\n';
1435 next = memchr(line, '\n', eob - line);
1436 next = next ? next + 1 : eob;
1440 strbuf_add(&buf, line + 1, next - (line + 1));
1444 it->value = strbuf_detach(&buf, &it->len);
1448 eof = memchr(line, ' ', next - line);
1451 else if (standard_header_field(line, eof - line) ||
1452 excluded_header_field(line, eof - line, exclude))
1455 it = xcalloc(1, sizeof(*it));
1456 it->key = xmemdupz(line, eof-line);
1460 strbuf_add(&buf, eof + 1, next - (eof + 1));
1463 it->value = strbuf_detach(&buf, &it->len);
1467 void free_commit_extra_headers(struct commit_extra_header *extra)
1470 struct commit_extra_header *next = extra->next;
1478 int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1479 struct commit_list *parents, struct object_id *ret,
1480 const char *author, const char *sign_commit)
1482 struct commit_extra_header *extra = NULL, **tail = &extra;
1485 append_merge_tag_headers(parents, &tail);
1486 result = commit_tree_extended(msg, msg_len, tree, parents, ret,
1487 author, sign_commit, extra);
1488 free_commit_extra_headers(extra);
1492 static int find_invalid_utf8(const char *buf, int len)
1495 static const unsigned int max_codepoint[] = {
1496 0x7f, 0x7ff, 0xffff, 0x10ffff
1500 unsigned char c = *buf++;
1501 int bytes, bad_offset;
1502 unsigned int codepoint;
1503 unsigned int min_val, max_val;
1508 /* Simple US-ASCII? No worries. */
1512 bad_offset = offset-1;
1515 * Count how many more high bits set: that's how
1516 * many more bytes this sequence should have.
1525 * Must be between 1 and 3 more bytes. Longer sequences result in
1526 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1528 if (bytes < 1 || 3 < bytes)
1531 /* Do we *have* that many bytes? */
1536 * Place the encoded bits at the bottom of the value and compute the
1539 codepoint = (c & 0x7f) >> bytes;
1540 min_val = max_codepoint[bytes-1] + 1;
1541 max_val = max_codepoint[bytes];
1546 /* And verify that they are good continuation bytes */
1549 codepoint |= *buf & 0x3f;
1550 if ((*buf++ & 0xc0) != 0x80)
1554 /* Reject codepoints that are out of range for the sequence length. */
1555 if (codepoint < min_val || codepoint > max_val)
1557 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1558 if ((codepoint & 0x1ff800) == 0xd800)
1560 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1561 if ((codepoint & 0xfffe) == 0xfffe)
1563 /* So are anything in the range U+FDD0..U+FDEF. */
1564 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
1571 * This verifies that the buffer is in proper utf8 format.
1573 * If it isn't, it assumes any non-utf8 characters are Latin1,
1574 * and does the conversion.
1576 static int verify_utf8(struct strbuf *buf)
1584 unsigned char replace[2];
1586 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1592 strbuf_remove(buf, pos, 1);
1594 /* We know 'c' must be in the range 128-255 */
1595 replace[0] = 0xc0 + (c >> 6);
1596 replace[1] = 0x80 + (c & 0x3f);
1597 strbuf_insert(buf, pos, replace, 2);
1602 static const char commit_utf8_warn[] =
1603 N_("Warning: commit message did not conform to UTF-8.\n"
1604 "You may want to amend it after fixing the message, or set the config\n"
1605 "variable i18n.commitencoding to the encoding your project uses.\n");
1607 int commit_tree_extended(const char *msg, size_t msg_len,
1608 const struct object_id *tree,
1609 struct commit_list *parents, struct object_id *ret,
1610 const char *author, const char *sign_commit,
1611 struct commit_extra_header *extra)
1614 int encoding_is_utf8;
1615 struct strbuf buffer;
1617 assert_oid_type(tree, OBJ_TREE);
1619 if (memchr(msg, '\0', msg_len))
1620 return error("a NUL byte in commit log message not allowed.");
1622 /* Not having i18n.commitencoding is the same as having utf-8 */
1623 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1625 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1626 strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree));
1629 * NOTE! This ordering means that the same exact tree merged with a
1630 * different order of parents will be a _different_ changeset even
1631 * if everything else stays the same.
1634 struct commit *parent = pop_commit(&parents);
1635 strbuf_addf(&buffer, "parent %s\n",
1636 oid_to_hex(&parent->object.oid));
1639 /* Person/date information */
1641 author = git_author_info(IDENT_STRICT);
1642 strbuf_addf(&buffer, "author %s\n", author);
1643 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1644 if (!encoding_is_utf8)
1645 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1648 add_extra_header(&buffer, extra);
1649 extra = extra->next;
1651 strbuf_addch(&buffer, '\n');
1653 /* And add the comment */
1654 strbuf_add(&buffer, msg, msg_len);
1656 /* And check the encoding */
1657 if (encoding_is_utf8 && !verify_utf8(&buffer))
1658 fprintf(stderr, _(commit_utf8_warn));
1660 if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
1665 result = write_object_file(buffer.buf, buffer.len, commit_type, ret);
1667 strbuf_release(&buffer);
1671 define_commit_slab(merge_desc_slab, struct merge_remote_desc *);
1672 static struct merge_desc_slab merge_desc_slab = COMMIT_SLAB_INIT(1, merge_desc_slab);
1674 struct merge_remote_desc *merge_remote_util(struct commit *commit)
1676 return *merge_desc_slab_at(&merge_desc_slab, commit);
1679 void set_merge_remote_desc(struct commit *commit,
1680 const char *name, struct object *obj)
1682 struct merge_remote_desc *desc;
1683 FLEX_ALLOC_STR(desc, name, name);
1685 *merge_desc_slab_at(&merge_desc_slab, commit) = desc;
1688 struct commit *get_merge_parent(const char *name)
1691 struct commit *commit;
1692 struct object_id oid;
1693 if (get_oid(name, &oid))
1695 obj = parse_object(&oid);
1696 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1697 if (commit && !merge_remote_util(commit))
1698 set_merge_remote_desc(commit, name, obj);
1703 * Append a commit to the end of the commit_list.
1705 * next starts by pointing to the variable that holds the head of an
1706 * empty commit_list, and is updated to point to the "next" field of
1707 * the last item on the list as new commits are appended.
1711 * struct commit_list *list;
1712 * struct commit_list **next = &list;
1714 * next = commit_list_append(c1, next);
1715 * next = commit_list_append(c2, next);
1716 * assert(commit_list_count(list) == 2);
1719 struct commit_list **commit_list_append(struct commit *commit,
1720 struct commit_list **next)
1722 struct commit_list *new_commit = xmalloc(sizeof(struct commit_list));
1723 new_commit->item = commit;
1725 new_commit->next = NULL;
1726 return &new_commit->next;
1729 const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1731 int key_len = strlen(key);
1732 const char *line = msg;
1735 const char *eol = strchrnul(line, '\n');
1740 if (eol - line > key_len &&
1741 !strncmp(line, key, key_len) &&
1742 line[key_len] == ' ') {
1743 *out_len = eol - line - key_len - 1;
1744 return line + key_len + 1;
1746 line = *eol ? eol + 1 : NULL;
1752 * Inspect the given string and determine the true "end" of the log message, in
1753 * order to find where to put a new Signed-off-by: line. Ignored are
1754 * trailing comment lines and blank lines. To support "git commit -s
1755 * --amend" on an existing commit, we also ignore "Conflicts:". To
1756 * support "git commit -v", we truncate at cut lines.
1758 * Returns the number of bytes from the tail to ignore, to be fed as
1759 * the second parameter to append_signoff().
1761 int ignore_non_trailer(const char *buf, size_t len)
1765 int in_old_conflicts_block = 0;
1766 size_t cutoff = wt_status_locate_end(buf, len);
1768 while (bol < cutoff) {
1769 const char *next_line = memchr(buf + bol, '\n', len - bol);
1772 next_line = buf + len;
1776 if (buf[bol] == comment_line_char || buf[bol] == '\n') {
1777 /* is this the first of the run of comments? */
1780 /* otherwise, it is just continuing */
1781 } else if (starts_with(buf + bol, "Conflicts:\n")) {
1782 in_old_conflicts_block = 1;
1785 } else if (in_old_conflicts_block && buf[bol] == '\t') {
1786 ; /* a pathname in the conflicts block */
1788 /* the previous was not trailing comment */
1790 in_old_conflicts_block = 0;
1792 bol = next_line - buf;
1794 return boc ? len - boc : len - cutoff;