l10n: bg.po: Updated Bulgarian translation (3608t)
[git.git] / commit.c
blob0030e79940ff8564b5c8c5ebab462eb1bd745174
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "commit-graph.h"
5 #include "pkt-line.h"
6 #include "utf8.h"
7 #include "diff.h"
8 #include "revision.h"
9 #include "notes.h"
10 #include "gpg-interface.h"
11 #include "mergesort.h"
12 #include "commit-slab.h"
13 #include "prio-queue.h"
14 #include "sha1-lookup.h"
15 #include "wt-status.h"
16 #include "advice.h"
18 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
20 int save_commit_buffer = 1;
22 const char *commit_type = "commit";
24 struct commit *lookup_commit_reference_gently(const struct object_id *oid,
25 int quiet)
27 struct object *obj = deref_tag(parse_object(oid), NULL, 0);
29 if (!obj)
30 return NULL;
31 return object_as_type(obj, OBJ_COMMIT, quiet);
34 struct commit *lookup_commit_reference(const struct object_id *oid)
36 return lookup_commit_reference_gently(oid, 0);
39 struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name)
41 struct commit *c = lookup_commit_reference(oid);
42 if (!c)
43 die(_("could not parse %s"), ref_name);
44 if (oidcmp(oid, &c->object.oid)) {
45 warning(_("%s %s is not a commit!"),
46 ref_name, oid_to_hex(oid));
48 return c;
51 struct commit *lookup_commit(const struct object_id *oid)
53 struct object *obj = lookup_object(oid->hash);
54 if (!obj)
55 return create_object(oid->hash, alloc_commit_node());
56 return object_as_type(obj, OBJ_COMMIT, 0);
59 struct commit *lookup_commit_reference_by_name(const char *name)
61 struct object_id oid;
62 struct commit *commit;
64 if (get_oid_committish(name, &oid))
65 return NULL;
66 commit = lookup_commit_reference(&oid);
67 if (parse_commit(commit))
68 return NULL;
69 return commit;
72 static timestamp_t parse_commit_date(const char *buf, const char *tail)
74 const char *dateptr;
76 if (buf + 6 >= tail)
77 return 0;
78 if (memcmp(buf, "author", 6))
79 return 0;
80 while (buf < tail && *buf++ != '\n')
81 /* nada */;
82 if (buf + 9 >= tail)
83 return 0;
84 if (memcmp(buf, "committer", 9))
85 return 0;
86 while (buf < tail && *buf++ != '>')
87 /* nada */;
88 if (buf >= tail)
89 return 0;
90 dateptr = buf;
91 while (buf < tail && *buf++ != '\n')
92 /* nada */;
93 if (buf >= tail)
94 return 0;
95 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
96 return parse_timestamp(dateptr, NULL, 10);
99 static struct commit_graft **commit_graft;
100 static int commit_graft_alloc, commit_graft_nr;
102 static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
104 struct commit_graft **commit_graft_table = table;
105 return commit_graft_table[index]->oid.hash;
108 static int commit_graft_pos(const unsigned char *sha1)
110 return sha1_pos(sha1, commit_graft, commit_graft_nr,
111 commit_graft_sha1_access);
114 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
116 int pos = commit_graft_pos(graft->oid.hash);
118 if (0 <= pos) {
119 if (ignore_dups)
120 free(graft);
121 else {
122 free(commit_graft[pos]);
123 commit_graft[pos] = graft;
125 return 1;
127 pos = -pos - 1;
128 ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc);
129 commit_graft_nr++;
130 if (pos < commit_graft_nr)
131 MOVE_ARRAY(commit_graft + pos + 1, commit_graft + pos,
132 commit_graft_nr - pos - 1);
133 commit_graft[pos] = graft;
134 return 0;
137 struct commit_graft *read_graft_line(struct strbuf *line)
139 /* The format is just "Commit Parent1 Parent2 ...\n" */
140 int i, phase;
141 const char *tail = NULL;
142 struct commit_graft *graft = NULL;
143 struct object_id dummy_oid, *oid;
145 strbuf_rtrim(line);
146 if (!line->len || line->buf[0] == '#')
147 return NULL;
149 * phase 0 verifies line, counts hashes in line and allocates graft
150 * phase 1 fills graft
152 for (phase = 0; phase < 2; phase++) {
153 oid = graft ? &graft->oid : &dummy_oid;
154 if (parse_oid_hex(line->buf, oid, &tail))
155 goto bad_graft_data;
156 for (i = 0; *tail != '\0'; i++) {
157 oid = graft ? &graft->parent[i] : &dummy_oid;
158 if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
159 goto bad_graft_data;
161 if (!graft) {
162 graft = xmalloc(st_add(sizeof(*graft),
163 st_mult(sizeof(struct object_id), i)));
164 graft->nr_parent = i;
167 return graft;
169 bad_graft_data:
170 error("bad graft data: %s", line->buf);
171 assert(!graft);
172 return NULL;
175 static int read_graft_file(const char *graft_file)
177 FILE *fp = fopen_or_warn(graft_file, "r");
178 struct strbuf buf = STRBUF_INIT;
179 if (!fp)
180 return -1;
181 if (advice_graft_file_deprecated)
182 advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
183 "and will be removed in a future Git version.\n"
184 "\n"
185 "Please use \"git replace --convert-graft-file\"\n"
186 "to convert the grafts into replace refs.\n"
187 "\n"
188 "Turn this message off by running\n"
189 "\"git config advice.graftFileDeprecated false\""));
190 while (!strbuf_getwholeline(&buf, fp, '\n')) {
191 /* The format is just "Commit Parent1 Parent2 ...\n" */
192 struct commit_graft *graft = read_graft_line(&buf);
193 if (!graft)
194 continue;
195 if (register_commit_graft(graft, 1))
196 error("duplicate graft data: %s", buf.buf);
198 fclose(fp);
199 strbuf_release(&buf);
200 return 0;
203 static void prepare_commit_graft(void)
205 static int commit_graft_prepared;
206 char *graft_file;
208 if (commit_graft_prepared)
209 return;
210 if (!startup_info->have_repository)
211 return;
213 graft_file = get_graft_file();
214 read_graft_file(graft_file);
215 /* make sure shallows are read */
216 is_repository_shallow();
217 commit_graft_prepared = 1;
220 struct commit_graft *lookup_commit_graft(const struct object_id *oid)
222 int pos;
223 prepare_commit_graft();
224 pos = commit_graft_pos(oid->hash);
225 if (pos < 0)
226 return NULL;
227 return commit_graft[pos];
230 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
232 int i, ret;
233 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
234 ret = fn(commit_graft[i], cb_data);
235 return ret;
238 int unregister_shallow(const struct object_id *oid)
240 int pos = commit_graft_pos(oid->hash);
241 if (pos < 0)
242 return -1;
243 if (pos + 1 < commit_graft_nr)
244 MOVE_ARRAY(commit_graft + pos, commit_graft + pos + 1,
245 commit_graft_nr - pos - 1);
246 commit_graft_nr--;
247 return 0;
250 struct commit_buffer {
251 void *buffer;
252 unsigned long size;
254 define_commit_slab(buffer_slab, struct commit_buffer);
255 static struct buffer_slab buffer_slab = COMMIT_SLAB_INIT(1, buffer_slab);
257 void set_commit_buffer(struct commit *commit, void *buffer, unsigned long size)
259 struct commit_buffer *v = buffer_slab_at(&buffer_slab, commit);
260 v->buffer = buffer;
261 v->size = size;
264 const void *get_cached_commit_buffer(const struct commit *commit, unsigned long *sizep)
266 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
267 if (!v) {
268 if (sizep)
269 *sizep = 0;
270 return NULL;
272 if (sizep)
273 *sizep = v->size;
274 return v->buffer;
277 const void *get_commit_buffer(const struct commit *commit, unsigned long *sizep)
279 const void *ret = get_cached_commit_buffer(commit, sizep);
280 if (!ret) {
281 enum object_type type;
282 unsigned long size;
283 ret = read_object_file(&commit->object.oid, &type, &size);
284 if (!ret)
285 die("cannot read commit object %s",
286 oid_to_hex(&commit->object.oid));
287 if (type != OBJ_COMMIT)
288 die("expected commit for %s, got %s",
289 oid_to_hex(&commit->object.oid), type_name(type));
290 if (sizep)
291 *sizep = size;
293 return ret;
296 void unuse_commit_buffer(const struct commit *commit, const void *buffer)
298 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
299 if (!(v && v->buffer == buffer))
300 free((void *)buffer);
303 void free_commit_buffer(struct commit *commit)
305 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
306 if (v) {
307 FREE_AND_NULL(v->buffer);
308 v->size = 0;
312 struct tree *get_commit_tree(const struct commit *commit)
314 if (commit->maybe_tree || !commit->object.parsed)
315 return commit->maybe_tree;
317 if (commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
318 BUG("commit has NULL tree, but was not loaded from commit-graph");
320 return get_commit_tree_in_graph(commit);
323 struct object_id *get_commit_tree_oid(const struct commit *commit)
325 return &get_commit_tree(commit)->object.oid;
328 const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
330 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
331 void *ret;
333 if (!v) {
334 if (sizep)
335 *sizep = 0;
336 return NULL;
338 ret = v->buffer;
339 if (sizep)
340 *sizep = v->size;
342 v->buffer = NULL;
343 v->size = 0;
344 return ret;
347 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
349 const char *tail = buffer;
350 const char *bufptr = buffer;
351 struct object_id parent;
352 struct commit_list **pptr;
353 struct commit_graft *graft;
354 const int tree_entry_len = GIT_SHA1_HEXSZ + 5;
355 const int parent_entry_len = GIT_SHA1_HEXSZ + 7;
357 if (item->object.parsed)
358 return 0;
359 item->object.parsed = 1;
360 tail += size;
361 if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
362 bufptr[tree_entry_len] != '\n')
363 return error("bogus commit object %s", oid_to_hex(&item->object.oid));
364 if (get_oid_hex(bufptr + 5, &parent) < 0)
365 return error("bad tree pointer in commit %s",
366 oid_to_hex(&item->object.oid));
367 item->maybe_tree = lookup_tree(&parent);
368 bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
369 pptr = &item->parents;
371 graft = lookup_commit_graft(&item->object.oid);
372 while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
373 struct commit *new_parent;
375 if (tail <= bufptr + parent_entry_len + 1 ||
376 get_oid_hex(bufptr + 7, &parent) ||
377 bufptr[parent_entry_len] != '\n')
378 return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
379 bufptr += parent_entry_len + 1;
381 * The clone is shallow if nr_parent < 0, and we must
382 * not traverse its real parents even when we unhide them.
384 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
385 continue;
386 new_parent = lookup_commit(&parent);
387 if (new_parent)
388 pptr = &commit_list_insert(new_parent, pptr)->next;
390 if (graft) {
391 int i;
392 struct commit *new_parent;
393 for (i = 0; i < graft->nr_parent; i++) {
394 new_parent = lookup_commit(&graft->parent[i]);
395 if (!new_parent)
396 continue;
397 pptr = &commit_list_insert(new_parent, pptr)->next;
400 item->date = parse_commit_date(bufptr, tail);
402 return 0;
405 int parse_commit_gently(struct commit *item, int quiet_on_missing)
407 enum object_type type;
408 void *buffer;
409 unsigned long size;
410 int ret;
412 if (!item)
413 return -1;
414 if (item->object.parsed)
415 return 0;
416 if (parse_commit_in_graph(item))
417 return 0;
418 buffer = read_object_file(&item->object.oid, &type, &size);
419 if (!buffer)
420 return quiet_on_missing ? -1 :
421 error("Could not read %s",
422 oid_to_hex(&item->object.oid));
423 if (type != OBJ_COMMIT) {
424 free(buffer);
425 return error("Object %s not a commit",
426 oid_to_hex(&item->object.oid));
428 ret = parse_commit_buffer(item, buffer, size);
429 if (save_commit_buffer && !ret) {
430 set_commit_buffer(item, buffer, size);
431 return 0;
433 free(buffer);
434 return ret;
437 void parse_commit_or_die(struct commit *item)
439 if (parse_commit(item))
440 die("unable to parse commit %s",
441 item ? oid_to_hex(&item->object.oid) : "(null)");
444 int find_commit_subject(const char *commit_buffer, const char **subject)
446 const char *eol;
447 const char *p = commit_buffer;
449 while (*p && (*p != '\n' || p[1] != '\n'))
450 p++;
451 if (*p) {
452 p = skip_blank_lines(p + 2);
453 eol = strchrnul(p, '\n');
454 } else
455 eol = p;
457 *subject = p;
459 return eol - p;
462 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
464 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
465 new_list->item = item;
466 new_list->next = *list_p;
467 *list_p = new_list;
468 return new_list;
471 unsigned commit_list_count(const struct commit_list *l)
473 unsigned c = 0;
474 for (; l; l = l->next )
475 c++;
476 return c;
479 struct commit_list *copy_commit_list(struct commit_list *list)
481 struct commit_list *head = NULL;
482 struct commit_list **pp = &head;
483 while (list) {
484 pp = commit_list_append(list->item, pp);
485 list = list->next;
487 return head;
490 void free_commit_list(struct commit_list *list)
492 while (list)
493 pop_commit(&list);
496 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
498 struct commit_list **pp = list;
499 struct commit_list *p;
500 while ((p = *pp) != NULL) {
501 if (p->item->date < item->date) {
502 break;
504 pp = &p->next;
506 return commit_list_insert(item, pp);
509 static int commit_list_compare_by_date(const void *a, const void *b)
511 timestamp_t a_date = ((const struct commit_list *)a)->item->date;
512 timestamp_t b_date = ((const struct commit_list *)b)->item->date;
513 if (a_date < b_date)
514 return 1;
515 if (a_date > b_date)
516 return -1;
517 return 0;
520 static void *commit_list_get_next(const void *a)
522 return ((const struct commit_list *)a)->next;
525 static void commit_list_set_next(void *a, void *next)
527 ((struct commit_list *)a)->next = next;
530 void commit_list_sort_by_date(struct commit_list **list)
532 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
533 commit_list_compare_by_date);
536 struct commit *pop_most_recent_commit(struct commit_list **list,
537 unsigned int mark)
539 struct commit *ret = pop_commit(list);
540 struct commit_list *parents = ret->parents;
542 while (parents) {
543 struct commit *commit = parents->item;
544 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
545 commit->object.flags |= mark;
546 commit_list_insert_by_date(commit, list);
548 parents = parents->next;
550 return ret;
553 static void clear_commit_marks_1(struct commit_list **plist,
554 struct commit *commit, unsigned int mark)
556 while (commit) {
557 struct commit_list *parents;
559 if (!(mark & commit->object.flags))
560 return;
562 commit->object.flags &= ~mark;
564 parents = commit->parents;
565 if (!parents)
566 return;
568 while ((parents = parents->next))
569 commit_list_insert(parents->item, plist);
571 commit = commit->parents->item;
575 void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
577 struct commit_list *list = NULL;
579 while (nr--) {
580 clear_commit_marks_1(&list, *commit, mark);
581 commit++;
583 while (list)
584 clear_commit_marks_1(&list, pop_commit(&list), mark);
587 void clear_commit_marks(struct commit *commit, unsigned int mark)
589 clear_commit_marks_many(1, &commit, mark);
592 struct commit *pop_commit(struct commit_list **stack)
594 struct commit_list *top = *stack;
595 struct commit *item = top ? top->item : NULL;
597 if (top) {
598 *stack = top->next;
599 free(top);
601 return item;
605 * Topological sort support
608 /* count number of children that have not been emitted */
609 define_commit_slab(indegree_slab, int);
611 /* record author-date for each commit object */
612 define_commit_slab(author_date_slab, unsigned long);
614 static void record_author_date(struct author_date_slab *author_date,
615 struct commit *commit)
617 const char *buffer = get_commit_buffer(commit, NULL);
618 struct ident_split ident;
619 const char *ident_line;
620 size_t ident_len;
621 char *date_end;
622 timestamp_t date;
624 ident_line = find_commit_header(buffer, "author", &ident_len);
625 if (!ident_line)
626 goto fail_exit; /* no author line */
627 if (split_ident_line(&ident, ident_line, ident_len) ||
628 !ident.date_begin || !ident.date_end)
629 goto fail_exit; /* malformed "author" line */
631 date = parse_timestamp(ident.date_begin, &date_end, 10);
632 if (date_end != ident.date_end)
633 goto fail_exit; /* malformed date */
634 *(author_date_slab_at(author_date, commit)) = date;
636 fail_exit:
637 unuse_commit_buffer(commit, buffer);
640 static int compare_commits_by_author_date(const void *a_, const void *b_,
641 void *cb_data)
643 const struct commit *a = a_, *b = b_;
644 struct author_date_slab *author_date = cb_data;
645 timestamp_t a_date = *(author_date_slab_at(author_date, a));
646 timestamp_t b_date = *(author_date_slab_at(author_date, 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;
656 int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
658 const struct commit *a = a_, *b = b_;
659 /* newer commits with larger date first */
660 if (a->date < b->date)
661 return 1;
662 else if (a->date > b->date)
663 return -1;
664 return 0;
668 * Performs an in-place topological sort on the list supplied.
670 void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
672 struct commit_list *next, *orig = *list;
673 struct commit_list **pptr;
674 struct indegree_slab indegree;
675 struct prio_queue queue;
676 struct commit *commit;
677 struct author_date_slab author_date;
679 if (!orig)
680 return;
681 *list = NULL;
683 init_indegree_slab(&indegree);
684 memset(&queue, '\0', sizeof(queue));
686 switch (sort_order) {
687 default: /* REV_SORT_IN_GRAPH_ORDER */
688 queue.compare = NULL;
689 break;
690 case REV_SORT_BY_COMMIT_DATE:
691 queue.compare = compare_commits_by_commit_date;
692 break;
693 case REV_SORT_BY_AUTHOR_DATE:
694 init_author_date_slab(&author_date);
695 queue.compare = compare_commits_by_author_date;
696 queue.cb_data = &author_date;
697 break;
700 /* Mark them and clear the indegree */
701 for (next = orig; next; next = next->next) {
702 struct commit *commit = next->item;
703 *(indegree_slab_at(&indegree, commit)) = 1;
704 /* also record the author dates, if needed */
705 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
706 record_author_date(&author_date, commit);
709 /* update the indegree */
710 for (next = orig; next; next = next->next) {
711 struct commit_list *parents = next->item->parents;
712 while (parents) {
713 struct commit *parent = parents->item;
714 int *pi = indegree_slab_at(&indegree, parent);
716 if (*pi)
717 (*pi)++;
718 parents = parents->next;
723 * find the tips
725 * tips are nodes not reachable from any other node in the list
727 * the tips serve as a starting set for the work queue.
729 for (next = orig; next; next = next->next) {
730 struct commit *commit = next->item;
732 if (*(indegree_slab_at(&indegree, commit)) == 1)
733 prio_queue_put(&queue, commit);
737 * This is unfortunate; the initial tips need to be shown
738 * in the order given from the revision traversal machinery.
740 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
741 prio_queue_reverse(&queue);
743 /* We no longer need the commit list */
744 free_commit_list(orig);
746 pptr = list;
747 *list = NULL;
748 while ((commit = prio_queue_get(&queue)) != NULL) {
749 struct commit_list *parents;
751 for (parents = commit->parents; parents ; parents = parents->next) {
752 struct commit *parent = parents->item;
753 int *pi = indegree_slab_at(&indegree, parent);
755 if (!*pi)
756 continue;
759 * parents are only enqueued for emission
760 * when all their children have been emitted thereby
761 * guaranteeing topological order.
763 if (--(*pi) == 1)
764 prio_queue_put(&queue, parent);
767 * all children of commit have already been
768 * emitted. we can emit it now.
770 *(indegree_slab_at(&indegree, commit)) = 0;
772 pptr = &commit_list_insert(commit, pptr)->next;
775 clear_indegree_slab(&indegree);
776 clear_prio_queue(&queue);
777 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
778 clear_author_date_slab(&author_date);
781 /* merge-base stuff */
783 /* Remember to update object flag allocation in object.h */
784 #define PARENT1 (1u<<16)
785 #define PARENT2 (1u<<17)
786 #define STALE (1u<<18)
787 #define RESULT (1u<<19)
789 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
791 static int queue_has_nonstale(struct prio_queue *queue)
793 int i;
794 for (i = 0; i < queue->nr; i++) {
795 struct commit *commit = queue->array[i].data;
796 if (!(commit->object.flags & STALE))
797 return 1;
799 return 0;
802 /* all input commits in one and twos[] must have been parsed! */
803 static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
805 struct prio_queue queue = { compare_commits_by_commit_date };
806 struct commit_list *result = NULL;
807 int i;
809 one->object.flags |= PARENT1;
810 if (!n) {
811 commit_list_append(one, &result);
812 return result;
814 prio_queue_put(&queue, one);
816 for (i = 0; i < n; i++) {
817 twos[i]->object.flags |= PARENT2;
818 prio_queue_put(&queue, twos[i]);
821 while (queue_has_nonstale(&queue)) {
822 struct commit *commit = prio_queue_get(&queue);
823 struct commit_list *parents;
824 int flags;
826 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
827 if (flags == (PARENT1 | PARENT2)) {
828 if (!(commit->object.flags & RESULT)) {
829 commit->object.flags |= RESULT;
830 commit_list_insert_by_date(commit, &result);
832 /* Mark parents of a found merge stale */
833 flags |= STALE;
835 parents = commit->parents;
836 while (parents) {
837 struct commit *p = parents->item;
838 parents = parents->next;
839 if ((p->object.flags & flags) == flags)
840 continue;
841 if (parse_commit(p))
842 return NULL;
843 p->object.flags |= flags;
844 prio_queue_put(&queue, p);
848 clear_prio_queue(&queue);
849 return result;
852 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
854 struct commit_list *list = NULL;
855 struct commit_list *result = NULL;
856 int i;
858 for (i = 0; i < n; i++) {
859 if (one == twos[i])
861 * We do not mark this even with RESULT so we do not
862 * have to clean it up.
864 return commit_list_insert(one, &result);
867 if (parse_commit(one))
868 return NULL;
869 for (i = 0; i < n; i++) {
870 if (parse_commit(twos[i]))
871 return NULL;
874 list = paint_down_to_common(one, n, twos);
876 while (list) {
877 struct commit *commit = pop_commit(&list);
878 if (!(commit->object.flags & STALE))
879 commit_list_insert_by_date(commit, &result);
881 return result;
884 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
886 struct commit_list *i, *j, *k, *ret = NULL;
888 if (!in)
889 return ret;
891 commit_list_insert(in->item, &ret);
893 for (i = in->next; i; i = i->next) {
894 struct commit_list *new_commits = NULL, *end = NULL;
896 for (j = ret; j; j = j->next) {
897 struct commit_list *bases;
898 bases = get_merge_bases(i->item, j->item);
899 if (!new_commits)
900 new_commits = bases;
901 else
902 end->next = bases;
903 for (k = bases; k; k = k->next)
904 end = k;
906 ret = new_commits;
908 return ret;
911 static int remove_redundant(struct commit **array, int cnt)
914 * Some commit in the array may be an ancestor of
915 * another commit. Move such commit to the end of
916 * the array, and return the number of commits that
917 * are independent from each other.
919 struct commit **work;
920 unsigned char *redundant;
921 int *filled_index;
922 int i, j, filled;
924 work = xcalloc(cnt, sizeof(*work));
925 redundant = xcalloc(cnt, 1);
926 ALLOC_ARRAY(filled_index, cnt - 1);
928 for (i = 0; i < cnt; i++)
929 parse_commit(array[i]);
930 for (i = 0; i < cnt; i++) {
931 struct commit_list *common;
933 if (redundant[i])
934 continue;
935 for (j = filled = 0; j < cnt; j++) {
936 if (i == j || redundant[j])
937 continue;
938 filled_index[filled] = j;
939 work[filled++] = array[j];
941 common = paint_down_to_common(array[i], filled, work);
942 if (array[i]->object.flags & PARENT2)
943 redundant[i] = 1;
944 for (j = 0; j < filled; j++)
945 if (work[j]->object.flags & PARENT1)
946 redundant[filled_index[j]] = 1;
947 clear_commit_marks(array[i], all_flags);
948 clear_commit_marks_many(filled, work, all_flags);
949 free_commit_list(common);
952 /* Now collect the result */
953 COPY_ARRAY(work, array, cnt);
954 for (i = filled = 0; i < cnt; i++)
955 if (!redundant[i])
956 array[filled++] = work[i];
957 for (j = filled, i = 0; i < cnt; i++)
958 if (redundant[i])
959 array[j++] = work[i];
960 free(work);
961 free(redundant);
962 free(filled_index);
963 return filled;
966 static struct commit_list *get_merge_bases_many_0(struct commit *one,
967 int n,
968 struct commit **twos,
969 int cleanup)
971 struct commit_list *list;
972 struct commit **rslt;
973 struct commit_list *result;
974 int cnt, i;
976 result = merge_bases_many(one, n, twos);
977 for (i = 0; i < n; i++) {
978 if (one == twos[i])
979 return result;
981 if (!result || !result->next) {
982 if (cleanup) {
983 clear_commit_marks(one, all_flags);
984 clear_commit_marks_many(n, twos, all_flags);
986 return result;
989 /* There are more than one */
990 cnt = commit_list_count(result);
991 rslt = xcalloc(cnt, sizeof(*rslt));
992 for (list = result, i = 0; list; list = list->next)
993 rslt[i++] = list->item;
994 free_commit_list(result);
996 clear_commit_marks(one, all_flags);
997 clear_commit_marks_many(n, twos, all_flags);
999 cnt = remove_redundant(rslt, cnt);
1000 result = NULL;
1001 for (i = 0; i < cnt; i++)
1002 commit_list_insert_by_date(rslt[i], &result);
1003 free(rslt);
1004 return result;
1007 struct commit_list *get_merge_bases_many(struct commit *one,
1008 int n,
1009 struct commit **twos)
1011 return get_merge_bases_many_0(one, n, twos, 1);
1014 struct commit_list *get_merge_bases_many_dirty(struct commit *one,
1015 int n,
1016 struct commit **twos)
1018 return get_merge_bases_many_0(one, n, twos, 0);
1021 struct commit_list *get_merge_bases(struct commit *one, struct commit *two)
1023 return get_merge_bases_many_0(one, 1, &two, 1);
1027 * Is "commit" a descendant of one of the elements on the "with_commit" list?
1029 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
1031 if (!with_commit)
1032 return 1;
1033 while (with_commit) {
1034 struct commit *other;
1036 other = with_commit->item;
1037 with_commit = with_commit->next;
1038 if (in_merge_bases(other, commit))
1039 return 1;
1041 return 0;
1045 * Is "commit" an ancestor of one of the "references"?
1047 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
1049 struct commit_list *bases;
1050 int ret = 0, i;
1052 if (parse_commit(commit))
1053 return ret;
1054 for (i = 0; i < nr_reference; i++)
1055 if (parse_commit(reference[i]))
1056 return ret;
1058 bases = paint_down_to_common(commit, nr_reference, reference);
1059 if (commit->object.flags & PARENT2)
1060 ret = 1;
1061 clear_commit_marks(commit, all_flags);
1062 clear_commit_marks_many(nr_reference, reference, all_flags);
1063 free_commit_list(bases);
1064 return ret;
1068 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1070 int in_merge_bases(struct commit *commit, struct commit *reference)
1072 return in_merge_bases_many(commit, 1, &reference);
1075 struct commit_list *reduce_heads(struct commit_list *heads)
1077 struct commit_list *p;
1078 struct commit_list *result = NULL, **tail = &result;
1079 struct commit **array;
1080 int num_head, i;
1082 if (!heads)
1083 return NULL;
1085 /* Uniquify */
1086 for (p = heads; p; p = p->next)
1087 p->item->object.flags &= ~STALE;
1088 for (p = heads, num_head = 0; p; p = p->next) {
1089 if (p->item->object.flags & STALE)
1090 continue;
1091 p->item->object.flags |= STALE;
1092 num_head++;
1094 array = xcalloc(num_head, sizeof(*array));
1095 for (p = heads, i = 0; p; p = p->next) {
1096 if (p->item->object.flags & STALE) {
1097 array[i++] = p->item;
1098 p->item->object.flags &= ~STALE;
1101 num_head = remove_redundant(array, num_head);
1102 for (i = 0; i < num_head; i++)
1103 tail = &commit_list_insert(array[i], tail)->next;
1104 free(array);
1105 return result;
1108 void reduce_heads_replace(struct commit_list **heads)
1110 struct commit_list *result = reduce_heads(*heads);
1111 free_commit_list(*heads);
1112 *heads = result;
1115 static const char gpg_sig_header[] = "gpgsig";
1116 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
1118 static int do_sign_commit(struct strbuf *buf, const char *keyid)
1120 struct strbuf sig = STRBUF_INIT;
1121 int inspos, copypos;
1122 const char *eoh;
1124 /* find the end of the header */
1125 eoh = strstr(buf->buf, "\n\n");
1126 if (!eoh)
1127 inspos = buf->len;
1128 else
1129 inspos = eoh - buf->buf + 1;
1131 if (!keyid || !*keyid)
1132 keyid = get_signing_key();
1133 if (sign_buffer(buf, &sig, keyid)) {
1134 strbuf_release(&sig);
1135 return -1;
1138 for (copypos = 0; sig.buf[copypos]; ) {
1139 const char *bol = sig.buf + copypos;
1140 const char *eol = strchrnul(bol, '\n');
1141 int len = (eol - bol) + !!*eol;
1143 if (!copypos) {
1144 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1145 inspos += gpg_sig_header_len;
1147 strbuf_insert(buf, inspos++, " ", 1);
1148 strbuf_insert(buf, inspos, bol, len);
1149 inspos += len;
1150 copypos += len;
1152 strbuf_release(&sig);
1153 return 0;
1156 int parse_signed_commit(const struct commit *commit,
1157 struct strbuf *payload, struct strbuf *signature)
1160 unsigned long size;
1161 const char *buffer = get_commit_buffer(commit, &size);
1162 int in_signature, saw_signature = -1;
1163 const char *line, *tail;
1165 line = buffer;
1166 tail = buffer + size;
1167 in_signature = 0;
1168 saw_signature = 0;
1169 while (line < tail) {
1170 const char *sig = NULL;
1171 const char *next = memchr(line, '\n', tail - line);
1173 next = next ? next + 1 : tail;
1174 if (in_signature && line[0] == ' ')
1175 sig = line + 1;
1176 else if (starts_with(line, gpg_sig_header) &&
1177 line[gpg_sig_header_len] == ' ')
1178 sig = line + gpg_sig_header_len + 1;
1179 if (sig) {
1180 strbuf_add(signature, sig, next - sig);
1181 saw_signature = 1;
1182 in_signature = 1;
1183 } else {
1184 if (*line == '\n')
1185 /* dump the whole remainder of the buffer */
1186 next = tail;
1187 strbuf_add(payload, line, next - line);
1188 in_signature = 0;
1190 line = next;
1192 unuse_commit_buffer(commit, buffer);
1193 return saw_signature;
1196 int remove_signature(struct strbuf *buf)
1198 const char *line = buf->buf;
1199 const char *tail = buf->buf + buf->len;
1200 int in_signature = 0;
1201 const char *sig_start = NULL;
1202 const char *sig_end = NULL;
1204 while (line < tail) {
1205 const char *next = memchr(line, '\n', tail - line);
1206 next = next ? next + 1 : tail;
1208 if (in_signature && line[0] == ' ')
1209 sig_end = next;
1210 else if (starts_with(line, gpg_sig_header) &&
1211 line[gpg_sig_header_len] == ' ') {
1212 sig_start = line;
1213 sig_end = next;
1214 in_signature = 1;
1215 } else {
1216 if (*line == '\n')
1217 /* dump the whole remainder of the buffer */
1218 next = tail;
1219 in_signature = 0;
1221 line = next;
1224 if (sig_start)
1225 strbuf_remove(buf, sig_start - buf->buf, sig_end - sig_start);
1227 return sig_start != NULL;
1230 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1232 struct merge_remote_desc *desc;
1233 struct commit_extra_header *mergetag;
1234 char *buf;
1235 unsigned long size, len;
1236 enum object_type type;
1238 desc = merge_remote_util(parent);
1239 if (!desc || !desc->obj)
1240 return;
1241 buf = read_object_file(&desc->obj->oid, &type, &size);
1242 if (!buf || type != OBJ_TAG)
1243 goto free_return;
1244 len = parse_signature(buf, size);
1245 if (size == len)
1246 goto free_return;
1248 * We could verify this signature and either omit the tag when
1249 * it does not validate, but the integrator may not have the
1250 * public key of the signer of the tag he is merging, while a
1251 * later auditor may have it while auditing, so let's not run
1252 * verify-signed-buffer here for now...
1254 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1255 * warn("warning: signed tag unverified.");
1257 mergetag = xcalloc(1, sizeof(*mergetag));
1258 mergetag->key = xstrdup("mergetag");
1259 mergetag->value = buf;
1260 mergetag->len = size;
1262 **tail = mergetag;
1263 *tail = &mergetag->next;
1264 return;
1266 free_return:
1267 free(buf);
1270 int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
1272 struct strbuf payload = STRBUF_INIT;
1273 struct strbuf signature = STRBUF_INIT;
1274 int ret = 1;
1276 sigc->result = 'N';
1278 if (parse_signed_commit(commit, &payload, &signature) <= 0)
1279 goto out;
1280 ret = check_signature(payload.buf, payload.len, signature.buf,
1281 signature.len, sigc);
1283 out:
1284 strbuf_release(&payload);
1285 strbuf_release(&signature);
1287 return ret;
1292 void append_merge_tag_headers(struct commit_list *parents,
1293 struct commit_extra_header ***tail)
1295 while (parents) {
1296 struct commit *parent = parents->item;
1297 handle_signed_tag(parent, tail);
1298 parents = parents->next;
1302 static void add_extra_header(struct strbuf *buffer,
1303 struct commit_extra_header *extra)
1305 strbuf_addstr(buffer, extra->key);
1306 if (extra->len)
1307 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1308 else
1309 strbuf_addch(buffer, '\n');
1312 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1313 const char **exclude)
1315 struct commit_extra_header *extra = NULL;
1316 unsigned long size;
1317 const char *buffer = get_commit_buffer(commit, &size);
1318 extra = read_commit_extra_header_lines(buffer, size, exclude);
1319 unuse_commit_buffer(commit, buffer);
1320 return extra;
1323 int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
1325 struct commit_extra_header *extra, *to_free;
1326 int res = 0;
1328 to_free = read_commit_extra_headers(commit, NULL);
1329 for (extra = to_free; !res && extra; extra = extra->next) {
1330 if (strcmp(extra->key, "mergetag"))
1331 continue; /* not a merge tag */
1332 res = fn(commit, extra, data);
1334 free_commit_extra_headers(to_free);
1335 return res;
1338 static inline int standard_header_field(const char *field, size_t len)
1340 return ((len == 4 && !memcmp(field, "tree", 4)) ||
1341 (len == 6 && !memcmp(field, "parent", 6)) ||
1342 (len == 6 && !memcmp(field, "author", 6)) ||
1343 (len == 9 && !memcmp(field, "committer", 9)) ||
1344 (len == 8 && !memcmp(field, "encoding", 8)));
1347 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1349 if (!exclude)
1350 return 0;
1352 while (*exclude) {
1353 size_t xlen = strlen(*exclude);
1354 if (len == xlen && !memcmp(field, *exclude, xlen))
1355 return 1;
1356 exclude++;
1358 return 0;
1361 static struct commit_extra_header *read_commit_extra_header_lines(
1362 const char *buffer, size_t size,
1363 const char **exclude)
1365 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1366 const char *line, *next, *eof, *eob;
1367 struct strbuf buf = STRBUF_INIT;
1369 for (line = buffer, eob = line + size;
1370 line < eob && *line != '\n';
1371 line = next) {
1372 next = memchr(line, '\n', eob - line);
1373 next = next ? next + 1 : eob;
1374 if (*line == ' ') {
1375 /* continuation */
1376 if (it)
1377 strbuf_add(&buf, line + 1, next - (line + 1));
1378 continue;
1380 if (it)
1381 it->value = strbuf_detach(&buf, &it->len);
1382 strbuf_reset(&buf);
1383 it = NULL;
1385 eof = memchr(line, ' ', next - line);
1386 if (!eof)
1387 eof = next;
1388 else if (standard_header_field(line, eof - line) ||
1389 excluded_header_field(line, eof - line, exclude))
1390 continue;
1392 it = xcalloc(1, sizeof(*it));
1393 it->key = xmemdupz(line, eof-line);
1394 *tail = it;
1395 tail = &it->next;
1396 if (eof + 1 < next)
1397 strbuf_add(&buf, eof + 1, next - (eof + 1));
1399 if (it)
1400 it->value = strbuf_detach(&buf, &it->len);
1401 return extra;
1404 void free_commit_extra_headers(struct commit_extra_header *extra)
1406 while (extra) {
1407 struct commit_extra_header *next = extra->next;
1408 free(extra->key);
1409 free(extra->value);
1410 free(extra);
1411 extra = next;
1415 int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1416 struct commit_list *parents, struct object_id *ret,
1417 const char *author, const char *sign_commit)
1419 struct commit_extra_header *extra = NULL, **tail = &extra;
1420 int result;
1422 append_merge_tag_headers(parents, &tail);
1423 result = commit_tree_extended(msg, msg_len, tree, parents, ret,
1424 author, sign_commit, extra);
1425 free_commit_extra_headers(extra);
1426 return result;
1429 static int find_invalid_utf8(const char *buf, int len)
1431 int offset = 0;
1432 static const unsigned int max_codepoint[] = {
1433 0x7f, 0x7ff, 0xffff, 0x10ffff
1436 while (len) {
1437 unsigned char c = *buf++;
1438 int bytes, bad_offset;
1439 unsigned int codepoint;
1440 unsigned int min_val, max_val;
1442 len--;
1443 offset++;
1445 /* Simple US-ASCII? No worries. */
1446 if (c < 0x80)
1447 continue;
1449 bad_offset = offset-1;
1452 * Count how many more high bits set: that's how
1453 * many more bytes this sequence should have.
1455 bytes = 0;
1456 while (c & 0x40) {
1457 c <<= 1;
1458 bytes++;
1462 * Must be between 1 and 3 more bytes. Longer sequences result in
1463 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1465 if (bytes < 1 || 3 < bytes)
1466 return bad_offset;
1468 /* Do we *have* that many bytes? */
1469 if (len < bytes)
1470 return bad_offset;
1473 * Place the encoded bits at the bottom of the value and compute the
1474 * valid range.
1476 codepoint = (c & 0x7f) >> bytes;
1477 min_val = max_codepoint[bytes-1] + 1;
1478 max_val = max_codepoint[bytes];
1480 offset += bytes;
1481 len -= bytes;
1483 /* And verify that they are good continuation bytes */
1484 do {
1485 codepoint <<= 6;
1486 codepoint |= *buf & 0x3f;
1487 if ((*buf++ & 0xc0) != 0x80)
1488 return bad_offset;
1489 } while (--bytes);
1491 /* Reject codepoints that are out of range for the sequence length. */
1492 if (codepoint < min_val || codepoint > max_val)
1493 return bad_offset;
1494 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1495 if ((codepoint & 0x1ff800) == 0xd800)
1496 return bad_offset;
1497 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1498 if ((codepoint & 0xfffe) == 0xfffe)
1499 return bad_offset;
1500 /* So are anything in the range U+FDD0..U+FDEF. */
1501 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
1502 return bad_offset;
1504 return -1;
1508 * This verifies that the buffer is in proper utf8 format.
1510 * If it isn't, it assumes any non-utf8 characters are Latin1,
1511 * and does the conversion.
1513 static int verify_utf8(struct strbuf *buf)
1515 int ok = 1;
1516 long pos = 0;
1518 for (;;) {
1519 int bad;
1520 unsigned char c;
1521 unsigned char replace[2];
1523 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1524 if (bad < 0)
1525 return ok;
1526 pos += bad;
1527 ok = 0;
1528 c = buf->buf[pos];
1529 strbuf_remove(buf, pos, 1);
1531 /* We know 'c' must be in the range 128-255 */
1532 replace[0] = 0xc0 + (c >> 6);
1533 replace[1] = 0x80 + (c & 0x3f);
1534 strbuf_insert(buf, pos, replace, 2);
1535 pos += 2;
1539 static const char commit_utf8_warn[] =
1540 N_("Warning: commit message did not conform to UTF-8.\n"
1541 "You may want to amend it after fixing the message, or set the config\n"
1542 "variable i18n.commitencoding to the encoding your project uses.\n");
1544 int commit_tree_extended(const char *msg, size_t msg_len,
1545 const struct object_id *tree,
1546 struct commit_list *parents, struct object_id *ret,
1547 const char *author, const char *sign_commit,
1548 struct commit_extra_header *extra)
1550 int result;
1551 int encoding_is_utf8;
1552 struct strbuf buffer;
1554 assert_oid_type(tree, OBJ_TREE);
1556 if (memchr(msg, '\0', msg_len))
1557 return error("a NUL byte in commit log message not allowed.");
1559 /* Not having i18n.commitencoding is the same as having utf-8 */
1560 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1562 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1563 strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree));
1566 * NOTE! This ordering means that the same exact tree merged with a
1567 * different order of parents will be a _different_ changeset even
1568 * if everything else stays the same.
1570 while (parents) {
1571 struct commit *parent = pop_commit(&parents);
1572 strbuf_addf(&buffer, "parent %s\n",
1573 oid_to_hex(&parent->object.oid));
1576 /* Person/date information */
1577 if (!author)
1578 author = git_author_info(IDENT_STRICT);
1579 strbuf_addf(&buffer, "author %s\n", author);
1580 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1581 if (!encoding_is_utf8)
1582 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1584 while (extra) {
1585 add_extra_header(&buffer, extra);
1586 extra = extra->next;
1588 strbuf_addch(&buffer, '\n');
1590 /* And add the comment */
1591 strbuf_add(&buffer, msg, msg_len);
1593 /* And check the encoding */
1594 if (encoding_is_utf8 && !verify_utf8(&buffer))
1595 fprintf(stderr, _(commit_utf8_warn));
1597 if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
1598 result = -1;
1599 goto out;
1602 result = write_object_file(buffer.buf, buffer.len, commit_type, ret);
1603 out:
1604 strbuf_release(&buffer);
1605 return result;
1608 void set_merge_remote_desc(struct commit *commit,
1609 const char *name, struct object *obj)
1611 struct merge_remote_desc *desc;
1612 FLEX_ALLOC_STR(desc, name, name);
1613 desc->obj = obj;
1614 commit->util = desc;
1617 struct commit *get_merge_parent(const char *name)
1619 struct object *obj;
1620 struct commit *commit;
1621 struct object_id oid;
1622 if (get_oid(name, &oid))
1623 return NULL;
1624 obj = parse_object(&oid);
1625 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1626 if (commit && !commit->util)
1627 set_merge_remote_desc(commit, name, obj);
1628 return commit;
1632 * Append a commit to the end of the commit_list.
1634 * next starts by pointing to the variable that holds the head of an
1635 * empty commit_list, and is updated to point to the "next" field of
1636 * the last item on the list as new commits are appended.
1638 * Usage example:
1640 * struct commit_list *list;
1641 * struct commit_list **next = &list;
1643 * next = commit_list_append(c1, next);
1644 * next = commit_list_append(c2, next);
1645 * assert(commit_list_count(list) == 2);
1646 * return list;
1648 struct commit_list **commit_list_append(struct commit *commit,
1649 struct commit_list **next)
1651 struct commit_list *new_commit = xmalloc(sizeof(struct commit_list));
1652 new_commit->item = commit;
1653 *next = new_commit;
1654 new_commit->next = NULL;
1655 return &new_commit->next;
1658 const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1660 int key_len = strlen(key);
1661 const char *line = msg;
1663 while (line) {
1664 const char *eol = strchrnul(line, '\n');
1666 if (line == eol)
1667 return NULL;
1669 if (eol - line > key_len &&
1670 !strncmp(line, key, key_len) &&
1671 line[key_len] == ' ') {
1672 *out_len = eol - line - key_len - 1;
1673 return line + key_len + 1;
1675 line = *eol ? eol + 1 : NULL;
1677 return NULL;
1681 * Inspect the given string and determine the true "end" of the log message, in
1682 * order to find where to put a new Signed-off-by: line. Ignored are
1683 * trailing comment lines and blank lines. To support "git commit -s
1684 * --amend" on an existing commit, we also ignore "Conflicts:". To
1685 * support "git commit -v", we truncate at cut lines.
1687 * Returns the number of bytes from the tail to ignore, to be fed as
1688 * the second parameter to append_signoff().
1690 int ignore_non_trailer(const char *buf, size_t len)
1692 int boc = 0;
1693 int bol = 0;
1694 int in_old_conflicts_block = 0;
1695 size_t cutoff = wt_status_locate_end(buf, len);
1697 while (bol < cutoff) {
1698 const char *next_line = memchr(buf + bol, '\n', len - bol);
1700 if (!next_line)
1701 next_line = buf + len;
1702 else
1703 next_line++;
1705 if (buf[bol] == comment_line_char || buf[bol] == '\n') {
1706 /* is this the first of the run of comments? */
1707 if (!boc)
1708 boc = bol;
1709 /* otherwise, it is just continuing */
1710 } else if (starts_with(buf + bol, "Conflicts:\n")) {
1711 in_old_conflicts_block = 1;
1712 if (!boc)
1713 boc = bol;
1714 } else if (in_old_conflicts_block && buf[bol] == '\t') {
1715 ; /* a pathname in the conflicts block */
1716 } else if (boc) {
1717 /* the previous was not trailing comment */
1718 boc = 0;
1719 in_old_conflicts_block = 0;
1721 bol = next_line - buf;
1723 return boc ? len - boc : len - cutoff;