commit.c: use the generic "sha1_pos" function for lookup
[git.git] / commit.c
blob6ceee6acbc0a6c3c44d38e91c9f138a89fb3d075
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"
13 #include "sha1-lookup.h"
15 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
17 int save_commit_buffer = 1;
19 const char *commit_type = "commit";
20 static int commit_count;
22 static struct commit *check_commit(struct object *obj,
23 const unsigned char *sha1,
24 int quiet)
26 if (obj->type != OBJ_COMMIT) {
27 if (!quiet)
28 error("Object %s is a %s, not a commit",
29 sha1_to_hex(sha1), typename(obj->type));
30 return NULL;
32 return (struct commit *) obj;
35 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
36 int quiet)
38 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
40 if (!obj)
41 return NULL;
42 return check_commit(obj, sha1, quiet);
45 struct commit *lookup_commit_reference(const unsigned char *sha1)
47 return lookup_commit_reference_gently(sha1, 0);
50 struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name)
52 struct commit *c = lookup_commit_reference(sha1);
53 if (!c)
54 die(_("could not parse %s"), ref_name);
55 if (hashcmp(sha1, c->object.sha1)) {
56 warning(_("%s %s is not a commit!"),
57 ref_name, sha1_to_hex(sha1));
59 return c;
62 struct commit *lookup_commit(const unsigned char *sha1)
64 struct object *obj = lookup_object(sha1);
65 if (!obj) {
66 struct commit *c = alloc_commit_node();
67 c->index = commit_count++;
68 return create_object(sha1, OBJ_COMMIT, c);
70 if (!obj->type)
71 obj->type = OBJ_COMMIT;
72 return check_commit(obj, sha1, 0);
75 struct commit *lookup_commit_reference_by_name(const char *name)
77 unsigned char sha1[20];
78 struct commit *commit;
80 if (get_sha1_committish(name, sha1))
81 return NULL;
82 commit = lookup_commit_reference(sha1);
83 if (parse_commit(commit))
84 return NULL;
85 return commit;
88 static unsigned long parse_commit_date(const char *buf, const char *tail)
90 const char *dateptr;
92 if (buf + 6 >= tail)
93 return 0;
94 if (memcmp(buf, "author", 6))
95 return 0;
96 while (buf < tail && *buf++ != '\n')
97 /* nada */;
98 if (buf + 9 >= tail)
99 return 0;
100 if (memcmp(buf, "committer", 9))
101 return 0;
102 while (buf < tail && *buf++ != '>')
103 /* nada */;
104 if (buf >= tail)
105 return 0;
106 dateptr = buf;
107 while (buf < tail && *buf++ != '\n')
108 /* nada */;
109 if (buf >= tail)
110 return 0;
111 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
112 return strtoul(dateptr, NULL, 10);
115 static struct commit_graft **commit_graft;
116 static int commit_graft_alloc, commit_graft_nr;
118 static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
120 struct commit_graft **commit_graft_table = table;
121 return commit_graft_table[index]->sha1;
124 static int commit_graft_pos(const unsigned char *sha1)
126 return sha1_pos(sha1, commit_graft, commit_graft_nr,
127 commit_graft_sha1_access);
130 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
132 int pos = commit_graft_pos(graft->sha1);
134 if (0 <= pos) {
135 if (ignore_dups)
136 free(graft);
137 else {
138 free(commit_graft[pos]);
139 commit_graft[pos] = graft;
141 return 1;
143 pos = -pos - 1;
144 if (commit_graft_alloc <= ++commit_graft_nr) {
145 commit_graft_alloc = alloc_nr(commit_graft_alloc);
146 commit_graft = xrealloc(commit_graft,
147 sizeof(*commit_graft) *
148 commit_graft_alloc);
150 if (pos < commit_graft_nr)
151 memmove(commit_graft + pos + 1,
152 commit_graft + pos,
153 (commit_graft_nr - pos - 1) *
154 sizeof(*commit_graft));
155 commit_graft[pos] = graft;
156 return 0;
159 struct commit_graft *read_graft_line(char *buf, int len)
161 /* The format is just "Commit Parent1 Parent2 ...\n" */
162 int i;
163 struct commit_graft *graft = NULL;
165 while (len && isspace(buf[len-1]))
166 buf[--len] = '\0';
167 if (buf[0] == '#' || buf[0] == '\0')
168 return NULL;
169 if ((len + 1) % 41)
170 goto bad_graft_data;
171 i = (len + 1) / 41 - 1;
172 graft = xmalloc(sizeof(*graft) + 20 * i);
173 graft->nr_parent = i;
174 if (get_sha1_hex(buf, graft->sha1))
175 goto bad_graft_data;
176 for (i = 40; i < len; i += 41) {
177 if (buf[i] != ' ')
178 goto bad_graft_data;
179 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
180 goto bad_graft_data;
182 return graft;
184 bad_graft_data:
185 error("bad graft data: %s", buf);
186 free(graft);
187 return NULL;
190 static int read_graft_file(const char *graft_file)
192 FILE *fp = fopen(graft_file, "r");
193 struct strbuf buf = STRBUF_INIT;
194 if (!fp)
195 return -1;
196 while (!strbuf_getwholeline(&buf, fp, '\n')) {
197 /* The format is just "Commit Parent1 Parent2 ...\n" */
198 struct commit_graft *graft = read_graft_line(buf.buf, buf.len);
199 if (!graft)
200 continue;
201 if (register_commit_graft(graft, 1))
202 error("duplicate graft data: %s", buf.buf);
204 fclose(fp);
205 strbuf_release(&buf);
206 return 0;
209 static void prepare_commit_graft(void)
211 static int commit_graft_prepared;
212 char *graft_file;
214 if (commit_graft_prepared)
215 return;
216 graft_file = get_graft_file();
217 read_graft_file(graft_file);
218 /* make sure shallows are read */
219 is_repository_shallow();
220 commit_graft_prepared = 1;
223 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
225 int pos;
226 prepare_commit_graft();
227 pos = commit_graft_pos(sha1);
228 if (pos < 0)
229 return NULL;
230 return commit_graft[pos];
233 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
235 int i, ret;
236 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
237 ret = fn(commit_graft[i], cb_data);
238 return ret;
241 int unregister_shallow(const unsigned char *sha1)
243 int pos = commit_graft_pos(sha1);
244 if (pos < 0)
245 return -1;
246 if (pos + 1 < commit_graft_nr)
247 memmove(commit_graft + pos, commit_graft + pos + 1,
248 sizeof(struct commit_graft *)
249 * (commit_graft_nr - pos - 1));
250 commit_graft_nr--;
251 return 0;
254 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
256 const char *tail = buffer;
257 const char *bufptr = buffer;
258 unsigned char parent[20];
259 struct commit_list **pptr;
260 struct commit_graft *graft;
262 if (item->object.parsed)
263 return 0;
264 item->object.parsed = 1;
265 tail += size;
266 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
267 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
268 if (get_sha1_hex(bufptr + 5, parent) < 0)
269 return error("bad tree pointer in commit %s",
270 sha1_to_hex(item->object.sha1));
271 item->tree = lookup_tree(parent);
272 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
273 pptr = &item->parents;
275 graft = lookup_commit_graft(item->object.sha1);
276 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
277 struct commit *new_parent;
279 if (tail <= bufptr + 48 ||
280 get_sha1_hex(bufptr + 7, parent) ||
281 bufptr[47] != '\n')
282 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
283 bufptr += 48;
285 * The clone is shallow if nr_parent < 0, and we must
286 * not traverse its real parents even when we unhide them.
288 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
289 continue;
290 new_parent = lookup_commit(parent);
291 if (new_parent)
292 pptr = &commit_list_insert(new_parent, pptr)->next;
294 if (graft) {
295 int i;
296 struct commit *new_parent;
297 for (i = 0; i < graft->nr_parent; i++) {
298 new_parent = lookup_commit(graft->parent[i]);
299 if (!new_parent)
300 continue;
301 pptr = &commit_list_insert(new_parent, pptr)->next;
304 item->date = parse_commit_date(bufptr, tail);
306 return 0;
309 int parse_commit(struct commit *item)
311 enum object_type type;
312 void *buffer;
313 unsigned long size;
314 int ret;
316 if (!item)
317 return -1;
318 if (item->object.parsed)
319 return 0;
320 buffer = read_sha1_file(item->object.sha1, &type, &size);
321 if (!buffer)
322 return error("Could not read %s",
323 sha1_to_hex(item->object.sha1));
324 if (type != OBJ_COMMIT) {
325 free(buffer);
326 return error("Object %s not a commit",
327 sha1_to_hex(item->object.sha1));
329 ret = parse_commit_buffer(item, buffer, size);
330 if (save_commit_buffer && !ret) {
331 item->buffer = buffer;
332 return 0;
334 free(buffer);
335 return ret;
338 void parse_commit_or_die(struct commit *item)
340 if (parse_commit(item))
341 die("unable to parse commit %s",
342 item ? sha1_to_hex(item->object.sha1) : "(null)");
345 int find_commit_subject(const char *commit_buffer, const char **subject)
347 const char *eol;
348 const char *p = commit_buffer;
350 while (*p && (*p != '\n' || p[1] != '\n'))
351 p++;
352 if (*p) {
353 p += 2;
354 for (eol = p; *eol && *eol != '\n'; eol++)
355 ; /* do nothing */
356 } else
357 eol = p;
359 *subject = p;
361 return eol - p;
364 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
366 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
367 new_list->item = item;
368 new_list->next = *list_p;
369 *list_p = new_list;
370 return new_list;
373 unsigned commit_list_count(const struct commit_list *l)
375 unsigned c = 0;
376 for (; l; l = l->next )
377 c++;
378 return c;
381 struct commit_list *copy_commit_list(struct commit_list *list)
383 struct commit_list *head = NULL;
384 struct commit_list **pp = &head;
385 while (list) {
386 struct commit_list *new;
387 new = xmalloc(sizeof(struct commit_list));
388 new->item = list->item;
389 new->next = NULL;
390 *pp = new;
391 pp = &new->next;
392 list = list->next;
394 return head;
397 void free_commit_list(struct commit_list *list)
399 while (list) {
400 struct commit_list *temp = list;
401 list = temp->next;
402 free(temp);
406 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
408 struct commit_list **pp = list;
409 struct commit_list *p;
410 while ((p = *pp) != NULL) {
411 if (p->item->date < item->date) {
412 break;
414 pp = &p->next;
416 return commit_list_insert(item, pp);
419 static int commit_list_compare_by_date(const void *a, const void *b)
421 unsigned long a_date = ((const struct commit_list *)a)->item->date;
422 unsigned long b_date = ((const struct commit_list *)b)->item->date;
423 if (a_date < b_date)
424 return 1;
425 if (a_date > b_date)
426 return -1;
427 return 0;
430 static void *commit_list_get_next(const void *a)
432 return ((const struct commit_list *)a)->next;
435 static void commit_list_set_next(void *a, void *next)
437 ((struct commit_list *)a)->next = next;
440 void commit_list_sort_by_date(struct commit_list **list)
442 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
443 commit_list_compare_by_date);
446 struct commit *pop_most_recent_commit(struct commit_list **list,
447 unsigned int mark)
449 struct commit *ret = (*list)->item;
450 struct commit_list *parents = ret->parents;
451 struct commit_list *old = *list;
453 *list = (*list)->next;
454 free(old);
456 while (parents) {
457 struct commit *commit = parents->item;
458 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
459 commit->object.flags |= mark;
460 commit_list_insert_by_date(commit, list);
462 parents = parents->next;
464 return ret;
467 static void clear_commit_marks_1(struct commit_list **plist,
468 struct commit *commit, unsigned int mark)
470 while (commit) {
471 struct commit_list *parents;
473 if (!(mark & commit->object.flags))
474 return;
476 commit->object.flags &= ~mark;
478 parents = commit->parents;
479 if (!parents)
480 return;
482 while ((parents = parents->next))
483 commit_list_insert(parents->item, plist);
485 commit = commit->parents->item;
489 void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
491 struct commit_list *list = NULL;
493 while (nr--) {
494 commit_list_insert(*commit, &list);
495 commit++;
497 while (list)
498 clear_commit_marks_1(&list, pop_commit(&list), mark);
501 void clear_commit_marks(struct commit *commit, unsigned int mark)
503 clear_commit_marks_many(1, &commit, mark);
506 void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
508 struct object *object;
509 struct commit *commit;
510 unsigned int i;
512 for (i = 0; i < a->nr; i++) {
513 object = a->objects[i].item;
514 commit = lookup_commit_reference_gently(object->sha1, 1);
515 if (commit)
516 clear_commit_marks(commit, mark);
520 struct commit *pop_commit(struct commit_list **stack)
522 struct commit_list *top = *stack;
523 struct commit *item = top ? top->item : NULL;
525 if (top) {
526 *stack = top->next;
527 free(top);
529 return item;
533 * Topological sort support
536 /* count number of children that have not been emitted */
537 define_commit_slab(indegree_slab, int);
539 /* record author-date for each commit object */
540 define_commit_slab(author_date_slab, unsigned long);
542 static void record_author_date(struct author_date_slab *author_date,
543 struct commit *commit)
545 const char *buf, *line_end;
546 char *buffer = NULL;
547 struct ident_split ident;
548 char *date_end;
549 unsigned long date;
551 if (!commit->buffer) {
552 unsigned long size;
553 enum object_type type;
554 buffer = read_sha1_file(commit->object.sha1, &type, &size);
555 if (!buffer)
556 return;
559 for (buf = commit->buffer ? commit->buffer : buffer;
560 buf;
561 buf = line_end + 1) {
562 line_end = strchrnul(buf, '\n');
563 if (!starts_with(buf, "author ")) {
564 if (!line_end[0] || line_end[1] == '\n')
565 return; /* end of header */
566 continue;
568 if (split_ident_line(&ident,
569 buf + strlen("author "),
570 line_end - (buf + strlen("author "))) ||
571 !ident.date_begin || !ident.date_end)
572 goto fail_exit; /* malformed "author" line */
573 break;
576 date = strtoul(ident.date_begin, &date_end, 10);
577 if (date_end != ident.date_end)
578 goto fail_exit; /* malformed date */
579 *(author_date_slab_at(author_date, commit)) = date;
581 fail_exit:
582 free(buffer);
585 static int compare_commits_by_author_date(const void *a_, const void *b_,
586 void *cb_data)
588 const struct commit *a = a_, *b = b_;
589 struct author_date_slab *author_date = cb_data;
590 unsigned long a_date = *(author_date_slab_at(author_date, a));
591 unsigned long b_date = *(author_date_slab_at(author_date, b));
593 /* newer commits with larger date first */
594 if (a_date < b_date)
595 return 1;
596 else if (a_date > b_date)
597 return -1;
598 return 0;
601 int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
603 const struct commit *a = a_, *b = b_;
604 /* newer commits with larger date first */
605 if (a->date < b->date)
606 return 1;
607 else if (a->date > b->date)
608 return -1;
609 return 0;
613 * Performs an in-place topological sort on the list supplied.
615 void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
617 struct commit_list *next, *orig = *list;
618 struct commit_list **pptr;
619 struct indegree_slab indegree;
620 struct prio_queue queue;
621 struct commit *commit;
622 struct author_date_slab author_date;
624 if (!orig)
625 return;
626 *list = NULL;
628 init_indegree_slab(&indegree);
629 memset(&queue, '\0', sizeof(queue));
631 switch (sort_order) {
632 default: /* REV_SORT_IN_GRAPH_ORDER */
633 queue.compare = NULL;
634 break;
635 case REV_SORT_BY_COMMIT_DATE:
636 queue.compare = compare_commits_by_commit_date;
637 break;
638 case REV_SORT_BY_AUTHOR_DATE:
639 init_author_date_slab(&author_date);
640 queue.compare = compare_commits_by_author_date;
641 queue.cb_data = &author_date;
642 break;
645 /* Mark them and clear the indegree */
646 for (next = orig; next; next = next->next) {
647 struct commit *commit = next->item;
648 *(indegree_slab_at(&indegree, commit)) = 1;
649 /* also record the author dates, if needed */
650 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
651 record_author_date(&author_date, commit);
654 /* update the indegree */
655 for (next = orig; next; next = next->next) {
656 struct commit_list *parents = next->item->parents;
657 while (parents) {
658 struct commit *parent = parents->item;
659 int *pi = indegree_slab_at(&indegree, parent);
661 if (*pi)
662 (*pi)++;
663 parents = parents->next;
668 * find the tips
670 * tips are nodes not reachable from any other node in the list
672 * the tips serve as a starting set for the work queue.
674 for (next = orig; next; next = next->next) {
675 struct commit *commit = next->item;
677 if (*(indegree_slab_at(&indegree, commit)) == 1)
678 prio_queue_put(&queue, commit);
682 * This is unfortunate; the initial tips need to be shown
683 * in the order given from the revision traversal machinery.
685 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
686 prio_queue_reverse(&queue);
688 /* We no longer need the commit list */
689 free_commit_list(orig);
691 pptr = list;
692 *list = NULL;
693 while ((commit = prio_queue_get(&queue)) != NULL) {
694 struct commit_list *parents;
696 for (parents = commit->parents; parents ; parents = parents->next) {
697 struct commit *parent = parents->item;
698 int *pi = indegree_slab_at(&indegree, parent);
700 if (!*pi)
701 continue;
704 * parents are only enqueued for emission
705 * when all their children have been emitted thereby
706 * guaranteeing topological order.
708 if (--(*pi) == 1)
709 prio_queue_put(&queue, parent);
712 * all children of commit have already been
713 * emitted. we can emit it now.
715 *(indegree_slab_at(&indegree, commit)) = 0;
717 pptr = &commit_list_insert(commit, pptr)->next;
720 clear_indegree_slab(&indegree);
721 clear_prio_queue(&queue);
722 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
723 clear_author_date_slab(&author_date);
726 /* merge-base stuff */
728 /* bits #0..15 in revision.h */
729 #define PARENT1 (1u<<16)
730 #define PARENT2 (1u<<17)
731 #define STALE (1u<<18)
732 #define RESULT (1u<<19)
734 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
736 static struct commit *interesting(struct commit_list *list)
738 while (list) {
739 struct commit *commit = list->item;
740 list = list->next;
741 if (commit->object.flags & STALE)
742 continue;
743 return commit;
745 return NULL;
748 /* all input commits in one and twos[] must have been parsed! */
749 static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
751 struct commit_list *list = NULL;
752 struct commit_list *result = NULL;
753 int i;
755 one->object.flags |= PARENT1;
756 commit_list_insert_by_date(one, &list);
757 if (!n)
758 return list;
759 for (i = 0; i < n; i++) {
760 twos[i]->object.flags |= PARENT2;
761 commit_list_insert_by_date(twos[i], &list);
764 while (interesting(list)) {
765 struct commit *commit;
766 struct commit_list *parents;
767 struct commit_list *next;
768 int flags;
770 commit = list->item;
771 next = list->next;
772 free(list);
773 list = next;
775 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
776 if (flags == (PARENT1 | PARENT2)) {
777 if (!(commit->object.flags & RESULT)) {
778 commit->object.flags |= RESULT;
779 commit_list_insert_by_date(commit, &result);
781 /* Mark parents of a found merge stale */
782 flags |= STALE;
784 parents = commit->parents;
785 while (parents) {
786 struct commit *p = parents->item;
787 parents = parents->next;
788 if ((p->object.flags & flags) == flags)
789 continue;
790 if (parse_commit(p))
791 return NULL;
792 p->object.flags |= flags;
793 commit_list_insert_by_date(p, &list);
797 free_commit_list(list);
798 return result;
801 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
803 struct commit_list *list = NULL;
804 struct commit_list *result = NULL;
805 int i;
807 for (i = 0; i < n; i++) {
808 if (one == twos[i])
810 * We do not mark this even with RESULT so we do not
811 * have to clean it up.
813 return commit_list_insert(one, &result);
816 if (parse_commit(one))
817 return NULL;
818 for (i = 0; i < n; i++) {
819 if (parse_commit(twos[i]))
820 return NULL;
823 list = paint_down_to_common(one, n, twos);
825 while (list) {
826 struct commit_list *next = list->next;
827 if (!(list->item->object.flags & STALE))
828 commit_list_insert_by_date(list->item, &result);
829 free(list);
830 list = next;
832 return result;
835 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
837 struct commit_list *i, *j, *k, *ret = NULL;
839 if (!in)
840 return ret;
842 commit_list_insert(in->item, &ret);
844 for (i = in->next; i; i = i->next) {
845 struct commit_list *new = NULL, *end = NULL;
847 for (j = ret; j; j = j->next) {
848 struct commit_list *bases;
849 bases = get_merge_bases(i->item, j->item, 1);
850 if (!new)
851 new = bases;
852 else
853 end->next = bases;
854 for (k = bases; k; k = k->next)
855 end = k;
857 ret = new;
859 return ret;
862 static int remove_redundant(struct commit **array, int cnt)
865 * Some commit in the array may be an ancestor of
866 * another commit. Move such commit to the end of
867 * the array, and return the number of commits that
868 * are independent from each other.
870 struct commit **work;
871 unsigned char *redundant;
872 int *filled_index;
873 int i, j, filled;
875 work = xcalloc(cnt, sizeof(*work));
876 redundant = xcalloc(cnt, 1);
877 filled_index = xmalloc(sizeof(*filled_index) * (cnt - 1));
879 for (i = 0; i < cnt; i++)
880 parse_commit(array[i]);
881 for (i = 0; i < cnt; i++) {
882 struct commit_list *common;
884 if (redundant[i])
885 continue;
886 for (j = filled = 0; j < cnt; j++) {
887 if (i == j || redundant[j])
888 continue;
889 filled_index[filled] = j;
890 work[filled++] = array[j];
892 common = paint_down_to_common(array[i], filled, work);
893 if (array[i]->object.flags & PARENT2)
894 redundant[i] = 1;
895 for (j = 0; j < filled; j++)
896 if (work[j]->object.flags & PARENT1)
897 redundant[filled_index[j]] = 1;
898 clear_commit_marks(array[i], all_flags);
899 for (j = 0; j < filled; j++)
900 clear_commit_marks(work[j], all_flags);
901 free_commit_list(common);
904 /* Now collect the result */
905 memcpy(work, array, sizeof(*array) * cnt);
906 for (i = filled = 0; i < cnt; i++)
907 if (!redundant[i])
908 array[filled++] = work[i];
909 for (j = filled, i = 0; i < cnt; i++)
910 if (redundant[i])
911 array[j++] = work[i];
912 free(work);
913 free(redundant);
914 free(filled_index);
915 return filled;
918 struct commit_list *get_merge_bases_many(struct commit *one,
919 int n,
920 struct commit **twos,
921 int cleanup)
923 struct commit_list *list;
924 struct commit **rslt;
925 struct commit_list *result;
926 int cnt, i;
928 result = merge_bases_many(one, n, twos);
929 for (i = 0; i < n; i++) {
930 if (one == twos[i])
931 return result;
933 if (!result || !result->next) {
934 if (cleanup) {
935 clear_commit_marks(one, all_flags);
936 clear_commit_marks_many(n, twos, all_flags);
938 return result;
941 /* There are more than one */
942 cnt = 0;
943 list = result;
944 while (list) {
945 list = list->next;
946 cnt++;
948 rslt = xcalloc(cnt, sizeof(*rslt));
949 for (list = result, i = 0; list; list = list->next)
950 rslt[i++] = list->item;
951 free_commit_list(result);
953 clear_commit_marks(one, all_flags);
954 clear_commit_marks_many(n, twos, all_flags);
956 cnt = remove_redundant(rslt, cnt);
957 result = NULL;
958 for (i = 0; i < cnt; i++)
959 commit_list_insert_by_date(rslt[i], &result);
960 free(rslt);
961 return result;
964 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
965 int cleanup)
967 return get_merge_bases_many(one, 1, &two, cleanup);
971 * Is "commit" a descendant of one of the elements on the "with_commit" list?
973 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
975 if (!with_commit)
976 return 1;
977 while (with_commit) {
978 struct commit *other;
980 other = with_commit->item;
981 with_commit = with_commit->next;
982 if (in_merge_bases(other, commit))
983 return 1;
985 return 0;
989 * Is "commit" an ancestor of one of the "references"?
991 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
993 struct commit_list *bases;
994 int ret = 0, i;
996 if (parse_commit(commit))
997 return ret;
998 for (i = 0; i < nr_reference; i++)
999 if (parse_commit(reference[i]))
1000 return ret;
1002 bases = paint_down_to_common(commit, nr_reference, reference);
1003 if (commit->object.flags & PARENT2)
1004 ret = 1;
1005 clear_commit_marks(commit, all_flags);
1006 clear_commit_marks_many(nr_reference, reference, all_flags);
1007 free_commit_list(bases);
1008 return ret;
1012 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1014 int in_merge_bases(struct commit *commit, struct commit *reference)
1016 return in_merge_bases_many(commit, 1, &reference);
1019 struct commit_list *reduce_heads(struct commit_list *heads)
1021 struct commit_list *p;
1022 struct commit_list *result = NULL, **tail = &result;
1023 struct commit **array;
1024 int num_head, i;
1026 if (!heads)
1027 return NULL;
1029 /* Uniquify */
1030 for (p = heads; p; p = p->next)
1031 p->item->object.flags &= ~STALE;
1032 for (p = heads, num_head = 0; p; p = p->next) {
1033 if (p->item->object.flags & STALE)
1034 continue;
1035 p->item->object.flags |= STALE;
1036 num_head++;
1038 array = xcalloc(sizeof(*array), num_head);
1039 for (p = heads, i = 0; p; p = p->next) {
1040 if (p->item->object.flags & STALE) {
1041 array[i++] = p->item;
1042 p->item->object.flags &= ~STALE;
1045 num_head = remove_redundant(array, num_head);
1046 for (i = 0; i < num_head; i++)
1047 tail = &commit_list_insert(array[i], tail)->next;
1048 return result;
1051 static const char gpg_sig_header[] = "gpgsig";
1052 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
1054 static int do_sign_commit(struct strbuf *buf, const char *keyid)
1056 struct strbuf sig = STRBUF_INIT;
1057 int inspos, copypos;
1059 /* find the end of the header */
1060 inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
1062 if (!keyid || !*keyid)
1063 keyid = get_signing_key();
1064 if (sign_buffer(buf, &sig, keyid)) {
1065 strbuf_release(&sig);
1066 return -1;
1069 for (copypos = 0; sig.buf[copypos]; ) {
1070 const char *bol = sig.buf + copypos;
1071 const char *eol = strchrnul(bol, '\n');
1072 int len = (eol - bol) + !!*eol;
1074 if (!copypos) {
1075 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1076 inspos += gpg_sig_header_len;
1078 strbuf_insert(buf, inspos++, " ", 1);
1079 strbuf_insert(buf, inspos, bol, len);
1080 inspos += len;
1081 copypos += len;
1083 strbuf_release(&sig);
1084 return 0;
1087 int parse_signed_commit(const unsigned char *sha1,
1088 struct strbuf *payload, struct strbuf *signature)
1090 unsigned long size;
1091 enum object_type type;
1092 char *buffer = read_sha1_file(sha1, &type, &size);
1093 int in_signature, saw_signature = -1;
1094 char *line, *tail;
1096 if (!buffer || type != OBJ_COMMIT)
1097 goto cleanup;
1099 line = buffer;
1100 tail = buffer + size;
1101 in_signature = 0;
1102 saw_signature = 0;
1103 while (line < tail) {
1104 const char *sig = NULL;
1105 char *next = memchr(line, '\n', tail - line);
1107 next = next ? next + 1 : tail;
1108 if (in_signature && line[0] == ' ')
1109 sig = line + 1;
1110 else if (starts_with(line, gpg_sig_header) &&
1111 line[gpg_sig_header_len] == ' ')
1112 sig = line + gpg_sig_header_len + 1;
1113 if (sig) {
1114 strbuf_add(signature, sig, next - sig);
1115 saw_signature = 1;
1116 in_signature = 1;
1117 } else {
1118 if (*line == '\n')
1119 /* dump the whole remainder of the buffer */
1120 next = tail;
1121 strbuf_add(payload, line, next - line);
1122 in_signature = 0;
1124 line = next;
1126 cleanup:
1127 free(buffer);
1128 return saw_signature;
1131 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1133 struct merge_remote_desc *desc;
1134 struct commit_extra_header *mergetag;
1135 char *buf;
1136 unsigned long size, len;
1137 enum object_type type;
1139 desc = merge_remote_util(parent);
1140 if (!desc || !desc->obj)
1141 return;
1142 buf = read_sha1_file(desc->obj->sha1, &type, &size);
1143 if (!buf || type != OBJ_TAG)
1144 goto free_return;
1145 len = parse_signature(buf, size);
1146 if (size == len)
1147 goto free_return;
1149 * We could verify this signature and either omit the tag when
1150 * it does not validate, but the integrator may not have the
1151 * public key of the signer of the tag he is merging, while a
1152 * later auditor may have it while auditing, so let's not run
1153 * verify-signed-buffer here for now...
1155 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1156 * warn("warning: signed tag unverified.");
1158 mergetag = xcalloc(1, sizeof(*mergetag));
1159 mergetag->key = xstrdup("mergetag");
1160 mergetag->value = buf;
1161 mergetag->len = size;
1163 **tail = mergetag;
1164 *tail = &mergetag->next;
1165 return;
1167 free_return:
1168 free(buf);
1171 static struct {
1172 char result;
1173 const char *check;
1174 } sigcheck_gpg_status[] = {
1175 { 'G', "\n[GNUPG:] GOODSIG " },
1176 { 'B', "\n[GNUPG:] BADSIG " },
1177 { 'U', "\n[GNUPG:] TRUST_NEVER" },
1178 { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
1181 static void parse_gpg_output(struct signature_check *sigc)
1183 const char *buf = sigc->gpg_status;
1184 int i;
1186 /* Iterate over all search strings */
1187 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
1188 const char *found, *next;
1190 if (starts_with(buf, sigcheck_gpg_status[i].check + 1)) {
1191 /* At the very beginning of the buffer */
1192 found = buf + strlen(sigcheck_gpg_status[i].check + 1);
1193 } else {
1194 found = strstr(buf, sigcheck_gpg_status[i].check);
1195 if (!found)
1196 continue;
1197 found += strlen(sigcheck_gpg_status[i].check);
1199 sigc->result = sigcheck_gpg_status[i].result;
1200 /* The trust messages are not followed by key/signer information */
1201 if (sigc->result != 'U') {
1202 sigc->key = xmemdupz(found, 16);
1203 found += 17;
1204 next = strchrnul(found, '\n');
1205 sigc->signer = xmemdupz(found, next - found);
1210 void check_commit_signature(const struct commit* commit, struct signature_check *sigc)
1212 struct strbuf payload = STRBUF_INIT;
1213 struct strbuf signature = STRBUF_INIT;
1214 struct strbuf gpg_output = STRBUF_INIT;
1215 struct strbuf gpg_status = STRBUF_INIT;
1216 int status;
1218 sigc->result = 'N';
1220 if (parse_signed_commit(commit->object.sha1,
1221 &payload, &signature) <= 0)
1222 goto out;
1223 status = verify_signed_buffer(payload.buf, payload.len,
1224 signature.buf, signature.len,
1225 &gpg_output, &gpg_status);
1226 if (status && !gpg_output.len)
1227 goto out;
1228 sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
1229 sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
1230 parse_gpg_output(sigc);
1232 out:
1233 strbuf_release(&gpg_status);
1234 strbuf_release(&gpg_output);
1235 strbuf_release(&payload);
1236 strbuf_release(&signature);
1241 void append_merge_tag_headers(struct commit_list *parents,
1242 struct commit_extra_header ***tail)
1244 while (parents) {
1245 struct commit *parent = parents->item;
1246 handle_signed_tag(parent, tail);
1247 parents = parents->next;
1251 static void add_extra_header(struct strbuf *buffer,
1252 struct commit_extra_header *extra)
1254 strbuf_addstr(buffer, extra->key);
1255 if (extra->len)
1256 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1257 else
1258 strbuf_addch(buffer, '\n');
1261 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1262 const char **exclude)
1264 struct commit_extra_header *extra = NULL;
1265 unsigned long size;
1266 enum object_type type;
1267 char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
1268 if (buffer && type == OBJ_COMMIT)
1269 extra = read_commit_extra_header_lines(buffer, size, exclude);
1270 free(buffer);
1271 return extra;
1274 static inline int standard_header_field(const char *field, size_t len)
1276 return ((len == 4 && !memcmp(field, "tree ", 5)) ||
1277 (len == 6 && !memcmp(field, "parent ", 7)) ||
1278 (len == 6 && !memcmp(field, "author ", 7)) ||
1279 (len == 9 && !memcmp(field, "committer ", 10)) ||
1280 (len == 8 && !memcmp(field, "encoding ", 9)));
1283 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1285 if (!exclude)
1286 return 0;
1288 while (*exclude) {
1289 size_t xlen = strlen(*exclude);
1290 if (len == xlen &&
1291 !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
1292 return 1;
1293 exclude++;
1295 return 0;
1298 static struct commit_extra_header *read_commit_extra_header_lines(
1299 const char *buffer, size_t size,
1300 const char **exclude)
1302 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1303 const char *line, *next, *eof, *eob;
1304 struct strbuf buf = STRBUF_INIT;
1306 for (line = buffer, eob = line + size;
1307 line < eob && *line != '\n';
1308 line = next) {
1309 next = memchr(line, '\n', eob - line);
1310 next = next ? next + 1 : eob;
1311 if (*line == ' ') {
1312 /* continuation */
1313 if (it)
1314 strbuf_add(&buf, line + 1, next - (line + 1));
1315 continue;
1317 if (it)
1318 it->value = strbuf_detach(&buf, &it->len);
1319 strbuf_reset(&buf);
1320 it = NULL;
1322 eof = strchr(line, ' ');
1323 if (next <= eof)
1324 eof = next;
1326 if (standard_header_field(line, eof - line) ||
1327 excluded_header_field(line, eof - line, exclude))
1328 continue;
1330 it = xcalloc(1, sizeof(*it));
1331 it->key = xmemdupz(line, eof-line);
1332 *tail = it;
1333 tail = &it->next;
1334 if (eof + 1 < next)
1335 strbuf_add(&buf, eof + 1, next - (eof + 1));
1337 if (it)
1338 it->value = strbuf_detach(&buf, &it->len);
1339 return extra;
1342 void free_commit_extra_headers(struct commit_extra_header *extra)
1344 while (extra) {
1345 struct commit_extra_header *next = extra->next;
1346 free(extra->key);
1347 free(extra->value);
1348 free(extra);
1349 extra = next;
1353 int commit_tree(const struct strbuf *msg, const unsigned char *tree,
1354 struct commit_list *parents, unsigned char *ret,
1355 const char *author, const char *sign_commit)
1357 struct commit_extra_header *extra = NULL, **tail = &extra;
1358 int result;
1360 append_merge_tag_headers(parents, &tail);
1361 result = commit_tree_extended(msg, tree, parents, ret,
1362 author, sign_commit, extra);
1363 free_commit_extra_headers(extra);
1364 return result;
1367 static int find_invalid_utf8(const char *buf, int len)
1369 int offset = 0;
1370 static const unsigned int max_codepoint[] = {
1371 0x7f, 0x7ff, 0xffff, 0x10ffff
1374 while (len) {
1375 unsigned char c = *buf++;
1376 int bytes, bad_offset;
1377 unsigned int codepoint;
1378 unsigned int min_val, max_val;
1380 len--;
1381 offset++;
1383 /* Simple US-ASCII? No worries. */
1384 if (c < 0x80)
1385 continue;
1387 bad_offset = offset-1;
1390 * Count how many more high bits set: that's how
1391 * many more bytes this sequence should have.
1393 bytes = 0;
1394 while (c & 0x40) {
1395 c <<= 1;
1396 bytes++;
1400 * Must be between 1 and 3 more bytes. Longer sequences result in
1401 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1403 if (bytes < 1 || 3 < bytes)
1404 return bad_offset;
1406 /* Do we *have* that many bytes? */
1407 if (len < bytes)
1408 return bad_offset;
1411 * Place the encoded bits at the bottom of the value and compute the
1412 * valid range.
1414 codepoint = (c & 0x7f) >> bytes;
1415 min_val = max_codepoint[bytes-1] + 1;
1416 max_val = max_codepoint[bytes];
1418 offset += bytes;
1419 len -= bytes;
1421 /* And verify that they are good continuation bytes */
1422 do {
1423 codepoint <<= 6;
1424 codepoint |= *buf & 0x3f;
1425 if ((*buf++ & 0xc0) != 0x80)
1426 return bad_offset;
1427 } while (--bytes);
1429 /* Reject codepoints that are out of range for the sequence length. */
1430 if (codepoint < min_val || codepoint > max_val)
1431 return bad_offset;
1432 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1433 if ((codepoint & 0x1ff800) == 0xd800)
1434 return bad_offset;
1435 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1436 if ((codepoint & 0xfffe) == 0xfffe)
1437 return bad_offset;
1438 /* So are anything in the range U+FDD0..U+FDEF. */
1439 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
1440 return bad_offset;
1442 return -1;
1446 * This verifies that the buffer is in proper utf8 format.
1448 * If it isn't, it assumes any non-utf8 characters are Latin1,
1449 * and does the conversion.
1451 static int verify_utf8(struct strbuf *buf)
1453 int ok = 1;
1454 long pos = 0;
1456 for (;;) {
1457 int bad;
1458 unsigned char c;
1459 unsigned char replace[2];
1461 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1462 if (bad < 0)
1463 return ok;
1464 pos += bad;
1465 ok = 0;
1466 c = buf->buf[pos];
1467 strbuf_remove(buf, pos, 1);
1469 /* We know 'c' must be in the range 128-255 */
1470 replace[0] = 0xc0 + (c >> 6);
1471 replace[1] = 0x80 + (c & 0x3f);
1472 strbuf_insert(buf, pos, replace, 2);
1473 pos += 2;
1477 static const char commit_utf8_warn[] =
1478 "Warning: commit message did not conform to UTF-8.\n"
1479 "You may want to amend it after fixing the message, or set the config\n"
1480 "variable i18n.commitencoding to the encoding your project uses.\n";
1482 int commit_tree_extended(const struct strbuf *msg, const unsigned char *tree,
1483 struct commit_list *parents, unsigned char *ret,
1484 const char *author, const char *sign_commit,
1485 struct commit_extra_header *extra)
1487 int result;
1488 int encoding_is_utf8;
1489 struct strbuf buffer;
1491 assert_sha1_type(tree, OBJ_TREE);
1493 if (memchr(msg->buf, '\0', msg->len))
1494 return error("a NUL byte in commit log message not allowed.");
1496 /* Not having i18n.commitencoding is the same as having utf-8 */
1497 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1499 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1500 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1503 * NOTE! This ordering means that the same exact tree merged with a
1504 * different order of parents will be a _different_ changeset even
1505 * if everything else stays the same.
1507 while (parents) {
1508 struct commit_list *next = parents->next;
1509 struct commit *parent = parents->item;
1511 strbuf_addf(&buffer, "parent %s\n",
1512 sha1_to_hex(parent->object.sha1));
1513 free(parents);
1514 parents = next;
1517 /* Person/date information */
1518 if (!author)
1519 author = git_author_info(IDENT_STRICT);
1520 strbuf_addf(&buffer, "author %s\n", author);
1521 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1522 if (!encoding_is_utf8)
1523 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1525 while (extra) {
1526 add_extra_header(&buffer, extra);
1527 extra = extra->next;
1529 strbuf_addch(&buffer, '\n');
1531 /* And add the comment */
1532 strbuf_addbuf(&buffer, msg);
1534 /* And check the encoding */
1535 if (encoding_is_utf8 && !verify_utf8(&buffer))
1536 fprintf(stderr, commit_utf8_warn);
1538 if (sign_commit && do_sign_commit(&buffer, sign_commit))
1539 return -1;
1541 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1542 strbuf_release(&buffer);
1543 return result;
1546 struct commit *get_merge_parent(const char *name)
1548 struct object *obj;
1549 struct commit *commit;
1550 unsigned char sha1[20];
1551 if (get_sha1(name, sha1))
1552 return NULL;
1553 obj = parse_object(sha1);
1554 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1555 if (commit && !commit->util) {
1556 struct merge_remote_desc *desc;
1557 desc = xmalloc(sizeof(*desc));
1558 desc->obj = obj;
1559 desc->name = strdup(name);
1560 commit->util = desc;
1562 return commit;
1566 * Append a commit to the end of the commit_list.
1568 * next starts by pointing to the variable that holds the head of an
1569 * empty commit_list, and is updated to point to the "next" field of
1570 * the last item on the list as new commits are appended.
1572 * Usage example:
1574 * struct commit_list *list;
1575 * struct commit_list **next = &list;
1577 * next = commit_list_append(c1, next);
1578 * next = commit_list_append(c2, next);
1579 * assert(commit_list_count(list) == 2);
1580 * return list;
1582 struct commit_list **commit_list_append(struct commit *commit,
1583 struct commit_list **next)
1585 struct commit_list *new = xmalloc(sizeof(struct commit_list));
1586 new->item = commit;
1587 *next = new;
1588 new->next = NULL;
1589 return &new->next;
1592 void print_commit_list(struct commit_list *list,
1593 const char *format_cur,
1594 const char *format_last)
1596 for ( ; list; list = list->next) {
1597 const char *format = list->next ? format_cur : format_last;
1598 printf(format, sha1_to_hex(list->item->object.sha1));