4 #include "repository.h"
5 #include "object-store.h"
12 #include "gpg-interface.h"
13 #include "mergesort.h"
14 #include "commit-slab.h"
15 #include "prio-queue.h"
16 #include "sha1-lookup.h"
17 #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 const unsigned char *commit_graft_sha1_access(size_t index, void *table)
103 struct commit_graft **commit_graft_table = table;
104 return commit_graft_table[index]->oid.hash;
107 static int commit_graft_pos(struct repository *r, const unsigned char *sha1)
109 return sha1_pos(sha1, r->parsed_objects->grafts,
110 r->parsed_objects->grafts_nr,
111 commit_graft_sha1_access);
114 int register_commit_graft(struct repository *r, struct commit_graft *graft,
117 int pos = commit_graft_pos(r, graft->oid.hash);
123 free(r->parsed_objects->grafts[pos]);
124 r->parsed_objects->grafts[pos] = graft;
129 ALLOC_GROW(r->parsed_objects->grafts,
130 r->parsed_objects->grafts_nr + 1,
131 r->parsed_objects->grafts_alloc);
132 r->parsed_objects->grafts_nr++;
133 if (pos < r->parsed_objects->grafts_nr)
134 memmove(r->parsed_objects->grafts + pos + 1,
135 r->parsed_objects->grafts + pos,
136 (r->parsed_objects->grafts_nr - pos - 1) *
137 sizeof(*r->parsed_objects->grafts));
138 r->parsed_objects->grafts[pos] = graft;
142 struct commit_graft *read_graft_line(struct strbuf *line)
144 /* The format is just "Commit Parent1 Parent2 ...\n" */
146 const char *tail = NULL;
147 struct commit_graft *graft = NULL;
148 struct object_id dummy_oid, *oid;
151 if (!line->len || line->buf[0] == '#')
154 * phase 0 verifies line, counts hashes in line and allocates graft
155 * phase 1 fills graft
157 for (phase = 0; phase < 2; phase++) {
158 oid = graft ? &graft->oid : &dummy_oid;
159 if (parse_oid_hex(line->buf, oid, &tail))
161 for (i = 0; *tail != '\0'; i++) {
162 oid = graft ? &graft->parent[i] : &dummy_oid;
163 if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
167 graft = xmalloc(st_add(sizeof(*graft),
168 st_mult(sizeof(struct object_id), i)));
169 graft->nr_parent = i;
175 error("bad graft data: %s", line->buf);
180 static int read_graft_file(struct repository *r, const char *graft_file)
182 FILE *fp = fopen_or_warn(graft_file, "r");
183 struct strbuf buf = STRBUF_INIT;
186 while (!strbuf_getwholeline(&buf, fp, '\n')) {
187 /* The format is just "Commit Parent1 Parent2 ...\n" */
188 struct commit_graft *graft = read_graft_line(&buf);
191 if (register_commit_graft(r, graft, 1))
192 error("duplicate graft data: %s", buf.buf);
195 strbuf_release(&buf);
199 #define prepare_commit_graft(r) prepare_commit_graft_##r()
200 static void prepare_commit_graft_the_repository(void)
202 static int commit_graft_prepared;
205 if (commit_graft_prepared)
207 graft_file = get_graft_file();
208 read_graft_file(the_repository, graft_file);
209 /* make sure shallows are read */
210 is_repository_shallow(the_repository);
211 commit_graft_prepared = 1;
214 struct commit_graft *lookup_commit_graft_the_repository(const struct object_id *oid)
217 prepare_commit_graft(the_repository);
218 pos = commit_graft_pos(the_repository, oid->hash);
221 return the_repository->parsed_objects->grafts[pos];
224 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
227 for (i = ret = 0; i < the_repository->parsed_objects->grafts_nr && !ret; i++)
228 ret = fn(the_repository->parsed_objects->grafts[i], cb_data);
232 int unregister_shallow(const struct object_id *oid)
234 int pos = commit_graft_pos(the_repository, oid->hash);
237 if (pos + 1 < the_repository->parsed_objects->grafts_nr)
238 MOVE_ARRAY(the_repository->parsed_objects->grafts + pos,
239 the_repository->parsed_objects->grafts + pos + 1,
240 the_repository->parsed_objects->grafts_nr - pos - 1);
241 the_repository->parsed_objects->grafts_nr--;
245 struct commit_buffer {
249 define_commit_slab(buffer_slab, struct commit_buffer);
250 static struct buffer_slab buffer_slab = COMMIT_SLAB_INIT(1, buffer_slab);
252 void set_commit_buffer(struct commit *commit, void *buffer, unsigned long size)
254 struct commit_buffer *v = buffer_slab_at(&buffer_slab, commit);
259 const void *get_cached_commit_buffer(const struct commit *commit, unsigned long *sizep)
261 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
272 const void *get_commit_buffer(const struct commit *commit, unsigned long *sizep)
274 const void *ret = get_cached_commit_buffer(commit, sizep);
276 enum object_type type;
278 ret = read_object_file(&commit->object.oid, &type, &size);
280 die("cannot read commit object %s",
281 oid_to_hex(&commit->object.oid));
282 if (type != OBJ_COMMIT)
283 die("expected commit for %s, got %s",
284 oid_to_hex(&commit->object.oid), type_name(type));
291 void unuse_commit_buffer(const struct commit *commit, const void *buffer)
293 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
294 if (!(v && v->buffer == buffer))
295 free((void *)buffer);
298 void free_commit_buffer(struct commit *commit)
300 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
302 FREE_AND_NULL(v->buffer);
307 void release_commit_memory(struct commit *c)
311 free_commit_buffer(c);
312 free_commit_list(c->parents);
313 /* TODO: what about commit->util? */
315 c->object.parsed = 0;
318 const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
320 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
337 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
339 const char *tail = buffer;
340 const char *bufptr = buffer;
341 struct object_id parent;
342 struct commit_list **pptr;
343 struct commit_graft *graft;
344 const int tree_entry_len = GIT_SHA1_HEXSZ + 5;
345 const int parent_entry_len = GIT_SHA1_HEXSZ + 7;
347 if (item->object.parsed)
349 item->object.parsed = 1;
351 if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
352 bufptr[tree_entry_len] != '\n')
353 return error("bogus commit object %s", oid_to_hex(&item->object.oid));
354 if (get_sha1_hex(bufptr + 5, parent.hash) < 0)
355 return error("bad tree pointer in commit %s",
356 oid_to_hex(&item->object.oid));
357 item->tree = lookup_tree(&parent);
358 bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
359 pptr = &item->parents;
361 graft = lookup_commit_graft(the_repository, &item->object.oid);
362 while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
363 struct commit *new_parent;
365 if (tail <= bufptr + parent_entry_len + 1 ||
366 get_sha1_hex(bufptr + 7, parent.hash) ||
367 bufptr[parent_entry_len] != '\n')
368 return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
369 bufptr += parent_entry_len + 1;
371 * The clone is shallow if nr_parent < 0, and we must
372 * not traverse its real parents even when we unhide them.
374 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
376 new_parent = lookup_commit(&parent);
378 pptr = &commit_list_insert(new_parent, pptr)->next;
382 struct commit *new_parent;
383 for (i = 0; i < graft->nr_parent; i++) {
384 new_parent = lookup_commit(&graft->parent[i]);
387 pptr = &commit_list_insert(new_parent, pptr)->next;
390 item->date = parse_commit_date(bufptr, tail);
395 int parse_commit_gently(struct commit *item, int quiet_on_missing)
397 enum object_type type;
404 if (item->object.parsed)
406 buffer = read_object_file(&item->object.oid, &type, &size);
408 return quiet_on_missing ? -1 :
409 error("Could not read %s",
410 oid_to_hex(&item->object.oid));
411 if (type != OBJ_COMMIT) {
413 return error("Object %s not a commit",
414 oid_to_hex(&item->object.oid));
416 ret = parse_commit_buffer(item, buffer, size);
417 if (save_commit_buffer && !ret) {
418 set_commit_buffer(item, buffer, size);
425 void parse_commit_or_die(struct commit *item)
427 if (parse_commit(item))
428 die("unable to parse commit %s",
429 item ? oid_to_hex(&item->object.oid) : "(null)");
432 int find_commit_subject(const char *commit_buffer, const char **subject)
435 const char *p = commit_buffer;
437 while (*p && (*p != '\n' || p[1] != '\n'))
440 p = skip_blank_lines(p + 2);
441 eol = strchrnul(p, '\n');
450 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
452 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
453 new_list->item = item;
454 new_list->next = *list_p;
459 unsigned commit_list_count(const struct commit_list *l)
462 for (; l; l = l->next )
467 struct commit_list *copy_commit_list(struct commit_list *list)
469 struct commit_list *head = NULL;
470 struct commit_list **pp = &head;
472 pp = commit_list_append(list->item, pp);
478 void free_commit_list(struct commit_list *list)
484 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
486 struct commit_list **pp = list;
487 struct commit_list *p;
488 while ((p = *pp) != NULL) {
489 if (p->item->date < item->date) {
494 return commit_list_insert(item, pp);
497 static int commit_list_compare_by_date(const void *a, const void *b)
499 timestamp_t a_date = ((const struct commit_list *)a)->item->date;
500 timestamp_t b_date = ((const struct commit_list *)b)->item->date;
508 static void *commit_list_get_next(const void *a)
510 return ((const struct commit_list *)a)->next;
513 static void commit_list_set_next(void *a, void *next)
515 ((struct commit_list *)a)->next = next;
518 void commit_list_sort_by_date(struct commit_list **list)
520 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
521 commit_list_compare_by_date);
524 struct commit *pop_most_recent_commit(struct commit_list **list,
527 struct commit *ret = pop_commit(list);
528 struct commit_list *parents = ret->parents;
531 struct commit *commit = parents->item;
532 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
533 commit->object.flags |= mark;
534 commit_list_insert_by_date(commit, list);
536 parents = parents->next;
541 static void clear_commit_marks_1(struct commit_list **plist,
542 struct commit *commit, unsigned int mark)
545 struct commit_list *parents;
547 if (!(mark & commit->object.flags))
550 commit->object.flags &= ~mark;
552 parents = commit->parents;
556 while ((parents = parents->next))
557 commit_list_insert(parents->item, plist);
559 commit = commit->parents->item;
563 void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
565 struct commit_list *list = NULL;
568 clear_commit_marks_1(&list, *commit, mark);
572 clear_commit_marks_1(&list, pop_commit(&list), mark);
575 void clear_commit_marks(struct commit *commit, unsigned int mark)
577 clear_commit_marks_many(1, &commit, mark);
580 struct commit *pop_commit(struct commit_list **stack)
582 struct commit_list *top = *stack;
583 struct commit *item = top ? top->item : NULL;
593 * Topological sort support
596 /* count number of children that have not been emitted */
597 define_commit_slab(indegree_slab, int);
599 /* record author-date for each commit object */
600 define_commit_slab(author_date_slab, unsigned long);
602 static void record_author_date(struct author_date_slab *author_date,
603 struct commit *commit)
605 const char *buffer = get_commit_buffer(commit, NULL);
606 struct ident_split ident;
607 const char *ident_line;
612 ident_line = find_commit_header(buffer, "author", &ident_len);
614 goto fail_exit; /* no author line */
615 if (split_ident_line(&ident, ident_line, ident_len) ||
616 !ident.date_begin || !ident.date_end)
617 goto fail_exit; /* malformed "author" line */
619 date = parse_timestamp(ident.date_begin, &date_end, 10);
620 if (date_end != ident.date_end)
621 goto fail_exit; /* malformed date */
622 *(author_date_slab_at(author_date, commit)) = date;
625 unuse_commit_buffer(commit, buffer);
628 static int compare_commits_by_author_date(const void *a_, const void *b_,
631 const struct commit *a = a_, *b = b_;
632 struct author_date_slab *author_date = cb_data;
633 timestamp_t a_date = *(author_date_slab_at(author_date, a));
634 timestamp_t b_date = *(author_date_slab_at(author_date, b));
636 /* newer commits with larger date first */
639 else if (a_date > b_date)
644 int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
646 const struct commit *a = a_, *b = b_;
647 /* newer commits with larger date first */
648 if (a->date < b->date)
650 else if (a->date > b->date)
656 * Performs an in-place topological sort on the list supplied.
658 void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
660 struct commit_list *next, *orig = *list;
661 struct commit_list **pptr;
662 struct indegree_slab indegree;
663 struct prio_queue queue;
664 struct commit *commit;
665 struct author_date_slab author_date;
671 init_indegree_slab(&indegree);
672 memset(&queue, '\0', sizeof(queue));
674 switch (sort_order) {
675 default: /* REV_SORT_IN_GRAPH_ORDER */
676 queue.compare = NULL;
678 case REV_SORT_BY_COMMIT_DATE:
679 queue.compare = compare_commits_by_commit_date;
681 case REV_SORT_BY_AUTHOR_DATE:
682 init_author_date_slab(&author_date);
683 queue.compare = compare_commits_by_author_date;
684 queue.cb_data = &author_date;
688 /* Mark them and clear the indegree */
689 for (next = orig; next; next = next->next) {
690 struct commit *commit = next->item;
691 *(indegree_slab_at(&indegree, commit)) = 1;
692 /* also record the author dates, if needed */
693 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
694 record_author_date(&author_date, commit);
697 /* update the indegree */
698 for (next = orig; next; next = next->next) {
699 struct commit_list *parents = next->item->parents;
701 struct commit *parent = parents->item;
702 int *pi = indegree_slab_at(&indegree, parent);
706 parents = parents->next;
713 * tips are nodes not reachable from any other node in the list
715 * the tips serve as a starting set for the work queue.
717 for (next = orig; next; next = next->next) {
718 struct commit *commit = next->item;
720 if (*(indegree_slab_at(&indegree, commit)) == 1)
721 prio_queue_put(&queue, commit);
725 * This is unfortunate; the initial tips need to be shown
726 * in the order given from the revision traversal machinery.
728 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
729 prio_queue_reverse(&queue);
731 /* We no longer need the commit list */
732 free_commit_list(orig);
736 while ((commit = prio_queue_get(&queue)) != NULL) {
737 struct commit_list *parents;
739 for (parents = commit->parents; parents ; parents = parents->next) {
740 struct commit *parent = parents->item;
741 int *pi = indegree_slab_at(&indegree, parent);
747 * parents are only enqueued for emission
748 * when all their children have been emitted thereby
749 * guaranteeing topological order.
752 prio_queue_put(&queue, parent);
755 * all children of commit have already been
756 * emitted. we can emit it now.
758 *(indegree_slab_at(&indegree, commit)) = 0;
760 pptr = &commit_list_insert(commit, pptr)->next;
763 clear_indegree_slab(&indegree);
764 clear_prio_queue(&queue);
765 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
766 clear_author_date_slab(&author_date);
769 /* merge-base stuff */
771 /* Remember to update object flag allocation in object.h */
772 #define PARENT1 (1u<<16)
773 #define PARENT2 (1u<<17)
774 #define STALE (1u<<18)
775 #define RESULT (1u<<19)
777 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
779 static int queue_has_nonstale(struct prio_queue *queue)
782 for (i = 0; i < queue->nr; i++) {
783 struct commit *commit = queue->array[i].data;
784 if (!(commit->object.flags & STALE))
790 /* all input commits in one and twos[] must have been parsed! */
791 static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
793 struct prio_queue queue = { compare_commits_by_commit_date };
794 struct commit_list *result = NULL;
797 one->object.flags |= PARENT1;
799 commit_list_append(one, &result);
802 prio_queue_put(&queue, one);
804 for (i = 0; i < n; i++) {
805 twos[i]->object.flags |= PARENT2;
806 prio_queue_put(&queue, twos[i]);
809 while (queue_has_nonstale(&queue)) {
810 struct commit *commit = prio_queue_get(&queue);
811 struct commit_list *parents;
814 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
815 if (flags == (PARENT1 | PARENT2)) {
816 if (!(commit->object.flags & RESULT)) {
817 commit->object.flags |= RESULT;
818 commit_list_insert_by_date(commit, &result);
820 /* Mark parents of a found merge stale */
823 parents = commit->parents;
825 struct commit *p = parents->item;
826 parents = parents->next;
827 if ((p->object.flags & flags) == flags)
831 p->object.flags |= flags;
832 prio_queue_put(&queue, p);
836 clear_prio_queue(&queue);
840 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
842 struct commit_list *list = NULL;
843 struct commit_list *result = NULL;
846 for (i = 0; i < n; i++) {
849 * We do not mark this even with RESULT so we do not
850 * have to clean it up.
852 return commit_list_insert(one, &result);
855 if (parse_commit(one))
857 for (i = 0; i < n; i++) {
858 if (parse_commit(twos[i]))
862 list = paint_down_to_common(one, n, twos);
865 struct commit *commit = pop_commit(&list);
866 if (!(commit->object.flags & STALE))
867 commit_list_insert_by_date(commit, &result);
872 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
874 struct commit_list *i, *j, *k, *ret = NULL;
879 commit_list_insert(in->item, &ret);
881 for (i = in->next; i; i = i->next) {
882 struct commit_list *new_commits = NULL, *end = NULL;
884 for (j = ret; j; j = j->next) {
885 struct commit_list *bases;
886 bases = get_merge_bases(i->item, j->item);
891 for (k = bases; k; k = k->next)
899 static int remove_redundant(struct commit **array, int cnt)
902 * Some commit in the array may be an ancestor of
903 * another commit. Move such commit to the end of
904 * the array, and return the number of commits that
905 * are independent from each other.
907 struct commit **work;
908 unsigned char *redundant;
912 work = xcalloc(cnt, sizeof(*work));
913 redundant = xcalloc(cnt, 1);
914 ALLOC_ARRAY(filled_index, cnt - 1);
916 for (i = 0; i < cnt; i++)
917 parse_commit(array[i]);
918 for (i = 0; i < cnt; i++) {
919 struct commit_list *common;
923 for (j = filled = 0; j < cnt; j++) {
924 if (i == j || redundant[j])
926 filled_index[filled] = j;
927 work[filled++] = array[j];
929 common = paint_down_to_common(array[i], filled, work);
930 if (array[i]->object.flags & PARENT2)
932 for (j = 0; j < filled; j++)
933 if (work[j]->object.flags & PARENT1)
934 redundant[filled_index[j]] = 1;
935 clear_commit_marks(array[i], all_flags);
936 clear_commit_marks_many(filled, work, all_flags);
937 free_commit_list(common);
940 /* Now collect the result */
941 COPY_ARRAY(work, array, cnt);
942 for (i = filled = 0; i < cnt; i++)
944 array[filled++] = work[i];
945 for (j = filled, i = 0; i < cnt; i++)
947 array[j++] = work[i];
954 static struct commit_list *get_merge_bases_many_0(struct commit *one,
956 struct commit **twos,
959 struct commit_list *list;
960 struct commit **rslt;
961 struct commit_list *result;
964 result = merge_bases_many(one, n, twos);
965 for (i = 0; i < n; i++) {
969 if (!result || !result->next) {
971 clear_commit_marks(one, all_flags);
972 clear_commit_marks_many(n, twos, all_flags);
977 /* There are more than one */
978 cnt = commit_list_count(result);
979 rslt = xcalloc(cnt, sizeof(*rslt));
980 for (list = result, i = 0; list; list = list->next)
981 rslt[i++] = list->item;
982 free_commit_list(result);
984 clear_commit_marks(one, all_flags);
985 clear_commit_marks_many(n, twos, all_flags);
987 cnt = remove_redundant(rslt, cnt);
989 for (i = 0; i < cnt; i++)
990 commit_list_insert_by_date(rslt[i], &result);
995 struct commit_list *get_merge_bases_many(struct commit *one,
997 struct commit **twos)
999 return get_merge_bases_many_0(one, n, twos, 1);
1002 struct commit_list *get_merge_bases_many_dirty(struct commit *one,
1004 struct commit **twos)
1006 return get_merge_bases_many_0(one, n, twos, 0);
1009 struct commit_list *get_merge_bases(struct commit *one, struct commit *two)
1011 return get_merge_bases_many_0(one, 1, &two, 1);
1015 * Is "commit" a descendant of one of the elements on the "with_commit" list?
1017 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
1021 while (with_commit) {
1022 struct commit *other;
1024 other = with_commit->item;
1025 with_commit = with_commit->next;
1026 if (in_merge_bases(other, commit))
1033 * Is "commit" an ancestor of one of the "references"?
1035 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
1037 struct commit_list *bases;
1040 if (parse_commit(commit))
1042 for (i = 0; i < nr_reference; i++)
1043 if (parse_commit(reference[i]))
1046 bases = paint_down_to_common(commit, nr_reference, reference);
1047 if (commit->object.flags & PARENT2)
1049 clear_commit_marks(commit, all_flags);
1050 clear_commit_marks_many(nr_reference, reference, all_flags);
1051 free_commit_list(bases);
1056 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1058 int in_merge_bases(struct commit *commit, struct commit *reference)
1060 return in_merge_bases_many(commit, 1, &reference);
1063 struct commit_list *reduce_heads(struct commit_list *heads)
1065 struct commit_list *p;
1066 struct commit_list *result = NULL, **tail = &result;
1067 struct commit **array;
1074 for (p = heads; p; p = p->next)
1075 p->item->object.flags &= ~STALE;
1076 for (p = heads, num_head = 0; p; p = p->next) {
1077 if (p->item->object.flags & STALE)
1079 p->item->object.flags |= STALE;
1082 array = xcalloc(num_head, sizeof(*array));
1083 for (p = heads, i = 0; p; p = p->next) {
1084 if (p->item->object.flags & STALE) {
1085 array[i++] = p->item;
1086 p->item->object.flags &= ~STALE;
1089 num_head = remove_redundant(array, num_head);
1090 for (i = 0; i < num_head; i++)
1091 tail = &commit_list_insert(array[i], tail)->next;
1096 void reduce_heads_replace(struct commit_list **heads)
1098 struct commit_list *result = reduce_heads(*heads);
1099 free_commit_list(*heads);
1103 static const char gpg_sig_header[] = "gpgsig";
1104 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
1106 static int do_sign_commit(struct strbuf *buf, const char *keyid)
1108 struct strbuf sig = STRBUF_INIT;
1109 int inspos, copypos;
1112 /* find the end of the header */
1113 eoh = strstr(buf->buf, "\n\n");
1117 inspos = eoh - buf->buf + 1;
1119 if (!keyid || !*keyid)
1120 keyid = get_signing_key();
1121 if (sign_buffer(buf, &sig, keyid)) {
1122 strbuf_release(&sig);
1126 for (copypos = 0; sig.buf[copypos]; ) {
1127 const char *bol = sig.buf + copypos;
1128 const char *eol = strchrnul(bol, '\n');
1129 int len = (eol - bol) + !!*eol;
1132 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1133 inspos += gpg_sig_header_len;
1135 strbuf_insert(buf, inspos++, " ", 1);
1136 strbuf_insert(buf, inspos, bol, len);
1140 strbuf_release(&sig);
1144 int parse_signed_commit(const struct commit *commit,
1145 struct strbuf *payload, struct strbuf *signature)
1149 const char *buffer = get_commit_buffer(commit, &size);
1150 int in_signature, saw_signature = -1;
1151 const char *line, *tail;
1154 tail = buffer + size;
1157 while (line < tail) {
1158 const char *sig = NULL;
1159 const char *next = memchr(line, '\n', tail - line);
1161 next = next ? next + 1 : tail;
1162 if (in_signature && line[0] == ' ')
1164 else if (starts_with(line, gpg_sig_header) &&
1165 line[gpg_sig_header_len] == ' ')
1166 sig = line + gpg_sig_header_len + 1;
1168 strbuf_add(signature, sig, next - sig);
1173 /* dump the whole remainder of the buffer */
1175 strbuf_add(payload, line, next - line);
1180 unuse_commit_buffer(commit, buffer);
1181 return saw_signature;
1184 int remove_signature(struct strbuf *buf)
1186 const char *line = buf->buf;
1187 const char *tail = buf->buf + buf->len;
1188 int in_signature = 0;
1189 const char *sig_start = NULL;
1190 const char *sig_end = NULL;
1192 while (line < tail) {
1193 const char *next = memchr(line, '\n', tail - line);
1194 next = next ? next + 1 : tail;
1196 if (in_signature && line[0] == ' ')
1198 else if (starts_with(line, gpg_sig_header) &&
1199 line[gpg_sig_header_len] == ' ') {
1205 /* dump the whole remainder of the buffer */
1213 strbuf_remove(buf, sig_start - buf->buf, sig_end - sig_start);
1215 return sig_start != NULL;
1218 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1220 struct merge_remote_desc *desc;
1221 struct commit_extra_header *mergetag;
1223 unsigned long size, len;
1224 enum object_type type;
1226 desc = merge_remote_util(parent);
1227 if (!desc || !desc->obj)
1229 buf = read_object_file(&desc->obj->oid, &type, &size);
1230 if (!buf || type != OBJ_TAG)
1232 len = parse_signature(buf, size);
1236 * We could verify this signature and either omit the tag when
1237 * it does not validate, but the integrator may not have the
1238 * public key of the signer of the tag he is merging, while a
1239 * later auditor may have it while auditing, so let's not run
1240 * verify-signed-buffer here for now...
1242 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1243 * warn("warning: signed tag unverified.");
1245 mergetag = xcalloc(1, sizeof(*mergetag));
1246 mergetag->key = xstrdup("mergetag");
1247 mergetag->value = buf;
1248 mergetag->len = size;
1251 *tail = &mergetag->next;
1258 int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
1260 struct strbuf payload = STRBUF_INIT;
1261 struct strbuf signature = STRBUF_INIT;
1266 if (parse_signed_commit(commit, &payload, &signature) <= 0)
1268 ret = check_signature(payload.buf, payload.len, signature.buf,
1269 signature.len, sigc);
1272 strbuf_release(&payload);
1273 strbuf_release(&signature);
1280 void append_merge_tag_headers(struct commit_list *parents,
1281 struct commit_extra_header ***tail)
1284 struct commit *parent = parents->item;
1285 handle_signed_tag(parent, tail);
1286 parents = parents->next;
1290 static void add_extra_header(struct strbuf *buffer,
1291 struct commit_extra_header *extra)
1293 strbuf_addstr(buffer, extra->key);
1295 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1297 strbuf_addch(buffer, '\n');
1300 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1301 const char **exclude)
1303 struct commit_extra_header *extra = NULL;
1305 const char *buffer = get_commit_buffer(commit, &size);
1306 extra = read_commit_extra_header_lines(buffer, size, exclude);
1307 unuse_commit_buffer(commit, buffer);
1311 void for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
1313 struct commit_extra_header *extra, *to_free;
1315 to_free = read_commit_extra_headers(commit, NULL);
1316 for (extra = to_free; extra; extra = extra->next) {
1317 if (strcmp(extra->key, "mergetag"))
1318 continue; /* not a merge tag */
1319 fn(commit, extra, data);
1321 free_commit_extra_headers(to_free);
1324 static inline int standard_header_field(const char *field, size_t len)
1326 return ((len == 4 && !memcmp(field, "tree", 4)) ||
1327 (len == 6 && !memcmp(field, "parent", 6)) ||
1328 (len == 6 && !memcmp(field, "author", 6)) ||
1329 (len == 9 && !memcmp(field, "committer", 9)) ||
1330 (len == 8 && !memcmp(field, "encoding", 8)));
1333 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1339 size_t xlen = strlen(*exclude);
1340 if (len == xlen && !memcmp(field, *exclude, xlen))
1347 static struct commit_extra_header *read_commit_extra_header_lines(
1348 const char *buffer, size_t size,
1349 const char **exclude)
1351 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1352 const char *line, *next, *eof, *eob;
1353 struct strbuf buf = STRBUF_INIT;
1355 for (line = buffer, eob = line + size;
1356 line < eob && *line != '\n';
1358 next = memchr(line, '\n', eob - line);
1359 next = next ? next + 1 : eob;
1363 strbuf_add(&buf, line + 1, next - (line + 1));
1367 it->value = strbuf_detach(&buf, &it->len);
1371 eof = memchr(line, ' ', next - line);
1374 else if (standard_header_field(line, eof - line) ||
1375 excluded_header_field(line, eof - line, exclude))
1378 it = xcalloc(1, sizeof(*it));
1379 it->key = xmemdupz(line, eof-line);
1383 strbuf_add(&buf, eof + 1, next - (eof + 1));
1386 it->value = strbuf_detach(&buf, &it->len);
1390 void free_commit_extra_headers(struct commit_extra_header *extra)
1393 struct commit_extra_header *next = extra->next;
1401 int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1402 struct commit_list *parents, struct object_id *ret,
1403 const char *author, const char *sign_commit)
1405 struct commit_extra_header *extra = NULL, **tail = &extra;
1408 append_merge_tag_headers(parents, &tail);
1409 result = commit_tree_extended(msg, msg_len, tree, parents, ret,
1410 author, sign_commit, extra);
1411 free_commit_extra_headers(extra);
1415 static int find_invalid_utf8(const char *buf, int len)
1418 static const unsigned int max_codepoint[] = {
1419 0x7f, 0x7ff, 0xffff, 0x10ffff
1423 unsigned char c = *buf++;
1424 int bytes, bad_offset;
1425 unsigned int codepoint;
1426 unsigned int min_val, max_val;
1431 /* Simple US-ASCII? No worries. */
1435 bad_offset = offset-1;
1438 * Count how many more high bits set: that's how
1439 * many more bytes this sequence should have.
1448 * Must be between 1 and 3 more bytes. Longer sequences result in
1449 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1451 if (bytes < 1 || 3 < bytes)
1454 /* Do we *have* that many bytes? */
1459 * Place the encoded bits at the bottom of the value and compute the
1462 codepoint = (c & 0x7f) >> bytes;
1463 min_val = max_codepoint[bytes-1] + 1;
1464 max_val = max_codepoint[bytes];
1469 /* And verify that they are good continuation bytes */
1472 codepoint |= *buf & 0x3f;
1473 if ((*buf++ & 0xc0) != 0x80)
1477 /* Reject codepoints that are out of range for the sequence length. */
1478 if (codepoint < min_val || codepoint > max_val)
1480 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1481 if ((codepoint & 0x1ff800) == 0xd800)
1483 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1484 if ((codepoint & 0xfffe) == 0xfffe)
1486 /* So are anything in the range U+FDD0..U+FDEF. */
1487 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
1494 * This verifies that the buffer is in proper utf8 format.
1496 * If it isn't, it assumes any non-utf8 characters are Latin1,
1497 * and does the conversion.
1499 static int verify_utf8(struct strbuf *buf)
1507 unsigned char replace[2];
1509 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1515 strbuf_remove(buf, pos, 1);
1517 /* We know 'c' must be in the range 128-255 */
1518 replace[0] = 0xc0 + (c >> 6);
1519 replace[1] = 0x80 + (c & 0x3f);
1520 strbuf_insert(buf, pos, replace, 2);
1525 static const char commit_utf8_warn[] =
1526 N_("Warning: commit message did not conform to UTF-8.\n"
1527 "You may want to amend it after fixing the message, or set the config\n"
1528 "variable i18n.commitencoding to the encoding your project uses.\n");
1530 int commit_tree_extended(const char *msg, size_t msg_len,
1531 const struct object_id *tree,
1532 struct commit_list *parents, struct object_id *ret,
1533 const char *author, const char *sign_commit,
1534 struct commit_extra_header *extra)
1537 int encoding_is_utf8;
1538 struct strbuf buffer;
1540 assert_oid_type(tree, OBJ_TREE);
1542 if (memchr(msg, '\0', msg_len))
1543 return error("a NUL byte in commit log message not allowed.");
1545 /* Not having i18n.commitencoding is the same as having utf-8 */
1546 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1548 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1549 strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree));
1552 * NOTE! This ordering means that the same exact tree merged with a
1553 * different order of parents will be a _different_ changeset even
1554 * if everything else stays the same.
1557 struct commit *parent = pop_commit(&parents);
1558 strbuf_addf(&buffer, "parent %s\n",
1559 oid_to_hex(&parent->object.oid));
1562 /* Person/date information */
1564 author = git_author_info(IDENT_STRICT);
1565 strbuf_addf(&buffer, "author %s\n", author);
1566 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1567 if (!encoding_is_utf8)
1568 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1571 add_extra_header(&buffer, extra);
1572 extra = extra->next;
1574 strbuf_addch(&buffer, '\n');
1576 /* And add the comment */
1577 strbuf_add(&buffer, msg, msg_len);
1579 /* And check the encoding */
1580 if (encoding_is_utf8 && !verify_utf8(&buffer))
1581 fprintf(stderr, _(commit_utf8_warn));
1583 if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
1588 result = write_object_file(buffer.buf, buffer.len, commit_type, ret);
1590 strbuf_release(&buffer);
1594 void set_merge_remote_desc(struct commit *commit,
1595 const char *name, struct object *obj)
1597 struct merge_remote_desc *desc;
1598 FLEX_ALLOC_STR(desc, name, name);
1600 commit->util = desc;
1603 struct commit *get_merge_parent(const char *name)
1606 struct commit *commit;
1607 struct object_id oid;
1608 if (get_oid(name, &oid))
1610 obj = parse_object(&oid);
1611 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1612 if (commit && !commit->util)
1613 set_merge_remote_desc(commit, name, obj);
1618 * Append a commit to the end of the commit_list.
1620 * next starts by pointing to the variable that holds the head of an
1621 * empty commit_list, and is updated to point to the "next" field of
1622 * the last item on the list as new commits are appended.
1626 * struct commit_list *list;
1627 * struct commit_list **next = &list;
1629 * next = commit_list_append(c1, next);
1630 * next = commit_list_append(c2, next);
1631 * assert(commit_list_count(list) == 2);
1634 struct commit_list **commit_list_append(struct commit *commit,
1635 struct commit_list **next)
1637 struct commit_list *new_commit = xmalloc(sizeof(struct commit_list));
1638 new_commit->item = commit;
1640 new_commit->next = NULL;
1641 return &new_commit->next;
1644 const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1646 int key_len = strlen(key);
1647 const char *line = msg;
1650 const char *eol = strchrnul(line, '\n');
1655 if (eol - line > key_len &&
1656 !strncmp(line, key, key_len) &&
1657 line[key_len] == ' ') {
1658 *out_len = eol - line - key_len - 1;
1659 return line + key_len + 1;
1661 line = *eol ? eol + 1 : NULL;
1667 * Inspect the given string and determine the true "end" of the log message, in
1668 * order to find where to put a new Signed-off-by: line. Ignored are
1669 * trailing comment lines and blank lines. To support "git commit -s
1670 * --amend" on an existing commit, we also ignore "Conflicts:". To
1671 * support "git commit -v", we truncate at cut lines.
1673 * Returns the number of bytes from the tail to ignore, to be fed as
1674 * the second parameter to append_signoff().
1676 int ignore_non_trailer(const char *buf, size_t len)
1680 int in_old_conflicts_block = 0;
1681 size_t cutoff = wt_status_locate_end(buf, len);
1683 while (bol < cutoff) {
1684 const char *next_line = memchr(buf + bol, '\n', len - bol);
1687 next_line = buf + len;
1691 if (buf[bol] == comment_line_char || buf[bol] == '\n') {
1692 /* is this the first of the run of comments? */
1695 /* otherwise, it is just continuing */
1696 } else if (starts_with(buf + bol, "Conflicts:\n")) {
1697 in_old_conflicts_block = 1;
1700 } else if (in_old_conflicts_block && buf[bol] == '\t') {
1701 ; /* a pathname in the conflicts block */
1703 /* the previous was not trailing comment */
1705 in_old_conflicts_block = 0;
1707 bol = next_line - buf;
1709 return boc ? len - boc : len - cutoff;