Merge branch 'cl/p4-use-diff-tree'
[git/mingw.git] / commit.c
blob11509ffc471c548a1367068b3d7c2f15d19361f4
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "pkt-line.h"
5 #include "utf8.h"
6 #include "diff.h"
7 #include "revision.h"
8 #include "notes.h"
9 #include "gpg-interface.h"
10 #include "mergesort.h"
11 #include "commit-slab.h"
12 #include "prio-queue.h"
14 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
16 int save_commit_buffer = 1;
18 const char *commit_type = "commit";
19 static int commit_count;
21 static struct commit *check_commit(struct object *obj,
22 const unsigned char *sha1,
23 int quiet)
25 if (obj->type != OBJ_COMMIT) {
26 if (!quiet)
27 error("Object %s is a %s, not a commit",
28 sha1_to_hex(sha1), typename(obj->type));
29 return NULL;
31 return (struct commit *) obj;
34 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
35 int quiet)
37 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
39 if (!obj)
40 return NULL;
41 return check_commit(obj, sha1, quiet);
44 struct commit *lookup_commit_reference(const unsigned char *sha1)
46 return lookup_commit_reference_gently(sha1, 0);
49 struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name)
51 struct commit *c = lookup_commit_reference(sha1);
52 if (!c)
53 die(_("could not parse %s"), ref_name);
54 if (hashcmp(sha1, c->object.sha1)) {
55 warning(_("%s %s is not a commit!"),
56 ref_name, sha1_to_hex(sha1));
58 return c;
61 struct commit *lookup_commit(const unsigned char *sha1)
63 struct object *obj = lookup_object(sha1);
64 if (!obj) {
65 struct commit *c = alloc_commit_node();
66 c->index = commit_count++;
67 return create_object(sha1, OBJ_COMMIT, c);
69 if (!obj->type)
70 obj->type = OBJ_COMMIT;
71 return check_commit(obj, sha1, 0);
74 struct commit *lookup_commit_reference_by_name(const char *name)
76 unsigned char sha1[20];
77 struct commit *commit;
79 if (get_sha1_committish(name, sha1))
80 return NULL;
81 commit = lookup_commit_reference(sha1);
82 if (parse_commit(commit))
83 return NULL;
84 return commit;
87 static unsigned long parse_commit_date(const char *buf, const char *tail)
89 const char *dateptr;
91 if (buf + 6 >= tail)
92 return 0;
93 if (memcmp(buf, "author", 6))
94 return 0;
95 while (buf < tail && *buf++ != '\n')
96 /* nada */;
97 if (buf + 9 >= tail)
98 return 0;
99 if (memcmp(buf, "committer", 9))
100 return 0;
101 while (buf < tail && *buf++ != '>')
102 /* nada */;
103 if (buf >= tail)
104 return 0;
105 dateptr = buf;
106 while (buf < tail && *buf++ != '\n')
107 /* nada */;
108 if (buf >= tail)
109 return 0;
110 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
111 return strtoul(dateptr, NULL, 10);
114 static struct commit_graft **commit_graft;
115 static int commit_graft_alloc, commit_graft_nr;
117 static int commit_graft_pos(const unsigned char *sha1)
119 int lo, hi;
120 lo = 0;
121 hi = commit_graft_nr;
122 while (lo < hi) {
123 int mi = (lo + hi) / 2;
124 struct commit_graft *graft = commit_graft[mi];
125 int cmp = hashcmp(sha1, graft->sha1);
126 if (!cmp)
127 return mi;
128 if (cmp < 0)
129 hi = mi;
130 else
131 lo = mi + 1;
133 return -lo - 1;
136 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
138 int pos = commit_graft_pos(graft->sha1);
140 if (0 <= pos) {
141 if (ignore_dups)
142 free(graft);
143 else {
144 free(commit_graft[pos]);
145 commit_graft[pos] = graft;
147 return 1;
149 pos = -pos - 1;
150 if (commit_graft_alloc <= ++commit_graft_nr) {
151 commit_graft_alloc = alloc_nr(commit_graft_alloc);
152 commit_graft = xrealloc(commit_graft,
153 sizeof(*commit_graft) *
154 commit_graft_alloc);
156 if (pos < commit_graft_nr)
157 memmove(commit_graft + pos + 1,
158 commit_graft + pos,
159 (commit_graft_nr - pos - 1) *
160 sizeof(*commit_graft));
161 commit_graft[pos] = graft;
162 return 0;
165 struct commit_graft *read_graft_line(char *buf, int len)
167 /* The format is just "Commit Parent1 Parent2 ...\n" */
168 int i;
169 struct commit_graft *graft = NULL;
171 while (len && isspace(buf[len-1]))
172 buf[--len] = '\0';
173 if (buf[0] == '#' || buf[0] == '\0')
174 return NULL;
175 if ((len + 1) % 41)
176 goto bad_graft_data;
177 i = (len + 1) / 41 - 1;
178 graft = xmalloc(sizeof(*graft) + 20 * i);
179 graft->nr_parent = i;
180 if (get_sha1_hex(buf, graft->sha1))
181 goto bad_graft_data;
182 for (i = 40; i < len; i += 41) {
183 if (buf[i] != ' ')
184 goto bad_graft_data;
185 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
186 goto bad_graft_data;
188 return graft;
190 bad_graft_data:
191 error("bad graft data: %s", buf);
192 free(graft);
193 return NULL;
196 static int read_graft_file(const char *graft_file)
198 FILE *fp = fopen(graft_file, "r");
199 char buf[1024];
200 if (!fp)
201 return -1;
202 while (fgets(buf, sizeof(buf), fp)) {
203 /* The format is just "Commit Parent1 Parent2 ...\n" */
204 int len = strlen(buf);
205 struct commit_graft *graft = read_graft_line(buf, len);
206 if (!graft)
207 continue;
208 if (register_commit_graft(graft, 1))
209 error("duplicate graft data: %s", buf);
211 fclose(fp);
212 return 0;
215 static void prepare_commit_graft(void)
217 static int commit_graft_prepared;
218 char *graft_file;
220 if (commit_graft_prepared)
221 return;
222 graft_file = get_graft_file();
223 read_graft_file(graft_file);
224 /* make sure shallows are read */
225 is_repository_shallow();
226 commit_graft_prepared = 1;
229 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
231 int pos;
232 prepare_commit_graft();
233 pos = commit_graft_pos(sha1);
234 if (pos < 0)
235 return NULL;
236 return commit_graft[pos];
239 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
241 int i, ret;
242 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
243 ret = fn(commit_graft[i], cb_data);
244 return ret;
247 int unregister_shallow(const unsigned char *sha1)
249 int pos = commit_graft_pos(sha1);
250 if (pos < 0)
251 return -1;
252 if (pos + 1 < commit_graft_nr)
253 memmove(commit_graft + pos, commit_graft + pos + 1,
254 sizeof(struct commit_graft *)
255 * (commit_graft_nr - pos - 1));
256 commit_graft_nr--;
257 return 0;
260 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
262 const char *tail = buffer;
263 const char *bufptr = buffer;
264 unsigned char parent[20];
265 struct commit_list **pptr;
266 struct commit_graft *graft;
268 if (item->object.parsed)
269 return 0;
270 item->object.parsed = 1;
271 tail += size;
272 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
273 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
274 if (get_sha1_hex(bufptr + 5, parent) < 0)
275 return error("bad tree pointer in commit %s",
276 sha1_to_hex(item->object.sha1));
277 item->tree = lookup_tree(parent);
278 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
279 pptr = &item->parents;
281 graft = lookup_commit_graft(item->object.sha1);
282 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
283 struct commit *new_parent;
285 if (tail <= bufptr + 48 ||
286 get_sha1_hex(bufptr + 7, parent) ||
287 bufptr[47] != '\n')
288 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
289 bufptr += 48;
291 * The clone is shallow if nr_parent < 0, and we must
292 * not traverse its real parents even when we unhide them.
294 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
295 continue;
296 new_parent = lookup_commit(parent);
297 if (new_parent)
298 pptr = &commit_list_insert(new_parent, pptr)->next;
300 if (graft) {
301 int i;
302 struct commit *new_parent;
303 for (i = 0; i < graft->nr_parent; i++) {
304 new_parent = lookup_commit(graft->parent[i]);
305 if (!new_parent)
306 continue;
307 pptr = &commit_list_insert(new_parent, pptr)->next;
310 item->date = parse_commit_date(bufptr, tail);
312 return 0;
315 int parse_commit(struct commit *item)
317 enum object_type type;
318 void *buffer;
319 unsigned long size;
320 int ret;
322 if (!item)
323 return -1;
324 if (item->object.parsed)
325 return 0;
326 buffer = read_sha1_file(item->object.sha1, &type, &size);
327 if (!buffer)
328 return error("Could not read %s",
329 sha1_to_hex(item->object.sha1));
330 if (type != OBJ_COMMIT) {
331 free(buffer);
332 return error("Object %s not a commit",
333 sha1_to_hex(item->object.sha1));
335 ret = parse_commit_buffer(item, buffer, size);
336 if (save_commit_buffer && !ret) {
337 item->buffer = buffer;
338 return 0;
340 free(buffer);
341 return ret;
344 void parse_commit_or_die(struct commit *item)
346 if (parse_commit(item))
347 die("unable to parse commit %s",
348 item ? sha1_to_hex(item->object.sha1) : "(null)");
351 int find_commit_subject(const char *commit_buffer, const char **subject)
353 const char *eol;
354 const char *p = commit_buffer;
356 while (*p && (*p != '\n' || p[1] != '\n'))
357 p++;
358 if (*p) {
359 p += 2;
360 for (eol = p; *eol && *eol != '\n'; eol++)
361 ; /* do nothing */
362 } else
363 eol = p;
365 *subject = p;
367 return eol - p;
370 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
372 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
373 new_list->item = item;
374 new_list->next = *list_p;
375 *list_p = new_list;
376 return new_list;
379 unsigned commit_list_count(const struct commit_list *l)
381 unsigned c = 0;
382 for (; l; l = l->next )
383 c++;
384 return c;
387 struct commit_list *copy_commit_list(struct commit_list *list)
389 struct commit_list *head = NULL;
390 struct commit_list **pp = &head;
391 while (list) {
392 struct commit_list *new;
393 new = xmalloc(sizeof(struct commit_list));
394 new->item = list->item;
395 new->next = NULL;
396 *pp = new;
397 pp = &new->next;
398 list = list->next;
400 return head;
403 void free_commit_list(struct commit_list *list)
405 while (list) {
406 struct commit_list *temp = list;
407 list = temp->next;
408 free(temp);
412 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
414 struct commit_list **pp = list;
415 struct commit_list *p;
416 while ((p = *pp) != NULL) {
417 if (p->item->date < item->date) {
418 break;
420 pp = &p->next;
422 return commit_list_insert(item, pp);
425 static int commit_list_compare_by_date(const void *a, const void *b)
427 unsigned long a_date = ((const struct commit_list *)a)->item->date;
428 unsigned long b_date = ((const struct commit_list *)b)->item->date;
429 if (a_date < b_date)
430 return 1;
431 if (a_date > b_date)
432 return -1;
433 return 0;
436 static void *commit_list_get_next(const void *a)
438 return ((const struct commit_list *)a)->next;
441 static void commit_list_set_next(void *a, void *next)
443 ((struct commit_list *)a)->next = next;
446 void commit_list_sort_by_date(struct commit_list **list)
448 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
449 commit_list_compare_by_date);
452 struct commit *pop_most_recent_commit(struct commit_list **list,
453 unsigned int mark)
455 struct commit *ret = (*list)->item;
456 struct commit_list *parents = ret->parents;
457 struct commit_list *old = *list;
459 *list = (*list)->next;
460 free(old);
462 while (parents) {
463 struct commit *commit = parents->item;
464 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
465 commit->object.flags |= mark;
466 commit_list_insert_by_date(commit, list);
468 parents = parents->next;
470 return ret;
473 static void clear_commit_marks_1(struct commit_list **plist,
474 struct commit *commit, unsigned int mark)
476 while (commit) {
477 struct commit_list *parents;
479 if (!(mark & commit->object.flags))
480 return;
482 commit->object.flags &= ~mark;
484 parents = commit->parents;
485 if (!parents)
486 return;
488 while ((parents = parents->next))
489 commit_list_insert(parents->item, plist);
491 commit = commit->parents->item;
495 void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
497 struct commit_list *list = NULL;
499 while (nr--) {
500 commit_list_insert(*commit, &list);
501 commit++;
503 while (list)
504 clear_commit_marks_1(&list, pop_commit(&list), mark);
507 void clear_commit_marks(struct commit *commit, unsigned int mark)
509 clear_commit_marks_many(1, &commit, mark);
512 void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
514 struct object *object;
515 struct commit *commit;
516 unsigned int i;
518 for (i = 0; i < a->nr; i++) {
519 object = a->objects[i].item;
520 commit = lookup_commit_reference_gently(object->sha1, 1);
521 if (commit)
522 clear_commit_marks(commit, mark);
526 struct commit *pop_commit(struct commit_list **stack)
528 struct commit_list *top = *stack;
529 struct commit *item = top ? top->item : NULL;
531 if (top) {
532 *stack = top->next;
533 free(top);
535 return item;
539 * Topological sort support
542 /* count number of children that have not been emitted */
543 define_commit_slab(indegree_slab, int);
545 /* record author-date for each commit object */
546 define_commit_slab(author_date_slab, unsigned long);
548 static void record_author_date(struct author_date_slab *author_date,
549 struct commit *commit)
551 const char *buf, *line_end;
552 char *buffer = NULL;
553 struct ident_split ident;
554 char *date_end;
555 unsigned long date;
557 if (!commit->buffer) {
558 unsigned long size;
559 enum object_type type;
560 buffer = read_sha1_file(commit->object.sha1, &type, &size);
561 if (!buffer)
562 return;
565 for (buf = commit->buffer ? commit->buffer : buffer;
566 buf;
567 buf = line_end + 1) {
568 line_end = strchrnul(buf, '\n');
569 if (prefixcmp(buf, "author ")) {
570 if (!line_end[0] || line_end[1] == '\n')
571 return; /* end of header */
572 continue;
574 if (split_ident_line(&ident,
575 buf + strlen("author "),
576 line_end - (buf + strlen("author "))) ||
577 !ident.date_begin || !ident.date_end)
578 goto fail_exit; /* malformed "author" line */
579 break;
582 date = strtoul(ident.date_begin, &date_end, 10);
583 if (date_end != ident.date_end)
584 goto fail_exit; /* malformed date */
585 *(author_date_slab_at(author_date, commit)) = date;
587 fail_exit:
588 free(buffer);
591 static int compare_commits_by_author_date(const void *a_, const void *b_,
592 void *cb_data)
594 const struct commit *a = a_, *b = b_;
595 struct author_date_slab *author_date = cb_data;
596 unsigned long a_date = *(author_date_slab_at(author_date, a));
597 unsigned long b_date = *(author_date_slab_at(author_date, b));
599 /* newer commits with larger date first */
600 if (a_date < b_date)
601 return 1;
602 else if (a_date > b_date)
603 return -1;
604 return 0;
607 int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
609 const struct commit *a = a_, *b = b_;
610 /* newer commits with larger date first */
611 if (a->date < b->date)
612 return 1;
613 else if (a->date > b->date)
614 return -1;
615 return 0;
619 * Performs an in-place topological sort on the list supplied.
621 void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
623 struct commit_list *next, *orig = *list;
624 struct commit_list **pptr;
625 struct indegree_slab indegree;
626 struct prio_queue queue;
627 struct commit *commit;
628 struct author_date_slab author_date;
630 if (!orig)
631 return;
632 *list = NULL;
634 init_indegree_slab(&indegree);
635 memset(&queue, '\0', sizeof(queue));
637 switch (sort_order) {
638 default: /* REV_SORT_IN_GRAPH_ORDER */
639 queue.compare = NULL;
640 break;
641 case REV_SORT_BY_COMMIT_DATE:
642 queue.compare = compare_commits_by_commit_date;
643 break;
644 case REV_SORT_BY_AUTHOR_DATE:
645 init_author_date_slab(&author_date);
646 queue.compare = compare_commits_by_author_date;
647 queue.cb_data = &author_date;
648 break;
651 /* Mark them and clear the indegree */
652 for (next = orig; next; next = next->next) {
653 struct commit *commit = next->item;
654 *(indegree_slab_at(&indegree, commit)) = 1;
655 /* also record the author dates, if needed */
656 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
657 record_author_date(&author_date, commit);
660 /* update the indegree */
661 for (next = orig; next; next = next->next) {
662 struct commit_list *parents = next->item->parents;
663 while (parents) {
664 struct commit *parent = parents->item;
665 int *pi = indegree_slab_at(&indegree, parent);
667 if (*pi)
668 (*pi)++;
669 parents = parents->next;
674 * find the tips
676 * tips are nodes not reachable from any other node in the list
678 * the tips serve as a starting set for the work queue.
680 for (next = orig; next; next = next->next) {
681 struct commit *commit = next->item;
683 if (*(indegree_slab_at(&indegree, commit)) == 1)
684 prio_queue_put(&queue, commit);
688 * This is unfortunate; the initial tips need to be shown
689 * in the order given from the revision traversal machinery.
691 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
692 prio_queue_reverse(&queue);
694 /* We no longer need the commit list */
695 free_commit_list(orig);
697 pptr = list;
698 *list = NULL;
699 while ((commit = prio_queue_get(&queue)) != NULL) {
700 struct commit_list *parents;
702 for (parents = commit->parents; parents ; parents = parents->next) {
703 struct commit *parent = parents->item;
704 int *pi = indegree_slab_at(&indegree, parent);
706 if (!*pi)
707 continue;
710 * parents are only enqueued for emission
711 * when all their children have been emitted thereby
712 * guaranteeing topological order.
714 if (--(*pi) == 1)
715 prio_queue_put(&queue, parent);
718 * all children of commit have already been
719 * emitted. we can emit it now.
721 *(indegree_slab_at(&indegree, commit)) = 0;
723 pptr = &commit_list_insert(commit, pptr)->next;
726 clear_indegree_slab(&indegree);
727 clear_prio_queue(&queue);
728 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
729 clear_author_date_slab(&author_date);
732 /* merge-base stuff */
734 /* bits #0..15 in revision.h */
735 #define PARENT1 (1u<<16)
736 #define PARENT2 (1u<<17)
737 #define STALE (1u<<18)
738 #define RESULT (1u<<19)
740 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
742 static struct commit *interesting(struct commit_list *list)
744 while (list) {
745 struct commit *commit = list->item;
746 list = list->next;
747 if (commit->object.flags & STALE)
748 continue;
749 return commit;
751 return NULL;
754 /* all input commits in one and twos[] must have been parsed! */
755 static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
757 struct commit_list *list = NULL;
758 struct commit_list *result = NULL;
759 int i;
761 one->object.flags |= PARENT1;
762 commit_list_insert_by_date(one, &list);
763 if (!n)
764 return list;
765 for (i = 0; i < n; i++) {
766 twos[i]->object.flags |= PARENT2;
767 commit_list_insert_by_date(twos[i], &list);
770 while (interesting(list)) {
771 struct commit *commit;
772 struct commit_list *parents;
773 struct commit_list *next;
774 int flags;
776 commit = list->item;
777 next = list->next;
778 free(list);
779 list = next;
781 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
782 if (flags == (PARENT1 | PARENT2)) {
783 if (!(commit->object.flags & RESULT)) {
784 commit->object.flags |= RESULT;
785 commit_list_insert_by_date(commit, &result);
787 /* Mark parents of a found merge stale */
788 flags |= STALE;
790 parents = commit->parents;
791 while (parents) {
792 struct commit *p = parents->item;
793 parents = parents->next;
794 if ((p->object.flags & flags) == flags)
795 continue;
796 if (parse_commit(p))
797 return NULL;
798 p->object.flags |= flags;
799 commit_list_insert_by_date(p, &list);
803 free_commit_list(list);
804 return result;
807 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
809 struct commit_list *list = NULL;
810 struct commit_list *result = NULL;
811 int i;
813 for (i = 0; i < n; i++) {
814 if (one == twos[i])
816 * We do not mark this even with RESULT so we do not
817 * have to clean it up.
819 return commit_list_insert(one, &result);
822 if (parse_commit(one))
823 return NULL;
824 for (i = 0; i < n; i++) {
825 if (parse_commit(twos[i]))
826 return NULL;
829 list = paint_down_to_common(one, n, twos);
831 while (list) {
832 struct commit_list *next = list->next;
833 if (!(list->item->object.flags & STALE))
834 commit_list_insert_by_date(list->item, &result);
835 free(list);
836 list = next;
838 return result;
841 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
843 struct commit_list *i, *j, *k, *ret = NULL;
844 struct commit_list **pptr = &ret;
846 for (i = in; i; i = i->next) {
847 if (!ret)
848 pptr = &commit_list_insert(i->item, pptr)->next;
849 else {
850 struct commit_list *new = NULL, *end = NULL;
852 for (j = ret; j; j = j->next) {
853 struct commit_list *bases;
854 bases = get_merge_bases(i->item, j->item, 1);
855 if (!new)
856 new = bases;
857 else
858 end->next = bases;
859 for (k = bases; k; k = k->next)
860 end = k;
862 ret = new;
865 return ret;
868 static int remove_redundant(struct commit **array, int cnt)
871 * Some commit in the array may be an ancestor of
872 * another commit. Move such commit to the end of
873 * the array, and return the number of commits that
874 * are independent from each other.
876 struct commit **work;
877 unsigned char *redundant;
878 int *filled_index;
879 int i, j, filled;
881 work = xcalloc(cnt, sizeof(*work));
882 redundant = xcalloc(cnt, 1);
883 filled_index = xmalloc(sizeof(*filled_index) * (cnt - 1));
885 for (i = 0; i < cnt; i++)
886 parse_commit(array[i]);
887 for (i = 0; i < cnt; i++) {
888 struct commit_list *common;
890 if (redundant[i])
891 continue;
892 for (j = filled = 0; j < cnt; j++) {
893 if (i == j || redundant[j])
894 continue;
895 filled_index[filled] = j;
896 work[filled++] = array[j];
898 common = paint_down_to_common(array[i], filled, work);
899 if (array[i]->object.flags & PARENT2)
900 redundant[i] = 1;
901 for (j = 0; j < filled; j++)
902 if (work[j]->object.flags & PARENT1)
903 redundant[filled_index[j]] = 1;
904 clear_commit_marks(array[i], all_flags);
905 for (j = 0; j < filled; j++)
906 clear_commit_marks(work[j], all_flags);
907 free_commit_list(common);
910 /* Now collect the result */
911 memcpy(work, array, sizeof(*array) * cnt);
912 for (i = filled = 0; i < cnt; i++)
913 if (!redundant[i])
914 array[filled++] = work[i];
915 for (j = filled, i = 0; i < cnt; i++)
916 if (redundant[i])
917 array[j++] = work[i];
918 free(work);
919 free(redundant);
920 free(filled_index);
921 return filled;
924 struct commit_list *get_merge_bases_many(struct commit *one,
925 int n,
926 struct commit **twos,
927 int cleanup)
929 struct commit_list *list;
930 struct commit **rslt;
931 struct commit_list *result;
932 int cnt, i;
934 result = merge_bases_many(one, n, twos);
935 for (i = 0; i < n; i++) {
936 if (one == twos[i])
937 return result;
939 if (!result || !result->next) {
940 if (cleanup) {
941 clear_commit_marks(one, all_flags);
942 clear_commit_marks_many(n, twos, all_flags);
944 return result;
947 /* There are more than one */
948 cnt = 0;
949 list = result;
950 while (list) {
951 list = list->next;
952 cnt++;
954 rslt = xcalloc(cnt, sizeof(*rslt));
955 for (list = result, i = 0; list; list = list->next)
956 rslt[i++] = list->item;
957 free_commit_list(result);
959 clear_commit_marks(one, all_flags);
960 clear_commit_marks_many(n, twos, all_flags);
962 cnt = remove_redundant(rslt, cnt);
963 result = NULL;
964 for (i = 0; i < cnt; i++)
965 commit_list_insert_by_date(rslt[i], &result);
966 free(rslt);
967 return result;
970 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
971 int cleanup)
973 return get_merge_bases_many(one, 1, &two, cleanup);
977 * Is "commit" a descendant of one of the elements on the "with_commit" list?
979 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
981 if (!with_commit)
982 return 1;
983 while (with_commit) {
984 struct commit *other;
986 other = with_commit->item;
987 with_commit = with_commit->next;
988 if (in_merge_bases(other, commit))
989 return 1;
991 return 0;
995 * Is "commit" an ancestor of one of the "references"?
997 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
999 struct commit_list *bases;
1000 int ret = 0, i;
1002 if (parse_commit(commit))
1003 return ret;
1004 for (i = 0; i < nr_reference; i++)
1005 if (parse_commit(reference[i]))
1006 return ret;
1008 bases = paint_down_to_common(commit, nr_reference, reference);
1009 if (commit->object.flags & PARENT2)
1010 ret = 1;
1011 clear_commit_marks(commit, all_flags);
1012 clear_commit_marks_many(nr_reference, reference, all_flags);
1013 free_commit_list(bases);
1014 return ret;
1018 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1020 int in_merge_bases(struct commit *commit, struct commit *reference)
1022 return in_merge_bases_many(commit, 1, &reference);
1025 struct commit_list *reduce_heads(struct commit_list *heads)
1027 struct commit_list *p;
1028 struct commit_list *result = NULL, **tail = &result;
1029 struct commit **array;
1030 int num_head, i;
1032 if (!heads)
1033 return NULL;
1035 /* Uniquify */
1036 for (p = heads; p; p = p->next)
1037 p->item->object.flags &= ~STALE;
1038 for (p = heads, num_head = 0; p; p = p->next) {
1039 if (p->item->object.flags & STALE)
1040 continue;
1041 p->item->object.flags |= STALE;
1042 num_head++;
1044 array = xcalloc(sizeof(*array), num_head);
1045 for (p = heads, i = 0; p; p = p->next) {
1046 if (p->item->object.flags & STALE) {
1047 array[i++] = p->item;
1048 p->item->object.flags &= ~STALE;
1051 num_head = remove_redundant(array, num_head);
1052 for (i = 0; i < num_head; i++)
1053 tail = &commit_list_insert(array[i], tail)->next;
1054 return result;
1057 static const char gpg_sig_header[] = "gpgsig";
1058 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
1060 static int do_sign_commit(struct strbuf *buf, const char *keyid)
1062 struct strbuf sig = STRBUF_INIT;
1063 int inspos, copypos;
1065 /* find the end of the header */
1066 inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
1068 if (!keyid || !*keyid)
1069 keyid = get_signing_key();
1070 if (sign_buffer(buf, &sig, keyid)) {
1071 strbuf_release(&sig);
1072 return -1;
1075 for (copypos = 0; sig.buf[copypos]; ) {
1076 const char *bol = sig.buf + copypos;
1077 const char *eol = strchrnul(bol, '\n');
1078 int len = (eol - bol) + !!*eol;
1080 if (!copypos) {
1081 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1082 inspos += gpg_sig_header_len;
1084 strbuf_insert(buf, inspos++, " ", 1);
1085 strbuf_insert(buf, inspos, bol, len);
1086 inspos += len;
1087 copypos += len;
1089 strbuf_release(&sig);
1090 return 0;
1093 int parse_signed_commit(const unsigned char *sha1,
1094 struct strbuf *payload, struct strbuf *signature)
1096 unsigned long size;
1097 enum object_type type;
1098 char *buffer = read_sha1_file(sha1, &type, &size);
1099 int in_signature, saw_signature = -1;
1100 char *line, *tail;
1102 if (!buffer || type != OBJ_COMMIT)
1103 goto cleanup;
1105 line = buffer;
1106 tail = buffer + size;
1107 in_signature = 0;
1108 saw_signature = 0;
1109 while (line < tail) {
1110 const char *sig = NULL;
1111 char *next = memchr(line, '\n', tail - line);
1113 next = next ? next + 1 : tail;
1114 if (in_signature && line[0] == ' ')
1115 sig = line + 1;
1116 else if (!prefixcmp(line, gpg_sig_header) &&
1117 line[gpg_sig_header_len] == ' ')
1118 sig = line + gpg_sig_header_len + 1;
1119 if (sig) {
1120 strbuf_add(signature, sig, next - sig);
1121 saw_signature = 1;
1122 in_signature = 1;
1123 } else {
1124 if (*line == '\n')
1125 /* dump the whole remainder of the buffer */
1126 next = tail;
1127 strbuf_add(payload, line, next - line);
1128 in_signature = 0;
1130 line = next;
1132 cleanup:
1133 free(buffer);
1134 return saw_signature;
1137 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1139 struct merge_remote_desc *desc;
1140 struct commit_extra_header *mergetag;
1141 char *buf;
1142 unsigned long size, len;
1143 enum object_type type;
1145 desc = merge_remote_util(parent);
1146 if (!desc || !desc->obj)
1147 return;
1148 buf = read_sha1_file(desc->obj->sha1, &type, &size);
1149 if (!buf || type != OBJ_TAG)
1150 goto free_return;
1151 len = parse_signature(buf, size);
1152 if (size == len)
1153 goto free_return;
1155 * We could verify this signature and either omit the tag when
1156 * it does not validate, but the integrator may not have the
1157 * public key of the signer of the tag he is merging, while a
1158 * later auditor may have it while auditing, so let's not run
1159 * verify-signed-buffer here for now...
1161 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1162 * warn("warning: signed tag unverified.");
1164 mergetag = xcalloc(1, sizeof(*mergetag));
1165 mergetag->key = xstrdup("mergetag");
1166 mergetag->value = buf;
1167 mergetag->len = size;
1169 **tail = mergetag;
1170 *tail = &mergetag->next;
1171 return;
1173 free_return:
1174 free(buf);
1177 static struct {
1178 char result;
1179 const char *check;
1180 } sigcheck_gpg_status[] = {
1181 { 'G', "\n[GNUPG:] GOODSIG " },
1182 { 'B', "\n[GNUPG:] BADSIG " },
1183 { 'U', "\n[GNUPG:] TRUST_NEVER" },
1184 { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
1187 static void parse_gpg_output(struct signature_check *sigc)
1189 const char *buf = sigc->gpg_status;
1190 int i;
1192 /* Iterate over all search strings */
1193 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
1194 const char *found, *next;
1196 if (!prefixcmp(buf, sigcheck_gpg_status[i].check + 1)) {
1197 /* At the very beginning of the buffer */
1198 found = buf + strlen(sigcheck_gpg_status[i].check + 1);
1199 } else {
1200 found = strstr(buf, sigcheck_gpg_status[i].check);
1201 if (!found)
1202 continue;
1203 found += strlen(sigcheck_gpg_status[i].check);
1205 sigc->result = sigcheck_gpg_status[i].result;
1206 /* The trust messages are not followed by key/signer information */
1207 if (sigc->result != 'U') {
1208 sigc->key = xmemdupz(found, 16);
1209 found += 17;
1210 next = strchrnul(found, '\n');
1211 sigc->signer = xmemdupz(found, next - found);
1216 void check_commit_signature(const struct commit* commit, struct signature_check *sigc)
1218 struct strbuf payload = STRBUF_INIT;
1219 struct strbuf signature = STRBUF_INIT;
1220 struct strbuf gpg_output = STRBUF_INIT;
1221 struct strbuf gpg_status = STRBUF_INIT;
1222 int status;
1224 sigc->result = 'N';
1226 if (parse_signed_commit(commit->object.sha1,
1227 &payload, &signature) <= 0)
1228 goto out;
1229 status = verify_signed_buffer(payload.buf, payload.len,
1230 signature.buf, signature.len,
1231 &gpg_output, &gpg_status);
1232 if (status && !gpg_output.len)
1233 goto out;
1234 sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
1235 sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
1236 parse_gpg_output(sigc);
1238 out:
1239 strbuf_release(&gpg_status);
1240 strbuf_release(&gpg_output);
1241 strbuf_release(&payload);
1242 strbuf_release(&signature);
1247 void append_merge_tag_headers(struct commit_list *parents,
1248 struct commit_extra_header ***tail)
1250 while (parents) {
1251 struct commit *parent = parents->item;
1252 handle_signed_tag(parent, tail);
1253 parents = parents->next;
1257 static void add_extra_header(struct strbuf *buffer,
1258 struct commit_extra_header *extra)
1260 strbuf_addstr(buffer, extra->key);
1261 if (extra->len)
1262 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1263 else
1264 strbuf_addch(buffer, '\n');
1267 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1268 const char **exclude)
1270 struct commit_extra_header *extra = NULL;
1271 unsigned long size;
1272 enum object_type type;
1273 char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
1274 if (buffer && type == OBJ_COMMIT)
1275 extra = read_commit_extra_header_lines(buffer, size, exclude);
1276 free(buffer);
1277 return extra;
1280 static inline int standard_header_field(const char *field, size_t len)
1282 return ((len == 4 && !memcmp(field, "tree ", 5)) ||
1283 (len == 6 && !memcmp(field, "parent ", 7)) ||
1284 (len == 6 && !memcmp(field, "author ", 7)) ||
1285 (len == 9 && !memcmp(field, "committer ", 10)) ||
1286 (len == 8 && !memcmp(field, "encoding ", 9)));
1289 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1291 if (!exclude)
1292 return 0;
1294 while (*exclude) {
1295 size_t xlen = strlen(*exclude);
1296 if (len == xlen &&
1297 !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
1298 return 1;
1299 exclude++;
1301 return 0;
1304 static struct commit_extra_header *read_commit_extra_header_lines(
1305 const char *buffer, size_t size,
1306 const char **exclude)
1308 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1309 const char *line, *next, *eof, *eob;
1310 struct strbuf buf = STRBUF_INIT;
1312 for (line = buffer, eob = line + size;
1313 line < eob && *line != '\n';
1314 line = next) {
1315 next = memchr(line, '\n', eob - line);
1316 next = next ? next + 1 : eob;
1317 if (*line == ' ') {
1318 /* continuation */
1319 if (it)
1320 strbuf_add(&buf, line + 1, next - (line + 1));
1321 continue;
1323 if (it)
1324 it->value = strbuf_detach(&buf, &it->len);
1325 strbuf_reset(&buf);
1326 it = NULL;
1328 eof = strchr(line, ' ');
1329 if (next <= eof)
1330 eof = next;
1332 if (standard_header_field(line, eof - line) ||
1333 excluded_header_field(line, eof - line, exclude))
1334 continue;
1336 it = xcalloc(1, sizeof(*it));
1337 it->key = xmemdupz(line, eof-line);
1338 *tail = it;
1339 tail = &it->next;
1340 if (eof + 1 < next)
1341 strbuf_add(&buf, eof + 1, next - (eof + 1));
1343 if (it)
1344 it->value = strbuf_detach(&buf, &it->len);
1345 return extra;
1348 void free_commit_extra_headers(struct commit_extra_header *extra)
1350 while (extra) {
1351 struct commit_extra_header *next = extra->next;
1352 free(extra->key);
1353 free(extra->value);
1354 free(extra);
1355 extra = next;
1359 int commit_tree(const struct strbuf *msg, unsigned char *tree,
1360 struct commit_list *parents, unsigned char *ret,
1361 const char *author, const char *sign_commit)
1363 struct commit_extra_header *extra = NULL, **tail = &extra;
1364 int result;
1366 append_merge_tag_headers(parents, &tail);
1367 result = commit_tree_extended(msg, tree, parents, ret,
1368 author, sign_commit, extra);
1369 free_commit_extra_headers(extra);
1370 return result;
1373 static int find_invalid_utf8(const char *buf, int len)
1375 int offset = 0;
1376 static const unsigned int max_codepoint[] = {
1377 0x7f, 0x7ff, 0xffff, 0x10ffff
1380 while (len) {
1381 unsigned char c = *buf++;
1382 int bytes, bad_offset;
1383 unsigned int codepoint;
1384 unsigned int min_val, max_val;
1386 len--;
1387 offset++;
1389 /* Simple US-ASCII? No worries. */
1390 if (c < 0x80)
1391 continue;
1393 bad_offset = offset-1;
1396 * Count how many more high bits set: that's how
1397 * many more bytes this sequence should have.
1399 bytes = 0;
1400 while (c & 0x40) {
1401 c <<= 1;
1402 bytes++;
1406 * Must be between 1 and 3 more bytes. Longer sequences result in
1407 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1409 if (bytes < 1 || 3 < bytes)
1410 return bad_offset;
1412 /* Do we *have* that many bytes? */
1413 if (len < bytes)
1414 return bad_offset;
1417 * Place the encoded bits at the bottom of the value and compute the
1418 * valid range.
1420 codepoint = (c & 0x7f) >> bytes;
1421 min_val = max_codepoint[bytes-1] + 1;
1422 max_val = max_codepoint[bytes];
1424 offset += bytes;
1425 len -= bytes;
1427 /* And verify that they are good continuation bytes */
1428 do {
1429 codepoint <<= 6;
1430 codepoint |= *buf & 0x3f;
1431 if ((*buf++ & 0xc0) != 0x80)
1432 return bad_offset;
1433 } while (--bytes);
1435 /* Reject codepoints that are out of range for the sequence length. */
1436 if (codepoint < min_val || codepoint > max_val)
1437 return bad_offset;
1438 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1439 if ((codepoint & 0x1ff800) == 0xd800)
1440 return bad_offset;
1441 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1442 if ((codepoint & 0xfffe) == 0xfffe)
1443 return bad_offset;
1444 /* So are anything in the range U+FDD0..U+FDEF. */
1445 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
1446 return bad_offset;
1448 return -1;
1452 * This verifies that the buffer is in proper utf8 format.
1454 * If it isn't, it assumes any non-utf8 characters are Latin1,
1455 * and does the conversion.
1457 static int verify_utf8(struct strbuf *buf)
1459 int ok = 1;
1460 long pos = 0;
1462 for (;;) {
1463 int bad;
1464 unsigned char c;
1465 unsigned char replace[2];
1467 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1468 if (bad < 0)
1469 return ok;
1470 pos += bad;
1471 ok = 0;
1472 c = buf->buf[pos];
1473 strbuf_remove(buf, pos, 1);
1475 /* We know 'c' must be in the range 128-255 */
1476 replace[0] = 0xc0 + (c >> 6);
1477 replace[1] = 0x80 + (c & 0x3f);
1478 strbuf_insert(buf, pos, replace, 2);
1479 pos += 2;
1483 static const char commit_utf8_warn[] =
1484 "Warning: commit message did not conform to UTF-8.\n"
1485 "You may want to amend it after fixing the message, or set the config\n"
1486 "variable i18n.commitencoding to the encoding your project uses.\n";
1488 int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
1489 struct commit_list *parents, unsigned char *ret,
1490 const char *author, const char *sign_commit,
1491 struct commit_extra_header *extra)
1493 int result;
1494 int encoding_is_utf8;
1495 struct strbuf buffer;
1497 assert_sha1_type(tree, OBJ_TREE);
1499 if (memchr(msg->buf, '\0', msg->len))
1500 return error("a NUL byte in commit log message not allowed.");
1502 /* Not having i18n.commitencoding is the same as having utf-8 */
1503 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1505 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1506 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1509 * NOTE! This ordering means that the same exact tree merged with a
1510 * different order of parents will be a _different_ changeset even
1511 * if everything else stays the same.
1513 while (parents) {
1514 struct commit_list *next = parents->next;
1515 struct commit *parent = parents->item;
1517 strbuf_addf(&buffer, "parent %s\n",
1518 sha1_to_hex(parent->object.sha1));
1519 free(parents);
1520 parents = next;
1523 /* Person/date information */
1524 if (!author)
1525 author = git_author_info(IDENT_STRICT);
1526 strbuf_addf(&buffer, "author %s\n", author);
1527 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1528 if (!encoding_is_utf8)
1529 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1531 while (extra) {
1532 add_extra_header(&buffer, extra);
1533 extra = extra->next;
1535 strbuf_addch(&buffer, '\n');
1537 /* And add the comment */
1538 strbuf_addbuf(&buffer, msg);
1540 /* And check the encoding */
1541 if (encoding_is_utf8 && !verify_utf8(&buffer))
1542 fprintf(stderr, commit_utf8_warn);
1544 if (sign_commit && do_sign_commit(&buffer, sign_commit))
1545 return -1;
1547 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1548 strbuf_release(&buffer);
1549 return result;
1552 struct commit *get_merge_parent(const char *name)
1554 struct object *obj;
1555 struct commit *commit;
1556 unsigned char sha1[20];
1557 if (get_sha1(name, sha1))
1558 return NULL;
1559 obj = parse_object(sha1);
1560 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1561 if (commit && !commit->util) {
1562 struct merge_remote_desc *desc;
1563 desc = xmalloc(sizeof(*desc));
1564 desc->obj = obj;
1565 desc->name = strdup(name);
1566 commit->util = desc;
1568 return commit;
1572 * Append a commit to the end of the commit_list.
1574 * next starts by pointing to the variable that holds the head of an
1575 * empty commit_list, and is updated to point to the "next" field of
1576 * the last item on the list as new commits are appended.
1578 * Usage example:
1580 * struct commit_list *list;
1581 * struct commit_list **next = &list;
1583 * next = commit_list_append(c1, next);
1584 * next = commit_list_append(c2, next);
1585 * assert(commit_list_count(list) == 2);
1586 * return list;
1588 struct commit_list **commit_list_append(struct commit *commit,
1589 struct commit_list **next)
1591 struct commit_list *new = xmalloc(sizeof(struct commit_list));
1592 new->item = commit;
1593 *next = new;
1594 new->next = NULL;
1595 return &new->next;
1598 void print_commit_list(struct commit_list *list,
1599 const char *format_cur,
1600 const char *format_last)
1602 for ( ; list; list = list->next) {
1603 const char *format = list->next ? format_cur : format_last;
1604 printf(format, sha1_to_hex(list->item->object.sha1));