4 #include "commit-graph.h"
11 #include "gpg-interface.h"
12 #include "mergesort.h"
13 #include "commit-slab.h"
14 #include "prio-queue.h"
15 #include "sha1-lookup.h"
16 #include "wt-status.h"
19 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
21 int save_commit_buffer = 1;
23 const char *commit_type = "commit";
25 struct commit *lookup_commit_reference_gently(const struct object_id *oid,
28 struct object *obj = deref_tag(parse_object(oid), NULL, 0);
32 return object_as_type(obj, OBJ_COMMIT, quiet);
35 struct commit *lookup_commit_reference(const struct object_id *oid)
37 return lookup_commit_reference_gently(oid, 0);
40 struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name)
42 struct commit *c = lookup_commit_reference(oid);
44 die(_("could not parse %s"), ref_name);
45 if (oidcmp(oid, &c->object.oid)) {
46 warning(_("%s %s is not a commit!"),
47 ref_name, oid_to_hex(oid));
52 struct commit *lookup_commit(const struct object_id *oid)
54 struct object *obj = lookup_object(oid->hash);
56 return create_object(the_repository, oid->hash,
57 alloc_commit_node(the_repository));
58 return object_as_type(obj, OBJ_COMMIT, 0);
61 struct commit *lookup_commit_reference_by_name(const char *name)
64 struct commit *commit;
66 if (get_oid_committish(name, &oid))
68 commit = lookup_commit_reference(&oid);
69 if (parse_commit(commit))
74 static timestamp_t parse_commit_date(const char *buf, const char *tail)
80 if (memcmp(buf, "author", 6))
82 while (buf < tail && *buf++ != '\n')
86 if (memcmp(buf, "committer", 9))
88 while (buf < tail && *buf++ != '>')
93 while (buf < tail && *buf++ != '\n')
97 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
98 return parse_timestamp(dateptr, NULL, 10);
101 static struct commit_graft **commit_graft;
102 static int commit_graft_alloc, commit_graft_nr;
104 static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
106 struct commit_graft **commit_graft_table = table;
107 return commit_graft_table[index]->oid.hash;
110 static int commit_graft_pos(const unsigned char *sha1)
112 return sha1_pos(sha1, commit_graft, commit_graft_nr,
113 commit_graft_sha1_access);
116 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
118 int pos = commit_graft_pos(graft->oid.hash);
124 free(commit_graft[pos]);
125 commit_graft[pos] = graft;
130 ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc);
132 if (pos < commit_graft_nr)
133 MOVE_ARRAY(commit_graft + pos + 1, commit_graft + pos,
134 commit_graft_nr - pos - 1);
135 commit_graft[pos] = graft;
139 struct commit_graft *read_graft_line(struct strbuf *line)
141 /* The format is just "Commit Parent1 Parent2 ...\n" */
143 const char *tail = NULL;
144 struct commit_graft *graft = NULL;
145 struct object_id dummy_oid, *oid;
148 if (!line->len || line->buf[0] == '#')
151 * phase 0 verifies line, counts hashes in line and allocates graft
152 * phase 1 fills graft
154 for (phase = 0; phase < 2; phase++) {
155 oid = graft ? &graft->oid : &dummy_oid;
156 if (parse_oid_hex(line->buf, oid, &tail))
158 for (i = 0; *tail != '\0'; i++) {
159 oid = graft ? &graft->parent[i] : &dummy_oid;
160 if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
164 graft = xmalloc(st_add(sizeof(*graft),
165 st_mult(sizeof(struct object_id), i)));
166 graft->nr_parent = i;
172 error("bad graft data: %s", line->buf);
177 static int read_graft_file(const char *graft_file)
179 FILE *fp = fopen_or_warn(graft_file, "r");
180 struct strbuf buf = STRBUF_INIT;
183 if (advice_graft_file_deprecated)
184 advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
185 "and will be removed in a future Git version.\n"
187 "Please use \"git replace --convert-graft-file\"\n"
188 "to convert the grafts into replace refs.\n"
190 "Turn this message off by running\n"
191 "\"git config advice.graftFileDeprecated false\""));
192 while (!strbuf_getwholeline(&buf, fp, '\n')) {
193 /* The format is just "Commit Parent1 Parent2 ...\n" */
194 struct commit_graft *graft = read_graft_line(&buf);
197 if (register_commit_graft(graft, 1))
198 error("duplicate graft data: %s", buf.buf);
201 strbuf_release(&buf);
205 static void prepare_commit_graft(void)
207 static int commit_graft_prepared;
210 if (commit_graft_prepared)
212 if (!startup_info->have_repository)
215 graft_file = get_graft_file();
216 read_graft_file(graft_file);
217 /* make sure shallows are read */
218 is_repository_shallow();
219 commit_graft_prepared = 1;
222 struct commit_graft *lookup_commit_graft(const struct object_id *oid)
225 prepare_commit_graft();
226 pos = commit_graft_pos(oid->hash);
229 return commit_graft[pos];
232 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
235 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
236 ret = fn(commit_graft[i], cb_data);
240 int unregister_shallow(const struct object_id *oid)
242 int pos = commit_graft_pos(oid->hash);
245 if (pos + 1 < commit_graft_nr)
246 MOVE_ARRAY(commit_graft + pos, commit_graft + pos + 1,
247 commit_graft_nr - pos - 1);
252 struct commit_buffer {
256 define_commit_slab(buffer_slab, struct commit_buffer);
257 static struct buffer_slab buffer_slab = COMMIT_SLAB_INIT(1, buffer_slab);
259 void set_commit_buffer(struct commit *commit, void *buffer, unsigned long size)
261 struct commit_buffer *v = buffer_slab_at(&buffer_slab, commit);
266 const void *get_cached_commit_buffer(const struct commit *commit, unsigned long *sizep)
268 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
279 const void *get_commit_buffer(const struct commit *commit, unsigned long *sizep)
281 const void *ret = get_cached_commit_buffer(commit, sizep);
283 enum object_type type;
285 ret = read_object_file(&commit->object.oid, &type, &size);
287 die("cannot read commit object %s",
288 oid_to_hex(&commit->object.oid));
289 if (type != OBJ_COMMIT)
290 die("expected commit for %s, got %s",
291 oid_to_hex(&commit->object.oid), type_name(type));
298 void unuse_commit_buffer(const struct commit *commit, const void *buffer)
300 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
301 if (!(v && v->buffer == buffer))
302 free((void *)buffer);
305 void free_commit_buffer(struct commit *commit)
307 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
309 FREE_AND_NULL(v->buffer);
314 struct tree *get_commit_tree(const struct commit *commit)
316 if (commit->maybe_tree || !commit->object.parsed)
317 return commit->maybe_tree;
319 if (commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
320 BUG("commit has NULL tree, but was not loaded from commit-graph");
322 return get_commit_tree_in_graph(commit);
325 struct object_id *get_commit_tree_oid(const struct commit *commit)
327 return &get_commit_tree(commit)->object.oid;
330 void release_commit_memory(struct commit *c)
332 c->maybe_tree = NULL;
334 free_commit_buffer(c);
335 free_commit_list(c->parents);
336 /* TODO: what about commit->util? */
338 c->object.parsed = 0;
341 const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
343 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
360 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size, int check_graph)
362 const char *tail = buffer;
363 const char *bufptr = buffer;
364 struct object_id parent;
365 struct commit_list **pptr;
366 struct commit_graft *graft;
367 const int tree_entry_len = GIT_SHA1_HEXSZ + 5;
368 const int parent_entry_len = GIT_SHA1_HEXSZ + 7;
370 if (item->object.parsed)
372 item->object.parsed = 1;
374 if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
375 bufptr[tree_entry_len] != '\n')
376 return error("bogus commit object %s", oid_to_hex(&item->object.oid));
377 if (get_oid_hex(bufptr + 5, &parent) < 0)
378 return error("bad tree pointer in commit %s",
379 oid_to_hex(&item->object.oid));
380 item->maybe_tree = lookup_tree(&parent);
381 bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
382 pptr = &item->parents;
384 graft = lookup_commit_graft(&item->object.oid);
385 while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
386 struct commit *new_parent;
388 if (tail <= bufptr + parent_entry_len + 1 ||
389 get_oid_hex(bufptr + 7, &parent) ||
390 bufptr[parent_entry_len] != '\n')
391 return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
392 bufptr += parent_entry_len + 1;
394 * The clone is shallow if nr_parent < 0, and we must
395 * not traverse its real parents even when we unhide them.
397 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
399 new_parent = lookup_commit(&parent);
401 pptr = &commit_list_insert(new_parent, pptr)->next;
405 struct commit *new_parent;
406 for (i = 0; i < graft->nr_parent; i++) {
407 new_parent = lookup_commit(&graft->parent[i]);
410 pptr = &commit_list_insert(new_parent, pptr)->next;
413 item->date = parse_commit_date(bufptr, tail);
416 load_commit_graph_info(item);
421 int parse_commit_gently(struct commit *item, int quiet_on_missing)
423 enum object_type type;
430 if (item->object.parsed)
432 if (parse_commit_in_graph(item))
434 buffer = read_object_file(&item->object.oid, &type, &size);
436 return quiet_on_missing ? -1 :
437 error("Could not read %s",
438 oid_to_hex(&item->object.oid));
439 if (type != OBJ_COMMIT) {
441 return error("Object %s not a commit",
442 oid_to_hex(&item->object.oid));
444 ret = parse_commit_buffer(item, buffer, size, 0);
445 if (save_commit_buffer && !ret) {
446 set_commit_buffer(item, buffer, size);
453 void parse_commit_or_die(struct commit *item)
455 if (parse_commit(item))
456 die("unable to parse commit %s",
457 item ? oid_to_hex(&item->object.oid) : "(null)");
460 int find_commit_subject(const char *commit_buffer, const char **subject)
463 const char *p = commit_buffer;
465 while (*p && (*p != '\n' || p[1] != '\n'))
468 p = skip_blank_lines(p + 2);
469 eol = strchrnul(p, '\n');
478 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
480 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
481 new_list->item = item;
482 new_list->next = *list_p;
487 unsigned commit_list_count(const struct commit_list *l)
490 for (; l; l = l->next )
495 struct commit_list *copy_commit_list(struct commit_list *list)
497 struct commit_list *head = NULL;
498 struct commit_list **pp = &head;
500 pp = commit_list_append(list->item, pp);
506 void free_commit_list(struct commit_list *list)
512 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
514 struct commit_list **pp = list;
515 struct commit_list *p;
516 while ((p = *pp) != NULL) {
517 if (p->item->date < item->date) {
522 return commit_list_insert(item, pp);
525 static int commit_list_compare_by_date(const void *a, const void *b)
527 timestamp_t a_date = ((const struct commit_list *)a)->item->date;
528 timestamp_t b_date = ((const struct commit_list *)b)->item->date;
536 static void *commit_list_get_next(const void *a)
538 return ((const struct commit_list *)a)->next;
541 static void commit_list_set_next(void *a, void *next)
543 ((struct commit_list *)a)->next = next;
546 void commit_list_sort_by_date(struct commit_list **list)
548 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
549 commit_list_compare_by_date);
552 struct commit *pop_most_recent_commit(struct commit_list **list,
555 struct commit *ret = pop_commit(list);
556 struct commit_list *parents = ret->parents;
559 struct commit *commit = parents->item;
560 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
561 commit->object.flags |= mark;
562 commit_list_insert_by_date(commit, list);
564 parents = parents->next;
569 static void clear_commit_marks_1(struct commit_list **plist,
570 struct commit *commit, unsigned int mark)
573 struct commit_list *parents;
575 if (!(mark & commit->object.flags))
578 commit->object.flags &= ~mark;
580 parents = commit->parents;
584 while ((parents = parents->next))
585 commit_list_insert(parents->item, plist);
587 commit = commit->parents->item;
591 void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
593 struct commit_list *list = NULL;
596 clear_commit_marks_1(&list, *commit, mark);
600 clear_commit_marks_1(&list, pop_commit(&list), mark);
603 void clear_commit_marks(struct commit *commit, unsigned int mark)
605 clear_commit_marks_many(1, &commit, mark);
608 struct commit *pop_commit(struct commit_list **stack)
610 struct commit_list *top = *stack;
611 struct commit *item = top ? top->item : NULL;
621 * Topological sort support
624 /* count number of children that have not been emitted */
625 define_commit_slab(indegree_slab, int);
627 /* record author-date for each commit object */
628 define_commit_slab(author_date_slab, unsigned long);
630 static void record_author_date(struct author_date_slab *author_date,
631 struct commit *commit)
633 const char *buffer = get_commit_buffer(commit, NULL);
634 struct ident_split ident;
635 const char *ident_line;
640 ident_line = find_commit_header(buffer, "author", &ident_len);
642 goto fail_exit; /* no author line */
643 if (split_ident_line(&ident, ident_line, ident_len) ||
644 !ident.date_begin || !ident.date_end)
645 goto fail_exit; /* malformed "author" line */
647 date = parse_timestamp(ident.date_begin, &date_end, 10);
648 if (date_end != ident.date_end)
649 goto fail_exit; /* malformed date */
650 *(author_date_slab_at(author_date, commit)) = date;
653 unuse_commit_buffer(commit, buffer);
656 static int compare_commits_by_author_date(const void *a_, const void *b_,
659 const struct commit *a = a_, *b = b_;
660 struct author_date_slab *author_date = cb_data;
661 timestamp_t a_date = *(author_date_slab_at(author_date, a));
662 timestamp_t b_date = *(author_date_slab_at(author_date, b));
664 /* newer commits with larger date first */
667 else if (a_date > b_date)
672 int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused)
674 const struct commit *a = a_, *b = b_;
676 /* newer commits first */
677 if (a->generation < b->generation)
679 else if (a->generation > b->generation)
682 /* use date as a heuristic when generations are equal */
683 if (a->date < b->date)
685 else if (a->date > b->date)
690 int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
692 const struct commit *a = a_, *b = b_;
693 /* newer commits with larger date first */
694 if (a->date < b->date)
696 else if (a->date > b->date)
702 * Performs an in-place topological sort on the list supplied.
704 void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
706 struct commit_list *next, *orig = *list;
707 struct commit_list **pptr;
708 struct indegree_slab indegree;
709 struct prio_queue queue;
710 struct commit *commit;
711 struct author_date_slab author_date;
717 init_indegree_slab(&indegree);
718 memset(&queue, '\0', sizeof(queue));
720 switch (sort_order) {
721 default: /* REV_SORT_IN_GRAPH_ORDER */
722 queue.compare = NULL;
724 case REV_SORT_BY_COMMIT_DATE:
725 queue.compare = compare_commits_by_commit_date;
727 case REV_SORT_BY_AUTHOR_DATE:
728 init_author_date_slab(&author_date);
729 queue.compare = compare_commits_by_author_date;
730 queue.cb_data = &author_date;
734 /* Mark them and clear the indegree */
735 for (next = orig; next; next = next->next) {
736 struct commit *commit = next->item;
737 *(indegree_slab_at(&indegree, commit)) = 1;
738 /* also record the author dates, if needed */
739 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
740 record_author_date(&author_date, commit);
743 /* update the indegree */
744 for (next = orig; next; next = next->next) {
745 struct commit_list *parents = next->item->parents;
747 struct commit *parent = parents->item;
748 int *pi = indegree_slab_at(&indegree, parent);
752 parents = parents->next;
759 * tips are nodes not reachable from any other node in the list
761 * the tips serve as a starting set for the work queue.
763 for (next = orig; next; next = next->next) {
764 struct commit *commit = next->item;
766 if (*(indegree_slab_at(&indegree, commit)) == 1)
767 prio_queue_put(&queue, commit);
771 * This is unfortunate; the initial tips need to be shown
772 * in the order given from the revision traversal machinery.
774 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
775 prio_queue_reverse(&queue);
777 /* We no longer need the commit list */
778 free_commit_list(orig);
782 while ((commit = prio_queue_get(&queue)) != NULL) {
783 struct commit_list *parents;
785 for (parents = commit->parents; parents ; parents = parents->next) {
786 struct commit *parent = parents->item;
787 int *pi = indegree_slab_at(&indegree, parent);
793 * parents are only enqueued for emission
794 * when all their children have been emitted thereby
795 * guaranteeing topological order.
798 prio_queue_put(&queue, parent);
801 * all children of commit have already been
802 * emitted. we can emit it now.
804 *(indegree_slab_at(&indegree, commit)) = 0;
806 pptr = &commit_list_insert(commit, pptr)->next;
809 clear_indegree_slab(&indegree);
810 clear_prio_queue(&queue);
811 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
812 clear_author_date_slab(&author_date);
815 /* merge-base stuff */
817 /* Remember to update object flag allocation in object.h */
818 #define PARENT1 (1u<<16)
819 #define PARENT2 (1u<<17)
820 #define STALE (1u<<18)
821 #define RESULT (1u<<19)
823 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
825 static int queue_has_nonstale(struct prio_queue *queue)
828 for (i = 0; i < queue->nr; i++) {
829 struct commit *commit = queue->array[i].data;
830 if (!(commit->object.flags & STALE))
836 /* all input commits in one and twos[] must have been parsed! */
837 static struct commit_list *paint_down_to_common(struct commit *one, int n,
838 struct commit **twos,
841 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
842 struct commit_list *result = NULL;
844 uint32_t last_gen = GENERATION_NUMBER_INFINITY;
846 one->object.flags |= PARENT1;
848 commit_list_append(one, &result);
851 prio_queue_put(&queue, one);
853 for (i = 0; i < n; i++) {
854 twos[i]->object.flags |= PARENT2;
855 prio_queue_put(&queue, twos[i]);
858 while (queue_has_nonstale(&queue)) {
859 struct commit *commit = prio_queue_get(&queue);
860 struct commit_list *parents;
863 if (commit->generation > last_gen)
864 BUG("bad generation skip %8x > %8x at %s",
865 commit->generation, last_gen,
866 oid_to_hex(&commit->object.oid));
867 last_gen = commit->generation;
869 if (commit->generation < min_generation)
872 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
873 if (flags == (PARENT1 | PARENT2)) {
874 if (!(commit->object.flags & RESULT)) {
875 commit->object.flags |= RESULT;
876 commit_list_insert_by_date(commit, &result);
878 /* Mark parents of a found merge stale */
881 parents = commit->parents;
883 struct commit *p = parents->item;
884 parents = parents->next;
885 if ((p->object.flags & flags) == flags)
889 p->object.flags |= flags;
890 prio_queue_put(&queue, p);
894 clear_prio_queue(&queue);
898 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
900 struct commit_list *list = NULL;
901 struct commit_list *result = NULL;
904 for (i = 0; i < n; i++) {
907 * We do not mark this even with RESULT so we do not
908 * have to clean it up.
910 return commit_list_insert(one, &result);
913 if (parse_commit(one))
915 for (i = 0; i < n; i++) {
916 if (parse_commit(twos[i]))
920 list = paint_down_to_common(one, n, twos, 0);
923 struct commit *commit = pop_commit(&list);
924 if (!(commit->object.flags & STALE))
925 commit_list_insert_by_date(commit, &result);
930 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
932 struct commit_list *i, *j, *k, *ret = NULL;
937 commit_list_insert(in->item, &ret);
939 for (i = in->next; i; i = i->next) {
940 struct commit_list *new_commits = NULL, *end = NULL;
942 for (j = ret; j; j = j->next) {
943 struct commit_list *bases;
944 bases = get_merge_bases(i->item, j->item);
949 for (k = bases; k; k = k->next)
957 static int remove_redundant(struct commit **array, int cnt)
960 * Some commit in the array may be an ancestor of
961 * another commit. Move such commit to the end of
962 * the array, and return the number of commits that
963 * are independent from each other.
965 struct commit **work;
966 unsigned char *redundant;
970 work = xcalloc(cnt, sizeof(*work));
971 redundant = xcalloc(cnt, 1);
972 ALLOC_ARRAY(filled_index, cnt - 1);
974 for (i = 0; i < cnt; i++)
975 parse_commit(array[i]);
976 for (i = 0; i < cnt; i++) {
977 struct commit_list *common;
978 uint32_t min_generation = array[i]->generation;
982 for (j = filled = 0; j < cnt; j++) {
983 if (i == j || redundant[j])
985 filled_index[filled] = j;
986 work[filled++] = array[j];
988 if (array[j]->generation < min_generation)
989 min_generation = array[j]->generation;
991 common = paint_down_to_common(array[i], filled, work,
993 if (array[i]->object.flags & PARENT2)
995 for (j = 0; j < filled; j++)
996 if (work[j]->object.flags & PARENT1)
997 redundant[filled_index[j]] = 1;
998 clear_commit_marks(array[i], all_flags);
999 clear_commit_marks_many(filled, work, all_flags);
1000 free_commit_list(common);
1003 /* Now collect the result */
1004 COPY_ARRAY(work, array, cnt);
1005 for (i = filled = 0; i < cnt; i++)
1007 array[filled++] = work[i];
1008 for (j = filled, i = 0; i < cnt; i++)
1010 array[j++] = work[i];
1017 static struct commit_list *get_merge_bases_many_0(struct commit *one,
1019 struct commit **twos,
1022 struct commit_list *list;
1023 struct commit **rslt;
1024 struct commit_list *result;
1027 result = merge_bases_many(one, n, twos);
1028 for (i = 0; i < n; i++) {
1032 if (!result || !result->next) {
1034 clear_commit_marks(one, all_flags);
1035 clear_commit_marks_many(n, twos, all_flags);
1040 /* There are more than one */
1041 cnt = commit_list_count(result);
1042 rslt = xcalloc(cnt, sizeof(*rslt));
1043 for (list = result, i = 0; list; list = list->next)
1044 rslt[i++] = list->item;
1045 free_commit_list(result);
1047 clear_commit_marks(one, all_flags);
1048 clear_commit_marks_many(n, twos, all_flags);
1050 cnt = remove_redundant(rslt, cnt);
1052 for (i = 0; i < cnt; i++)
1053 commit_list_insert_by_date(rslt[i], &result);
1058 struct commit_list *get_merge_bases_many(struct commit *one,
1060 struct commit **twos)
1062 return get_merge_bases_many_0(one, n, twos, 1);
1065 struct commit_list *get_merge_bases_many_dirty(struct commit *one,
1067 struct commit **twos)
1069 return get_merge_bases_many_0(one, n, twos, 0);
1072 struct commit_list *get_merge_bases(struct commit *one, struct commit *two)
1074 return get_merge_bases_many_0(one, 1, &two, 1);
1078 * Is "commit" a descendant of one of the elements on the "with_commit" list?
1080 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
1084 while (with_commit) {
1085 struct commit *other;
1087 other = with_commit->item;
1088 with_commit = with_commit->next;
1089 if (in_merge_bases(other, commit))
1096 * Is "commit" an ancestor of one of the "references"?
1098 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
1100 struct commit_list *bases;
1102 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
1104 if (parse_commit(commit))
1106 for (i = 0; i < nr_reference; i++) {
1107 if (parse_commit(reference[i]))
1109 if (reference[i]->generation < min_generation)
1110 min_generation = reference[i]->generation;
1113 if (commit->generation > min_generation)
1116 bases = paint_down_to_common(commit, nr_reference, reference, commit->generation);
1117 if (commit->object.flags & PARENT2)
1119 clear_commit_marks(commit, all_flags);
1120 clear_commit_marks_many(nr_reference, reference, all_flags);
1121 free_commit_list(bases);
1126 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1128 int in_merge_bases(struct commit *commit, struct commit *reference)
1130 return in_merge_bases_many(commit, 1, &reference);
1133 struct commit_list *reduce_heads(struct commit_list *heads)
1135 struct commit_list *p;
1136 struct commit_list *result = NULL, **tail = &result;
1137 struct commit **array;
1144 for (p = heads; p; p = p->next)
1145 p->item->object.flags &= ~STALE;
1146 for (p = heads, num_head = 0; p; p = p->next) {
1147 if (p->item->object.flags & STALE)
1149 p->item->object.flags |= STALE;
1152 array = xcalloc(num_head, sizeof(*array));
1153 for (p = heads, i = 0; p; p = p->next) {
1154 if (p->item->object.flags & STALE) {
1155 array[i++] = p->item;
1156 p->item->object.flags &= ~STALE;
1159 num_head = remove_redundant(array, num_head);
1160 for (i = 0; i < num_head; i++)
1161 tail = &commit_list_insert(array[i], tail)->next;
1166 void reduce_heads_replace(struct commit_list **heads)
1168 struct commit_list *result = reduce_heads(*heads);
1169 free_commit_list(*heads);
1173 static const char gpg_sig_header[] = "gpgsig";
1174 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
1176 static int do_sign_commit(struct strbuf *buf, const char *keyid)
1178 struct strbuf sig = STRBUF_INIT;
1179 int inspos, copypos;
1182 /* find the end of the header */
1183 eoh = strstr(buf->buf, "\n\n");
1187 inspos = eoh - buf->buf + 1;
1189 if (!keyid || !*keyid)
1190 keyid = get_signing_key();
1191 if (sign_buffer(buf, &sig, keyid)) {
1192 strbuf_release(&sig);
1196 for (copypos = 0; sig.buf[copypos]; ) {
1197 const char *bol = sig.buf + copypos;
1198 const char *eol = strchrnul(bol, '\n');
1199 int len = (eol - bol) + !!*eol;
1202 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1203 inspos += gpg_sig_header_len;
1205 strbuf_insert(buf, inspos++, " ", 1);
1206 strbuf_insert(buf, inspos, bol, len);
1210 strbuf_release(&sig);
1214 int parse_signed_commit(const struct commit *commit,
1215 struct strbuf *payload, struct strbuf *signature)
1219 const char *buffer = get_commit_buffer(commit, &size);
1220 int in_signature, saw_signature = -1;
1221 const char *line, *tail;
1224 tail = buffer + size;
1227 while (line < tail) {
1228 const char *sig = NULL;
1229 const char *next = memchr(line, '\n', tail - line);
1231 next = next ? next + 1 : tail;
1232 if (in_signature && line[0] == ' ')
1234 else if (starts_with(line, gpg_sig_header) &&
1235 line[gpg_sig_header_len] == ' ')
1236 sig = line + gpg_sig_header_len + 1;
1238 strbuf_add(signature, sig, next - sig);
1243 /* dump the whole remainder of the buffer */
1245 strbuf_add(payload, line, next - line);
1250 unuse_commit_buffer(commit, buffer);
1251 return saw_signature;
1254 int remove_signature(struct strbuf *buf)
1256 const char *line = buf->buf;
1257 const char *tail = buf->buf + buf->len;
1258 int in_signature = 0;
1259 const char *sig_start = NULL;
1260 const char *sig_end = NULL;
1262 while (line < tail) {
1263 const char *next = memchr(line, '\n', tail - line);
1264 next = next ? next + 1 : tail;
1266 if (in_signature && line[0] == ' ')
1268 else if (starts_with(line, gpg_sig_header) &&
1269 line[gpg_sig_header_len] == ' ') {
1275 /* dump the whole remainder of the buffer */
1283 strbuf_remove(buf, sig_start - buf->buf, sig_end - sig_start);
1285 return sig_start != NULL;
1288 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1290 struct merge_remote_desc *desc;
1291 struct commit_extra_header *mergetag;
1293 unsigned long size, len;
1294 enum object_type type;
1296 desc = merge_remote_util(parent);
1297 if (!desc || !desc->obj)
1299 buf = read_object_file(&desc->obj->oid, &type, &size);
1300 if (!buf || type != OBJ_TAG)
1302 len = parse_signature(buf, size);
1306 * We could verify this signature and either omit the tag when
1307 * it does not validate, but the integrator may not have the
1308 * public key of the signer of the tag he is merging, while a
1309 * later auditor may have it while auditing, so let's not run
1310 * verify-signed-buffer here for now...
1312 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1313 * warn("warning: signed tag unverified.");
1315 mergetag = xcalloc(1, sizeof(*mergetag));
1316 mergetag->key = xstrdup("mergetag");
1317 mergetag->value = buf;
1318 mergetag->len = size;
1321 *tail = &mergetag->next;
1328 int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
1330 struct strbuf payload = STRBUF_INIT;
1331 struct strbuf signature = STRBUF_INIT;
1336 if (parse_signed_commit(commit, &payload, &signature) <= 0)
1338 ret = check_signature(payload.buf, payload.len, signature.buf,
1339 signature.len, sigc);
1342 strbuf_release(&payload);
1343 strbuf_release(&signature);
1350 void append_merge_tag_headers(struct commit_list *parents,
1351 struct commit_extra_header ***tail)
1354 struct commit *parent = parents->item;
1355 handle_signed_tag(parent, tail);
1356 parents = parents->next;
1360 static void add_extra_header(struct strbuf *buffer,
1361 struct commit_extra_header *extra)
1363 strbuf_addstr(buffer, extra->key);
1365 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1367 strbuf_addch(buffer, '\n');
1370 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1371 const char **exclude)
1373 struct commit_extra_header *extra = NULL;
1375 const char *buffer = get_commit_buffer(commit, &size);
1376 extra = read_commit_extra_header_lines(buffer, size, exclude);
1377 unuse_commit_buffer(commit, buffer);
1381 int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
1383 struct commit_extra_header *extra, *to_free;
1386 to_free = read_commit_extra_headers(commit, NULL);
1387 for (extra = to_free; !res && extra; extra = extra->next) {
1388 if (strcmp(extra->key, "mergetag"))
1389 continue; /* not a merge tag */
1390 res = fn(commit, extra, data);
1392 free_commit_extra_headers(to_free);
1396 static inline int standard_header_field(const char *field, size_t len)
1398 return ((len == 4 && !memcmp(field, "tree", 4)) ||
1399 (len == 6 && !memcmp(field, "parent", 6)) ||
1400 (len == 6 && !memcmp(field, "author", 6)) ||
1401 (len == 9 && !memcmp(field, "committer", 9)) ||
1402 (len == 8 && !memcmp(field, "encoding", 8)));
1405 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1411 size_t xlen = strlen(*exclude);
1412 if (len == xlen && !memcmp(field, *exclude, xlen))
1419 static struct commit_extra_header *read_commit_extra_header_lines(
1420 const char *buffer, size_t size,
1421 const char **exclude)
1423 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1424 const char *line, *next, *eof, *eob;
1425 struct strbuf buf = STRBUF_INIT;
1427 for (line = buffer, eob = line + size;
1428 line < eob && *line != '\n';
1430 next = memchr(line, '\n', eob - line);
1431 next = next ? next + 1 : eob;
1435 strbuf_add(&buf, line + 1, next - (line + 1));
1439 it->value = strbuf_detach(&buf, &it->len);
1443 eof = memchr(line, ' ', next - line);
1446 else if (standard_header_field(line, eof - line) ||
1447 excluded_header_field(line, eof - line, exclude))
1450 it = xcalloc(1, sizeof(*it));
1451 it->key = xmemdupz(line, eof-line);
1455 strbuf_add(&buf, eof + 1, next - (eof + 1));
1458 it->value = strbuf_detach(&buf, &it->len);
1462 void free_commit_extra_headers(struct commit_extra_header *extra)
1465 struct commit_extra_header *next = extra->next;
1473 int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1474 struct commit_list *parents, struct object_id *ret,
1475 const char *author, const char *sign_commit)
1477 struct commit_extra_header *extra = NULL, **tail = &extra;
1480 append_merge_tag_headers(parents, &tail);
1481 result = commit_tree_extended(msg, msg_len, tree, parents, ret,
1482 author, sign_commit, extra);
1483 free_commit_extra_headers(extra);
1487 static int find_invalid_utf8(const char *buf, int len)
1490 static const unsigned int max_codepoint[] = {
1491 0x7f, 0x7ff, 0xffff, 0x10ffff
1495 unsigned char c = *buf++;
1496 int bytes, bad_offset;
1497 unsigned int codepoint;
1498 unsigned int min_val, max_val;
1503 /* Simple US-ASCII? No worries. */
1507 bad_offset = offset-1;
1510 * Count how many more high bits set: that's how
1511 * many more bytes this sequence should have.
1520 * Must be between 1 and 3 more bytes. Longer sequences result in
1521 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1523 if (bytes < 1 || 3 < bytes)
1526 /* Do we *have* that many bytes? */
1531 * Place the encoded bits at the bottom of the value and compute the
1534 codepoint = (c & 0x7f) >> bytes;
1535 min_val = max_codepoint[bytes-1] + 1;
1536 max_val = max_codepoint[bytes];
1541 /* And verify that they are good continuation bytes */
1544 codepoint |= *buf & 0x3f;
1545 if ((*buf++ & 0xc0) != 0x80)
1549 /* Reject codepoints that are out of range for the sequence length. */
1550 if (codepoint < min_val || codepoint > max_val)
1552 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1553 if ((codepoint & 0x1ff800) == 0xd800)
1555 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1556 if ((codepoint & 0xfffe) == 0xfffe)
1558 /* So are anything in the range U+FDD0..U+FDEF. */
1559 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
1566 * This verifies that the buffer is in proper utf8 format.
1568 * If it isn't, it assumes any non-utf8 characters are Latin1,
1569 * and does the conversion.
1571 static int verify_utf8(struct strbuf *buf)
1579 unsigned char replace[2];
1581 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1587 strbuf_remove(buf, pos, 1);
1589 /* We know 'c' must be in the range 128-255 */
1590 replace[0] = 0xc0 + (c >> 6);
1591 replace[1] = 0x80 + (c & 0x3f);
1592 strbuf_insert(buf, pos, replace, 2);
1597 static const char commit_utf8_warn[] =
1598 N_("Warning: commit message did not conform to UTF-8.\n"
1599 "You may want to amend it after fixing the message, or set the config\n"
1600 "variable i18n.commitencoding to the encoding your project uses.\n");
1602 int commit_tree_extended(const char *msg, size_t msg_len,
1603 const struct object_id *tree,
1604 struct commit_list *parents, struct object_id *ret,
1605 const char *author, const char *sign_commit,
1606 struct commit_extra_header *extra)
1609 int encoding_is_utf8;
1610 struct strbuf buffer;
1612 assert_oid_type(tree, OBJ_TREE);
1614 if (memchr(msg, '\0', msg_len))
1615 return error("a NUL byte in commit log message not allowed.");
1617 /* Not having i18n.commitencoding is the same as having utf-8 */
1618 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1620 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1621 strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree));
1624 * NOTE! This ordering means that the same exact tree merged with a
1625 * different order of parents will be a _different_ changeset even
1626 * if everything else stays the same.
1629 struct commit *parent = pop_commit(&parents);
1630 strbuf_addf(&buffer, "parent %s\n",
1631 oid_to_hex(&parent->object.oid));
1634 /* Person/date information */
1636 author = git_author_info(IDENT_STRICT);
1637 strbuf_addf(&buffer, "author %s\n", author);
1638 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1639 if (!encoding_is_utf8)
1640 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1643 add_extra_header(&buffer, extra);
1644 extra = extra->next;
1646 strbuf_addch(&buffer, '\n');
1648 /* And add the comment */
1649 strbuf_add(&buffer, msg, msg_len);
1651 /* And check the encoding */
1652 if (encoding_is_utf8 && !verify_utf8(&buffer))
1653 fprintf(stderr, _(commit_utf8_warn));
1655 if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
1660 result = write_object_file(buffer.buf, buffer.len, commit_type, ret);
1662 strbuf_release(&buffer);
1666 define_commit_slab(merge_desc_slab, struct merge_remote_desc *);
1667 static struct merge_desc_slab merge_desc_slab = COMMIT_SLAB_INIT(1, merge_desc_slab);
1669 struct merge_remote_desc *merge_remote_util(struct commit *commit)
1671 return *merge_desc_slab_at(&merge_desc_slab, commit);
1674 void set_merge_remote_desc(struct commit *commit,
1675 const char *name, struct object *obj)
1677 struct merge_remote_desc *desc;
1678 FLEX_ALLOC_STR(desc, name, name);
1680 *merge_desc_slab_at(&merge_desc_slab, commit) = desc;
1683 struct commit *get_merge_parent(const char *name)
1686 struct commit *commit;
1687 struct object_id oid;
1688 if (get_oid(name, &oid))
1690 obj = parse_object(&oid);
1691 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1692 if (commit && !merge_remote_util(commit))
1693 set_merge_remote_desc(commit, name, obj);
1698 * Append a commit to the end of the commit_list.
1700 * next starts by pointing to the variable that holds the head of an
1701 * empty commit_list, and is updated to point to the "next" field of
1702 * the last item on the list as new commits are appended.
1706 * struct commit_list *list;
1707 * struct commit_list **next = &list;
1709 * next = commit_list_append(c1, next);
1710 * next = commit_list_append(c2, next);
1711 * assert(commit_list_count(list) == 2);
1714 struct commit_list **commit_list_append(struct commit *commit,
1715 struct commit_list **next)
1717 struct commit_list *new_commit = xmalloc(sizeof(struct commit_list));
1718 new_commit->item = commit;
1720 new_commit->next = NULL;
1721 return &new_commit->next;
1724 const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1726 int key_len = strlen(key);
1727 const char *line = msg;
1730 const char *eol = strchrnul(line, '\n');
1735 if (eol - line > key_len &&
1736 !strncmp(line, key, key_len) &&
1737 line[key_len] == ' ') {
1738 *out_len = eol - line - key_len - 1;
1739 return line + key_len + 1;
1741 line = *eol ? eol + 1 : NULL;
1747 * Inspect the given string and determine the true "end" of the log message, in
1748 * order to find where to put a new Signed-off-by: line. Ignored are
1749 * trailing comment lines and blank lines. To support "git commit -s
1750 * --amend" on an existing commit, we also ignore "Conflicts:". To
1751 * support "git commit -v", we truncate at cut lines.
1753 * Returns the number of bytes from the tail to ignore, to be fed as
1754 * the second parameter to append_signoff().
1756 int ignore_non_trailer(const char *buf, size_t len)
1760 int in_old_conflicts_block = 0;
1761 size_t cutoff = wt_status_locate_end(buf, len);
1763 while (bol < cutoff) {
1764 const char *next_line = memchr(buf + bol, '\n', len - bol);
1767 next_line = buf + len;
1771 if (buf[bol] == comment_line_char || buf[bol] == '\n') {
1772 /* is this the first of the run of comments? */
1775 /* otherwise, it is just continuing */
1776 } else if (starts_with(buf + bol, "Conflicts:\n")) {
1777 in_old_conflicts_block = 1;
1780 } else if (in_old_conflicts_block && buf[bol] == '\t') {
1781 ; /* a pathname in the conflicts block */
1783 /* the previous was not trailing comment */
1785 in_old_conflicts_block = 0;
1787 bol = next_line - buf;
1789 return boc ? len - boc : len - cutoff;