4 #include "object-store.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"
18 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
20 int save_commit_buffer = 1;
22 const char *commit_type = "commit";
24 struct commit *lookup_commit_reference_gently(const struct object_id *oid,
27 struct object *obj = deref_tag(parse_object(oid), NULL, 0);
31 return object_as_type(obj, OBJ_COMMIT, quiet);
34 struct commit *lookup_commit_reference(const struct object_id *oid)
36 return lookup_commit_reference_gently(oid, 0);
39 struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name)
41 struct commit *c = lookup_commit_reference(oid);
43 die(_("could not parse %s"), ref_name);
44 if (oidcmp(oid, &c->object.oid)) {
45 warning(_("%s %s is not a commit!"),
46 ref_name, oid_to_hex(oid));
51 struct commit *lookup_commit(const struct object_id *oid)
53 struct object *obj = lookup_object(oid->hash);
55 return create_object(the_repository, oid->hash,
56 alloc_commit_node(the_repository));
57 return object_as_type(obj, OBJ_COMMIT, 0);
60 struct commit *lookup_commit_reference_by_name(const char *name)
63 struct commit *commit;
65 if (get_oid_committish(name, &oid))
67 commit = lookup_commit_reference(&oid);
68 if (parse_commit(commit))
73 static timestamp_t parse_commit_date(const char *buf, const char *tail)
79 if (memcmp(buf, "author", 6))
81 while (buf < tail && *buf++ != '\n')
85 if (memcmp(buf, "committer", 9))
87 while (buf < tail && *buf++ != '>')
92 while (buf < tail && *buf++ != '\n')
96 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
97 return parse_timestamp(dateptr, NULL, 10);
100 static struct commit_graft **commit_graft;
101 static int commit_graft_alloc, commit_graft_nr;
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(const unsigned char *sha1)
111 return sha1_pos(sha1, commit_graft, commit_graft_nr,
112 commit_graft_sha1_access);
115 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
117 int pos = commit_graft_pos(graft->oid.hash);
123 free(commit_graft[pos]);
124 commit_graft[pos] = graft;
129 ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc);
131 if (pos < commit_graft_nr)
132 MOVE_ARRAY(commit_graft + pos + 1, commit_graft + pos,
133 commit_graft_nr - pos - 1);
134 commit_graft[pos] = graft;
138 struct commit_graft *read_graft_line(struct strbuf *line)
140 /* The format is just "Commit Parent1 Parent2 ...\n" */
142 const char *tail = NULL;
143 struct commit_graft *graft = NULL;
144 struct object_id dummy_oid, *oid;
147 if (!line->len || line->buf[0] == '#')
150 * phase 0 verifies line, counts hashes in line and allocates graft
151 * phase 1 fills graft
153 for (phase = 0; phase < 2; phase++) {
154 oid = graft ? &graft->oid : &dummy_oid;
155 if (parse_oid_hex(line->buf, oid, &tail))
157 for (i = 0; *tail != '\0'; i++) {
158 oid = graft ? &graft->parent[i] : &dummy_oid;
159 if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
163 graft = xmalloc(st_add(sizeof(*graft),
164 st_mult(sizeof(struct object_id), i)));
165 graft->nr_parent = i;
171 error("bad graft data: %s", line->buf);
176 static int read_graft_file(const char *graft_file)
178 FILE *fp = fopen_or_warn(graft_file, "r");
179 struct strbuf buf = STRBUF_INIT;
182 while (!strbuf_getwholeline(&buf, fp, '\n')) {
183 /* The format is just "Commit Parent1 Parent2 ...\n" */
184 struct commit_graft *graft = read_graft_line(&buf);
187 if (register_commit_graft(graft, 1))
188 error("duplicate graft data: %s", buf.buf);
191 strbuf_release(&buf);
195 static void prepare_commit_graft(void)
197 static int commit_graft_prepared;
200 if (commit_graft_prepared)
202 graft_file = get_graft_file();
203 read_graft_file(graft_file);
204 /* make sure shallows are read */
205 is_repository_shallow();
206 commit_graft_prepared = 1;
209 struct commit_graft *lookup_commit_graft(const struct object_id *oid)
212 prepare_commit_graft();
213 pos = commit_graft_pos(oid->hash);
216 return commit_graft[pos];
219 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
222 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
223 ret = fn(commit_graft[i], cb_data);
227 int unregister_shallow(const struct object_id *oid)
229 int pos = commit_graft_pos(oid->hash);
232 if (pos + 1 < commit_graft_nr)
233 MOVE_ARRAY(commit_graft + pos, commit_graft + pos + 1,
234 commit_graft_nr - pos - 1);
239 struct commit_buffer {
243 define_commit_slab(buffer_slab, struct commit_buffer);
244 static struct buffer_slab buffer_slab = COMMIT_SLAB_INIT(1, buffer_slab);
246 void set_commit_buffer(struct commit *commit, void *buffer, unsigned long size)
248 struct commit_buffer *v = buffer_slab_at(&buffer_slab, commit);
253 const void *get_cached_commit_buffer(const struct commit *commit, unsigned long *sizep)
255 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
266 const void *get_commit_buffer(const struct commit *commit, unsigned long *sizep)
268 const void *ret = get_cached_commit_buffer(commit, sizep);
270 enum object_type type;
272 ret = read_object_file(&commit->object.oid, &type, &size);
274 die("cannot read commit object %s",
275 oid_to_hex(&commit->object.oid));
276 if (type != OBJ_COMMIT)
277 die("expected commit for %s, got %s",
278 oid_to_hex(&commit->object.oid), type_name(type));
285 void unuse_commit_buffer(const struct commit *commit, const void *buffer)
287 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
288 if (!(v && v->buffer == buffer))
289 free((void *)buffer);
292 void free_commit_buffer(struct commit *commit)
294 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
296 FREE_AND_NULL(v->buffer);
301 void release_commit_memory(struct commit *c)
305 free_commit_buffer(c);
306 free_commit_list(c->parents);
307 /* TODO: what about commit->util? */
309 c->object.parsed = 0;
312 const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
314 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
331 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
333 const char *tail = buffer;
334 const char *bufptr = buffer;
335 struct object_id parent;
336 struct commit_list **pptr;
337 struct commit_graft *graft;
338 const int tree_entry_len = GIT_SHA1_HEXSZ + 5;
339 const int parent_entry_len = GIT_SHA1_HEXSZ + 7;
341 if (item->object.parsed)
343 item->object.parsed = 1;
345 if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
346 bufptr[tree_entry_len] != '\n')
347 return error("bogus commit object %s", oid_to_hex(&item->object.oid));
348 if (get_sha1_hex(bufptr + 5, parent.hash) < 0)
349 return error("bad tree pointer in commit %s",
350 oid_to_hex(&item->object.oid));
351 item->tree = lookup_tree(&parent);
352 bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
353 pptr = &item->parents;
355 graft = lookup_commit_graft(&item->object.oid);
356 while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
357 struct commit *new_parent;
359 if (tail <= bufptr + parent_entry_len + 1 ||
360 get_sha1_hex(bufptr + 7, parent.hash) ||
361 bufptr[parent_entry_len] != '\n')
362 return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
363 bufptr += parent_entry_len + 1;
365 * The clone is shallow if nr_parent < 0, and we must
366 * not traverse its real parents even when we unhide them.
368 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
370 new_parent = lookup_commit(&parent);
372 pptr = &commit_list_insert(new_parent, pptr)->next;
376 struct commit *new_parent;
377 for (i = 0; i < graft->nr_parent; i++) {
378 new_parent = lookup_commit(&graft->parent[i]);
381 pptr = &commit_list_insert(new_parent, pptr)->next;
384 item->date = parse_commit_date(bufptr, tail);
389 int parse_commit_gently(struct commit *item, int quiet_on_missing)
391 enum object_type type;
398 if (item->object.parsed)
400 buffer = read_object_file(&item->object.oid, &type, &size);
402 return quiet_on_missing ? -1 :
403 error("Could not read %s",
404 oid_to_hex(&item->object.oid));
405 if (type != OBJ_COMMIT) {
407 return error("Object %s not a commit",
408 oid_to_hex(&item->object.oid));
410 ret = parse_commit_buffer(item, buffer, size);
411 if (save_commit_buffer && !ret) {
412 set_commit_buffer(item, buffer, size);
419 void parse_commit_or_die(struct commit *item)
421 if (parse_commit(item))
422 die("unable to parse commit %s",
423 item ? oid_to_hex(&item->object.oid) : "(null)");
426 int find_commit_subject(const char *commit_buffer, const char **subject)
429 const char *p = commit_buffer;
431 while (*p && (*p != '\n' || p[1] != '\n'))
434 p = skip_blank_lines(p + 2);
435 eol = strchrnul(p, '\n');
444 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
446 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
447 new_list->item = item;
448 new_list->next = *list_p;
453 unsigned commit_list_count(const struct commit_list *l)
456 for (; l; l = l->next )
461 struct commit_list *copy_commit_list(struct commit_list *list)
463 struct commit_list *head = NULL;
464 struct commit_list **pp = &head;
466 pp = commit_list_append(list->item, pp);
472 void free_commit_list(struct commit_list *list)
478 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
480 struct commit_list **pp = list;
481 struct commit_list *p;
482 while ((p = *pp) != NULL) {
483 if (p->item->date < item->date) {
488 return commit_list_insert(item, pp);
491 static int commit_list_compare_by_date(const void *a, const void *b)
493 timestamp_t a_date = ((const struct commit_list *)a)->item->date;
494 timestamp_t b_date = ((const struct commit_list *)b)->item->date;
502 static void *commit_list_get_next(const void *a)
504 return ((const struct commit_list *)a)->next;
507 static void commit_list_set_next(void *a, void *next)
509 ((struct commit_list *)a)->next = next;
512 void commit_list_sort_by_date(struct commit_list **list)
514 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
515 commit_list_compare_by_date);
518 struct commit *pop_most_recent_commit(struct commit_list **list,
521 struct commit *ret = pop_commit(list);
522 struct commit_list *parents = ret->parents;
525 struct commit *commit = parents->item;
526 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
527 commit->object.flags |= mark;
528 commit_list_insert_by_date(commit, list);
530 parents = parents->next;
535 static void clear_commit_marks_1(struct commit_list **plist,
536 struct commit *commit, unsigned int mark)
539 struct commit_list *parents;
541 if (!(mark & commit->object.flags))
544 commit->object.flags &= ~mark;
546 parents = commit->parents;
550 while ((parents = parents->next))
551 commit_list_insert(parents->item, plist);
553 commit = commit->parents->item;
557 void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
559 struct commit_list *list = NULL;
562 clear_commit_marks_1(&list, *commit, mark);
566 clear_commit_marks_1(&list, pop_commit(&list), mark);
569 void clear_commit_marks(struct commit *commit, unsigned int mark)
571 clear_commit_marks_many(1, &commit, mark);
574 struct commit *pop_commit(struct commit_list **stack)
576 struct commit_list *top = *stack;
577 struct commit *item = top ? top->item : NULL;
587 * Topological sort support
590 /* count number of children that have not been emitted */
591 define_commit_slab(indegree_slab, int);
593 /* record author-date for each commit object */
594 define_commit_slab(author_date_slab, unsigned long);
596 static void record_author_date(struct author_date_slab *author_date,
597 struct commit *commit)
599 const char *buffer = get_commit_buffer(commit, NULL);
600 struct ident_split ident;
601 const char *ident_line;
606 ident_line = find_commit_header(buffer, "author", &ident_len);
608 goto fail_exit; /* no author line */
609 if (split_ident_line(&ident, ident_line, ident_len) ||
610 !ident.date_begin || !ident.date_end)
611 goto fail_exit; /* malformed "author" line */
613 date = parse_timestamp(ident.date_begin, &date_end, 10);
614 if (date_end != ident.date_end)
615 goto fail_exit; /* malformed date */
616 *(author_date_slab_at(author_date, commit)) = date;
619 unuse_commit_buffer(commit, buffer);
622 static int compare_commits_by_author_date(const void *a_, const void *b_,
625 const struct commit *a = a_, *b = b_;
626 struct author_date_slab *author_date = cb_data;
627 timestamp_t a_date = *(author_date_slab_at(author_date, a));
628 timestamp_t b_date = *(author_date_slab_at(author_date, b));
630 /* newer commits with larger date first */
633 else if (a_date > b_date)
638 int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
640 const struct commit *a = a_, *b = b_;
641 /* newer commits with larger date first */
642 if (a->date < b->date)
644 else if (a->date > b->date)
650 * Performs an in-place topological sort on the list supplied.
652 void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
654 struct commit_list *next, *orig = *list;
655 struct commit_list **pptr;
656 struct indegree_slab indegree;
657 struct prio_queue queue;
658 struct commit *commit;
659 struct author_date_slab author_date;
665 init_indegree_slab(&indegree);
666 memset(&queue, '\0', sizeof(queue));
668 switch (sort_order) {
669 default: /* REV_SORT_IN_GRAPH_ORDER */
670 queue.compare = NULL;
672 case REV_SORT_BY_COMMIT_DATE:
673 queue.compare = compare_commits_by_commit_date;
675 case REV_SORT_BY_AUTHOR_DATE:
676 init_author_date_slab(&author_date);
677 queue.compare = compare_commits_by_author_date;
678 queue.cb_data = &author_date;
682 /* Mark them and clear the indegree */
683 for (next = orig; next; next = next->next) {
684 struct commit *commit = next->item;
685 *(indegree_slab_at(&indegree, commit)) = 1;
686 /* also record the author dates, if needed */
687 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
688 record_author_date(&author_date, commit);
691 /* update the indegree */
692 for (next = orig; next; next = next->next) {
693 struct commit_list *parents = next->item->parents;
695 struct commit *parent = parents->item;
696 int *pi = indegree_slab_at(&indegree, parent);
700 parents = parents->next;
707 * tips are nodes not reachable from any other node in the list
709 * the tips serve as a starting set for the work queue.
711 for (next = orig; next; next = next->next) {
712 struct commit *commit = next->item;
714 if (*(indegree_slab_at(&indegree, commit)) == 1)
715 prio_queue_put(&queue, commit);
719 * This is unfortunate; the initial tips need to be shown
720 * in the order given from the revision traversal machinery.
722 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
723 prio_queue_reverse(&queue);
725 /* We no longer need the commit list */
726 free_commit_list(orig);
730 while ((commit = prio_queue_get(&queue)) != NULL) {
731 struct commit_list *parents;
733 for (parents = commit->parents; parents ; parents = parents->next) {
734 struct commit *parent = parents->item;
735 int *pi = indegree_slab_at(&indegree, parent);
741 * parents are only enqueued for emission
742 * when all their children have been emitted thereby
743 * guaranteeing topological order.
746 prio_queue_put(&queue, parent);
749 * all children of commit have already been
750 * emitted. we can emit it now.
752 *(indegree_slab_at(&indegree, commit)) = 0;
754 pptr = &commit_list_insert(commit, pptr)->next;
757 clear_indegree_slab(&indegree);
758 clear_prio_queue(&queue);
759 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
760 clear_author_date_slab(&author_date);
763 /* merge-base stuff */
765 /* Remember to update object flag allocation in object.h */
766 #define PARENT1 (1u<<16)
767 #define PARENT2 (1u<<17)
768 #define STALE (1u<<18)
769 #define RESULT (1u<<19)
771 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
773 static int queue_has_nonstale(struct prio_queue *queue)
776 for (i = 0; i < queue->nr; i++) {
777 struct commit *commit = queue->array[i].data;
778 if (!(commit->object.flags & STALE))
784 /* all input commits in one and twos[] must have been parsed! */
785 static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
787 struct prio_queue queue = { compare_commits_by_commit_date };
788 struct commit_list *result = NULL;
791 one->object.flags |= PARENT1;
793 commit_list_append(one, &result);
796 prio_queue_put(&queue, one);
798 for (i = 0; i < n; i++) {
799 twos[i]->object.flags |= PARENT2;
800 prio_queue_put(&queue, twos[i]);
803 while (queue_has_nonstale(&queue)) {
804 struct commit *commit = prio_queue_get(&queue);
805 struct commit_list *parents;
808 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
809 if (flags == (PARENT1 | PARENT2)) {
810 if (!(commit->object.flags & RESULT)) {
811 commit->object.flags |= RESULT;
812 commit_list_insert_by_date(commit, &result);
814 /* Mark parents of a found merge stale */
817 parents = commit->parents;
819 struct commit *p = parents->item;
820 parents = parents->next;
821 if ((p->object.flags & flags) == flags)
825 p->object.flags |= flags;
826 prio_queue_put(&queue, p);
830 clear_prio_queue(&queue);
834 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
836 struct commit_list *list = NULL;
837 struct commit_list *result = NULL;
840 for (i = 0; i < n; i++) {
843 * We do not mark this even with RESULT so we do not
844 * have to clean it up.
846 return commit_list_insert(one, &result);
849 if (parse_commit(one))
851 for (i = 0; i < n; i++) {
852 if (parse_commit(twos[i]))
856 list = paint_down_to_common(one, n, twos);
859 struct commit *commit = pop_commit(&list);
860 if (!(commit->object.flags & STALE))
861 commit_list_insert_by_date(commit, &result);
866 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
868 struct commit_list *i, *j, *k, *ret = NULL;
873 commit_list_insert(in->item, &ret);
875 for (i = in->next; i; i = i->next) {
876 struct commit_list *new_commits = NULL, *end = NULL;
878 for (j = ret; j; j = j->next) {
879 struct commit_list *bases;
880 bases = get_merge_bases(i->item, j->item);
885 for (k = bases; k; k = k->next)
893 static int remove_redundant(struct commit **array, int cnt)
896 * Some commit in the array may be an ancestor of
897 * another commit. Move such commit to the end of
898 * the array, and return the number of commits that
899 * are independent from each other.
901 struct commit **work;
902 unsigned char *redundant;
906 work = xcalloc(cnt, sizeof(*work));
907 redundant = xcalloc(cnt, 1);
908 ALLOC_ARRAY(filled_index, cnt - 1);
910 for (i = 0; i < cnt; i++)
911 parse_commit(array[i]);
912 for (i = 0; i < cnt; i++) {
913 struct commit_list *common;
917 for (j = filled = 0; j < cnt; j++) {
918 if (i == j || redundant[j])
920 filled_index[filled] = j;
921 work[filled++] = array[j];
923 common = paint_down_to_common(array[i], filled, work);
924 if (array[i]->object.flags & PARENT2)
926 for (j = 0; j < filled; j++)
927 if (work[j]->object.flags & PARENT1)
928 redundant[filled_index[j]] = 1;
929 clear_commit_marks(array[i], all_flags);
930 clear_commit_marks_many(filled, work, all_flags);
931 free_commit_list(common);
934 /* Now collect the result */
935 COPY_ARRAY(work, array, cnt);
936 for (i = filled = 0; i < cnt; i++)
938 array[filled++] = work[i];
939 for (j = filled, i = 0; i < cnt; i++)
941 array[j++] = work[i];
948 static struct commit_list *get_merge_bases_many_0(struct commit *one,
950 struct commit **twos,
953 struct commit_list *list;
954 struct commit **rslt;
955 struct commit_list *result;
958 result = merge_bases_many(one, n, twos);
959 for (i = 0; i < n; i++) {
963 if (!result || !result->next) {
965 clear_commit_marks(one, all_flags);
966 clear_commit_marks_many(n, twos, all_flags);
971 /* There are more than one */
972 cnt = commit_list_count(result);
973 rslt = xcalloc(cnt, sizeof(*rslt));
974 for (list = result, i = 0; list; list = list->next)
975 rslt[i++] = list->item;
976 free_commit_list(result);
978 clear_commit_marks(one, all_flags);
979 clear_commit_marks_many(n, twos, all_flags);
981 cnt = remove_redundant(rslt, cnt);
983 for (i = 0; i < cnt; i++)
984 commit_list_insert_by_date(rslt[i], &result);
989 struct commit_list *get_merge_bases_many(struct commit *one,
991 struct commit **twos)
993 return get_merge_bases_many_0(one, n, twos, 1);
996 struct commit_list *get_merge_bases_many_dirty(struct commit *one,
998 struct commit **twos)
1000 return get_merge_bases_many_0(one, n, twos, 0);
1003 struct commit_list *get_merge_bases(struct commit *one, struct commit *two)
1005 return get_merge_bases_many_0(one, 1, &two, 1);
1009 * Is "commit" a descendant of one of the elements on the "with_commit" list?
1011 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
1015 while (with_commit) {
1016 struct commit *other;
1018 other = with_commit->item;
1019 with_commit = with_commit->next;
1020 if (in_merge_bases(other, commit))
1027 * Is "commit" an ancestor of one of the "references"?
1029 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
1031 struct commit_list *bases;
1034 if (parse_commit(commit))
1036 for (i = 0; i < nr_reference; i++)
1037 if (parse_commit(reference[i]))
1040 bases = paint_down_to_common(commit, nr_reference, reference);
1041 if (commit->object.flags & PARENT2)
1043 clear_commit_marks(commit, all_flags);
1044 clear_commit_marks_many(nr_reference, reference, all_flags);
1045 free_commit_list(bases);
1050 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1052 int in_merge_bases(struct commit *commit, struct commit *reference)
1054 return in_merge_bases_many(commit, 1, &reference);
1057 struct commit_list *reduce_heads(struct commit_list *heads)
1059 struct commit_list *p;
1060 struct commit_list *result = NULL, **tail = &result;
1061 struct commit **array;
1068 for (p = heads; p; p = p->next)
1069 p->item->object.flags &= ~STALE;
1070 for (p = heads, num_head = 0; p; p = p->next) {
1071 if (p->item->object.flags & STALE)
1073 p->item->object.flags |= STALE;
1076 array = xcalloc(num_head, sizeof(*array));
1077 for (p = heads, i = 0; p; p = p->next) {
1078 if (p->item->object.flags & STALE) {
1079 array[i++] = p->item;
1080 p->item->object.flags &= ~STALE;
1083 num_head = remove_redundant(array, num_head);
1084 for (i = 0; i < num_head; i++)
1085 tail = &commit_list_insert(array[i], tail)->next;
1090 void reduce_heads_replace(struct commit_list **heads)
1092 struct commit_list *result = reduce_heads(*heads);
1093 free_commit_list(*heads);
1097 static const char gpg_sig_header[] = "gpgsig";
1098 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
1100 static int do_sign_commit(struct strbuf *buf, const char *keyid)
1102 struct strbuf sig = STRBUF_INIT;
1103 int inspos, copypos;
1106 /* find the end of the header */
1107 eoh = strstr(buf->buf, "\n\n");
1111 inspos = eoh - buf->buf + 1;
1113 if (!keyid || !*keyid)
1114 keyid = get_signing_key();
1115 if (sign_buffer(buf, &sig, keyid)) {
1116 strbuf_release(&sig);
1120 for (copypos = 0; sig.buf[copypos]; ) {
1121 const char *bol = sig.buf + copypos;
1122 const char *eol = strchrnul(bol, '\n');
1123 int len = (eol - bol) + !!*eol;
1126 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1127 inspos += gpg_sig_header_len;
1129 strbuf_insert(buf, inspos++, " ", 1);
1130 strbuf_insert(buf, inspos, bol, len);
1134 strbuf_release(&sig);
1138 int parse_signed_commit(const struct commit *commit,
1139 struct strbuf *payload, struct strbuf *signature)
1143 const char *buffer = get_commit_buffer(commit, &size);
1144 int in_signature, saw_signature = -1;
1145 const char *line, *tail;
1148 tail = buffer + size;
1151 while (line < tail) {
1152 const char *sig = NULL;
1153 const char *next = memchr(line, '\n', tail - line);
1155 next = next ? next + 1 : tail;
1156 if (in_signature && line[0] == ' ')
1158 else if (starts_with(line, gpg_sig_header) &&
1159 line[gpg_sig_header_len] == ' ')
1160 sig = line + gpg_sig_header_len + 1;
1162 strbuf_add(signature, sig, next - sig);
1167 /* dump the whole remainder of the buffer */
1169 strbuf_add(payload, line, next - line);
1174 unuse_commit_buffer(commit, buffer);
1175 return saw_signature;
1178 int remove_signature(struct strbuf *buf)
1180 const char *line = buf->buf;
1181 const char *tail = buf->buf + buf->len;
1182 int in_signature = 0;
1183 const char *sig_start = NULL;
1184 const char *sig_end = NULL;
1186 while (line < tail) {
1187 const char *next = memchr(line, '\n', tail - line);
1188 next = next ? next + 1 : tail;
1190 if (in_signature && line[0] == ' ')
1192 else if (starts_with(line, gpg_sig_header) &&
1193 line[gpg_sig_header_len] == ' ') {
1199 /* dump the whole remainder of the buffer */
1207 strbuf_remove(buf, sig_start - buf->buf, sig_end - sig_start);
1209 return sig_start != NULL;
1212 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1214 struct merge_remote_desc *desc;
1215 struct commit_extra_header *mergetag;
1217 unsigned long size, len;
1218 enum object_type type;
1220 desc = merge_remote_util(parent);
1221 if (!desc || !desc->obj)
1223 buf = read_object_file(&desc->obj->oid, &type, &size);
1224 if (!buf || type != OBJ_TAG)
1226 len = parse_signature(buf, size);
1230 * We could verify this signature and either omit the tag when
1231 * it does not validate, but the integrator may not have the
1232 * public key of the signer of the tag he is merging, while a
1233 * later auditor may have it while auditing, so let's not run
1234 * verify-signed-buffer here for now...
1236 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1237 * warn("warning: signed tag unverified.");
1239 mergetag = xcalloc(1, sizeof(*mergetag));
1240 mergetag->key = xstrdup("mergetag");
1241 mergetag->value = buf;
1242 mergetag->len = size;
1245 *tail = &mergetag->next;
1252 int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
1254 struct strbuf payload = STRBUF_INIT;
1255 struct strbuf signature = STRBUF_INIT;
1260 if (parse_signed_commit(commit, &payload, &signature) <= 0)
1262 ret = check_signature(payload.buf, payload.len, signature.buf,
1263 signature.len, sigc);
1266 strbuf_release(&payload);
1267 strbuf_release(&signature);
1274 void append_merge_tag_headers(struct commit_list *parents,
1275 struct commit_extra_header ***tail)
1278 struct commit *parent = parents->item;
1279 handle_signed_tag(parent, tail);
1280 parents = parents->next;
1284 static void add_extra_header(struct strbuf *buffer,
1285 struct commit_extra_header *extra)
1287 strbuf_addstr(buffer, extra->key);
1289 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1291 strbuf_addch(buffer, '\n');
1294 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1295 const char **exclude)
1297 struct commit_extra_header *extra = NULL;
1299 const char *buffer = get_commit_buffer(commit, &size);
1300 extra = read_commit_extra_header_lines(buffer, size, exclude);
1301 unuse_commit_buffer(commit, buffer);
1305 void for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
1307 struct commit_extra_header *extra, *to_free;
1309 to_free = read_commit_extra_headers(commit, NULL);
1310 for (extra = to_free; extra; extra = extra->next) {
1311 if (strcmp(extra->key, "mergetag"))
1312 continue; /* not a merge tag */
1313 fn(commit, extra, data);
1315 free_commit_extra_headers(to_free);
1318 static inline int standard_header_field(const char *field, size_t len)
1320 return ((len == 4 && !memcmp(field, "tree", 4)) ||
1321 (len == 6 && !memcmp(field, "parent", 6)) ||
1322 (len == 6 && !memcmp(field, "author", 6)) ||
1323 (len == 9 && !memcmp(field, "committer", 9)) ||
1324 (len == 8 && !memcmp(field, "encoding", 8)));
1327 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1333 size_t xlen = strlen(*exclude);
1334 if (len == xlen && !memcmp(field, *exclude, xlen))
1341 static struct commit_extra_header *read_commit_extra_header_lines(
1342 const char *buffer, size_t size,
1343 const char **exclude)
1345 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1346 const char *line, *next, *eof, *eob;
1347 struct strbuf buf = STRBUF_INIT;
1349 for (line = buffer, eob = line + size;
1350 line < eob && *line != '\n';
1352 next = memchr(line, '\n', eob - line);
1353 next = next ? next + 1 : eob;
1357 strbuf_add(&buf, line + 1, next - (line + 1));
1361 it->value = strbuf_detach(&buf, &it->len);
1365 eof = memchr(line, ' ', next - line);
1368 else if (standard_header_field(line, eof - line) ||
1369 excluded_header_field(line, eof - line, exclude))
1372 it = xcalloc(1, sizeof(*it));
1373 it->key = xmemdupz(line, eof-line);
1377 strbuf_add(&buf, eof + 1, next - (eof + 1));
1380 it->value = strbuf_detach(&buf, &it->len);
1384 void free_commit_extra_headers(struct commit_extra_header *extra)
1387 struct commit_extra_header *next = extra->next;
1395 int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1396 struct commit_list *parents, struct object_id *ret,
1397 const char *author, const char *sign_commit)
1399 struct commit_extra_header *extra = NULL, **tail = &extra;
1402 append_merge_tag_headers(parents, &tail);
1403 result = commit_tree_extended(msg, msg_len, tree, parents, ret,
1404 author, sign_commit, extra);
1405 free_commit_extra_headers(extra);
1409 static int find_invalid_utf8(const char *buf, int len)
1412 static const unsigned int max_codepoint[] = {
1413 0x7f, 0x7ff, 0xffff, 0x10ffff
1417 unsigned char c = *buf++;
1418 int bytes, bad_offset;
1419 unsigned int codepoint;
1420 unsigned int min_val, max_val;
1425 /* Simple US-ASCII? No worries. */
1429 bad_offset = offset-1;
1432 * Count how many more high bits set: that's how
1433 * many more bytes this sequence should have.
1442 * Must be between 1 and 3 more bytes. Longer sequences result in
1443 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1445 if (bytes < 1 || 3 < bytes)
1448 /* Do we *have* that many bytes? */
1453 * Place the encoded bits at the bottom of the value and compute the
1456 codepoint = (c & 0x7f) >> bytes;
1457 min_val = max_codepoint[bytes-1] + 1;
1458 max_val = max_codepoint[bytes];
1463 /* And verify that they are good continuation bytes */
1466 codepoint |= *buf & 0x3f;
1467 if ((*buf++ & 0xc0) != 0x80)
1471 /* Reject codepoints that are out of range for the sequence length. */
1472 if (codepoint < min_val || codepoint > max_val)
1474 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1475 if ((codepoint & 0x1ff800) == 0xd800)
1477 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1478 if ((codepoint & 0xfffe) == 0xfffe)
1480 /* So are anything in the range U+FDD0..U+FDEF. */
1481 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
1488 * This verifies that the buffer is in proper utf8 format.
1490 * If it isn't, it assumes any non-utf8 characters are Latin1,
1491 * and does the conversion.
1493 static int verify_utf8(struct strbuf *buf)
1501 unsigned char replace[2];
1503 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1509 strbuf_remove(buf, pos, 1);
1511 /* We know 'c' must be in the range 128-255 */
1512 replace[0] = 0xc0 + (c >> 6);
1513 replace[1] = 0x80 + (c & 0x3f);
1514 strbuf_insert(buf, pos, replace, 2);
1519 static const char commit_utf8_warn[] =
1520 N_("Warning: commit message did not conform to UTF-8.\n"
1521 "You may want to amend it after fixing the message, or set the config\n"
1522 "variable i18n.commitencoding to the encoding your project uses.\n");
1524 int commit_tree_extended(const char *msg, size_t msg_len,
1525 const struct object_id *tree,
1526 struct commit_list *parents, struct object_id *ret,
1527 const char *author, const char *sign_commit,
1528 struct commit_extra_header *extra)
1531 int encoding_is_utf8;
1532 struct strbuf buffer;
1534 assert_oid_type(tree, OBJ_TREE);
1536 if (memchr(msg, '\0', msg_len))
1537 return error("a NUL byte in commit log message not allowed.");
1539 /* Not having i18n.commitencoding is the same as having utf-8 */
1540 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1542 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1543 strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree));
1546 * NOTE! This ordering means that the same exact tree merged with a
1547 * different order of parents will be a _different_ changeset even
1548 * if everything else stays the same.
1551 struct commit *parent = pop_commit(&parents);
1552 strbuf_addf(&buffer, "parent %s\n",
1553 oid_to_hex(&parent->object.oid));
1556 /* Person/date information */
1558 author = git_author_info(IDENT_STRICT);
1559 strbuf_addf(&buffer, "author %s\n", author);
1560 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1561 if (!encoding_is_utf8)
1562 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1565 add_extra_header(&buffer, extra);
1566 extra = extra->next;
1568 strbuf_addch(&buffer, '\n');
1570 /* And add the comment */
1571 strbuf_add(&buffer, msg, msg_len);
1573 /* And check the encoding */
1574 if (encoding_is_utf8 && !verify_utf8(&buffer))
1575 fprintf(stderr, _(commit_utf8_warn));
1577 if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
1582 result = write_object_file(buffer.buf, buffer.len, commit_type, ret);
1584 strbuf_release(&buffer);
1588 void set_merge_remote_desc(struct commit *commit,
1589 const char *name, struct object *obj)
1591 struct merge_remote_desc *desc;
1592 FLEX_ALLOC_STR(desc, name, name);
1594 commit->util = desc;
1597 struct commit *get_merge_parent(const char *name)
1600 struct commit *commit;
1601 struct object_id oid;
1602 if (get_oid(name, &oid))
1604 obj = parse_object(&oid);
1605 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1606 if (commit && !commit->util)
1607 set_merge_remote_desc(commit, name, obj);
1612 * Append a commit to the end of the commit_list.
1614 * next starts by pointing to the variable that holds the head of an
1615 * empty commit_list, and is updated to point to the "next" field of
1616 * the last item on the list as new commits are appended.
1620 * struct commit_list *list;
1621 * struct commit_list **next = &list;
1623 * next = commit_list_append(c1, next);
1624 * next = commit_list_append(c2, next);
1625 * assert(commit_list_count(list) == 2);
1628 struct commit_list **commit_list_append(struct commit *commit,
1629 struct commit_list **next)
1631 struct commit_list *new_commit = xmalloc(sizeof(struct commit_list));
1632 new_commit->item = commit;
1634 new_commit->next = NULL;
1635 return &new_commit->next;
1638 const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1640 int key_len = strlen(key);
1641 const char *line = msg;
1644 const char *eol = strchrnul(line, '\n');
1649 if (eol - line > key_len &&
1650 !strncmp(line, key, key_len) &&
1651 line[key_len] == ' ') {
1652 *out_len = eol - line - key_len - 1;
1653 return line + key_len + 1;
1655 line = *eol ? eol + 1 : NULL;
1661 * Inspect the given string and determine the true "end" of the log message, in
1662 * order to find where to put a new Signed-off-by: line. Ignored are
1663 * trailing comment lines and blank lines. To support "git commit -s
1664 * --amend" on an existing commit, we also ignore "Conflicts:". To
1665 * support "git commit -v", we truncate at cut lines.
1667 * Returns the number of bytes from the tail to ignore, to be fed as
1668 * the second parameter to append_signoff().
1670 int ignore_non_trailer(const char *buf, size_t len)
1674 int in_old_conflicts_block = 0;
1675 size_t cutoff = wt_status_locate_end(buf, len);
1677 while (bol < cutoff) {
1678 const char *next_line = memchr(buf + bol, '\n', len - bol);
1681 next_line = buf + len;
1685 if (buf[bol] == comment_line_char || buf[bol] == '\n') {
1686 /* is this the first of the run of comments? */
1689 /* otherwise, it is just continuing */
1690 } else if (starts_with(buf + bol, "Conflicts:\n")) {
1691 in_old_conflicts_block = 1;
1694 } else if (in_old_conflicts_block && buf[bol] == '\t') {
1695 ; /* a pathname in the conflicts block */
1697 /* the previous was not trailing comment */
1699 in_old_conflicts_block = 0;
1701 bol = next_line - buf;
1703 return boc ? len - boc : len - cutoff;