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