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