commit.c: add in_merge_bases_many()
[git/git.git] / commit.c
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
12 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
13
14 int save_commit_buffer = 1;
15
16 const char *commit_type = "commit";
17
18 static struct commit *check_commit(struct object *obj,
19 const unsigned char *sha1,
20 int quiet)
21 {
22 if (obj->type != OBJ_COMMIT) {
23 if (!quiet)
24 error("Object %s is a %s, not a commit",
25 sha1_to_hex(sha1), typename(obj->type));
26 return NULL;
27 }
28 return (struct commit *) obj;
29 }
30
31 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
32 int quiet)
33 {
34 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
35
36 if (!obj)
37 return NULL;
38 return check_commit(obj, sha1, quiet);
39 }
40
41 struct commit *lookup_commit_reference(const unsigned char *sha1)
42 {
43 return lookup_commit_reference_gently(sha1, 0);
44 }
45
46 struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name)
47 {
48 struct commit *c = lookup_commit_reference(sha1);
49 if (!c)
50 die(_("could not parse %s"), ref_name);
51 if (hashcmp(sha1, c->object.sha1)) {
52 warning(_("%s %s is not a commit!"),
53 ref_name, sha1_to_hex(sha1));
54 }
55 return c;
56 }
57
58 struct commit *lookup_commit(const unsigned char *sha1)
59 {
60 struct object *obj = lookup_object(sha1);
61 if (!obj)
62 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
63 if (!obj->type)
64 obj->type = OBJ_COMMIT;
65 return check_commit(obj, sha1, 0);
66 }
67
68 struct commit *lookup_commit_reference_by_name(const char *name)
69 {
70 unsigned char sha1[20];
71 struct commit *commit;
72
73 if (get_sha1_committish(name, sha1))
74 return NULL;
75 commit = lookup_commit_reference(sha1);
76 if (!commit || parse_commit(commit))
77 return NULL;
78 return commit;
79 }
80
81 static unsigned long parse_commit_date(const char *buf, const char *tail)
82 {
83 const char *dateptr;
84
85 if (buf + 6 >= tail)
86 return 0;
87 if (memcmp(buf, "author", 6))
88 return 0;
89 while (buf < tail && *buf++ != '\n')
90 /* nada */;
91 if (buf + 9 >= tail)
92 return 0;
93 if (memcmp(buf, "committer", 9))
94 return 0;
95 while (buf < tail && *buf++ != '>')
96 /* nada */;
97 if (buf >= tail)
98 return 0;
99 dateptr = buf;
100 while (buf < tail && *buf++ != '\n')
101 /* nada */;
102 if (buf >= tail)
103 return 0;
104 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
105 return strtoul(dateptr, NULL, 10);
106 }
107
108 static struct commit_graft **commit_graft;
109 static int commit_graft_alloc, commit_graft_nr;
110
111 static int commit_graft_pos(const unsigned char *sha1)
112 {
113 int lo, hi;
114 lo = 0;
115 hi = commit_graft_nr;
116 while (lo < hi) {
117 int mi = (lo + hi) / 2;
118 struct commit_graft *graft = commit_graft[mi];
119 int cmp = hashcmp(sha1, graft->sha1);
120 if (!cmp)
121 return mi;
122 if (cmp < 0)
123 hi = mi;
124 else
125 lo = mi + 1;
126 }
127 return -lo - 1;
128 }
129
130 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
131 {
132 int pos = commit_graft_pos(graft->sha1);
133
134 if (0 <= pos) {
135 if (ignore_dups)
136 free(graft);
137 else {
138 free(commit_graft[pos]);
139 commit_graft[pos] = graft;
140 }
141 return 1;
142 }
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);
149 }
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;
157 }
158
159 struct commit_graft *read_graft_line(char *buf, int len)
160 {
161 /* The format is just "Commit Parent1 Parent2 ...\n" */
162 int i;
163 struct commit_graft *graft = NULL;
164
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;
181 }
182 return graft;
183
184 bad_graft_data:
185 error("bad graft data: %s", buf);
186 free(graft);
187 return NULL;
188 }
189
190 static int read_graft_file(const char *graft_file)
191 {
192 FILE *fp = fopen(graft_file, "r");
193 char buf[1024];
194 if (!fp)
195 return -1;
196 while (fgets(buf, sizeof(buf), fp)) {
197 /* The format is just "Commit Parent1 Parent2 ...\n" */
198 int len = strlen(buf);
199 struct commit_graft *graft = read_graft_line(buf, len);
200 if (!graft)
201 continue;
202 if (register_commit_graft(graft, 1))
203 error("duplicate graft data: %s", buf);
204 }
205 fclose(fp);
206 return 0;
207 }
208
209 static void prepare_commit_graft(void)
210 {
211 static int commit_graft_prepared;
212 char *graft_file;
213
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;
221 }
222
223 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
224 {
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];
231 }
232
233 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
234 {
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;
239 }
240
241 int unregister_shallow(const unsigned char *sha1)
242 {
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;
252 }
253
254 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
255 {
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;
261
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;
274
275 graft = lookup_commit_graft(item->object.sha1);
276 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
277 struct commit *new_parent;
278
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;
284 /*
285 * The clone is shallow if nr_parent < 0, and we must
286 * not traverse its real parents even when we unhide them.
287 */
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;
293 }
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;
302 }
303 }
304 item->date = parse_commit_date(bufptr, tail);
305
306 return 0;
307 }
308
309 int parse_commit(struct commit *item)
310 {
311 enum object_type type;
312 void *buffer;
313 unsigned long size;
314 int ret;
315
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));
328 }
329 ret = parse_commit_buffer(item, buffer, size);
330 if (save_commit_buffer && !ret) {
331 item->buffer = buffer;
332 return 0;
333 }
334 free(buffer);
335 return ret;
336 }
337
338 int find_commit_subject(const char *commit_buffer, const char **subject)
339 {
340 const char *eol;
341 const char *p = commit_buffer;
342
343 while (*p && (*p != '\n' || p[1] != '\n'))
344 p++;
345 if (*p) {
346 p += 2;
347 for (eol = p; *eol && *eol != '\n'; eol++)
348 ; /* do nothing */
349 } else
350 eol = p;
351
352 *subject = p;
353
354 return eol - p;
355 }
356
357 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
358 {
359 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
360 new_list->item = item;
361 new_list->next = *list_p;
362 *list_p = new_list;
363 return new_list;
364 }
365
366 unsigned commit_list_count(const struct commit_list *l)
367 {
368 unsigned c = 0;
369 for (; l; l = l->next )
370 c++;
371 return c;
372 }
373
374 void free_commit_list(struct commit_list *list)
375 {
376 while (list) {
377 struct commit_list *temp = list;
378 list = temp->next;
379 free(temp);
380 }
381 }
382
383 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
384 {
385 struct commit_list **pp = list;
386 struct commit_list *p;
387 while ((p = *pp) != NULL) {
388 if (p->item->date < item->date) {
389 break;
390 }
391 pp = &p->next;
392 }
393 return commit_list_insert(item, pp);
394 }
395
396 static int commit_list_compare_by_date(const void *a, const void *b)
397 {
398 unsigned long a_date = ((const struct commit_list *)a)->item->date;
399 unsigned long b_date = ((const struct commit_list *)b)->item->date;
400 if (a_date < b_date)
401 return 1;
402 if (a_date > b_date)
403 return -1;
404 return 0;
405 }
406
407 static void *commit_list_get_next(const void *a)
408 {
409 return ((const struct commit_list *)a)->next;
410 }
411
412 static void commit_list_set_next(void *a, void *next)
413 {
414 ((struct commit_list *)a)->next = next;
415 }
416
417 void commit_list_sort_by_date(struct commit_list **list)
418 {
419 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
420 commit_list_compare_by_date);
421 }
422
423 struct commit *pop_most_recent_commit(struct commit_list **list,
424 unsigned int mark)
425 {
426 struct commit *ret = (*list)->item;
427 struct commit_list *parents = ret->parents;
428 struct commit_list *old = *list;
429
430 *list = (*list)->next;
431 free(old);
432
433 while (parents) {
434 struct commit *commit = parents->item;
435 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
436 commit->object.flags |= mark;
437 commit_list_insert_by_date(commit, list);
438 }
439 parents = parents->next;
440 }
441 return ret;
442 }
443
444 static void clear_commit_marks_1(struct commit_list **plist,
445 struct commit *commit, unsigned int mark)
446 {
447 while (commit) {
448 struct commit_list *parents;
449
450 if (!(mark & commit->object.flags))
451 return;
452
453 commit->object.flags &= ~mark;
454
455 parents = commit->parents;
456 if (!parents)
457 return;
458
459 while ((parents = parents->next))
460 commit_list_insert(parents->item, plist);
461
462 commit = commit->parents->item;
463 }
464 }
465
466 void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
467 {
468 struct commit_list *list = NULL;
469
470 while (nr--) {
471 commit_list_insert(*commit, &list);
472 commit++;
473 }
474 while (list)
475 clear_commit_marks_1(&list, pop_commit(&list), mark);
476 }
477
478 void clear_commit_marks(struct commit *commit, unsigned int mark)
479 {
480 clear_commit_marks_many(1, &commit, mark);
481 }
482
483 void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
484 {
485 struct object *object;
486 struct commit *commit;
487 unsigned int i;
488
489 for (i = 0; i < a->nr; i++) {
490 object = a->objects[i].item;
491 commit = lookup_commit_reference_gently(object->sha1, 1);
492 if (commit)
493 clear_commit_marks(commit, mark);
494 }
495 }
496
497 struct commit *pop_commit(struct commit_list **stack)
498 {
499 struct commit_list *top = *stack;
500 struct commit *item = top ? top->item : NULL;
501
502 if (top) {
503 *stack = top->next;
504 free(top);
505 }
506 return item;
507 }
508
509 /*
510 * Performs an in-place topological sort on the list supplied.
511 */
512 void sort_in_topological_order(struct commit_list ** list, int lifo)
513 {
514 struct commit_list *next, *orig = *list;
515 struct commit_list *work, **insert;
516 struct commit_list **pptr;
517
518 if (!orig)
519 return;
520 *list = NULL;
521
522 /* Mark them and clear the indegree */
523 for (next = orig; next; next = next->next) {
524 struct commit *commit = next->item;
525 commit->indegree = 1;
526 }
527
528 /* update the indegree */
529 for (next = orig; next; next = next->next) {
530 struct commit_list * parents = next->item->parents;
531 while (parents) {
532 struct commit *parent = parents->item;
533
534 if (parent->indegree)
535 parent->indegree++;
536 parents = parents->next;
537 }
538 }
539
540 /*
541 * find the tips
542 *
543 * tips are nodes not reachable from any other node in the list
544 *
545 * the tips serve as a starting set for the work queue.
546 */
547 work = NULL;
548 insert = &work;
549 for (next = orig; next; next = next->next) {
550 struct commit *commit = next->item;
551
552 if (commit->indegree == 1)
553 insert = &commit_list_insert(commit, insert)->next;
554 }
555
556 /* process the list in topological order */
557 if (!lifo)
558 commit_list_sort_by_date(&work);
559
560 pptr = list;
561 *list = NULL;
562 while (work) {
563 struct commit *commit;
564 struct commit_list *parents, *work_item;
565
566 work_item = work;
567 work = work_item->next;
568 work_item->next = NULL;
569
570 commit = work_item->item;
571 for (parents = commit->parents; parents ; parents = parents->next) {
572 struct commit *parent = parents->item;
573
574 if (!parent->indegree)
575 continue;
576
577 /*
578 * parents are only enqueued for emission
579 * when all their children have been emitted thereby
580 * guaranteeing topological order.
581 */
582 if (--parent->indegree == 1) {
583 if (!lifo)
584 commit_list_insert_by_date(parent, &work);
585 else
586 commit_list_insert(parent, &work);
587 }
588 }
589 /*
590 * work_item is a commit all of whose children
591 * have already been emitted. we can emit it now.
592 */
593 commit->indegree = 0;
594 *pptr = work_item;
595 pptr = &work_item->next;
596 }
597 }
598
599 /* merge-base stuff */
600
601 /* bits #0..15 in revision.h */
602 #define PARENT1 (1u<<16)
603 #define PARENT2 (1u<<17)
604 #define STALE (1u<<18)
605 #define RESULT (1u<<19)
606
607 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
608
609 static struct commit *interesting(struct commit_list *list)
610 {
611 while (list) {
612 struct commit *commit = list->item;
613 list = list->next;
614 if (commit->object.flags & STALE)
615 continue;
616 return commit;
617 }
618 return NULL;
619 }
620
621 /* all input commits in one and twos[] must have been parsed! */
622 static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
623 {
624 struct commit_list *list = NULL;
625 struct commit_list *result = NULL;
626 int i;
627
628 one->object.flags |= PARENT1;
629 commit_list_insert_by_date(one, &list);
630 if (!n)
631 return list;
632 for (i = 0; i < n; i++) {
633 twos[i]->object.flags |= PARENT2;
634 commit_list_insert_by_date(twos[i], &list);
635 }
636
637 while (interesting(list)) {
638 struct commit *commit;
639 struct commit_list *parents;
640 struct commit_list *next;
641 int flags;
642
643 commit = list->item;
644 next = list->next;
645 free(list);
646 list = next;
647
648 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
649 if (flags == (PARENT1 | PARENT2)) {
650 if (!(commit->object.flags & RESULT)) {
651 commit->object.flags |= RESULT;
652 commit_list_insert_by_date(commit, &result);
653 }
654 /* Mark parents of a found merge stale */
655 flags |= STALE;
656 }
657 parents = commit->parents;
658 while (parents) {
659 struct commit *p = parents->item;
660 parents = parents->next;
661 if ((p->object.flags & flags) == flags)
662 continue;
663 if (parse_commit(p))
664 return NULL;
665 p->object.flags |= flags;
666 commit_list_insert_by_date(p, &list);
667 }
668 }
669
670 free_commit_list(list);
671 return result;
672 }
673
674 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
675 {
676 struct commit_list *list = NULL;
677 struct commit_list *result = NULL;
678 int i;
679
680 for (i = 0; i < n; i++) {
681 if (one == twos[i])
682 /*
683 * We do not mark this even with RESULT so we do not
684 * have to clean it up.
685 */
686 return commit_list_insert(one, &result);
687 }
688
689 if (parse_commit(one))
690 return NULL;
691 for (i = 0; i < n; i++) {
692 if (parse_commit(twos[i]))
693 return NULL;
694 }
695
696 list = paint_down_to_common(one, n, twos);
697
698 while (list) {
699 struct commit_list *next = list->next;
700 if (!(list->item->object.flags & STALE))
701 commit_list_insert_by_date(list->item, &result);
702 free(list);
703 list = next;
704 }
705 return result;
706 }
707
708 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
709 {
710 struct commit_list *i, *j, *k, *ret = NULL;
711 struct commit_list **pptr = &ret;
712
713 for (i = in; i; i = i->next) {
714 if (!ret)
715 pptr = &commit_list_insert(i->item, pptr)->next;
716 else {
717 struct commit_list *new = NULL, *end = NULL;
718
719 for (j = ret; j; j = j->next) {
720 struct commit_list *bases;
721 bases = get_merge_bases(i->item, j->item, 1);
722 if (!new)
723 new = bases;
724 else
725 end->next = bases;
726 for (k = bases; k; k = k->next)
727 end = k;
728 }
729 ret = new;
730 }
731 }
732 return ret;
733 }
734
735 static int remove_redundant(struct commit **array, int cnt)
736 {
737 /*
738 * Some commit in the array may be an ancestor of
739 * another commit. Move such commit to the end of
740 * the array, and return the number of commits that
741 * are independent from each other.
742 */
743 struct commit **work;
744 unsigned char *redundant;
745 int *filled_index;
746 int i, j, filled;
747
748 work = xcalloc(cnt, sizeof(*work));
749 redundant = xcalloc(cnt, 1);
750 filled_index = xmalloc(sizeof(*filled_index) * (cnt - 1));
751
752 for (i = 0; i < cnt; i++)
753 parse_commit(array[i]);
754 for (i = 0; i < cnt; i++) {
755 struct commit_list *common;
756
757 if (redundant[i])
758 continue;
759 for (j = filled = 0; j < cnt; j++) {
760 if (i == j || redundant[j])
761 continue;
762 filled_index[filled] = j;
763 work[filled++] = array[j];
764 }
765 common = paint_down_to_common(array[i], filled, work);
766 if (array[i]->object.flags & PARENT2)
767 redundant[i] = 1;
768 for (j = 0; j < filled; j++)
769 if (work[j]->object.flags & PARENT1)
770 redundant[filled_index[j]] = 1;
771 clear_commit_marks(array[i], all_flags);
772 for (j = 0; j < filled; j++)
773 clear_commit_marks(work[j], all_flags);
774 free_commit_list(common);
775 }
776
777 /* Now collect the result */
778 memcpy(work, array, sizeof(*array) * cnt);
779 for (i = filled = 0; i < cnt; i++)
780 if (!redundant[i])
781 array[filled++] = work[i];
782 for (j = filled, i = 0; i < cnt; i++)
783 if (redundant[i])
784 array[j++] = work[i];
785 free(work);
786 free(redundant);
787 free(filled_index);
788 return filled;
789 }
790
791 struct commit_list *get_merge_bases_many(struct commit *one,
792 int n,
793 struct commit **twos,
794 int cleanup)
795 {
796 struct commit_list *list;
797 struct commit **rslt;
798 struct commit_list *result;
799 int cnt, i;
800
801 result = merge_bases_many(one, n, twos);
802 for (i = 0; i < n; i++) {
803 if (one == twos[i])
804 return result;
805 }
806 if (!result || !result->next) {
807 if (cleanup) {
808 clear_commit_marks(one, all_flags);
809 clear_commit_marks_many(n, twos, all_flags);
810 }
811 return result;
812 }
813
814 /* There are more than one */
815 cnt = 0;
816 list = result;
817 while (list) {
818 list = list->next;
819 cnt++;
820 }
821 rslt = xcalloc(cnt, sizeof(*rslt));
822 for (list = result, i = 0; list; list = list->next)
823 rslt[i++] = list->item;
824 free_commit_list(result);
825
826 clear_commit_marks(one, all_flags);
827 clear_commit_marks_many(n, twos, all_flags);
828
829 cnt = remove_redundant(rslt, cnt);
830 result = NULL;
831 for (i = 0; i < cnt; i++)
832 commit_list_insert_by_date(rslt[i], &result);
833 free(rslt);
834 return result;
835 }
836
837 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
838 int cleanup)
839 {
840 return get_merge_bases_many(one, 1, &two, cleanup);
841 }
842
843 /*
844 * Is "commit" a decendant of one of the elements on the "with_commit" list?
845 */
846 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
847 {
848 if (!with_commit)
849 return 1;
850 while (with_commit) {
851 struct commit *other;
852
853 other = with_commit->item;
854 with_commit = with_commit->next;
855 if (in_merge_bases(other, commit))
856 return 1;
857 }
858 return 0;
859 }
860
861 /*
862 * Is "commit" an ancestor of one of the "references"?
863 */
864 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
865 {
866 struct commit_list *bases;
867 int ret = 0, i;
868
869 if (parse_commit(commit))
870 return ret;
871 for (i = 0; i < nr_reference; i++)
872 if (parse_commit(reference[i]))
873 return ret;
874
875 bases = paint_down_to_common(commit, nr_reference, reference);
876 if (commit->object.flags & PARENT2)
877 ret = 1;
878 clear_commit_marks(commit, all_flags);
879 for (i = 0; i < nr_reference; i++)
880 clear_commit_marks(reference[i], all_flags);
881 free_commit_list(bases);
882 return ret;
883 }
884
885 /*
886 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
887 */
888 int in_merge_bases(struct commit *commit, struct commit *reference)
889 {
890 return in_merge_bases_many(commit, 1, &reference);
891 }
892
893 struct commit_list *reduce_heads(struct commit_list *heads)
894 {
895 struct commit_list *p;
896 struct commit_list *result = NULL, **tail = &result;
897 struct commit **array;
898 int num_head, i;
899
900 if (!heads)
901 return NULL;
902
903 /* Uniquify */
904 for (p = heads; p; p = p->next)
905 p->item->object.flags &= ~STALE;
906 for (p = heads, num_head = 0; p; p = p->next) {
907 if (p->item->object.flags & STALE)
908 continue;
909 p->item->object.flags |= STALE;
910 num_head++;
911 }
912 array = xcalloc(sizeof(*array), num_head);
913 for (p = heads, i = 0; p; p = p->next) {
914 if (p->item->object.flags & STALE) {
915 array[i++] = p->item;
916 p->item->object.flags &= ~STALE;
917 }
918 }
919 num_head = remove_redundant(array, num_head);
920 for (i = 0; i < num_head; i++)
921 tail = &commit_list_insert(array[i], tail)->next;
922 return result;
923 }
924
925 static const char gpg_sig_header[] = "gpgsig";
926 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
927
928 static int do_sign_commit(struct strbuf *buf, const char *keyid)
929 {
930 struct strbuf sig = STRBUF_INIT;
931 int inspos, copypos;
932
933 /* find the end of the header */
934 inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
935
936 if (!keyid || !*keyid)
937 keyid = get_signing_key();
938 if (sign_buffer(buf, &sig, keyid)) {
939 strbuf_release(&sig);
940 return -1;
941 }
942
943 for (copypos = 0; sig.buf[copypos]; ) {
944 const char *bol = sig.buf + copypos;
945 const char *eol = strchrnul(bol, '\n');
946 int len = (eol - bol) + !!*eol;
947
948 if (!copypos) {
949 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
950 inspos += gpg_sig_header_len;
951 }
952 strbuf_insert(buf, inspos++, " ", 1);
953 strbuf_insert(buf, inspos, bol, len);
954 inspos += len;
955 copypos += len;
956 }
957 strbuf_release(&sig);
958 return 0;
959 }
960
961 int parse_signed_commit(const unsigned char *sha1,
962 struct strbuf *payload, struct strbuf *signature)
963 {
964 unsigned long size;
965 enum object_type type;
966 char *buffer = read_sha1_file(sha1, &type, &size);
967 int in_signature, saw_signature = -1;
968 char *line, *tail;
969
970 if (!buffer || type != OBJ_COMMIT)
971 goto cleanup;
972
973 line = buffer;
974 tail = buffer + size;
975 in_signature = 0;
976 saw_signature = 0;
977 while (line < tail) {
978 const char *sig = NULL;
979 char *next = memchr(line, '\n', tail - line);
980
981 next = next ? next + 1 : tail;
982 if (in_signature && line[0] == ' ')
983 sig = line + 1;
984 else if (!prefixcmp(line, gpg_sig_header) &&
985 line[gpg_sig_header_len] == ' ')
986 sig = line + gpg_sig_header_len + 1;
987 if (sig) {
988 strbuf_add(signature, sig, next - sig);
989 saw_signature = 1;
990 in_signature = 1;
991 } else {
992 if (*line == '\n')
993 /* dump the whole remainder of the buffer */
994 next = tail;
995 strbuf_add(payload, line, next - line);
996 in_signature = 0;
997 }
998 line = next;
999 }
1000 cleanup:
1001 free(buffer);
1002 return saw_signature;
1003 }
1004
1005 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1006 {
1007 struct merge_remote_desc *desc;
1008 struct commit_extra_header *mergetag;
1009 char *buf;
1010 unsigned long size, len;
1011 enum object_type type;
1012
1013 desc = merge_remote_util(parent);
1014 if (!desc || !desc->obj)
1015 return;
1016 buf = read_sha1_file(desc->obj->sha1, &type, &size);
1017 if (!buf || type != OBJ_TAG)
1018 goto free_return;
1019 len = parse_signature(buf, size);
1020 if (size == len)
1021 goto free_return;
1022 /*
1023 * We could verify this signature and either omit the tag when
1024 * it does not validate, but the integrator may not have the
1025 * public key of the signer of the tag he is merging, while a
1026 * later auditor may have it while auditing, so let's not run
1027 * verify-signed-buffer here for now...
1028 *
1029 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1030 * warn("warning: signed tag unverified.");
1031 */
1032 mergetag = xcalloc(1, sizeof(*mergetag));
1033 mergetag->key = xstrdup("mergetag");
1034 mergetag->value = buf;
1035 mergetag->len = size;
1036
1037 **tail = mergetag;
1038 *tail = &mergetag->next;
1039 return;
1040
1041 free_return:
1042 free(buf);
1043 }
1044
1045 void append_merge_tag_headers(struct commit_list *parents,
1046 struct commit_extra_header ***tail)
1047 {
1048 while (parents) {
1049 struct commit *parent = parents->item;
1050 handle_signed_tag(parent, tail);
1051 parents = parents->next;
1052 }
1053 }
1054
1055 static void add_extra_header(struct strbuf *buffer,
1056 struct commit_extra_header *extra)
1057 {
1058 strbuf_addstr(buffer, extra->key);
1059 if (extra->len)
1060 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1061 else
1062 strbuf_addch(buffer, '\n');
1063 }
1064
1065 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1066 const char **exclude)
1067 {
1068 struct commit_extra_header *extra = NULL;
1069 unsigned long size;
1070 enum object_type type;
1071 char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
1072 if (buffer && type == OBJ_COMMIT)
1073 extra = read_commit_extra_header_lines(buffer, size, exclude);
1074 free(buffer);
1075 return extra;
1076 }
1077
1078 static inline int standard_header_field(const char *field, size_t len)
1079 {
1080 return ((len == 4 && !memcmp(field, "tree ", 5)) ||
1081 (len == 6 && !memcmp(field, "parent ", 7)) ||
1082 (len == 6 && !memcmp(field, "author ", 7)) ||
1083 (len == 9 && !memcmp(field, "committer ", 10)) ||
1084 (len == 8 && !memcmp(field, "encoding ", 9)));
1085 }
1086
1087 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1088 {
1089 if (!exclude)
1090 return 0;
1091
1092 while (*exclude) {
1093 size_t xlen = strlen(*exclude);
1094 if (len == xlen &&
1095 !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
1096 return 1;
1097 exclude++;
1098 }
1099 return 0;
1100 }
1101
1102 static struct commit_extra_header *read_commit_extra_header_lines(
1103 const char *buffer, size_t size,
1104 const char **exclude)
1105 {
1106 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1107 const char *line, *next, *eof, *eob;
1108 struct strbuf buf = STRBUF_INIT;
1109
1110 for (line = buffer, eob = line + size;
1111 line < eob && *line != '\n';
1112 line = next) {
1113 next = memchr(line, '\n', eob - line);
1114 next = next ? next + 1 : eob;
1115 if (*line == ' ') {
1116 /* continuation */
1117 if (it)
1118 strbuf_add(&buf, line + 1, next - (line + 1));
1119 continue;
1120 }
1121 if (it)
1122 it->value = strbuf_detach(&buf, &it->len);
1123 strbuf_reset(&buf);
1124 it = NULL;
1125
1126 eof = strchr(line, ' ');
1127 if (next <= eof)
1128 eof = next;
1129
1130 if (standard_header_field(line, eof - line) ||
1131 excluded_header_field(line, eof - line, exclude))
1132 continue;
1133
1134 it = xcalloc(1, sizeof(*it));
1135 it->key = xmemdupz(line, eof-line);
1136 *tail = it;
1137 tail = &it->next;
1138 if (eof + 1 < next)
1139 strbuf_add(&buf, eof + 1, next - (eof + 1));
1140 }
1141 if (it)
1142 it->value = strbuf_detach(&buf, &it->len);
1143 return extra;
1144 }
1145
1146 void free_commit_extra_headers(struct commit_extra_header *extra)
1147 {
1148 while (extra) {
1149 struct commit_extra_header *next = extra->next;
1150 free(extra->key);
1151 free(extra->value);
1152 free(extra);
1153 extra = next;
1154 }
1155 }
1156
1157 int commit_tree(const struct strbuf *msg, unsigned char *tree,
1158 struct commit_list *parents, unsigned char *ret,
1159 const char *author, const char *sign_commit)
1160 {
1161 struct commit_extra_header *extra = NULL, **tail = &extra;
1162 int result;
1163
1164 append_merge_tag_headers(parents, &tail);
1165 result = commit_tree_extended(msg, tree, parents, ret,
1166 author, sign_commit, extra);
1167 free_commit_extra_headers(extra);
1168 return result;
1169 }
1170
1171 static int find_invalid_utf8(const char *buf, int len)
1172 {
1173 int offset = 0;
1174
1175 while (len) {
1176 unsigned char c = *buf++;
1177 int bytes, bad_offset;
1178
1179 len--;
1180 offset++;
1181
1182 /* Simple US-ASCII? No worries. */
1183 if (c < 0x80)
1184 continue;
1185
1186 bad_offset = offset-1;
1187
1188 /*
1189 * Count how many more high bits set: that's how
1190 * many more bytes this sequence should have.
1191 */
1192 bytes = 0;
1193 while (c & 0x40) {
1194 c <<= 1;
1195 bytes++;
1196 }
1197
1198 /* Must be between 1 and 5 more bytes */
1199 if (bytes < 1 || bytes > 5)
1200 return bad_offset;
1201
1202 /* Do we *have* that many bytes? */
1203 if (len < bytes)
1204 return bad_offset;
1205
1206 offset += bytes;
1207 len -= bytes;
1208
1209 /* And verify that they are good continuation bytes */
1210 do {
1211 if ((*buf++ & 0xc0) != 0x80)
1212 return bad_offset;
1213 } while (--bytes);
1214
1215 /* We could/should check the value and length here too */
1216 }
1217 return -1;
1218 }
1219
1220 /*
1221 * This verifies that the buffer is in proper utf8 format.
1222 *
1223 * If it isn't, it assumes any non-utf8 characters are Latin1,
1224 * and does the conversion.
1225 *
1226 * Fixme: we should probably also disallow overlong forms and
1227 * invalid characters. But we don't do that currently.
1228 */
1229 static int verify_utf8(struct strbuf *buf)
1230 {
1231 int ok = 1;
1232 long pos = 0;
1233
1234 for (;;) {
1235 int bad;
1236 unsigned char c;
1237 unsigned char replace[2];
1238
1239 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1240 if (bad < 0)
1241 return ok;
1242 pos += bad;
1243 ok = 0;
1244 c = buf->buf[pos];
1245 strbuf_remove(buf, pos, 1);
1246
1247 /* We know 'c' must be in the range 128-255 */
1248 replace[0] = 0xc0 + (c >> 6);
1249 replace[1] = 0x80 + (c & 0x3f);
1250 strbuf_insert(buf, pos, replace, 2);
1251 pos += 2;
1252 }
1253 }
1254
1255 static const char commit_utf8_warn[] =
1256 "Warning: commit message did not conform to UTF-8.\n"
1257 "You may want to amend it after fixing the message, or set the config\n"
1258 "variable i18n.commitencoding to the encoding your project uses.\n";
1259
1260 int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
1261 struct commit_list *parents, unsigned char *ret,
1262 const char *author, const char *sign_commit,
1263 struct commit_extra_header *extra)
1264 {
1265 int result;
1266 int encoding_is_utf8;
1267 struct strbuf buffer;
1268
1269 assert_sha1_type(tree, OBJ_TREE);
1270
1271 if (memchr(msg->buf, '\0', msg->len))
1272 return error("a NUL byte in commit log message not allowed.");
1273
1274 /* Not having i18n.commitencoding is the same as having utf-8 */
1275 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1276
1277 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1278 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1279
1280 /*
1281 * NOTE! This ordering means that the same exact tree merged with a
1282 * different order of parents will be a _different_ changeset even
1283 * if everything else stays the same.
1284 */
1285 while (parents) {
1286 struct commit_list *next = parents->next;
1287 struct commit *parent = parents->item;
1288
1289 strbuf_addf(&buffer, "parent %s\n",
1290 sha1_to_hex(parent->object.sha1));
1291 free(parents);
1292 parents = next;
1293 }
1294
1295 /* Person/date information */
1296 if (!author)
1297 author = git_author_info(IDENT_STRICT);
1298 strbuf_addf(&buffer, "author %s\n", author);
1299 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1300 if (!encoding_is_utf8)
1301 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1302
1303 while (extra) {
1304 add_extra_header(&buffer, extra);
1305 extra = extra->next;
1306 }
1307 strbuf_addch(&buffer, '\n');
1308
1309 /* And add the comment */
1310 strbuf_addbuf(&buffer, msg);
1311
1312 /* And check the encoding */
1313 if (encoding_is_utf8 && !verify_utf8(&buffer))
1314 fprintf(stderr, commit_utf8_warn);
1315
1316 if (sign_commit && do_sign_commit(&buffer, sign_commit))
1317 return -1;
1318
1319 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1320 strbuf_release(&buffer);
1321 return result;
1322 }
1323
1324 struct commit *get_merge_parent(const char *name)
1325 {
1326 struct object *obj;
1327 struct commit *commit;
1328 unsigned char sha1[20];
1329 if (get_sha1(name, sha1))
1330 return NULL;
1331 obj = parse_object(sha1);
1332 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1333 if (commit && !commit->util) {
1334 struct merge_remote_desc *desc;
1335 desc = xmalloc(sizeof(*desc));
1336 desc->obj = obj;
1337 desc->name = strdup(name);
1338 commit->util = desc;
1339 }
1340 return commit;
1341 }
1342
1343 /*
1344 * Append a commit to the end of the commit_list.
1345 *
1346 * next starts by pointing to the variable that holds the head of an
1347 * empty commit_list, and is updated to point to the "next" field of
1348 * the last item on the list as new commits are appended.
1349 *
1350 * Usage example:
1351 *
1352 * struct commit_list *list;
1353 * struct commit_list **next = &list;
1354 *
1355 * next = commit_list_append(c1, next);
1356 * next = commit_list_append(c2, next);
1357 * assert(commit_list_count(list) == 2);
1358 * return list;
1359 */
1360 struct commit_list **commit_list_append(struct commit *commit,
1361 struct commit_list **next)
1362 {
1363 struct commit_list *new = xmalloc(sizeof(struct commit_list));
1364 new->item = commit;
1365 *next = new;
1366 new->next = NULL;
1367 return &new->next;
1368 }
1369
1370 void print_commit_list(struct commit_list *list,
1371 const char *format_cur,
1372 const char *format_last)
1373 {
1374 for ( ; list; list = list->next) {
1375 const char *format = list->next ? format_cur : format_last;
1376 printf(format, sha1_to_hex(list->item->object.sha1));
1377 }
1378 }