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