commit.c: use ALLOC_GROW() in register_commit_graft()
[git/mingw.git] / commit.c
blobe0043144b17a74f9b3ba1065e466af05a0c6a6ec
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 ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc);
151 commit_graft_nr++;
152 if (pos < commit_graft_nr)
153 memmove(commit_graft + pos + 1,
154 commit_graft + pos,
155 (commit_graft_nr - pos - 1) *
156 sizeof(*commit_graft));
157 commit_graft[pos] = graft;
158 return 0;
161 struct commit_graft *read_graft_line(char *buf, int len)
163 /* The format is just "Commit Parent1 Parent2 ...\n" */
164 int i;
165 struct commit_graft *graft = NULL;
167 while (len && isspace(buf[len-1]))
168 buf[--len] = '\0';
169 if (buf[0] == '#' || buf[0] == '\0')
170 return NULL;
171 if ((len + 1) % 41)
172 goto bad_graft_data;
173 i = (len + 1) / 41 - 1;
174 graft = xmalloc(sizeof(*graft) + 20 * i);
175 graft->nr_parent = i;
176 if (get_sha1_hex(buf, graft->sha1))
177 goto bad_graft_data;
178 for (i = 40; i < len; i += 41) {
179 if (buf[i] != ' ')
180 goto bad_graft_data;
181 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
182 goto bad_graft_data;
184 return graft;
186 bad_graft_data:
187 error("bad graft data: %s", buf);
188 free(graft);
189 return NULL;
192 static int read_graft_file(const char *graft_file)
194 FILE *fp = fopen(graft_file, "r");
195 struct strbuf buf = STRBUF_INIT;
196 if (!fp)
197 return -1;
198 while (!strbuf_getwholeline(&buf, fp, '\n')) {
199 /* The format is just "Commit Parent1 Parent2 ...\n" */
200 struct commit_graft *graft = read_graft_line(buf.buf, buf.len);
201 if (!graft)
202 continue;
203 if (register_commit_graft(graft, 1))
204 error("duplicate graft data: %s", buf.buf);
206 fclose(fp);
207 strbuf_release(&buf);
208 return 0;
211 static void prepare_commit_graft(void)
213 static int commit_graft_prepared;
214 char *graft_file;
216 if (commit_graft_prepared)
217 return;
218 graft_file = get_graft_file();
219 read_graft_file(graft_file);
220 /* make sure shallows are read */
221 is_repository_shallow();
222 commit_graft_prepared = 1;
225 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
227 int pos;
228 prepare_commit_graft();
229 pos = commit_graft_pos(sha1);
230 if (pos < 0)
231 return NULL;
232 return commit_graft[pos];
235 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
237 int i, ret;
238 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
239 ret = fn(commit_graft[i], cb_data);
240 return ret;
243 int unregister_shallow(const unsigned char *sha1)
245 int pos = commit_graft_pos(sha1);
246 if (pos < 0)
247 return -1;
248 if (pos + 1 < commit_graft_nr)
249 memmove(commit_graft + pos, commit_graft + pos + 1,
250 sizeof(struct commit_graft *)
251 * (commit_graft_nr - pos - 1));
252 commit_graft_nr--;
253 return 0;
256 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
258 const char *tail = buffer;
259 const char *bufptr = buffer;
260 unsigned char parent[20];
261 struct commit_list **pptr;
262 struct commit_graft *graft;
264 if (item->object.parsed)
265 return 0;
266 item->object.parsed = 1;
267 tail += size;
268 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
269 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
270 if (get_sha1_hex(bufptr + 5, parent) < 0)
271 return error("bad tree pointer in commit %s",
272 sha1_to_hex(item->object.sha1));
273 item->tree = lookup_tree(parent);
274 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
275 pptr = &item->parents;
277 graft = lookup_commit_graft(item->object.sha1);
278 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
279 struct commit *new_parent;
281 if (tail <= bufptr + 48 ||
282 get_sha1_hex(bufptr + 7, parent) ||
283 bufptr[47] != '\n')
284 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
285 bufptr += 48;
287 * The clone is shallow if nr_parent < 0, and we must
288 * not traverse its real parents even when we unhide them.
290 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
291 continue;
292 new_parent = lookup_commit(parent);
293 if (new_parent)
294 pptr = &commit_list_insert(new_parent, pptr)->next;
296 if (graft) {
297 int i;
298 struct commit *new_parent;
299 for (i = 0; i < graft->nr_parent; i++) {
300 new_parent = lookup_commit(graft->parent[i]);
301 if (!new_parent)
302 continue;
303 pptr = &commit_list_insert(new_parent, pptr)->next;
306 item->date = parse_commit_date(bufptr, tail);
308 return 0;
311 int parse_commit(struct commit *item)
313 enum object_type type;
314 void *buffer;
315 unsigned long size;
316 int ret;
318 if (!item)
319 return -1;
320 if (item->object.parsed)
321 return 0;
322 buffer = read_sha1_file(item->object.sha1, &type, &size);
323 if (!buffer)
324 return error("Could not read %s",
325 sha1_to_hex(item->object.sha1));
326 if (type != OBJ_COMMIT) {
327 free(buffer);
328 return error("Object %s not a commit",
329 sha1_to_hex(item->object.sha1));
331 ret = parse_commit_buffer(item, buffer, size);
332 if (save_commit_buffer && !ret) {
333 item->buffer = buffer;
334 return 0;
336 free(buffer);
337 return ret;
340 void parse_commit_or_die(struct commit *item)
342 if (parse_commit(item))
343 die("unable to parse commit %s",
344 item ? sha1_to_hex(item->object.sha1) : "(null)");
347 int find_commit_subject(const char *commit_buffer, const char **subject)
349 const char *eol;
350 const char *p = commit_buffer;
352 while (*p && (*p != '\n' || p[1] != '\n'))
353 p++;
354 if (*p) {
355 p += 2;
356 for (eol = p; *eol && *eol != '\n'; eol++)
357 ; /* do nothing */
358 } else
359 eol = p;
361 *subject = p;
363 return eol - p;
366 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
368 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
369 new_list->item = item;
370 new_list->next = *list_p;
371 *list_p = new_list;
372 return new_list;
375 unsigned commit_list_count(const struct commit_list *l)
377 unsigned c = 0;
378 for (; l; l = l->next )
379 c++;
380 return c;
383 struct commit_list *copy_commit_list(struct commit_list *list)
385 struct commit_list *head = NULL;
386 struct commit_list **pp = &head;
387 while (list) {
388 struct commit_list *new;
389 new = xmalloc(sizeof(struct commit_list));
390 new->item = list->item;
391 new->next = NULL;
392 *pp = new;
393 pp = &new->next;
394 list = list->next;
396 return head;
399 void free_commit_list(struct commit_list *list)
401 while (list) {
402 struct commit_list *temp = list;
403 list = temp->next;
404 free(temp);
408 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
410 struct commit_list **pp = list;
411 struct commit_list *p;
412 while ((p = *pp) != NULL) {
413 if (p->item->date < item->date) {
414 break;
416 pp = &p->next;
418 return commit_list_insert(item, pp);
421 static int commit_list_compare_by_date(const void *a, const void *b)
423 unsigned long a_date = ((const struct commit_list *)a)->item->date;
424 unsigned long b_date = ((const struct commit_list *)b)->item->date;
425 if (a_date < b_date)
426 return 1;
427 if (a_date > b_date)
428 return -1;
429 return 0;
432 static void *commit_list_get_next(const void *a)
434 return ((const struct commit_list *)a)->next;
437 static void commit_list_set_next(void *a, void *next)
439 ((struct commit_list *)a)->next = next;
442 void commit_list_sort_by_date(struct commit_list **list)
444 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
445 commit_list_compare_by_date);
448 struct commit *pop_most_recent_commit(struct commit_list **list,
449 unsigned int mark)
451 struct commit *ret = (*list)->item;
452 struct commit_list *parents = ret->parents;
453 struct commit_list *old = *list;
455 *list = (*list)->next;
456 free(old);
458 while (parents) {
459 struct commit *commit = parents->item;
460 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
461 commit->object.flags |= mark;
462 commit_list_insert_by_date(commit, list);
464 parents = parents->next;
466 return ret;
469 static void clear_commit_marks_1(struct commit_list **plist,
470 struct commit *commit, unsigned int mark)
472 while (commit) {
473 struct commit_list *parents;
475 if (!(mark & commit->object.flags))
476 return;
478 commit->object.flags &= ~mark;
480 parents = commit->parents;
481 if (!parents)
482 return;
484 while ((parents = parents->next))
485 commit_list_insert(parents->item, plist);
487 commit = commit->parents->item;
491 void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
493 struct commit_list *list = NULL;
495 while (nr--) {
496 commit_list_insert(*commit, &list);
497 commit++;
499 while (list)
500 clear_commit_marks_1(&list, pop_commit(&list), mark);
503 void clear_commit_marks(struct commit *commit, unsigned int mark)
505 clear_commit_marks_many(1, &commit, mark);
508 void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
510 struct object *object;
511 struct commit *commit;
512 unsigned int i;
514 for (i = 0; i < a->nr; i++) {
515 object = a->objects[i].item;
516 commit = lookup_commit_reference_gently(object->sha1, 1);
517 if (commit)
518 clear_commit_marks(commit, mark);
522 struct commit *pop_commit(struct commit_list **stack)
524 struct commit_list *top = *stack;
525 struct commit *item = top ? top->item : NULL;
527 if (top) {
528 *stack = top->next;
529 free(top);
531 return item;
535 * Topological sort support
538 /* count number of children that have not been emitted */
539 define_commit_slab(indegree_slab, int);
541 /* record author-date for each commit object */
542 define_commit_slab(author_date_slab, unsigned long);
544 static void record_author_date(struct author_date_slab *author_date,
545 struct commit *commit)
547 const char *buf, *line_end;
548 char *buffer = NULL;
549 struct ident_split ident;
550 char *date_end;
551 unsigned long date;
553 if (!commit->buffer) {
554 unsigned long size;
555 enum object_type type;
556 buffer = read_sha1_file(commit->object.sha1, &type, &size);
557 if (!buffer)
558 return;
561 for (buf = commit->buffer ? commit->buffer : buffer;
562 buf;
563 buf = line_end + 1) {
564 line_end = strchrnul(buf, '\n');
565 if (!starts_with(buf, "author ")) {
566 if (!line_end[0] || line_end[1] == '\n')
567 return; /* end of header */
568 continue;
570 if (split_ident_line(&ident,
571 buf + strlen("author "),
572 line_end - (buf + strlen("author "))) ||
573 !ident.date_begin || !ident.date_end)
574 goto fail_exit; /* malformed "author" line */
575 break;
578 date = strtoul(ident.date_begin, &date_end, 10);
579 if (date_end != ident.date_end)
580 goto fail_exit; /* malformed date */
581 *(author_date_slab_at(author_date, commit)) = date;
583 fail_exit:
584 free(buffer);
587 static int compare_commits_by_author_date(const void *a_, const void *b_,
588 void *cb_data)
590 const struct commit *a = a_, *b = b_;
591 struct author_date_slab *author_date = cb_data;
592 unsigned long a_date = *(author_date_slab_at(author_date, a));
593 unsigned long b_date = *(author_date_slab_at(author_date, b));
595 /* newer commits with larger date first */
596 if (a_date < b_date)
597 return 1;
598 else if (a_date > b_date)
599 return -1;
600 return 0;
603 int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
605 const struct commit *a = a_, *b = b_;
606 /* newer commits with larger date first */
607 if (a->date < b->date)
608 return 1;
609 else if (a->date > b->date)
610 return -1;
611 return 0;
615 * Performs an in-place topological sort on the list supplied.
617 void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
619 struct commit_list *next, *orig = *list;
620 struct commit_list **pptr;
621 struct indegree_slab indegree;
622 struct prio_queue queue;
623 struct commit *commit;
624 struct author_date_slab author_date;
626 if (!orig)
627 return;
628 *list = NULL;
630 init_indegree_slab(&indegree);
631 memset(&queue, '\0', sizeof(queue));
633 switch (sort_order) {
634 default: /* REV_SORT_IN_GRAPH_ORDER */
635 queue.compare = NULL;
636 break;
637 case REV_SORT_BY_COMMIT_DATE:
638 queue.compare = compare_commits_by_commit_date;
639 break;
640 case REV_SORT_BY_AUTHOR_DATE:
641 init_author_date_slab(&author_date);
642 queue.compare = compare_commits_by_author_date;
643 queue.cb_data = &author_date;
644 break;
647 /* Mark them and clear the indegree */
648 for (next = orig; next; next = next->next) {
649 struct commit *commit = next->item;
650 *(indegree_slab_at(&indegree, commit)) = 1;
651 /* also record the author dates, if needed */
652 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
653 record_author_date(&author_date, commit);
656 /* update the indegree */
657 for (next = orig; next; next = next->next) {
658 struct commit_list *parents = next->item->parents;
659 while (parents) {
660 struct commit *parent = parents->item;
661 int *pi = indegree_slab_at(&indegree, parent);
663 if (*pi)
664 (*pi)++;
665 parents = parents->next;
670 * find the tips
672 * tips are nodes not reachable from any other node in the list
674 * the tips serve as a starting set for the work queue.
676 for (next = orig; next; next = next->next) {
677 struct commit *commit = next->item;
679 if (*(indegree_slab_at(&indegree, commit)) == 1)
680 prio_queue_put(&queue, commit);
684 * This is unfortunate; the initial tips need to be shown
685 * in the order given from the revision traversal machinery.
687 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
688 prio_queue_reverse(&queue);
690 /* We no longer need the commit list */
691 free_commit_list(orig);
693 pptr = list;
694 *list = NULL;
695 while ((commit = prio_queue_get(&queue)) != NULL) {
696 struct commit_list *parents;
698 for (parents = commit->parents; parents ; parents = parents->next) {
699 struct commit *parent = parents->item;
700 int *pi = indegree_slab_at(&indegree, parent);
702 if (!*pi)
703 continue;
706 * parents are only enqueued for emission
707 * when all their children have been emitted thereby
708 * guaranteeing topological order.
710 if (--(*pi) == 1)
711 prio_queue_put(&queue, parent);
714 * all children of commit have already been
715 * emitted. we can emit it now.
717 *(indegree_slab_at(&indegree, commit)) = 0;
719 pptr = &commit_list_insert(commit, pptr)->next;
722 clear_indegree_slab(&indegree);
723 clear_prio_queue(&queue);
724 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
725 clear_author_date_slab(&author_date);
728 /* merge-base stuff */
730 /* bits #0..15 in revision.h */
731 #define PARENT1 (1u<<16)
732 #define PARENT2 (1u<<17)
733 #define STALE (1u<<18)
734 #define RESULT (1u<<19)
736 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
738 static struct commit *interesting(struct commit_list *list)
740 while (list) {
741 struct commit *commit = list->item;
742 list = list->next;
743 if (commit->object.flags & STALE)
744 continue;
745 return commit;
747 return NULL;
750 /* all input commits in one and twos[] must have been parsed! */
751 static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
753 struct commit_list *list = NULL;
754 struct commit_list *result = NULL;
755 int i;
757 one->object.flags |= PARENT1;
758 commit_list_insert_by_date(one, &list);
759 if (!n)
760 return list;
761 for (i = 0; i < n; i++) {
762 twos[i]->object.flags |= PARENT2;
763 commit_list_insert_by_date(twos[i], &list);
766 while (interesting(list)) {
767 struct commit *commit;
768 struct commit_list *parents;
769 struct commit_list *next;
770 int flags;
772 commit = list->item;
773 next = list->next;
774 free(list);
775 list = next;
777 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
778 if (flags == (PARENT1 | PARENT2)) {
779 if (!(commit->object.flags & RESULT)) {
780 commit->object.flags |= RESULT;
781 commit_list_insert_by_date(commit, &result);
783 /* Mark parents of a found merge stale */
784 flags |= STALE;
786 parents = commit->parents;
787 while (parents) {
788 struct commit *p = parents->item;
789 parents = parents->next;
790 if ((p->object.flags & flags) == flags)
791 continue;
792 if (parse_commit(p))
793 return NULL;
794 p->object.flags |= flags;
795 commit_list_insert_by_date(p, &list);
799 free_commit_list(list);
800 return result;
803 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
805 struct commit_list *list = NULL;
806 struct commit_list *result = NULL;
807 int i;
809 for (i = 0; i < n; i++) {
810 if (one == twos[i])
812 * We do not mark this even with RESULT so we do not
813 * have to clean it up.
815 return commit_list_insert(one, &result);
818 if (parse_commit(one))
819 return NULL;
820 for (i = 0; i < n; i++) {
821 if (parse_commit(twos[i]))
822 return NULL;
825 list = paint_down_to_common(one, n, twos);
827 while (list) {
828 struct commit_list *next = list->next;
829 if (!(list->item->object.flags & STALE))
830 commit_list_insert_by_date(list->item, &result);
831 free(list);
832 list = next;
834 return result;
837 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
839 struct commit_list *i, *j, *k, *ret = NULL;
841 if (!in)
842 return ret;
844 commit_list_insert(in->item, &ret);
846 for (i = in->next; i; i = i->next) {
847 struct commit_list *new = NULL, *end = NULL;
849 for (j = ret; j; j = j->next) {
850 struct commit_list *bases;
851 bases = get_merge_bases(i->item, j->item, 1);
852 if (!new)
853 new = bases;
854 else
855 end->next = bases;
856 for (k = bases; k; k = k->next)
857 end = k;
859 ret = new;
861 return ret;
864 static int remove_redundant(struct commit **array, int cnt)
867 * Some commit in the array may be an ancestor of
868 * another commit. Move such commit to the end of
869 * the array, and return the number of commits that
870 * are independent from each other.
872 struct commit **work;
873 unsigned char *redundant;
874 int *filled_index;
875 int i, j, filled;
877 work = xcalloc(cnt, sizeof(*work));
878 redundant = xcalloc(cnt, 1);
879 filled_index = xmalloc(sizeof(*filled_index) * (cnt - 1));
881 for (i = 0; i < cnt; i++)
882 parse_commit(array[i]);
883 for (i = 0; i < cnt; i++) {
884 struct commit_list *common;
886 if (redundant[i])
887 continue;
888 for (j = filled = 0; j < cnt; j++) {
889 if (i == j || redundant[j])
890 continue;
891 filled_index[filled] = j;
892 work[filled++] = array[j];
894 common = paint_down_to_common(array[i], filled, work);
895 if (array[i]->object.flags & PARENT2)
896 redundant[i] = 1;
897 for (j = 0; j < filled; j++)
898 if (work[j]->object.flags & PARENT1)
899 redundant[filled_index[j]] = 1;
900 clear_commit_marks(array[i], all_flags);
901 for (j = 0; j < filled; j++)
902 clear_commit_marks(work[j], all_flags);
903 free_commit_list(common);
906 /* Now collect the result */
907 memcpy(work, array, sizeof(*array) * cnt);
908 for (i = filled = 0; i < cnt; i++)
909 if (!redundant[i])
910 array[filled++] = work[i];
911 for (j = filled, i = 0; i < cnt; i++)
912 if (redundant[i])
913 array[j++] = work[i];
914 free(work);
915 free(redundant);
916 free(filled_index);
917 return filled;
920 struct commit_list *get_merge_bases_many(struct commit *one,
921 int n,
922 struct commit **twos,
923 int cleanup)
925 struct commit_list *list;
926 struct commit **rslt;
927 struct commit_list *result;
928 int cnt, i;
930 result = merge_bases_many(one, n, twos);
931 for (i = 0; i < n; i++) {
932 if (one == twos[i])
933 return result;
935 if (!result || !result->next) {
936 if (cleanup) {
937 clear_commit_marks(one, all_flags);
938 clear_commit_marks_many(n, twos, all_flags);
940 return result;
943 /* There are more than one */
944 cnt = 0;
945 list = result;
946 while (list) {
947 list = list->next;
948 cnt++;
950 rslt = xcalloc(cnt, sizeof(*rslt));
951 for (list = result, i = 0; list; list = list->next)
952 rslt[i++] = list->item;
953 free_commit_list(result);
955 clear_commit_marks(one, all_flags);
956 clear_commit_marks_many(n, twos, all_flags);
958 cnt = remove_redundant(rslt, cnt);
959 result = NULL;
960 for (i = 0; i < cnt; i++)
961 commit_list_insert_by_date(rslt[i], &result);
962 free(rslt);
963 return result;
966 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
967 int cleanup)
969 return get_merge_bases_many(one, 1, &two, cleanup);
973 * Is "commit" a descendant of one of the elements on the "with_commit" list?
975 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
977 if (!with_commit)
978 return 1;
979 while (with_commit) {
980 struct commit *other;
982 other = with_commit->item;
983 with_commit = with_commit->next;
984 if (in_merge_bases(other, commit))
985 return 1;
987 return 0;
991 * Is "commit" an ancestor of one of the "references"?
993 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
995 struct commit_list *bases;
996 int ret = 0, i;
998 if (parse_commit(commit))
999 return ret;
1000 for (i = 0; i < nr_reference; i++)
1001 if (parse_commit(reference[i]))
1002 return ret;
1004 bases = paint_down_to_common(commit, nr_reference, reference);
1005 if (commit->object.flags & PARENT2)
1006 ret = 1;
1007 clear_commit_marks(commit, all_flags);
1008 clear_commit_marks_many(nr_reference, reference, all_flags);
1009 free_commit_list(bases);
1010 return ret;
1014 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1016 int in_merge_bases(struct commit *commit, struct commit *reference)
1018 return in_merge_bases_many(commit, 1, &reference);
1021 struct commit_list *reduce_heads(struct commit_list *heads)
1023 struct commit_list *p;
1024 struct commit_list *result = NULL, **tail = &result;
1025 struct commit **array;
1026 int num_head, i;
1028 if (!heads)
1029 return NULL;
1031 /* Uniquify */
1032 for (p = heads; p; p = p->next)
1033 p->item->object.flags &= ~STALE;
1034 for (p = heads, num_head = 0; p; p = p->next) {
1035 if (p->item->object.flags & STALE)
1036 continue;
1037 p->item->object.flags |= STALE;
1038 num_head++;
1040 array = xcalloc(sizeof(*array), num_head);
1041 for (p = heads, i = 0; p; p = p->next) {
1042 if (p->item->object.flags & STALE) {
1043 array[i++] = p->item;
1044 p->item->object.flags &= ~STALE;
1047 num_head = remove_redundant(array, num_head);
1048 for (i = 0; i < num_head; i++)
1049 tail = &commit_list_insert(array[i], tail)->next;
1050 return result;
1053 static const char gpg_sig_header[] = "gpgsig";
1054 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
1056 static int do_sign_commit(struct strbuf *buf, const char *keyid)
1058 struct strbuf sig = STRBUF_INIT;
1059 int inspos, copypos;
1061 /* find the end of the header */
1062 inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
1064 if (!keyid || !*keyid)
1065 keyid = get_signing_key();
1066 if (sign_buffer(buf, &sig, keyid)) {
1067 strbuf_release(&sig);
1068 return -1;
1071 for (copypos = 0; sig.buf[copypos]; ) {
1072 const char *bol = sig.buf + copypos;
1073 const char *eol = strchrnul(bol, '\n');
1074 int len = (eol - bol) + !!*eol;
1076 if (!copypos) {
1077 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1078 inspos += gpg_sig_header_len;
1080 strbuf_insert(buf, inspos++, " ", 1);
1081 strbuf_insert(buf, inspos, bol, len);
1082 inspos += len;
1083 copypos += len;
1085 strbuf_release(&sig);
1086 return 0;
1089 int parse_signed_commit(const unsigned char *sha1,
1090 struct strbuf *payload, struct strbuf *signature)
1092 unsigned long size;
1093 enum object_type type;
1094 char *buffer = read_sha1_file(sha1, &type, &size);
1095 int in_signature, saw_signature = -1;
1096 char *line, *tail;
1098 if (!buffer || type != OBJ_COMMIT)
1099 goto cleanup;
1101 line = buffer;
1102 tail = buffer + size;
1103 in_signature = 0;
1104 saw_signature = 0;
1105 while (line < tail) {
1106 const char *sig = NULL;
1107 char *next = memchr(line, '\n', tail - line);
1109 next = next ? next + 1 : tail;
1110 if (in_signature && line[0] == ' ')
1111 sig = line + 1;
1112 else if (starts_with(line, gpg_sig_header) &&
1113 line[gpg_sig_header_len] == ' ')
1114 sig = line + gpg_sig_header_len + 1;
1115 if (sig) {
1116 strbuf_add(signature, sig, next - sig);
1117 saw_signature = 1;
1118 in_signature = 1;
1119 } else {
1120 if (*line == '\n')
1121 /* dump the whole remainder of the buffer */
1122 next = tail;
1123 strbuf_add(payload, line, next - line);
1124 in_signature = 0;
1126 line = next;
1128 cleanup:
1129 free(buffer);
1130 return saw_signature;
1133 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1135 struct merge_remote_desc *desc;
1136 struct commit_extra_header *mergetag;
1137 char *buf;
1138 unsigned long size, len;
1139 enum object_type type;
1141 desc = merge_remote_util(parent);
1142 if (!desc || !desc->obj)
1143 return;
1144 buf = read_sha1_file(desc->obj->sha1, &type, &size);
1145 if (!buf || type != OBJ_TAG)
1146 goto free_return;
1147 len = parse_signature(buf, size);
1148 if (size == len)
1149 goto free_return;
1151 * We could verify this signature and either omit the tag when
1152 * it does not validate, but the integrator may not have the
1153 * public key of the signer of the tag he is merging, while a
1154 * later auditor may have it while auditing, so let's not run
1155 * verify-signed-buffer here for now...
1157 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1158 * warn("warning: signed tag unverified.");
1160 mergetag = xcalloc(1, sizeof(*mergetag));
1161 mergetag->key = xstrdup("mergetag");
1162 mergetag->value = buf;
1163 mergetag->len = size;
1165 **tail = mergetag;
1166 *tail = &mergetag->next;
1167 return;
1169 free_return:
1170 free(buf);
1173 static struct {
1174 char result;
1175 const char *check;
1176 } sigcheck_gpg_status[] = {
1177 { 'G', "\n[GNUPG:] GOODSIG " },
1178 { 'B', "\n[GNUPG:] BADSIG " },
1179 { 'U', "\n[GNUPG:] TRUST_NEVER" },
1180 { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
1183 static void parse_gpg_output(struct signature_check *sigc)
1185 const char *buf = sigc->gpg_status;
1186 int i;
1188 /* Iterate over all search strings */
1189 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
1190 const char *found, *next;
1192 if (starts_with(buf, sigcheck_gpg_status[i].check + 1)) {
1193 /* At the very beginning of the buffer */
1194 found = buf + strlen(sigcheck_gpg_status[i].check + 1);
1195 } else {
1196 found = strstr(buf, sigcheck_gpg_status[i].check);
1197 if (!found)
1198 continue;
1199 found += strlen(sigcheck_gpg_status[i].check);
1201 sigc->result = sigcheck_gpg_status[i].result;
1202 /* The trust messages are not followed by key/signer information */
1203 if (sigc->result != 'U') {
1204 sigc->key = xmemdupz(found, 16);
1205 found += 17;
1206 next = strchrnul(found, '\n');
1207 sigc->signer = xmemdupz(found, next - found);
1212 void check_commit_signature(const struct commit* commit, struct signature_check *sigc)
1214 struct strbuf payload = STRBUF_INIT;
1215 struct strbuf signature = STRBUF_INIT;
1216 struct strbuf gpg_output = STRBUF_INIT;
1217 struct strbuf gpg_status = STRBUF_INIT;
1218 int status;
1220 sigc->result = 'N';
1222 if (parse_signed_commit(commit->object.sha1,
1223 &payload, &signature) <= 0)
1224 goto out;
1225 status = verify_signed_buffer(payload.buf, payload.len,
1226 signature.buf, signature.len,
1227 &gpg_output, &gpg_status);
1228 if (status && !gpg_output.len)
1229 goto out;
1230 sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
1231 sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
1232 parse_gpg_output(sigc);
1234 out:
1235 strbuf_release(&gpg_status);
1236 strbuf_release(&gpg_output);
1237 strbuf_release(&payload);
1238 strbuf_release(&signature);
1243 void append_merge_tag_headers(struct commit_list *parents,
1244 struct commit_extra_header ***tail)
1246 while (parents) {
1247 struct commit *parent = parents->item;
1248 handle_signed_tag(parent, tail);
1249 parents = parents->next;
1253 static void add_extra_header(struct strbuf *buffer,
1254 struct commit_extra_header *extra)
1256 strbuf_addstr(buffer, extra->key);
1257 if (extra->len)
1258 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1259 else
1260 strbuf_addch(buffer, '\n');
1263 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1264 const char **exclude)
1266 struct commit_extra_header *extra = NULL;
1267 unsigned long size;
1268 enum object_type type;
1269 char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
1270 if (buffer && type == OBJ_COMMIT)
1271 extra = read_commit_extra_header_lines(buffer, size, exclude);
1272 free(buffer);
1273 return extra;
1276 static inline int standard_header_field(const char *field, size_t len)
1278 return ((len == 4 && !memcmp(field, "tree ", 5)) ||
1279 (len == 6 && !memcmp(field, "parent ", 7)) ||
1280 (len == 6 && !memcmp(field, "author ", 7)) ||
1281 (len == 9 && !memcmp(field, "committer ", 10)) ||
1282 (len == 8 && !memcmp(field, "encoding ", 9)));
1285 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1287 if (!exclude)
1288 return 0;
1290 while (*exclude) {
1291 size_t xlen = strlen(*exclude);
1292 if (len == xlen &&
1293 !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
1294 return 1;
1295 exclude++;
1297 return 0;
1300 static struct commit_extra_header *read_commit_extra_header_lines(
1301 const char *buffer, size_t size,
1302 const char **exclude)
1304 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1305 const char *line, *next, *eof, *eob;
1306 struct strbuf buf = STRBUF_INIT;
1308 for (line = buffer, eob = line + size;
1309 line < eob && *line != '\n';
1310 line = next) {
1311 next = memchr(line, '\n', eob - line);
1312 next = next ? next + 1 : eob;
1313 if (*line == ' ') {
1314 /* continuation */
1315 if (it)
1316 strbuf_add(&buf, line + 1, next - (line + 1));
1317 continue;
1319 if (it)
1320 it->value = strbuf_detach(&buf, &it->len);
1321 strbuf_reset(&buf);
1322 it = NULL;
1324 eof = strchr(line, ' ');
1325 if (next <= eof)
1326 eof = next;
1328 if (standard_header_field(line, eof - line) ||
1329 excluded_header_field(line, eof - line, exclude))
1330 continue;
1332 it = xcalloc(1, sizeof(*it));
1333 it->key = xmemdupz(line, eof-line);
1334 *tail = it;
1335 tail = &it->next;
1336 if (eof + 1 < next)
1337 strbuf_add(&buf, eof + 1, next - (eof + 1));
1339 if (it)
1340 it->value = strbuf_detach(&buf, &it->len);
1341 return extra;
1344 void free_commit_extra_headers(struct commit_extra_header *extra)
1346 while (extra) {
1347 struct commit_extra_header *next = extra->next;
1348 free(extra->key);
1349 free(extra->value);
1350 free(extra);
1351 extra = next;
1355 int commit_tree(const struct strbuf *msg, const unsigned char *tree,
1356 struct commit_list *parents, unsigned char *ret,
1357 const char *author, const char *sign_commit)
1359 struct commit_extra_header *extra = NULL, **tail = &extra;
1360 int result;
1362 append_merge_tag_headers(parents, &tail);
1363 result = commit_tree_extended(msg, tree, parents, ret,
1364 author, sign_commit, extra);
1365 free_commit_extra_headers(extra);
1366 return result;
1369 static int find_invalid_utf8(const char *buf, int len)
1371 int offset = 0;
1372 static const unsigned int max_codepoint[] = {
1373 0x7f, 0x7ff, 0xffff, 0x10ffff
1376 while (len) {
1377 unsigned char c = *buf++;
1378 int bytes, bad_offset;
1379 unsigned int codepoint;
1380 unsigned int min_val, max_val;
1382 len--;
1383 offset++;
1385 /* Simple US-ASCII? No worries. */
1386 if (c < 0x80)
1387 continue;
1389 bad_offset = offset-1;
1392 * Count how many more high bits set: that's how
1393 * many more bytes this sequence should have.
1395 bytes = 0;
1396 while (c & 0x40) {
1397 c <<= 1;
1398 bytes++;
1402 * Must be between 1 and 3 more bytes. Longer sequences result in
1403 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1405 if (bytes < 1 || 3 < bytes)
1406 return bad_offset;
1408 /* Do we *have* that many bytes? */
1409 if (len < bytes)
1410 return bad_offset;
1413 * Place the encoded bits at the bottom of the value and compute the
1414 * valid range.
1416 codepoint = (c & 0x7f) >> bytes;
1417 min_val = max_codepoint[bytes-1] + 1;
1418 max_val = max_codepoint[bytes];
1420 offset += bytes;
1421 len -= bytes;
1423 /* And verify that they are good continuation bytes */
1424 do {
1425 codepoint <<= 6;
1426 codepoint |= *buf & 0x3f;
1427 if ((*buf++ & 0xc0) != 0x80)
1428 return bad_offset;
1429 } while (--bytes);
1431 /* Reject codepoints that are out of range for the sequence length. */
1432 if (codepoint < min_val || codepoint > max_val)
1433 return bad_offset;
1434 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1435 if ((codepoint & 0x1ff800) == 0xd800)
1436 return bad_offset;
1437 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1438 if ((codepoint & 0xfffe) == 0xfffe)
1439 return bad_offset;
1440 /* So are anything in the range U+FDD0..U+FDEF. */
1441 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
1442 return bad_offset;
1444 return -1;
1448 * This verifies that the buffer is in proper utf8 format.
1450 * If it isn't, it assumes any non-utf8 characters are Latin1,
1451 * and does the conversion.
1453 static int verify_utf8(struct strbuf *buf)
1455 int ok = 1;
1456 long pos = 0;
1458 for (;;) {
1459 int bad;
1460 unsigned char c;
1461 unsigned char replace[2];
1463 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1464 if (bad < 0)
1465 return ok;
1466 pos += bad;
1467 ok = 0;
1468 c = buf->buf[pos];
1469 strbuf_remove(buf, pos, 1);
1471 /* We know 'c' must be in the range 128-255 */
1472 replace[0] = 0xc0 + (c >> 6);
1473 replace[1] = 0x80 + (c & 0x3f);
1474 strbuf_insert(buf, pos, replace, 2);
1475 pos += 2;
1479 static const char commit_utf8_warn[] =
1480 "Warning: commit message did not conform to UTF-8.\n"
1481 "You may want to amend it after fixing the message, or set the config\n"
1482 "variable i18n.commitencoding to the encoding your project uses.\n";
1484 int commit_tree_extended(const struct strbuf *msg, const unsigned char *tree,
1485 struct commit_list *parents, unsigned char *ret,
1486 const char *author, const char *sign_commit,
1487 struct commit_extra_header *extra)
1489 int result;
1490 int encoding_is_utf8;
1491 struct strbuf buffer;
1493 assert_sha1_type(tree, OBJ_TREE);
1495 if (memchr(msg->buf, '\0', msg->len))
1496 return error("a NUL byte in commit log message not allowed.");
1498 /* Not having i18n.commitencoding is the same as having utf-8 */
1499 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1501 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1502 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1505 * NOTE! This ordering means that the same exact tree merged with a
1506 * different order of parents will be a _different_ changeset even
1507 * if everything else stays the same.
1509 while (parents) {
1510 struct commit_list *next = parents->next;
1511 struct commit *parent = parents->item;
1513 strbuf_addf(&buffer, "parent %s\n",
1514 sha1_to_hex(parent->object.sha1));
1515 free(parents);
1516 parents = next;
1519 /* Person/date information */
1520 if (!author)
1521 author = git_author_info(IDENT_STRICT);
1522 strbuf_addf(&buffer, "author %s\n", author);
1523 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1524 if (!encoding_is_utf8)
1525 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1527 while (extra) {
1528 add_extra_header(&buffer, extra);
1529 extra = extra->next;
1531 strbuf_addch(&buffer, '\n');
1533 /* And add the comment */
1534 strbuf_addbuf(&buffer, msg);
1536 /* And check the encoding */
1537 if (encoding_is_utf8 && !verify_utf8(&buffer))
1538 fprintf(stderr, commit_utf8_warn);
1540 if (sign_commit && do_sign_commit(&buffer, sign_commit))
1541 return -1;
1543 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1544 strbuf_release(&buffer);
1545 return result;
1548 struct commit *get_merge_parent(const char *name)
1550 struct object *obj;
1551 struct commit *commit;
1552 unsigned char sha1[20];
1553 if (get_sha1(name, sha1))
1554 return NULL;
1555 obj = parse_object(sha1);
1556 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1557 if (commit && !commit->util) {
1558 struct merge_remote_desc *desc;
1559 desc = xmalloc(sizeof(*desc));
1560 desc->obj = obj;
1561 desc->name = strdup(name);
1562 commit->util = desc;
1564 return commit;
1568 * Append a commit to the end of the commit_list.
1570 * next starts by pointing to the variable that holds the head of an
1571 * empty commit_list, and is updated to point to the "next" field of
1572 * the last item on the list as new commits are appended.
1574 * Usage example:
1576 * struct commit_list *list;
1577 * struct commit_list **next = &list;
1579 * next = commit_list_append(c1, next);
1580 * next = commit_list_append(c2, next);
1581 * assert(commit_list_count(list) == 2);
1582 * return list;
1584 struct commit_list **commit_list_append(struct commit *commit,
1585 struct commit_list **next)
1587 struct commit_list *new = xmalloc(sizeof(struct commit_list));
1588 new->item = commit;
1589 *next = new;
1590 new->next = NULL;
1591 return &new->next;
1594 void print_commit_list(struct commit_list *list,
1595 const char *format_cur,
1596 const char *format_last)
1598 for ( ; list; list = list->next) {
1599 const char *format = list->next ? format_cur : format_last;
1600 printf(format, sha1_to_hex(list->item->object.sha1));