revision: insert unsorted, then sort in prepare_revision_walk()
[git/jrn.git] / commit.c
blob0759b2ca655d8d5839fe46cba477135414d0826c
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"
12 int save_commit_buffer = 1;
14 const char *commit_type = "commit";
16 static struct commit *check_commit(struct object *obj,
17 const unsigned char *sha1,
18 int quiet)
20 if (obj->type != OBJ_COMMIT) {
21 if (!quiet)
22 error("Object %s is a %s, not a commit",
23 sha1_to_hex(sha1), typename(obj->type));
24 return NULL;
26 return (struct commit *) obj;
29 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
30 int quiet)
32 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
34 if (!obj)
35 return NULL;
36 return check_commit(obj, sha1, quiet);
39 struct commit *lookup_commit_reference(const unsigned char *sha1)
41 return lookup_commit_reference_gently(sha1, 0);
44 struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name)
46 struct commit *c = lookup_commit_reference(sha1);
47 if (!c)
48 die(_("could not parse %s"), ref_name);
49 if (hashcmp(sha1, c->object.sha1)) {
50 warning(_("%s %s is not a commit!"),
51 ref_name, sha1_to_hex(sha1));
53 return c;
56 struct commit *lookup_commit(const unsigned char *sha1)
58 struct object *obj = lookup_object(sha1);
59 if (!obj)
60 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
61 if (!obj->type)
62 obj->type = OBJ_COMMIT;
63 return check_commit(obj, sha1, 0);
66 struct commit *lookup_commit_reference_by_name(const char *name)
68 unsigned char sha1[20];
69 struct commit *commit;
71 if (get_sha1(name, sha1))
72 return NULL;
73 commit = lookup_commit_reference(sha1);
74 if (!commit || parse_commit(commit))
75 return NULL;
76 return commit;
79 static unsigned long parse_commit_date(const char *buf, const char *tail)
81 const char *dateptr;
83 if (buf + 6 >= tail)
84 return 0;
85 if (memcmp(buf, "author", 6))
86 return 0;
87 while (buf < tail && *buf++ != '\n')
88 /* nada */;
89 if (buf + 9 >= tail)
90 return 0;
91 if (memcmp(buf, "committer", 9))
92 return 0;
93 while (buf < tail && *buf++ != '>')
94 /* nada */;
95 if (buf >= tail)
96 return 0;
97 dateptr = buf;
98 while (buf < tail && *buf++ != '\n')
99 /* nada */;
100 if (buf >= tail)
101 return 0;
102 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
103 return strtoul(dateptr, NULL, 10);
106 static struct commit_graft **commit_graft;
107 static int commit_graft_alloc, commit_graft_nr;
109 static int commit_graft_pos(const unsigned char *sha1)
111 int lo, hi;
112 lo = 0;
113 hi = commit_graft_nr;
114 while (lo < hi) {
115 int mi = (lo + hi) / 2;
116 struct commit_graft *graft = commit_graft[mi];
117 int cmp = hashcmp(sha1, graft->sha1);
118 if (!cmp)
119 return mi;
120 if (cmp < 0)
121 hi = mi;
122 else
123 lo = mi + 1;
125 return -lo - 1;
128 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
130 int pos = commit_graft_pos(graft->sha1);
132 if (0 <= pos) {
133 if (ignore_dups)
134 free(graft);
135 else {
136 free(commit_graft[pos]);
137 commit_graft[pos] = graft;
139 return 1;
141 pos = -pos - 1;
142 if (commit_graft_alloc <= ++commit_graft_nr) {
143 commit_graft_alloc = alloc_nr(commit_graft_alloc);
144 commit_graft = xrealloc(commit_graft,
145 sizeof(*commit_graft) *
146 commit_graft_alloc);
148 if (pos < commit_graft_nr)
149 memmove(commit_graft + pos + 1,
150 commit_graft + pos,
151 (commit_graft_nr - pos - 1) *
152 sizeof(*commit_graft));
153 commit_graft[pos] = graft;
154 return 0;
157 struct commit_graft *read_graft_line(char *buf, int len)
159 /* The format is just "Commit Parent1 Parent2 ...\n" */
160 int i;
161 struct commit_graft *graft = NULL;
163 while (len && isspace(buf[len-1]))
164 buf[--len] = '\0';
165 if (buf[0] == '#' || buf[0] == '\0')
166 return NULL;
167 if ((len + 1) % 41)
168 goto bad_graft_data;
169 i = (len + 1) / 41 - 1;
170 graft = xmalloc(sizeof(*graft) + 20 * i);
171 graft->nr_parent = i;
172 if (get_sha1_hex(buf, graft->sha1))
173 goto bad_graft_data;
174 for (i = 40; i < len; i += 41) {
175 if (buf[i] != ' ')
176 goto bad_graft_data;
177 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
178 goto bad_graft_data;
180 return graft;
182 bad_graft_data:
183 error("bad graft data: %s", buf);
184 free(graft);
185 return NULL;
188 static int read_graft_file(const char *graft_file)
190 FILE *fp = fopen(graft_file, "r");
191 char buf[1024];
192 if (!fp)
193 return -1;
194 while (fgets(buf, sizeof(buf), fp)) {
195 /* The format is just "Commit Parent1 Parent2 ...\n" */
196 int len = strlen(buf);
197 struct commit_graft *graft = read_graft_line(buf, len);
198 if (!graft)
199 continue;
200 if (register_commit_graft(graft, 1))
201 error("duplicate graft data: %s", buf);
203 fclose(fp);
204 return 0;
207 static void prepare_commit_graft(void)
209 static int commit_graft_prepared;
210 char *graft_file;
212 if (commit_graft_prepared)
213 return;
214 graft_file = get_graft_file();
215 read_graft_file(graft_file);
216 /* make sure shallows are read */
217 is_repository_shallow();
218 commit_graft_prepared = 1;
221 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
223 int pos;
224 prepare_commit_graft();
225 pos = commit_graft_pos(sha1);
226 if (pos < 0)
227 return NULL;
228 return commit_graft[pos];
231 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
233 int i, ret;
234 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
235 ret = fn(commit_graft[i], cb_data);
236 return ret;
239 int unregister_shallow(const unsigned char *sha1)
241 int pos = commit_graft_pos(sha1);
242 if (pos < 0)
243 return -1;
244 if (pos + 1 < commit_graft_nr)
245 memmove(commit_graft + pos, commit_graft + pos + 1,
246 sizeof(struct commit_graft *)
247 * (commit_graft_nr - pos - 1));
248 commit_graft_nr--;
249 return 0;
252 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
254 const char *tail = buffer;
255 const char *bufptr = buffer;
256 unsigned char parent[20];
257 struct commit_list **pptr;
258 struct commit_graft *graft;
260 if (item->object.parsed)
261 return 0;
262 item->object.parsed = 1;
263 tail += size;
264 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
265 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
266 if (get_sha1_hex(bufptr + 5, parent) < 0)
267 return error("bad tree pointer in commit %s",
268 sha1_to_hex(item->object.sha1));
269 item->tree = lookup_tree(parent);
270 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
271 pptr = &item->parents;
273 graft = lookup_commit_graft(item->object.sha1);
274 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
275 struct commit *new_parent;
277 if (tail <= bufptr + 48 ||
278 get_sha1_hex(bufptr + 7, parent) ||
279 bufptr[47] != '\n')
280 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
281 bufptr += 48;
283 * The clone is shallow if nr_parent < 0, and we must
284 * not traverse its real parents even when we unhide them.
286 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
287 continue;
288 new_parent = lookup_commit(parent);
289 if (new_parent)
290 pptr = &commit_list_insert(new_parent, pptr)->next;
292 if (graft) {
293 int i;
294 struct commit *new_parent;
295 for (i = 0; i < graft->nr_parent; i++) {
296 new_parent = lookup_commit(graft->parent[i]);
297 if (!new_parent)
298 continue;
299 pptr = &commit_list_insert(new_parent, pptr)->next;
302 item->date = parse_commit_date(bufptr, tail);
304 return 0;
307 int parse_commit(struct commit *item)
309 enum object_type type;
310 void *buffer;
311 unsigned long size;
312 int ret;
314 if (!item)
315 return -1;
316 if (item->object.parsed)
317 return 0;
318 buffer = read_sha1_file(item->object.sha1, &type, &size);
319 if (!buffer)
320 return error("Could not read %s",
321 sha1_to_hex(item->object.sha1));
322 if (type != OBJ_COMMIT) {
323 free(buffer);
324 return error("Object %s not a commit",
325 sha1_to_hex(item->object.sha1));
327 ret = parse_commit_buffer(item, buffer, size);
328 if (save_commit_buffer && !ret) {
329 item->buffer = buffer;
330 return 0;
332 free(buffer);
333 return ret;
336 int find_commit_subject(const char *commit_buffer, const char **subject)
338 const char *eol;
339 const char *p = commit_buffer;
341 while (*p && (*p != '\n' || p[1] != '\n'))
342 p++;
343 if (*p) {
344 p += 2;
345 for (eol = p; *eol && *eol != '\n'; eol++)
346 ; /* do nothing */
347 } else
348 eol = p;
350 *subject = p;
352 return eol - p;
355 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
357 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
358 new_list->item = item;
359 new_list->next = *list_p;
360 *list_p = new_list;
361 return new_list;
364 void commit_list_reverse(struct commit_list **list_p)
366 struct commit_list *prev = NULL, *curr = *list_p, *next;
368 if (!list_p)
369 return;
370 while (curr) {
371 next = curr->next;
372 curr->next = prev;
373 prev = curr;
374 curr = next;
376 *list_p = prev;
379 unsigned commit_list_count(const struct commit_list *l)
381 unsigned c = 0;
382 for (; l; l = l->next )
383 c++;
384 return c;
387 void free_commit_list(struct commit_list *list)
389 while (list) {
390 struct commit_list *temp = list;
391 list = temp->next;
392 free(temp);
396 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
398 struct commit_list **pp = list;
399 struct commit_list *p;
400 while ((p = *pp) != NULL) {
401 if (p->item->date < item->date) {
402 break;
404 pp = &p->next;
406 return commit_list_insert(item, pp);
409 static int commit_list_compare_by_date(const void *a, const void *b)
411 unsigned long a_date = ((const struct commit_list *)a)->item->date;
412 unsigned long b_date = ((const struct commit_list *)b)->item->date;
413 if (a_date < b_date)
414 return 1;
415 if (a_date > b_date)
416 return -1;
417 return 0;
420 static void *commit_list_get_next(const void *a)
422 return ((const struct commit_list *)a)->next;
425 static void commit_list_set_next(void *a, void *next)
427 ((struct commit_list *)a)->next = next;
430 void commit_list_sort_by_date(struct commit_list **list)
432 *list = mergesort(*list, commit_list_get_next, commit_list_set_next,
433 commit_list_compare_by_date);
436 struct commit *pop_most_recent_commit(struct commit_list **list,
437 unsigned int mark)
439 struct commit *ret = (*list)->item;
440 struct commit_list *parents = ret->parents;
441 struct commit_list *old = *list;
443 *list = (*list)->next;
444 free(old);
446 while (parents) {
447 struct commit *commit = parents->item;
448 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
449 commit->object.flags |= mark;
450 commit_list_insert_by_date(commit, list);
452 parents = parents->next;
454 return ret;
457 void clear_commit_marks(struct commit *commit, unsigned int mark)
459 while (commit) {
460 struct commit_list *parents;
462 if (!(mark & commit->object.flags))
463 return;
465 commit->object.flags &= ~mark;
467 parents = commit->parents;
468 if (!parents)
469 return;
471 while ((parents = parents->next))
472 clear_commit_marks(parents->item, mark);
474 commit = commit->parents->item;
478 void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
480 struct object *object;
481 struct commit *commit;
482 unsigned int i;
484 for (i = 0; i < a->nr; i++) {
485 object = a->objects[i].item;
486 commit = lookup_commit_reference_gently(object->sha1, 1);
487 if (commit)
488 clear_commit_marks(commit, mark);
492 struct commit *pop_commit(struct commit_list **stack)
494 struct commit_list *top = *stack;
495 struct commit *item = top ? top->item : NULL;
497 if (top) {
498 *stack = top->next;
499 free(top);
501 return item;
505 * Performs an in-place topological sort on the list supplied.
507 void sort_in_topological_order(struct commit_list ** list, int lifo)
509 struct commit_list *next, *orig = *list;
510 struct commit_list *work, **insert;
511 struct commit_list **pptr;
513 if (!orig)
514 return;
515 *list = NULL;
517 /* Mark them and clear the indegree */
518 for (next = orig; next; next = next->next) {
519 struct commit *commit = next->item;
520 commit->indegree = 1;
523 /* update the indegree */
524 for (next = orig; next; next = next->next) {
525 struct commit_list * parents = next->item->parents;
526 while (parents) {
527 struct commit *parent = parents->item;
529 if (parent->indegree)
530 parent->indegree++;
531 parents = parents->next;
536 * find the tips
538 * tips are nodes not reachable from any other node in the list
540 * the tips serve as a starting set for the work queue.
542 work = NULL;
543 insert = &work;
544 for (next = orig; next; next = next->next) {
545 struct commit *commit = next->item;
547 if (commit->indegree == 1)
548 insert = &commit_list_insert(commit, insert)->next;
551 /* process the list in topological order */
552 if (!lifo)
553 commit_list_sort_by_date(&work);
555 pptr = list;
556 *list = NULL;
557 while (work) {
558 struct commit *commit;
559 struct commit_list *parents, *work_item;
561 work_item = work;
562 work = work_item->next;
563 work_item->next = NULL;
565 commit = work_item->item;
566 for (parents = commit->parents; parents ; parents = parents->next) {
567 struct commit *parent = parents->item;
569 if (!parent->indegree)
570 continue;
573 * parents are only enqueued for emission
574 * when all their children have been emitted thereby
575 * guaranteeing topological order.
577 if (--parent->indegree == 1) {
578 if (!lifo)
579 commit_list_insert_by_date(parent, &work);
580 else
581 commit_list_insert(parent, &work);
585 * work_item is a commit all of whose children
586 * have already been emitted. we can emit it now.
588 commit->indegree = 0;
589 *pptr = work_item;
590 pptr = &work_item->next;
594 /* merge-base stuff */
596 /* bits #0..15 in revision.h */
597 #define PARENT1 (1u<<16)
598 #define PARENT2 (1u<<17)
599 #define STALE (1u<<18)
600 #define RESULT (1u<<19)
602 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
604 static struct commit *interesting(struct commit_list *list)
606 while (list) {
607 struct commit *commit = list->item;
608 list = list->next;
609 if (commit->object.flags & STALE)
610 continue;
611 return commit;
613 return NULL;
616 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
618 struct commit_list *list = NULL;
619 struct commit_list *result = NULL;
620 int i;
622 for (i = 0; i < n; i++) {
623 if (one == twos[i])
625 * We do not mark this even with RESULT so we do not
626 * have to clean it up.
628 return commit_list_insert(one, &result);
631 if (parse_commit(one))
632 return NULL;
633 for (i = 0; i < n; i++) {
634 if (parse_commit(twos[i]))
635 return NULL;
638 one->object.flags |= PARENT1;
639 commit_list_insert_by_date(one, &list);
640 for (i = 0; i < n; i++) {
641 twos[i]->object.flags |= PARENT2;
642 commit_list_insert_by_date(twos[i], &list);
645 while (interesting(list)) {
646 struct commit *commit;
647 struct commit_list *parents;
648 struct commit_list *next;
649 int flags;
651 commit = list->item;
652 next = list->next;
653 free(list);
654 list = next;
656 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
657 if (flags == (PARENT1 | PARENT2)) {
658 if (!(commit->object.flags & RESULT)) {
659 commit->object.flags |= RESULT;
660 commit_list_insert_by_date(commit, &result);
662 /* Mark parents of a found merge stale */
663 flags |= STALE;
665 parents = commit->parents;
666 while (parents) {
667 struct commit *p = parents->item;
668 parents = parents->next;
669 if ((p->object.flags & flags) == flags)
670 continue;
671 if (parse_commit(p))
672 return NULL;
673 p->object.flags |= flags;
674 commit_list_insert_by_date(p, &list);
678 /* Clean up the result to remove stale ones */
679 free_commit_list(list);
680 list = result; result = NULL;
681 while (list) {
682 struct commit_list *next = list->next;
683 if (!(list->item->object.flags & STALE))
684 commit_list_insert_by_date(list->item, &result);
685 free(list);
686 list = next;
688 return result;
691 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
693 struct commit_list *i, *j, *k, *ret = NULL;
694 struct commit_list **pptr = &ret;
696 for (i = in; i; i = i->next) {
697 if (!ret)
698 pptr = &commit_list_insert(i->item, pptr)->next;
699 else {
700 struct commit_list *new = NULL, *end = NULL;
702 for (j = ret; j; j = j->next) {
703 struct commit_list *bases;
704 bases = get_merge_bases(i->item, j->item, 1);
705 if (!new)
706 new = bases;
707 else
708 end->next = bases;
709 for (k = bases; k; k = k->next)
710 end = k;
712 ret = new;
715 return ret;
718 struct commit_list *get_merge_bases_many(struct commit *one,
719 int n,
720 struct commit **twos,
721 int cleanup)
723 struct commit_list *list;
724 struct commit **rslt;
725 struct commit_list *result;
726 int cnt, i, j;
728 result = merge_bases_many(one, n, twos);
729 for (i = 0; i < n; i++) {
730 if (one == twos[i])
731 return result;
733 if (!result || !result->next) {
734 if (cleanup) {
735 clear_commit_marks(one, all_flags);
736 for (i = 0; i < n; i++)
737 clear_commit_marks(twos[i], all_flags);
739 return result;
742 /* There are more than one */
743 cnt = 0;
744 list = result;
745 while (list) {
746 list = list->next;
747 cnt++;
749 rslt = xcalloc(cnt, sizeof(*rslt));
750 for (list = result, i = 0; list; list = list->next)
751 rslt[i++] = list->item;
752 free_commit_list(result);
754 clear_commit_marks(one, all_flags);
755 for (i = 0; i < n; i++)
756 clear_commit_marks(twos[i], all_flags);
757 for (i = 0; i < cnt - 1; i++) {
758 for (j = i+1; j < cnt; j++) {
759 if (!rslt[i] || !rslt[j])
760 continue;
761 result = merge_bases_many(rslt[i], 1, &rslt[j]);
762 clear_commit_marks(rslt[i], all_flags);
763 clear_commit_marks(rslt[j], all_flags);
764 for (list = result; list; list = list->next) {
765 if (rslt[i] == list->item)
766 rslt[i] = NULL;
767 if (rslt[j] == list->item)
768 rslt[j] = NULL;
773 /* Surviving ones in rslt[] are the independent results */
774 result = NULL;
775 for (i = 0; i < cnt; i++) {
776 if (rslt[i])
777 commit_list_insert_by_date(rslt[i], &result);
779 free(rslt);
780 return result;
783 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
784 int cleanup)
786 return get_merge_bases_many(one, 1, &two, cleanup);
789 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
791 if (!with_commit)
792 return 1;
793 while (with_commit) {
794 struct commit *other;
796 other = with_commit->item;
797 with_commit = with_commit->next;
798 if (in_merge_bases(other, &commit, 1))
799 return 1;
801 return 0;
804 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
806 struct commit_list *bases, *b;
807 int ret = 0;
809 if (num == 1)
810 bases = get_merge_bases(commit, *reference, 1);
811 else
812 die("not yet");
813 for (b = bases; b; b = b->next) {
814 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
815 ret = 1;
816 break;
820 free_commit_list(bases);
821 return ret;
824 struct commit_list *reduce_heads(struct commit_list *heads)
826 struct commit_list *p;
827 struct commit_list *result = NULL, **tail = &result;
828 struct commit **other;
829 size_t num_head, num_other;
831 if (!heads)
832 return NULL;
834 /* Avoid unnecessary reallocations */
835 for (p = heads, num_head = 0; p; p = p->next)
836 num_head++;
837 other = xcalloc(sizeof(*other), num_head);
839 /* For each commit, see if it can be reached by others */
840 for (p = heads; p; p = p->next) {
841 struct commit_list *q, *base;
843 /* Do we already have this in the result? */
844 for (q = result; q; q = q->next)
845 if (p->item == q->item)
846 break;
847 if (q)
848 continue;
850 num_other = 0;
851 for (q = heads; q; q = q->next) {
852 if (p->item == q->item)
853 continue;
854 other[num_other++] = q->item;
856 if (num_other)
857 base = get_merge_bases_many(p->item, num_other, other, 1);
858 else
859 base = NULL;
861 * If p->item does not have anything common with other
862 * commits, there won't be any merge base. If it is
863 * reachable from some of the others, p->item will be
864 * the merge base. If its history is connected with
865 * others, but p->item is not reachable by others, we
866 * will get something other than p->item back.
868 if (!base || (base->item != p->item))
869 tail = &(commit_list_insert(p->item, tail)->next);
870 free_commit_list(base);
872 free(other);
873 return result;
876 static const char gpg_sig_header[] = "gpgsig";
877 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
879 static int do_sign_commit(struct strbuf *buf, const char *keyid)
881 struct strbuf sig = STRBUF_INIT;
882 int inspos, copypos;
884 /* find the end of the header */
885 inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
887 if (!keyid || !*keyid)
888 keyid = get_signing_key();
889 if (sign_buffer(buf, &sig, keyid)) {
890 strbuf_release(&sig);
891 return -1;
894 for (copypos = 0; sig.buf[copypos]; ) {
895 const char *bol = sig.buf + copypos;
896 const char *eol = strchrnul(bol, '\n');
897 int len = (eol - bol) + !!*eol;
899 if (!copypos) {
900 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
901 inspos += gpg_sig_header_len;
903 strbuf_insert(buf, inspos++, " ", 1);
904 strbuf_insert(buf, inspos, bol, len);
905 inspos += len;
906 copypos += len;
908 strbuf_release(&sig);
909 return 0;
912 int parse_signed_commit(const unsigned char *sha1,
913 struct strbuf *payload, struct strbuf *signature)
915 unsigned long size;
916 enum object_type type;
917 char *buffer = read_sha1_file(sha1, &type, &size);
918 int in_signature, saw_signature = -1;
919 char *line, *tail;
921 if (!buffer || type != OBJ_COMMIT)
922 goto cleanup;
924 line = buffer;
925 tail = buffer + size;
926 in_signature = 0;
927 saw_signature = 0;
928 while (line < tail) {
929 const char *sig = NULL;
930 char *next = memchr(line, '\n', tail - line);
932 next = next ? next + 1 : tail;
933 if (in_signature && line[0] == ' ')
934 sig = line + 1;
935 else if (!prefixcmp(line, gpg_sig_header) &&
936 line[gpg_sig_header_len] == ' ')
937 sig = line + gpg_sig_header_len + 1;
938 if (sig) {
939 strbuf_add(signature, sig, next - sig);
940 saw_signature = 1;
941 in_signature = 1;
942 } else {
943 if (*line == '\n')
944 /* dump the whole remainder of the buffer */
945 next = tail;
946 strbuf_add(payload, line, next - line);
947 in_signature = 0;
949 line = next;
951 cleanup:
952 free(buffer);
953 return saw_signature;
956 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
958 struct merge_remote_desc *desc;
959 struct commit_extra_header *mergetag;
960 char *buf;
961 unsigned long size, len;
962 enum object_type type;
964 desc = merge_remote_util(parent);
965 if (!desc || !desc->obj)
966 return;
967 buf = read_sha1_file(desc->obj->sha1, &type, &size);
968 if (!buf || type != OBJ_TAG)
969 goto free_return;
970 len = parse_signature(buf, size);
971 if (size == len)
972 goto free_return;
974 * We could verify this signature and either omit the tag when
975 * it does not validate, but the integrator may not have the
976 * public key of the signer of the tag he is merging, while a
977 * later auditor may have it while auditing, so let's not run
978 * verify-signed-buffer here for now...
980 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
981 * warn("warning: signed tag unverified.");
983 mergetag = xcalloc(1, sizeof(*mergetag));
984 mergetag->key = xstrdup("mergetag");
985 mergetag->value = buf;
986 mergetag->len = size;
988 **tail = mergetag;
989 *tail = &mergetag->next;
990 return;
992 free_return:
993 free(buf);
996 void append_merge_tag_headers(struct commit_list *parents,
997 struct commit_extra_header ***tail)
999 while (parents) {
1000 struct commit *parent = parents->item;
1001 handle_signed_tag(parent, tail);
1002 parents = parents->next;
1006 static void add_extra_header(struct strbuf *buffer,
1007 struct commit_extra_header *extra)
1009 strbuf_addstr(buffer, extra->key);
1010 if (extra->len)
1011 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1012 else
1013 strbuf_addch(buffer, '\n');
1016 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1017 const char **exclude)
1019 struct commit_extra_header *extra = NULL;
1020 unsigned long size;
1021 enum object_type type;
1022 char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
1023 if (buffer && type == OBJ_COMMIT)
1024 extra = read_commit_extra_header_lines(buffer, size, exclude);
1025 free(buffer);
1026 return extra;
1029 static inline int standard_header_field(const char *field, size_t len)
1031 return ((len == 4 && !memcmp(field, "tree ", 5)) ||
1032 (len == 6 && !memcmp(field, "parent ", 7)) ||
1033 (len == 6 && !memcmp(field, "author ", 7)) ||
1034 (len == 9 && !memcmp(field, "committer ", 10)) ||
1035 (len == 8 && !memcmp(field, "encoding ", 9)));
1038 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1040 if (!exclude)
1041 return 0;
1043 while (*exclude) {
1044 size_t xlen = strlen(*exclude);
1045 if (len == xlen &&
1046 !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
1047 return 1;
1048 exclude++;
1050 return 0;
1053 struct commit_extra_header *read_commit_extra_header_lines(const char *buffer, size_t size,
1054 const char **exclude)
1056 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1057 const char *line, *next, *eof, *eob;
1058 struct strbuf buf = STRBUF_INIT;
1060 for (line = buffer, eob = line + size;
1061 line < eob && *line != '\n';
1062 line = next) {
1063 next = memchr(line, '\n', eob - line);
1064 next = next ? next + 1 : eob;
1065 if (*line == ' ') {
1066 /* continuation */
1067 if (it)
1068 strbuf_add(&buf, line + 1, next - (line + 1));
1069 continue;
1071 if (it)
1072 it->value = strbuf_detach(&buf, &it->len);
1073 strbuf_reset(&buf);
1074 it = NULL;
1076 eof = strchr(line, ' ');
1077 if (next <= eof)
1078 eof = next;
1080 if (standard_header_field(line, eof - line) ||
1081 excluded_header_field(line, eof - line, exclude))
1082 continue;
1084 it = xcalloc(1, sizeof(*it));
1085 it->key = xmemdupz(line, eof-line);
1086 *tail = it;
1087 tail = &it->next;
1088 if (eof + 1 < next)
1089 strbuf_add(&buf, eof + 1, next - (eof + 1));
1091 if (it)
1092 it->value = strbuf_detach(&buf, &it->len);
1093 return extra;
1096 void free_commit_extra_headers(struct commit_extra_header *extra)
1098 while (extra) {
1099 struct commit_extra_header *next = extra->next;
1100 free(extra->key);
1101 free(extra->value);
1102 free(extra);
1103 extra = next;
1107 int commit_tree(const struct strbuf *msg, unsigned char *tree,
1108 struct commit_list *parents, unsigned char *ret,
1109 const char *author, const char *sign_commit)
1111 struct commit_extra_header *extra = NULL, **tail = &extra;
1112 int result;
1114 append_merge_tag_headers(parents, &tail);
1115 result = commit_tree_extended(msg, tree, parents, ret,
1116 author, sign_commit, extra);
1117 free_commit_extra_headers(extra);
1118 return result;
1121 static const char commit_utf8_warn[] =
1122 "Warning: commit message does not conform to UTF-8.\n"
1123 "You may want to amend it after fixing the message, or set the config\n"
1124 "variable i18n.commitencoding to the encoding your project uses.\n";
1126 int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
1127 struct commit_list *parents, unsigned char *ret,
1128 const char *author, const char *sign_commit,
1129 struct commit_extra_header *extra)
1131 int result;
1132 int encoding_is_utf8;
1133 struct strbuf buffer;
1135 assert_sha1_type(tree, OBJ_TREE);
1137 if (memchr(msg->buf, '\0', msg->len))
1138 return error("a NUL byte in commit log message not allowed.");
1140 /* Not having i18n.commitencoding is the same as having utf-8 */
1141 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1143 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1144 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1147 * NOTE! This ordering means that the same exact tree merged with a
1148 * different order of parents will be a _different_ changeset even
1149 * if everything else stays the same.
1151 while (parents) {
1152 struct commit_list *next = parents->next;
1153 struct commit *parent = parents->item;
1155 strbuf_addf(&buffer, "parent %s\n",
1156 sha1_to_hex(parent->object.sha1));
1157 free(parents);
1158 parents = next;
1161 /* Person/date information */
1162 if (!author)
1163 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
1164 strbuf_addf(&buffer, "author %s\n", author);
1165 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
1166 if (!encoding_is_utf8)
1167 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1169 while (extra) {
1170 add_extra_header(&buffer, extra);
1171 extra = extra->next;
1173 strbuf_addch(&buffer, '\n');
1175 /* And add the comment */
1176 strbuf_addbuf(&buffer, msg);
1178 /* And check the encoding */
1179 if (encoding_is_utf8 && !is_utf8(buffer.buf))
1180 fprintf(stderr, commit_utf8_warn);
1182 if (sign_commit && do_sign_commit(&buffer, sign_commit))
1183 return -1;
1185 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1186 strbuf_release(&buffer);
1187 return result;
1190 struct commit *get_merge_parent(const char *name)
1192 struct object *obj;
1193 struct commit *commit;
1194 unsigned char sha1[20];
1195 if (get_sha1(name, sha1))
1196 return NULL;
1197 obj = parse_object(sha1);
1198 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1199 if (commit && !commit->util) {
1200 struct merge_remote_desc *desc;
1201 desc = xmalloc(sizeof(*desc));
1202 desc->obj = obj;
1203 desc->name = strdup(name);
1204 commit->util = desc;
1206 return commit;