Merge branch 'jk/commit-info-slab' into next
[git/mjg.git] / commit.c
bloba1096a2b5a2c6b04155f1efc093da6fb5059551f
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"
13 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
15 int save_commit_buffer = 1;
17 const char *commit_type = "commit";
18 static int commit_count;
20 static struct commit *check_commit(struct object *obj,
21 const unsigned char *sha1,
22 int quiet)
24 if (obj->type != OBJ_COMMIT) {
25 if (!quiet)
26 error("Object %s is a %s, not a commit",
27 sha1_to_hex(sha1), typename(obj->type));
28 return NULL;
30 return (struct commit *) obj;
33 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
34 int quiet)
36 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
38 if (!obj)
39 return NULL;
40 return check_commit(obj, sha1, quiet);
43 struct commit *lookup_commit_reference(const unsigned char *sha1)
45 return lookup_commit_reference_gently(sha1, 0);
48 struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name)
50 struct commit *c = lookup_commit_reference(sha1);
51 if (!c)
52 die(_("could not parse %s"), ref_name);
53 if (hashcmp(sha1, c->object.sha1)) {
54 warning(_("%s %s is not a commit!"),
55 ref_name, sha1_to_hex(sha1));
57 return c;
60 struct commit *lookup_commit(const unsigned char *sha1)
62 struct object *obj = lookup_object(sha1);
63 if (!obj) {
64 struct commit *c = alloc_commit_node();
65 c->index = commit_count++;
66 return create_object(sha1, OBJ_COMMIT, c);
68 if (!obj->type)
69 obj->type = OBJ_COMMIT;
70 return check_commit(obj, sha1, 0);
73 struct commit *lookup_commit_reference_by_name(const char *name)
75 unsigned char sha1[20];
76 struct commit *commit;
78 if (get_sha1_committish(name, sha1))
79 return NULL;
80 commit = lookup_commit_reference(sha1);
81 if (!commit || parse_commit(commit))
82 return NULL;
83 return commit;
86 static unsigned long parse_commit_date(const char *buf, const char *tail)
88 const char *dateptr;
90 if (buf + 6 >= tail)
91 return 0;
92 if (memcmp(buf, "author", 6))
93 return 0;
94 while (buf < tail && *buf++ != '\n')
95 /* nada */;
96 if (buf + 9 >= tail)
97 return 0;
98 if (memcmp(buf, "committer", 9))
99 return 0;
100 while (buf < tail && *buf++ != '>')
101 /* nada */;
102 if (buf >= tail)
103 return 0;
104 dateptr = buf;
105 while (buf < tail && *buf++ != '\n')
106 /* nada */;
107 if (buf >= tail)
108 return 0;
109 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
110 return strtoul(dateptr, NULL, 10);
113 static struct commit_graft **commit_graft;
114 static int commit_graft_alloc, commit_graft_nr;
116 static int commit_graft_pos(const unsigned char *sha1)
118 int lo, hi;
119 lo = 0;
120 hi = commit_graft_nr;
121 while (lo < hi) {
122 int mi = (lo + hi) / 2;
123 struct commit_graft *graft = commit_graft[mi];
124 int cmp = hashcmp(sha1, graft->sha1);
125 if (!cmp)
126 return mi;
127 if (cmp < 0)
128 hi = mi;
129 else
130 lo = mi + 1;
132 return -lo - 1;
135 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
137 int pos = commit_graft_pos(graft->sha1);
139 if (0 <= pos) {
140 if (ignore_dups)
141 free(graft);
142 else {
143 free(commit_graft[pos]);
144 commit_graft[pos] = graft;
146 return 1;
148 pos = -pos - 1;
149 if (commit_graft_alloc <= ++commit_graft_nr) {
150 commit_graft_alloc = alloc_nr(commit_graft_alloc);
151 commit_graft = xrealloc(commit_graft,
152 sizeof(*commit_graft) *
153 commit_graft_alloc);
155 if (pos < commit_graft_nr)
156 memmove(commit_graft + pos + 1,
157 commit_graft + pos,
158 (commit_graft_nr - pos - 1) *
159 sizeof(*commit_graft));
160 commit_graft[pos] = graft;
161 return 0;
164 struct commit_graft *read_graft_line(char *buf, int len)
166 /* The format is just "Commit Parent1 Parent2 ...\n" */
167 int i;
168 struct commit_graft *graft = NULL;
170 while (len && isspace(buf[len-1]))
171 buf[--len] = '\0';
172 if (buf[0] == '#' || buf[0] == '\0')
173 return NULL;
174 if ((len + 1) % 41)
175 goto bad_graft_data;
176 i = (len + 1) / 41 - 1;
177 graft = xmalloc(sizeof(*graft) + 20 * i);
178 graft->nr_parent = i;
179 if (get_sha1_hex(buf, graft->sha1))
180 goto bad_graft_data;
181 for (i = 40; i < len; i += 41) {
182 if (buf[i] != ' ')
183 goto bad_graft_data;
184 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
185 goto bad_graft_data;
187 return graft;
189 bad_graft_data:
190 error("bad graft data: %s", buf);
191 free(graft);
192 return NULL;
195 static int read_graft_file(const char *graft_file)
197 FILE *fp = fopen(graft_file, "r");
198 char buf[1024];
199 if (!fp)
200 return -1;
201 while (fgets(buf, sizeof(buf), fp)) {
202 /* The format is just "Commit Parent1 Parent2 ...\n" */
203 int len = strlen(buf);
204 struct commit_graft *graft = read_graft_line(buf, len);
205 if (!graft)
206 continue;
207 if (register_commit_graft(graft, 1))
208 error("duplicate graft data: %s", buf);
210 fclose(fp);
211 return 0;
214 static void prepare_commit_graft(void)
216 static int commit_graft_prepared;
217 char *graft_file;
219 if (commit_graft_prepared)
220 return;
221 graft_file = get_graft_file();
222 read_graft_file(graft_file);
223 /* make sure shallows are read */
224 is_repository_shallow();
225 commit_graft_prepared = 1;
228 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
230 int pos;
231 prepare_commit_graft();
232 pos = commit_graft_pos(sha1);
233 if (pos < 0)
234 return NULL;
235 return commit_graft[pos];
238 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
240 int i, ret;
241 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
242 ret = fn(commit_graft[i], cb_data);
243 return ret;
246 int unregister_shallow(const unsigned char *sha1)
248 int pos = commit_graft_pos(sha1);
249 if (pos < 0)
250 return -1;
251 if (pos + 1 < commit_graft_nr)
252 memmove(commit_graft + pos, commit_graft + pos + 1,
253 sizeof(struct commit_graft *)
254 * (commit_graft_nr - pos - 1));
255 commit_graft_nr--;
256 return 0;
259 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
261 const char *tail = buffer;
262 const char *bufptr = buffer;
263 unsigned char parent[20];
264 struct commit_list **pptr;
265 struct commit_graft *graft;
267 if (item->object.parsed)
268 return 0;
269 item->object.parsed = 1;
270 tail += size;
271 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
272 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
273 if (get_sha1_hex(bufptr + 5, parent) < 0)
274 return error("bad tree pointer in commit %s",
275 sha1_to_hex(item->object.sha1));
276 item->tree = lookup_tree(parent);
277 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
278 pptr = &item->parents;
280 graft = lookup_commit_graft(item->object.sha1);
281 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
282 struct commit *new_parent;
284 if (tail <= bufptr + 48 ||
285 get_sha1_hex(bufptr + 7, parent) ||
286 bufptr[47] != '\n')
287 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
288 bufptr += 48;
290 * The clone is shallow if nr_parent < 0, and we must
291 * not traverse its real parents even when we unhide them.
293 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
294 continue;
295 new_parent = lookup_commit(parent);
296 if (new_parent)
297 pptr = &commit_list_insert(new_parent, pptr)->next;
299 if (graft) {
300 int i;
301 struct commit *new_parent;
302 for (i = 0; i < graft->nr_parent; i++) {
303 new_parent = lookup_commit(graft->parent[i]);
304 if (!new_parent)
305 continue;
306 pptr = &commit_list_insert(new_parent, pptr)->next;
309 item->date = parse_commit_date(bufptr, tail);
311 return 0;
314 int parse_commit(struct commit *item)
316 enum object_type type;
317 void *buffer;
318 unsigned long size;
319 int ret;
321 if (!item)
322 return -1;
323 if (item->object.parsed)
324 return 0;
325 buffer = read_sha1_file(item->object.sha1, &type, &size);
326 if (!buffer)
327 return error("Could not read %s",
328 sha1_to_hex(item->object.sha1));
329 if (type != OBJ_COMMIT) {
330 free(buffer);
331 return error("Object %s not a commit",
332 sha1_to_hex(item->object.sha1));
334 ret = parse_commit_buffer(item, buffer, size);
335 if (save_commit_buffer && !ret) {
336 item->buffer = buffer;
337 return 0;
339 free(buffer);
340 return ret;
343 int find_commit_subject(const char *commit_buffer, const char **subject)
345 const char *eol;
346 const char *p = commit_buffer;
348 while (*p && (*p != '\n' || p[1] != '\n'))
349 p++;
350 if (*p) {
351 p += 2;
352 for (eol = p; *eol && *eol != '\n'; eol++)
353 ; /* do nothing */
354 } else
355 eol = p;
357 *subject = p;
359 return eol - p;
362 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
364 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
365 new_list->item = item;
366 new_list->next = *list_p;
367 *list_p = new_list;
368 return new_list;
371 unsigned commit_list_count(const struct commit_list *l)
373 unsigned c = 0;
374 for (; l; l = l->next )
375 c++;
376 return c;
379 void free_commit_list(struct commit_list *list)
381 while (list) {
382 struct commit_list *temp = list;
383 list = temp->next;
384 free(temp);
388 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
390 struct commit_list **pp = list;
391 struct commit_list *p;
392 while ((p = *pp) != NULL) {
393 if (p->item->date < item->date) {
394 break;
396 pp = &p->next;
398 return commit_list_insert(item, pp);
401 static int commit_list_compare_by_date(const void *a, const void *b)
403 unsigned long a_date = ((const struct commit_list *)a)->item->date;
404 unsigned long b_date = ((const struct commit_list *)b)->item->date;
405 if (a_date < b_date)
406 return 1;
407 if (a_date > b_date)
408 return -1;
409 return 0;
412 static void *commit_list_get_next(const void *a)
414 return ((const struct commit_list *)a)->next;
417 static void commit_list_set_next(void *a, void *next)
419 ((struct commit_list *)a)->next = next;
422 void commit_list_sort_by_date(struct commit_list **list)
424 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
425 commit_list_compare_by_date);
428 struct commit *pop_most_recent_commit(struct commit_list **list,
429 unsigned int mark)
431 struct commit *ret = (*list)->item;
432 struct commit_list *parents = ret->parents;
433 struct commit_list *old = *list;
435 *list = (*list)->next;
436 free(old);
438 while (parents) {
439 struct commit *commit = parents->item;
440 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
441 commit->object.flags |= mark;
442 commit_list_insert_by_date(commit, list);
444 parents = parents->next;
446 return ret;
449 static void clear_commit_marks_1(struct commit_list **plist,
450 struct commit *commit, unsigned int mark)
452 while (commit) {
453 struct commit_list *parents;
455 if (!(mark & commit->object.flags))
456 return;
458 commit->object.flags &= ~mark;
460 parents = commit->parents;
461 if (!parents)
462 return;
464 while ((parents = parents->next))
465 commit_list_insert(parents->item, plist);
467 commit = commit->parents->item;
471 void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
473 struct commit_list *list = NULL;
475 while (nr--) {
476 commit_list_insert(*commit, &list);
477 commit++;
479 while (list)
480 clear_commit_marks_1(&list, pop_commit(&list), mark);
483 void clear_commit_marks(struct commit *commit, unsigned int mark)
485 clear_commit_marks_many(1, &commit, mark);
488 void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
490 struct object *object;
491 struct commit *commit;
492 unsigned int i;
494 for (i = 0; i < a->nr; i++) {
495 object = a->objects[i].item;
496 commit = lookup_commit_reference_gently(object->sha1, 1);
497 if (commit)
498 clear_commit_marks(commit, mark);
502 struct commit *pop_commit(struct commit_list **stack)
504 struct commit_list *top = *stack;
505 struct commit *item = top ? top->item : NULL;
507 if (top) {
508 *stack = top->next;
509 free(top);
511 return item;
515 * Topological sort support
518 /* count number of children that have not been emitted */
519 define_commit_slab(indegree_slab, int);
522 * Performs an in-place topological sort on the list supplied.
524 void sort_in_topological_order(struct commit_list ** list, int lifo)
526 struct commit_list *next, *orig = *list;
527 struct commit_list *work, **insert;
528 struct commit_list **pptr;
529 struct indegree_slab indegree;
531 if (!orig)
532 return;
533 *list = NULL;
535 init_indegree_slab(&indegree);
537 /* Mark them and clear the indegree */
538 for (next = orig; next; next = next->next) {
539 struct commit *commit = next->item;
540 *(indegree_slab_at(&indegree, commit)) = 1;
543 /* update the indegree */
544 for (next = orig; next; next = next->next) {
545 struct commit_list * parents = next->item->parents;
546 while (parents) {
547 struct commit *parent = parents->item;
548 int *pi = indegree_slab_at(&indegree, parent);
550 if (*pi)
551 (*pi)++;
552 parents = parents->next;
557 * find the tips
559 * tips are nodes not reachable from any other node in the list
561 * the tips serve as a starting set for the work queue.
563 work = NULL;
564 insert = &work;
565 for (next = orig; next; next = next->next) {
566 struct commit *commit = next->item;
568 if (*(indegree_slab_at(&indegree, commit)) == 1)
569 insert = &commit_list_insert(commit, insert)->next;
572 /* process the list in topological order */
573 if (!lifo)
574 commit_list_sort_by_date(&work);
576 pptr = list;
577 *list = NULL;
578 while (work) {
579 struct commit *commit;
580 struct commit_list *parents, *work_item;
582 work_item = work;
583 work = work_item->next;
584 work_item->next = NULL;
586 commit = work_item->item;
587 for (parents = commit->parents; parents ; parents = parents->next) {
588 struct commit *parent = parents->item;
589 int *pi = indegree_slab_at(&indegree, parent);
591 if (!*pi)
592 continue;
595 * parents are only enqueued for emission
596 * when all their children have been emitted thereby
597 * guaranteeing topological order.
599 if (--(*pi) == 1) {
600 if (!lifo)
601 commit_list_insert_by_date(parent, &work);
602 else
603 commit_list_insert(parent, &work);
607 * work_item is a commit all of whose children
608 * have already been emitted. we can emit it now.
610 *(indegree_slab_at(&indegree, commit)) = 0;
611 *pptr = work_item;
612 pptr = &work_item->next;
615 clear_indegree_slab(&indegree);
618 /* merge-base stuff */
620 /* bits #0..15 in revision.h */
621 #define PARENT1 (1u<<16)
622 #define PARENT2 (1u<<17)
623 #define STALE (1u<<18)
624 #define RESULT (1u<<19)
626 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
628 static struct commit *interesting(struct commit_list *list)
630 while (list) {
631 struct commit *commit = list->item;
632 list = list->next;
633 if (commit->object.flags & STALE)
634 continue;
635 return commit;
637 return NULL;
640 /* all input commits in one and twos[] must have been parsed! */
641 static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
643 struct commit_list *list = NULL;
644 struct commit_list *result = NULL;
645 int i;
647 one->object.flags |= PARENT1;
648 commit_list_insert_by_date(one, &list);
649 if (!n)
650 return list;
651 for (i = 0; i < n; i++) {
652 twos[i]->object.flags |= PARENT2;
653 commit_list_insert_by_date(twos[i], &list);
656 while (interesting(list)) {
657 struct commit *commit;
658 struct commit_list *parents;
659 struct commit_list *next;
660 int flags;
662 commit = list->item;
663 next = list->next;
664 free(list);
665 list = next;
667 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
668 if (flags == (PARENT1 | PARENT2)) {
669 if (!(commit->object.flags & RESULT)) {
670 commit->object.flags |= RESULT;
671 commit_list_insert_by_date(commit, &result);
673 /* Mark parents of a found merge stale */
674 flags |= STALE;
676 parents = commit->parents;
677 while (parents) {
678 struct commit *p = parents->item;
679 parents = parents->next;
680 if ((p->object.flags & flags) == flags)
681 continue;
682 if (parse_commit(p))
683 return NULL;
684 p->object.flags |= flags;
685 commit_list_insert_by_date(p, &list);
689 free_commit_list(list);
690 return result;
693 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
695 struct commit_list *list = NULL;
696 struct commit_list *result = NULL;
697 int i;
699 for (i = 0; i < n; i++) {
700 if (one == twos[i])
702 * We do not mark this even with RESULT so we do not
703 * have to clean it up.
705 return commit_list_insert(one, &result);
708 if (parse_commit(one))
709 return NULL;
710 for (i = 0; i < n; i++) {
711 if (parse_commit(twos[i]))
712 return NULL;
715 list = paint_down_to_common(one, n, twos);
717 while (list) {
718 struct commit_list *next = list->next;
719 if (!(list->item->object.flags & STALE))
720 commit_list_insert_by_date(list->item, &result);
721 free(list);
722 list = next;
724 return result;
727 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
729 struct commit_list *i, *j, *k, *ret = NULL;
730 struct commit_list **pptr = &ret;
732 for (i = in; i; i = i->next) {
733 if (!ret)
734 pptr = &commit_list_insert(i->item, pptr)->next;
735 else {
736 struct commit_list *new = NULL, *end = NULL;
738 for (j = ret; j; j = j->next) {
739 struct commit_list *bases;
740 bases = get_merge_bases(i->item, j->item, 1);
741 if (!new)
742 new = bases;
743 else
744 end->next = bases;
745 for (k = bases; k; k = k->next)
746 end = k;
748 ret = new;
751 return ret;
754 static int remove_redundant(struct commit **array, int cnt)
757 * Some commit in the array may be an ancestor of
758 * another commit. Move such commit to the end of
759 * the array, and return the number of commits that
760 * are independent from each other.
762 struct commit **work;
763 unsigned char *redundant;
764 int *filled_index;
765 int i, j, filled;
767 work = xcalloc(cnt, sizeof(*work));
768 redundant = xcalloc(cnt, 1);
769 filled_index = xmalloc(sizeof(*filled_index) * (cnt - 1));
771 for (i = 0; i < cnt; i++)
772 parse_commit(array[i]);
773 for (i = 0; i < cnt; i++) {
774 struct commit_list *common;
776 if (redundant[i])
777 continue;
778 for (j = filled = 0; j < cnt; j++) {
779 if (i == j || redundant[j])
780 continue;
781 filled_index[filled] = j;
782 work[filled++] = array[j];
784 common = paint_down_to_common(array[i], filled, work);
785 if (array[i]->object.flags & PARENT2)
786 redundant[i] = 1;
787 for (j = 0; j < filled; j++)
788 if (work[j]->object.flags & PARENT1)
789 redundant[filled_index[j]] = 1;
790 clear_commit_marks(array[i], all_flags);
791 for (j = 0; j < filled; j++)
792 clear_commit_marks(work[j], all_flags);
793 free_commit_list(common);
796 /* Now collect the result */
797 memcpy(work, array, sizeof(*array) * cnt);
798 for (i = filled = 0; i < cnt; i++)
799 if (!redundant[i])
800 array[filled++] = work[i];
801 for (j = filled, i = 0; i < cnt; i++)
802 if (redundant[i])
803 array[j++] = work[i];
804 free(work);
805 free(redundant);
806 free(filled_index);
807 return filled;
810 struct commit_list *get_merge_bases_many(struct commit *one,
811 int n,
812 struct commit **twos,
813 int cleanup)
815 struct commit_list *list;
816 struct commit **rslt;
817 struct commit_list *result;
818 int cnt, i;
820 result = merge_bases_many(one, n, twos);
821 for (i = 0; i < n; i++) {
822 if (one == twos[i])
823 return result;
825 if (!result || !result->next) {
826 if (cleanup) {
827 clear_commit_marks(one, all_flags);
828 clear_commit_marks_many(n, twos, all_flags);
830 return result;
833 /* There are more than one */
834 cnt = 0;
835 list = result;
836 while (list) {
837 list = list->next;
838 cnt++;
840 rslt = xcalloc(cnt, sizeof(*rslt));
841 for (list = result, i = 0; list; list = list->next)
842 rslt[i++] = list->item;
843 free_commit_list(result);
845 clear_commit_marks(one, all_flags);
846 clear_commit_marks_many(n, twos, all_flags);
848 cnt = remove_redundant(rslt, cnt);
849 result = NULL;
850 for (i = 0; i < cnt; i++)
851 commit_list_insert_by_date(rslt[i], &result);
852 free(rslt);
853 return result;
856 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
857 int cleanup)
859 return get_merge_bases_many(one, 1, &two, cleanup);
863 * Is "commit" a descendant of one of the elements on the "with_commit" list?
865 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
867 if (!with_commit)
868 return 1;
869 while (with_commit) {
870 struct commit *other;
872 other = with_commit->item;
873 with_commit = with_commit->next;
874 if (in_merge_bases(other, commit))
875 return 1;
877 return 0;
881 * Is "commit" an ancestor of one of the "references"?
883 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
885 struct commit_list *bases;
886 int ret = 0, i;
888 if (parse_commit(commit))
889 return ret;
890 for (i = 0; i < nr_reference; i++)
891 if (parse_commit(reference[i]))
892 return ret;
894 bases = paint_down_to_common(commit, nr_reference, reference);
895 if (commit->object.flags & PARENT2)
896 ret = 1;
897 clear_commit_marks(commit, all_flags);
898 clear_commit_marks_many(nr_reference, reference, all_flags);
899 free_commit_list(bases);
900 return ret;
904 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
906 int in_merge_bases(struct commit *commit, struct commit *reference)
908 return in_merge_bases_many(commit, 1, &reference);
911 struct commit_list *reduce_heads(struct commit_list *heads)
913 struct commit_list *p;
914 struct commit_list *result = NULL, **tail = &result;
915 struct commit **array;
916 int num_head, i;
918 if (!heads)
919 return NULL;
921 /* Uniquify */
922 for (p = heads; p; p = p->next)
923 p->item->object.flags &= ~STALE;
924 for (p = heads, num_head = 0; p; p = p->next) {
925 if (p->item->object.flags & STALE)
926 continue;
927 p->item->object.flags |= STALE;
928 num_head++;
930 array = xcalloc(sizeof(*array), num_head);
931 for (p = heads, i = 0; p; p = p->next) {
932 if (p->item->object.flags & STALE) {
933 array[i++] = p->item;
934 p->item->object.flags &= ~STALE;
937 num_head = remove_redundant(array, num_head);
938 for (i = 0; i < num_head; i++)
939 tail = &commit_list_insert(array[i], tail)->next;
940 return result;
943 static const char gpg_sig_header[] = "gpgsig";
944 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
946 static int do_sign_commit(struct strbuf *buf, const char *keyid)
948 struct strbuf sig = STRBUF_INIT;
949 int inspos, copypos;
951 /* find the end of the header */
952 inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
954 if (!keyid || !*keyid)
955 keyid = get_signing_key();
956 if (sign_buffer(buf, &sig, keyid)) {
957 strbuf_release(&sig);
958 return -1;
961 for (copypos = 0; sig.buf[copypos]; ) {
962 const char *bol = sig.buf + copypos;
963 const char *eol = strchrnul(bol, '\n');
964 int len = (eol - bol) + !!*eol;
966 if (!copypos) {
967 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
968 inspos += gpg_sig_header_len;
970 strbuf_insert(buf, inspos++, " ", 1);
971 strbuf_insert(buf, inspos, bol, len);
972 inspos += len;
973 copypos += len;
975 strbuf_release(&sig);
976 return 0;
979 int parse_signed_commit(const unsigned char *sha1,
980 struct strbuf *payload, struct strbuf *signature)
982 unsigned long size;
983 enum object_type type;
984 char *buffer = read_sha1_file(sha1, &type, &size);
985 int in_signature, saw_signature = -1;
986 char *line, *tail;
988 if (!buffer || type != OBJ_COMMIT)
989 goto cleanup;
991 line = buffer;
992 tail = buffer + size;
993 in_signature = 0;
994 saw_signature = 0;
995 while (line < tail) {
996 const char *sig = NULL;
997 char *next = memchr(line, '\n', tail - line);
999 next = next ? next + 1 : tail;
1000 if (in_signature && line[0] == ' ')
1001 sig = line + 1;
1002 else if (!prefixcmp(line, gpg_sig_header) &&
1003 line[gpg_sig_header_len] == ' ')
1004 sig = line + gpg_sig_header_len + 1;
1005 if (sig) {
1006 strbuf_add(signature, sig, next - sig);
1007 saw_signature = 1;
1008 in_signature = 1;
1009 } else {
1010 if (*line == '\n')
1011 /* dump the whole remainder of the buffer */
1012 next = tail;
1013 strbuf_add(payload, line, next - line);
1014 in_signature = 0;
1016 line = next;
1018 cleanup:
1019 free(buffer);
1020 return saw_signature;
1023 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1025 struct merge_remote_desc *desc;
1026 struct commit_extra_header *mergetag;
1027 char *buf;
1028 unsigned long size, len;
1029 enum object_type type;
1031 desc = merge_remote_util(parent);
1032 if (!desc || !desc->obj)
1033 return;
1034 buf = read_sha1_file(desc->obj->sha1, &type, &size);
1035 if (!buf || type != OBJ_TAG)
1036 goto free_return;
1037 len = parse_signature(buf, size);
1038 if (size == len)
1039 goto free_return;
1041 * We could verify this signature and either omit the tag when
1042 * it does not validate, but the integrator may not have the
1043 * public key of the signer of the tag he is merging, while a
1044 * later auditor may have it while auditing, so let's not run
1045 * verify-signed-buffer here for now...
1047 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1048 * warn("warning: signed tag unverified.");
1050 mergetag = xcalloc(1, sizeof(*mergetag));
1051 mergetag->key = xstrdup("mergetag");
1052 mergetag->value = buf;
1053 mergetag->len = size;
1055 **tail = mergetag;
1056 *tail = &mergetag->next;
1057 return;
1059 free_return:
1060 free(buf);
1063 static struct {
1064 char result;
1065 const char *check;
1066 } sigcheck_gpg_status[] = {
1067 { 'G', "\n[GNUPG:] GOODSIG " },
1068 { 'B', "\n[GNUPG:] BADSIG " },
1069 { 'U', "\n[GNUPG:] TRUST_NEVER" },
1070 { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
1073 static void parse_gpg_output(struct signature_check *sigc)
1075 const char *buf = sigc->gpg_status;
1076 int i;
1078 /* Iterate over all search strings */
1079 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
1080 const char *found, *next;
1082 if (!prefixcmp(buf, sigcheck_gpg_status[i].check + 1)) {
1083 /* At the very beginning of the buffer */
1084 found = buf + strlen(sigcheck_gpg_status[i].check + 1);
1085 } else {
1086 found = strstr(buf, sigcheck_gpg_status[i].check);
1087 if (!found)
1088 continue;
1089 found += strlen(sigcheck_gpg_status[i].check);
1091 sigc->result = sigcheck_gpg_status[i].result;
1092 /* The trust messages are not followed by key/signer information */
1093 if (sigc->result != 'U') {
1094 sigc->key = xmemdupz(found, 16);
1095 found += 17;
1096 next = strchrnul(found, '\n');
1097 sigc->signer = xmemdupz(found, next - found);
1102 void check_commit_signature(const struct commit* commit, struct signature_check *sigc)
1104 struct strbuf payload = STRBUF_INIT;
1105 struct strbuf signature = STRBUF_INIT;
1106 struct strbuf gpg_output = STRBUF_INIT;
1107 struct strbuf gpg_status = STRBUF_INIT;
1108 int status;
1110 sigc->result = 'N';
1112 if (parse_signed_commit(commit->object.sha1,
1113 &payload, &signature) <= 0)
1114 goto out;
1115 status = verify_signed_buffer(payload.buf, payload.len,
1116 signature.buf, signature.len,
1117 &gpg_output, &gpg_status);
1118 if (status && !gpg_output.len)
1119 goto out;
1120 sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
1121 sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
1122 parse_gpg_output(sigc);
1124 out:
1125 strbuf_release(&gpg_status);
1126 strbuf_release(&gpg_output);
1127 strbuf_release(&payload);
1128 strbuf_release(&signature);
1133 void append_merge_tag_headers(struct commit_list *parents,
1134 struct commit_extra_header ***tail)
1136 while (parents) {
1137 struct commit *parent = parents->item;
1138 handle_signed_tag(parent, tail);
1139 parents = parents->next;
1143 static void add_extra_header(struct strbuf *buffer,
1144 struct commit_extra_header *extra)
1146 strbuf_addstr(buffer, extra->key);
1147 if (extra->len)
1148 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1149 else
1150 strbuf_addch(buffer, '\n');
1153 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1154 const char **exclude)
1156 struct commit_extra_header *extra = NULL;
1157 unsigned long size;
1158 enum object_type type;
1159 char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
1160 if (buffer && type == OBJ_COMMIT)
1161 extra = read_commit_extra_header_lines(buffer, size, exclude);
1162 free(buffer);
1163 return extra;
1166 static inline int standard_header_field(const char *field, size_t len)
1168 return ((len == 4 && !memcmp(field, "tree ", 5)) ||
1169 (len == 6 && !memcmp(field, "parent ", 7)) ||
1170 (len == 6 && !memcmp(field, "author ", 7)) ||
1171 (len == 9 && !memcmp(field, "committer ", 10)) ||
1172 (len == 8 && !memcmp(field, "encoding ", 9)));
1175 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1177 if (!exclude)
1178 return 0;
1180 while (*exclude) {
1181 size_t xlen = strlen(*exclude);
1182 if (len == xlen &&
1183 !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
1184 return 1;
1185 exclude++;
1187 return 0;
1190 static struct commit_extra_header *read_commit_extra_header_lines(
1191 const char *buffer, size_t size,
1192 const char **exclude)
1194 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1195 const char *line, *next, *eof, *eob;
1196 struct strbuf buf = STRBUF_INIT;
1198 for (line = buffer, eob = line + size;
1199 line < eob && *line != '\n';
1200 line = next) {
1201 next = memchr(line, '\n', eob - line);
1202 next = next ? next + 1 : eob;
1203 if (*line == ' ') {
1204 /* continuation */
1205 if (it)
1206 strbuf_add(&buf, line + 1, next - (line + 1));
1207 continue;
1209 if (it)
1210 it->value = strbuf_detach(&buf, &it->len);
1211 strbuf_reset(&buf);
1212 it = NULL;
1214 eof = strchr(line, ' ');
1215 if (next <= eof)
1216 eof = next;
1218 if (standard_header_field(line, eof - line) ||
1219 excluded_header_field(line, eof - line, exclude))
1220 continue;
1222 it = xcalloc(1, sizeof(*it));
1223 it->key = xmemdupz(line, eof-line);
1224 *tail = it;
1225 tail = &it->next;
1226 if (eof + 1 < next)
1227 strbuf_add(&buf, eof + 1, next - (eof + 1));
1229 if (it)
1230 it->value = strbuf_detach(&buf, &it->len);
1231 return extra;
1234 void free_commit_extra_headers(struct commit_extra_header *extra)
1236 while (extra) {
1237 struct commit_extra_header *next = extra->next;
1238 free(extra->key);
1239 free(extra->value);
1240 free(extra);
1241 extra = next;
1245 int commit_tree(const struct strbuf *msg, unsigned char *tree,
1246 struct commit_list *parents, unsigned char *ret,
1247 const char *author, const char *sign_commit)
1249 struct commit_extra_header *extra = NULL, **tail = &extra;
1250 int result;
1252 append_merge_tag_headers(parents, &tail);
1253 result = commit_tree_extended(msg, tree, parents, ret,
1254 author, sign_commit, extra);
1255 free_commit_extra_headers(extra);
1256 return result;
1259 static int find_invalid_utf8(const char *buf, int len)
1261 int offset = 0;
1263 while (len) {
1264 unsigned char c = *buf++;
1265 int bytes, bad_offset;
1267 len--;
1268 offset++;
1270 /* Simple US-ASCII? No worries. */
1271 if (c < 0x80)
1272 continue;
1274 bad_offset = offset-1;
1277 * Count how many more high bits set: that's how
1278 * many more bytes this sequence should have.
1280 bytes = 0;
1281 while (c & 0x40) {
1282 c <<= 1;
1283 bytes++;
1286 /* Must be between 1 and 5 more bytes */
1287 if (bytes < 1 || bytes > 5)
1288 return bad_offset;
1290 /* Do we *have* that many bytes? */
1291 if (len < bytes)
1292 return bad_offset;
1294 offset += bytes;
1295 len -= bytes;
1297 /* And verify that they are good continuation bytes */
1298 do {
1299 if ((*buf++ & 0xc0) != 0x80)
1300 return bad_offset;
1301 } while (--bytes);
1303 /* We could/should check the value and length here too */
1305 return -1;
1309 * This verifies that the buffer is in proper utf8 format.
1311 * If it isn't, it assumes any non-utf8 characters are Latin1,
1312 * and does the conversion.
1314 * Fixme: we should probably also disallow overlong forms and
1315 * invalid characters. But we don't do that currently.
1317 static int verify_utf8(struct strbuf *buf)
1319 int ok = 1;
1320 long pos = 0;
1322 for (;;) {
1323 int bad;
1324 unsigned char c;
1325 unsigned char replace[2];
1327 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1328 if (bad < 0)
1329 return ok;
1330 pos += bad;
1331 ok = 0;
1332 c = buf->buf[pos];
1333 strbuf_remove(buf, pos, 1);
1335 /* We know 'c' must be in the range 128-255 */
1336 replace[0] = 0xc0 + (c >> 6);
1337 replace[1] = 0x80 + (c & 0x3f);
1338 strbuf_insert(buf, pos, replace, 2);
1339 pos += 2;
1343 static const char commit_utf8_warn[] =
1344 "Warning: commit message did not conform to UTF-8.\n"
1345 "You may want to amend it after fixing the message, or set the config\n"
1346 "variable i18n.commitencoding to the encoding your project uses.\n";
1348 int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
1349 struct commit_list *parents, unsigned char *ret,
1350 const char *author, const char *sign_commit,
1351 struct commit_extra_header *extra)
1353 int result;
1354 int encoding_is_utf8;
1355 struct strbuf buffer;
1357 assert_sha1_type(tree, OBJ_TREE);
1359 if (memchr(msg->buf, '\0', msg->len))
1360 return error("a NUL byte in commit log message not allowed.");
1362 /* Not having i18n.commitencoding is the same as having utf-8 */
1363 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1365 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1366 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1369 * NOTE! This ordering means that the same exact tree merged with a
1370 * different order of parents will be a _different_ changeset even
1371 * if everything else stays the same.
1373 while (parents) {
1374 struct commit_list *next = parents->next;
1375 struct commit *parent = parents->item;
1377 strbuf_addf(&buffer, "parent %s\n",
1378 sha1_to_hex(parent->object.sha1));
1379 free(parents);
1380 parents = next;
1383 /* Person/date information */
1384 if (!author)
1385 author = git_author_info(IDENT_STRICT);
1386 strbuf_addf(&buffer, "author %s\n", author);
1387 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1388 if (!encoding_is_utf8)
1389 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1391 while (extra) {
1392 add_extra_header(&buffer, extra);
1393 extra = extra->next;
1395 strbuf_addch(&buffer, '\n');
1397 /* And add the comment */
1398 strbuf_addbuf(&buffer, msg);
1400 /* And check the encoding */
1401 if (encoding_is_utf8 && !verify_utf8(&buffer))
1402 fprintf(stderr, commit_utf8_warn);
1404 if (sign_commit && do_sign_commit(&buffer, sign_commit))
1405 return -1;
1407 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1408 strbuf_release(&buffer);
1409 return result;
1412 struct commit *get_merge_parent(const char *name)
1414 struct object *obj;
1415 struct commit *commit;
1416 unsigned char sha1[20];
1417 if (get_sha1(name, sha1))
1418 return NULL;
1419 obj = parse_object(sha1);
1420 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1421 if (commit && !commit->util) {
1422 struct merge_remote_desc *desc;
1423 desc = xmalloc(sizeof(*desc));
1424 desc->obj = obj;
1425 desc->name = strdup(name);
1426 commit->util = desc;
1428 return commit;
1432 * Append a commit to the end of the commit_list.
1434 * next starts by pointing to the variable that holds the head of an
1435 * empty commit_list, and is updated to point to the "next" field of
1436 * the last item on the list as new commits are appended.
1438 * Usage example:
1440 * struct commit_list *list;
1441 * struct commit_list **next = &list;
1443 * next = commit_list_append(c1, next);
1444 * next = commit_list_append(c2, next);
1445 * assert(commit_list_count(list) == 2);
1446 * return list;
1448 struct commit_list **commit_list_append(struct commit *commit,
1449 struct commit_list **next)
1451 struct commit_list *new = xmalloc(sizeof(struct commit_list));
1452 new->item = commit;
1453 *next = new;
1454 new->next = NULL;
1455 return &new->next;
1458 void print_commit_list(struct commit_list *list,
1459 const char *format_cur,
1460 const char *format_last)
1462 for ( ; list; list = list->next) {
1463 const char *format = list->next ? format_cur : format_last;
1464 printf(format, sha1_to_hex(list->item->object.sha1));