t5318: use 'test_cmp_bin' to compare commit-graph files
[alt-git.git] / commit.c
blob598cf21cee68349952c4afcf512dfdafd0769010
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 "alloc.h"
11 #include "gpg-interface.h"
12 #include "mergesort.h"
13 #include "commit-slab.h"
14 #include "prio-queue.h"
15 #include "sha1-lookup.h"
16 #include "wt-status.h"
17 #include "advice.h"
19 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
21 int save_commit_buffer = 1;
23 const char *commit_type = "commit";
25 struct commit *lookup_commit_reference_gently(const struct object_id *oid,
26 int quiet)
28 struct object *obj = deref_tag(parse_object(oid), NULL, 0);
30 if (!obj)
31 return NULL;
32 return object_as_type(obj, OBJ_COMMIT, quiet);
35 struct commit *lookup_commit_reference(const struct object_id *oid)
37 return lookup_commit_reference_gently(oid, 0);
40 struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name)
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));
49 return c;
52 struct commit *lookup_commit(const struct object_id *oid)
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);
61 struct commit *lookup_commit_reference_by_name(const char *name)
63 struct object_id oid;
64 struct commit *commit;
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;
74 static timestamp_t parse_commit_date(const char *buf, const char *tail)
76 const char *dateptr;
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);
101 static struct commit_graft **commit_graft;
102 static int commit_graft_alloc, commit_graft_nr;
104 static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
106 struct commit_graft **commit_graft_table = table;
107 return commit_graft_table[index]->oid.hash;
110 static int commit_graft_pos(const unsigned char *sha1)
112 return sha1_pos(sha1, commit_graft, commit_graft_nr,
113 commit_graft_sha1_access);
116 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
118 int pos = commit_graft_pos(graft->oid.hash);
120 if (0 <= pos) {
121 if (ignore_dups)
122 free(graft);
123 else {
124 free(commit_graft[pos]);
125 commit_graft[pos] = graft;
127 return 1;
129 pos = -pos - 1;
130 ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc);
131 commit_graft_nr++;
132 if (pos < commit_graft_nr)
133 MOVE_ARRAY(commit_graft + pos + 1, commit_graft + pos,
134 commit_graft_nr - pos - 1);
135 commit_graft[pos] = graft;
136 return 0;
139 struct commit_graft *read_graft_line(struct strbuf *line)
141 /* The format is just "Commit Parent1 Parent2 ...\n" */
142 int i, phase;
143 const char *tail = NULL;
144 struct commit_graft *graft = NULL;
145 struct object_id dummy_oid, *oid;
147 strbuf_rtrim(line);
148 if (!line->len || line->buf[0] == '#')
149 return NULL;
151 * phase 0 verifies line, counts hashes in line and allocates graft
152 * phase 1 fills graft
154 for (phase = 0; phase < 2; phase++) {
155 oid = graft ? &graft->oid : &dummy_oid;
156 if (parse_oid_hex(line->buf, oid, &tail))
157 goto bad_graft_data;
158 for (i = 0; *tail != '\0'; i++) {
159 oid = graft ? &graft->parent[i] : &dummy_oid;
160 if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
161 goto bad_graft_data;
163 if (!graft) {
164 graft = xmalloc(st_add(sizeof(*graft),
165 st_mult(sizeof(struct object_id), i)));
166 graft->nr_parent = i;
169 return graft;
171 bad_graft_data:
172 error("bad graft data: %s", line->buf);
173 assert(!graft);
174 return NULL;
177 static int read_graft_file(const char *graft_file)
179 FILE *fp = fopen_or_warn(graft_file, "r");
180 struct strbuf buf = STRBUF_INIT;
181 if (!fp)
182 return -1;
183 if (advice_graft_file_deprecated)
184 advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
185 "and will be removed in a future Git version.\n"
186 "\n"
187 "Please use \"git replace --convert-graft-file\"\n"
188 "to convert the grafts into replace refs.\n"
189 "\n"
190 "Turn this message off by running\n"
191 "\"git config advice.graftFileDeprecated false\""));
192 while (!strbuf_getwholeline(&buf, fp, '\n')) {
193 /* The format is just "Commit Parent1 Parent2 ...\n" */
194 struct commit_graft *graft = read_graft_line(&buf);
195 if (!graft)
196 continue;
197 if (register_commit_graft(graft, 1))
198 error("duplicate graft data: %s", buf.buf);
200 fclose(fp);
201 strbuf_release(&buf);
202 return 0;
205 static void prepare_commit_graft(void)
207 static int commit_graft_prepared;
208 char *graft_file;
210 if (commit_graft_prepared)
211 return;
212 if (!startup_info->have_repository)
213 return;
215 graft_file = get_graft_file();
216 read_graft_file(graft_file);
217 /* make sure shallows are read */
218 is_repository_shallow();
219 commit_graft_prepared = 1;
222 struct commit_graft *lookup_commit_graft(const struct object_id *oid)
224 int pos;
225 prepare_commit_graft();
226 pos = commit_graft_pos(oid->hash);
227 if (pos < 0)
228 return NULL;
229 return commit_graft[pos];
232 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
234 int i, ret;
235 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
236 ret = fn(commit_graft[i], cb_data);
237 return ret;
240 int unregister_shallow(const struct object_id *oid)
242 int pos = commit_graft_pos(oid->hash);
243 if (pos < 0)
244 return -1;
245 if (pos + 1 < commit_graft_nr)
246 MOVE_ARRAY(commit_graft + pos, commit_graft + pos + 1,
247 commit_graft_nr - pos - 1);
248 commit_graft_nr--;
249 return 0;
252 struct commit_buffer {
253 void *buffer;
254 unsigned long size;
256 define_commit_slab(buffer_slab, struct commit_buffer);
257 static struct buffer_slab buffer_slab = COMMIT_SLAB_INIT(1, buffer_slab);
259 void set_commit_buffer(struct commit *commit, void *buffer, unsigned long size)
261 struct commit_buffer *v = buffer_slab_at(&buffer_slab, commit);
262 v->buffer = buffer;
263 v->size = size;
266 const void *get_cached_commit_buffer(const struct commit *commit, unsigned long *sizep)
268 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
269 if (!v) {
270 if (sizep)
271 *sizep = 0;
272 return NULL;
274 if (sizep)
275 *sizep = v->size;
276 return v->buffer;
279 const void *get_commit_buffer(const struct commit *commit, unsigned long *sizep)
281 const void *ret = get_cached_commit_buffer(commit, sizep);
282 if (!ret) {
283 enum object_type type;
284 unsigned long size;
285 ret = read_object_file(&commit->object.oid, &type, &size);
286 if (!ret)
287 die("cannot read commit object %s",
288 oid_to_hex(&commit->object.oid));
289 if (type != OBJ_COMMIT)
290 die("expected commit for %s, got %s",
291 oid_to_hex(&commit->object.oid), type_name(type));
292 if (sizep)
293 *sizep = size;
295 return ret;
298 void unuse_commit_buffer(const struct commit *commit, const void *buffer)
300 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
301 if (!(v && v->buffer == buffer))
302 free((void *)buffer);
305 void free_commit_buffer(struct commit *commit)
307 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
308 if (v) {
309 FREE_AND_NULL(v->buffer);
310 v->size = 0;
314 struct tree *get_commit_tree(const struct commit *commit)
316 if (commit->maybe_tree || !commit->object.parsed)
317 return commit->maybe_tree;
319 if (commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
320 BUG("commit has NULL tree, but was not loaded from commit-graph");
322 return get_commit_tree_in_graph(commit);
325 struct object_id *get_commit_tree_oid(const struct commit *commit)
327 return &get_commit_tree(commit)->object.oid;
330 void release_commit_memory(struct commit *c)
332 c->maybe_tree = NULL;
333 c->index = 0;
334 free_commit_buffer(c);
335 free_commit_list(c->parents);
336 /* TODO: what about commit->util? */
338 c->object.parsed = 0;
341 const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
343 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
344 void *ret;
346 if (!v) {
347 if (sizep)
348 *sizep = 0;
349 return NULL;
351 ret = v->buffer;
352 if (sizep)
353 *sizep = v->size;
355 v->buffer = NULL;
356 v->size = 0;
357 return ret;
360 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size, int check_graph)
362 const char *tail = buffer;
363 const char *bufptr = buffer;
364 struct object_id parent;
365 struct commit_list **pptr;
366 struct commit_graft *graft;
367 const int tree_entry_len = GIT_SHA1_HEXSZ + 5;
368 const int parent_entry_len = GIT_SHA1_HEXSZ + 7;
370 if (item->object.parsed)
371 return 0;
372 item->object.parsed = 1;
373 tail += size;
374 if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
375 bufptr[tree_entry_len] != '\n')
376 return error("bogus commit object %s", oid_to_hex(&item->object.oid));
377 if (get_oid_hex(bufptr + 5, &parent) < 0)
378 return error("bad tree pointer in commit %s",
379 oid_to_hex(&item->object.oid));
380 item->maybe_tree = lookup_tree(&parent);
381 bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
382 pptr = &item->parents;
384 graft = lookup_commit_graft(&item->object.oid);
385 while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
386 struct commit *new_parent;
388 if (tail <= bufptr + parent_entry_len + 1 ||
389 get_oid_hex(bufptr + 7, &parent) ||
390 bufptr[parent_entry_len] != '\n')
391 return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
392 bufptr += parent_entry_len + 1;
394 * The clone is shallow if nr_parent < 0, and we must
395 * not traverse its real parents even when we unhide them.
397 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
398 continue;
399 new_parent = lookup_commit(&parent);
400 if (new_parent)
401 pptr = &commit_list_insert(new_parent, pptr)->next;
403 if (graft) {
404 int i;
405 struct commit *new_parent;
406 for (i = 0; i < graft->nr_parent; i++) {
407 new_parent = lookup_commit(&graft->parent[i]);
408 if (!new_parent)
409 continue;
410 pptr = &commit_list_insert(new_parent, pptr)->next;
413 item->date = parse_commit_date(bufptr, tail);
415 if (check_graph)
416 load_commit_graph_info(item);
418 return 0;
421 int parse_commit_internal(struct commit *item, int quiet_on_missing, int use_commit_graph)
423 enum object_type type;
424 void *buffer;
425 unsigned long size;
426 int ret;
428 if (!item)
429 return -1;
430 if (item->object.parsed)
431 return 0;
432 if (use_commit_graph && parse_commit_in_graph(item))
433 return 0;
434 buffer = read_object_file(&item->object.oid, &type, &size);
435 if (!buffer)
436 return quiet_on_missing ? -1 :
437 error("Could not read %s",
438 oid_to_hex(&item->object.oid));
439 if (type != OBJ_COMMIT) {
440 free(buffer);
441 return error("Object %s not a commit",
442 oid_to_hex(&item->object.oid));
445 ret = parse_commit_buffer(item, buffer, size, 0);
446 if (save_commit_buffer && !ret) {
447 set_commit_buffer(item, buffer, size);
448 return 0;
450 free(buffer);
451 return ret;
454 int parse_commit_gently(struct commit *item, int quiet_on_missing)
456 return parse_commit_internal(item, quiet_on_missing, 1);
459 void parse_commit_or_die(struct commit *item)
461 if (parse_commit(item))
462 die("unable to parse commit %s",
463 item ? oid_to_hex(&item->object.oid) : "(null)");
466 int find_commit_subject(const char *commit_buffer, const char **subject)
468 const char *eol;
469 const char *p = commit_buffer;
471 while (*p && (*p != '\n' || p[1] != '\n'))
472 p++;
473 if (*p) {
474 p = skip_blank_lines(p + 2);
475 eol = strchrnul(p, '\n');
476 } else
477 eol = p;
479 *subject = p;
481 return eol - p;
484 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
486 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
487 new_list->item = item;
488 new_list->next = *list_p;
489 *list_p = new_list;
490 return new_list;
493 unsigned commit_list_count(const struct commit_list *l)
495 unsigned c = 0;
496 for (; l; l = l->next )
497 c++;
498 return c;
501 struct commit_list *copy_commit_list(struct commit_list *list)
503 struct commit_list *head = NULL;
504 struct commit_list **pp = &head;
505 while (list) {
506 pp = commit_list_append(list->item, pp);
507 list = list->next;
509 return head;
512 void free_commit_list(struct commit_list *list)
514 while (list)
515 pop_commit(&list);
518 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
520 struct commit_list **pp = list;
521 struct commit_list *p;
522 while ((p = *pp) != NULL) {
523 if (p->item->date < item->date) {
524 break;
526 pp = &p->next;
528 return commit_list_insert(item, pp);
531 static int commit_list_compare_by_date(const void *a, const void *b)
533 timestamp_t a_date = ((const struct commit_list *)a)->item->date;
534 timestamp_t b_date = ((const struct commit_list *)b)->item->date;
535 if (a_date < b_date)
536 return 1;
537 if (a_date > b_date)
538 return -1;
539 return 0;
542 static void *commit_list_get_next(const void *a)
544 return ((const struct commit_list *)a)->next;
547 static void commit_list_set_next(void *a, void *next)
549 ((struct commit_list *)a)->next = next;
552 void commit_list_sort_by_date(struct commit_list **list)
554 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
555 commit_list_compare_by_date);
558 struct commit *pop_most_recent_commit(struct commit_list **list,
559 unsigned int mark)
561 struct commit *ret = pop_commit(list);
562 struct commit_list *parents = ret->parents;
564 while (parents) {
565 struct commit *commit = parents->item;
566 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
567 commit->object.flags |= mark;
568 commit_list_insert_by_date(commit, list);
570 parents = parents->next;
572 return ret;
575 static void clear_commit_marks_1(struct commit_list **plist,
576 struct commit *commit, unsigned int mark)
578 while (commit) {
579 struct commit_list *parents;
581 if (!(mark & commit->object.flags))
582 return;
584 commit->object.flags &= ~mark;
586 parents = commit->parents;
587 if (!parents)
588 return;
590 while ((parents = parents->next))
591 commit_list_insert(parents->item, plist);
593 commit = commit->parents->item;
597 void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
599 struct commit_list *list = NULL;
601 while (nr--) {
602 clear_commit_marks_1(&list, *commit, mark);
603 commit++;
605 while (list)
606 clear_commit_marks_1(&list, pop_commit(&list), mark);
609 void clear_commit_marks(struct commit *commit, unsigned int mark)
611 clear_commit_marks_many(1, &commit, mark);
614 struct commit *pop_commit(struct commit_list **stack)
616 struct commit_list *top = *stack;
617 struct commit *item = top ? top->item : NULL;
619 if (top) {
620 *stack = top->next;
621 free(top);
623 return item;
627 * Topological sort support
630 /* count number of children that have not been emitted */
631 define_commit_slab(indegree_slab, int);
633 /* record author-date for each commit object */
634 define_commit_slab(author_date_slab, unsigned long);
636 static void record_author_date(struct author_date_slab *author_date,
637 struct commit *commit)
639 const char *buffer = get_commit_buffer(commit, NULL);
640 struct ident_split ident;
641 const char *ident_line;
642 size_t ident_len;
643 char *date_end;
644 timestamp_t date;
646 ident_line = find_commit_header(buffer, "author", &ident_len);
647 if (!ident_line)
648 goto fail_exit; /* no author line */
649 if (split_ident_line(&ident, ident_line, ident_len) ||
650 !ident.date_begin || !ident.date_end)
651 goto fail_exit; /* malformed "author" line */
653 date = parse_timestamp(ident.date_begin, &date_end, 10);
654 if (date_end != ident.date_end)
655 goto fail_exit; /* malformed date */
656 *(author_date_slab_at(author_date, commit)) = date;
658 fail_exit:
659 unuse_commit_buffer(commit, buffer);
662 static int compare_commits_by_author_date(const void *a_, const void *b_,
663 void *cb_data)
665 const struct commit *a = a_, *b = b_;
666 struct author_date_slab *author_date = cb_data;
667 timestamp_t a_date = *(author_date_slab_at(author_date, a));
668 timestamp_t b_date = *(author_date_slab_at(author_date, b));
670 /* newer commits with larger date first */
671 if (a_date < b_date)
672 return 1;
673 else if (a_date > b_date)
674 return -1;
675 return 0;
678 int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused)
680 const struct commit *a = a_, *b = b_;
682 /* newer commits first */
683 if (a->generation < b->generation)
684 return 1;
685 else if (a->generation > b->generation)
686 return -1;
688 /* use date as a heuristic when generations are equal */
689 if (a->date < b->date)
690 return 1;
691 else if (a->date > b->date)
692 return -1;
693 return 0;
696 int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
698 const struct commit *a = a_, *b = b_;
699 /* newer commits with larger date first */
700 if (a->date < b->date)
701 return 1;
702 else if (a->date > b->date)
703 return -1;
704 return 0;
708 * Performs an in-place topological sort on the list supplied.
710 void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
712 struct commit_list *next, *orig = *list;
713 struct commit_list **pptr;
714 struct indegree_slab indegree;
715 struct prio_queue queue;
716 struct commit *commit;
717 struct author_date_slab author_date;
719 if (!orig)
720 return;
721 *list = NULL;
723 init_indegree_slab(&indegree);
724 memset(&queue, '\0', sizeof(queue));
726 switch (sort_order) {
727 default: /* REV_SORT_IN_GRAPH_ORDER */
728 queue.compare = NULL;
729 break;
730 case REV_SORT_BY_COMMIT_DATE:
731 queue.compare = compare_commits_by_commit_date;
732 break;
733 case REV_SORT_BY_AUTHOR_DATE:
734 init_author_date_slab(&author_date);
735 queue.compare = compare_commits_by_author_date;
736 queue.cb_data = &author_date;
737 break;
740 /* Mark them and clear the indegree */
741 for (next = orig; next; next = next->next) {
742 struct commit *commit = next->item;
743 *(indegree_slab_at(&indegree, commit)) = 1;
744 /* also record the author dates, if needed */
745 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
746 record_author_date(&author_date, commit);
749 /* update the indegree */
750 for (next = orig; next; next = next->next) {
751 struct commit_list *parents = next->item->parents;
752 while (parents) {
753 struct commit *parent = parents->item;
754 int *pi = indegree_slab_at(&indegree, parent);
756 if (*pi)
757 (*pi)++;
758 parents = parents->next;
763 * find the tips
765 * tips are nodes not reachable from any other node in the list
767 * the tips serve as a starting set for the work queue.
769 for (next = orig; next; next = next->next) {
770 struct commit *commit = next->item;
772 if (*(indegree_slab_at(&indegree, commit)) == 1)
773 prio_queue_put(&queue, commit);
777 * This is unfortunate; the initial tips need to be shown
778 * in the order given from the revision traversal machinery.
780 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
781 prio_queue_reverse(&queue);
783 /* We no longer need the commit list */
784 free_commit_list(orig);
786 pptr = list;
787 *list = NULL;
788 while ((commit = prio_queue_get(&queue)) != NULL) {
789 struct commit_list *parents;
791 for (parents = commit->parents; parents ; parents = parents->next) {
792 struct commit *parent = parents->item;
793 int *pi = indegree_slab_at(&indegree, parent);
795 if (!*pi)
796 continue;
799 * parents are only enqueued for emission
800 * when all their children have been emitted thereby
801 * guaranteeing topological order.
803 if (--(*pi) == 1)
804 prio_queue_put(&queue, parent);
807 * all children of commit have already been
808 * emitted. we can emit it now.
810 *(indegree_slab_at(&indegree, commit)) = 0;
812 pptr = &commit_list_insert(commit, pptr)->next;
815 clear_indegree_slab(&indegree);
816 clear_prio_queue(&queue);
817 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
818 clear_author_date_slab(&author_date);
821 /* merge-base stuff */
823 /* Remember to update object flag allocation in object.h */
824 #define PARENT1 (1u<<16)
825 #define PARENT2 (1u<<17)
826 #define STALE (1u<<18)
827 #define RESULT (1u<<19)
829 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
831 static int queue_has_nonstale(struct prio_queue *queue)
833 int i;
834 for (i = 0; i < queue->nr; i++) {
835 struct commit *commit = queue->array[i].data;
836 if (!(commit->object.flags & STALE))
837 return 1;
839 return 0;
842 /* all input commits in one and twos[] must have been parsed! */
843 static struct commit_list *paint_down_to_common(struct commit *one, int n,
844 struct commit **twos,
845 int min_generation)
847 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
848 struct commit_list *result = NULL;
849 int i;
850 uint32_t last_gen = GENERATION_NUMBER_INFINITY;
852 one->object.flags |= PARENT1;
853 if (!n) {
854 commit_list_append(one, &result);
855 return result;
857 prio_queue_put(&queue, one);
859 for (i = 0; i < n; i++) {
860 twos[i]->object.flags |= PARENT2;
861 prio_queue_put(&queue, twos[i]);
864 while (queue_has_nonstale(&queue)) {
865 struct commit *commit = prio_queue_get(&queue);
866 struct commit_list *parents;
867 int flags;
869 if (commit->generation > last_gen)
870 BUG("bad generation skip %8x > %8x at %s",
871 commit->generation, last_gen,
872 oid_to_hex(&commit->object.oid));
873 last_gen = commit->generation;
875 if (commit->generation < min_generation)
876 break;
878 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
879 if (flags == (PARENT1 | PARENT2)) {
880 if (!(commit->object.flags & RESULT)) {
881 commit->object.flags |= RESULT;
882 commit_list_insert_by_date(commit, &result);
884 /* Mark parents of a found merge stale */
885 flags |= STALE;
887 parents = commit->parents;
888 while (parents) {
889 struct commit *p = parents->item;
890 parents = parents->next;
891 if ((p->object.flags & flags) == flags)
892 continue;
893 if (parse_commit(p))
894 return NULL;
895 p->object.flags |= flags;
896 prio_queue_put(&queue, p);
900 clear_prio_queue(&queue);
901 return result;
904 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
906 struct commit_list *list = NULL;
907 struct commit_list *result = NULL;
908 int i;
910 for (i = 0; i < n; i++) {
911 if (one == twos[i])
913 * We do not mark this even with RESULT so we do not
914 * have to clean it up.
916 return commit_list_insert(one, &result);
919 if (parse_commit(one))
920 return NULL;
921 for (i = 0; i < n; i++) {
922 if (parse_commit(twos[i]))
923 return NULL;
926 list = paint_down_to_common(one, n, twos, 0);
928 while (list) {
929 struct commit *commit = pop_commit(&list);
930 if (!(commit->object.flags & STALE))
931 commit_list_insert_by_date(commit, &result);
933 return result;
936 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
938 struct commit_list *i, *j, *k, *ret = NULL;
940 if (!in)
941 return ret;
943 commit_list_insert(in->item, &ret);
945 for (i = in->next; i; i = i->next) {
946 struct commit_list *new_commits = NULL, *end = NULL;
948 for (j = ret; j; j = j->next) {
949 struct commit_list *bases;
950 bases = get_merge_bases(i->item, j->item);
951 if (!new_commits)
952 new_commits = bases;
953 else
954 end->next = bases;
955 for (k = bases; k; k = k->next)
956 end = k;
958 ret = new_commits;
960 return ret;
963 static int remove_redundant(struct commit **array, int cnt)
966 * Some commit in the array may be an ancestor of
967 * another commit. Move such commit to the end of
968 * the array, and return the number of commits that
969 * are independent from each other.
971 struct commit **work;
972 unsigned char *redundant;
973 int *filled_index;
974 int i, j, filled;
976 work = xcalloc(cnt, sizeof(*work));
977 redundant = xcalloc(cnt, 1);
978 ALLOC_ARRAY(filled_index, cnt - 1);
980 for (i = 0; i < cnt; i++)
981 parse_commit(array[i]);
982 for (i = 0; i < cnt; i++) {
983 struct commit_list *common;
984 uint32_t min_generation = array[i]->generation;
986 if (redundant[i])
987 continue;
988 for (j = filled = 0; j < cnt; j++) {
989 if (i == j || redundant[j])
990 continue;
991 filled_index[filled] = j;
992 work[filled++] = array[j];
994 if (array[j]->generation < min_generation)
995 min_generation = array[j]->generation;
997 common = paint_down_to_common(array[i], filled, work,
998 min_generation);
999 if (array[i]->object.flags & PARENT2)
1000 redundant[i] = 1;
1001 for (j = 0; j < filled; j++)
1002 if (work[j]->object.flags & PARENT1)
1003 redundant[filled_index[j]] = 1;
1004 clear_commit_marks(array[i], all_flags);
1005 clear_commit_marks_many(filled, work, all_flags);
1006 free_commit_list(common);
1009 /* Now collect the result */
1010 COPY_ARRAY(work, array, cnt);
1011 for (i = filled = 0; i < cnt; i++)
1012 if (!redundant[i])
1013 array[filled++] = work[i];
1014 for (j = filled, i = 0; i < cnt; i++)
1015 if (redundant[i])
1016 array[j++] = work[i];
1017 free(work);
1018 free(redundant);
1019 free(filled_index);
1020 return filled;
1023 static struct commit_list *get_merge_bases_many_0(struct commit *one,
1024 int n,
1025 struct commit **twos,
1026 int cleanup)
1028 struct commit_list *list;
1029 struct commit **rslt;
1030 struct commit_list *result;
1031 int cnt, i;
1033 result = merge_bases_many(one, n, twos);
1034 for (i = 0; i < n; i++) {
1035 if (one == twos[i])
1036 return result;
1038 if (!result || !result->next) {
1039 if (cleanup) {
1040 clear_commit_marks(one, all_flags);
1041 clear_commit_marks_many(n, twos, all_flags);
1043 return result;
1046 /* There are more than one */
1047 cnt = commit_list_count(result);
1048 rslt = xcalloc(cnt, sizeof(*rslt));
1049 for (list = result, i = 0; list; list = list->next)
1050 rslt[i++] = list->item;
1051 free_commit_list(result);
1053 clear_commit_marks(one, all_flags);
1054 clear_commit_marks_many(n, twos, all_flags);
1056 cnt = remove_redundant(rslt, cnt);
1057 result = NULL;
1058 for (i = 0; i < cnt; i++)
1059 commit_list_insert_by_date(rslt[i], &result);
1060 free(rslt);
1061 return result;
1064 struct commit_list *get_merge_bases_many(struct commit *one,
1065 int n,
1066 struct commit **twos)
1068 return get_merge_bases_many_0(one, n, twos, 1);
1071 struct commit_list *get_merge_bases_many_dirty(struct commit *one,
1072 int n,
1073 struct commit **twos)
1075 return get_merge_bases_many_0(one, n, twos, 0);
1078 struct commit_list *get_merge_bases(struct commit *one, struct commit *two)
1080 return get_merge_bases_many_0(one, 1, &two, 1);
1084 * Is "commit" a descendant of one of the elements on the "with_commit" list?
1086 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
1088 if (!with_commit)
1089 return 1;
1090 while (with_commit) {
1091 struct commit *other;
1093 other = with_commit->item;
1094 with_commit = with_commit->next;
1095 if (in_merge_bases(other, commit))
1096 return 1;
1098 return 0;
1102 * Is "commit" an ancestor of one of the "references"?
1104 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
1106 struct commit_list *bases;
1107 int ret = 0, i;
1108 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
1110 if (parse_commit(commit))
1111 return ret;
1112 for (i = 0; i < nr_reference; i++) {
1113 if (parse_commit(reference[i]))
1114 return ret;
1115 if (reference[i]->generation < min_generation)
1116 min_generation = reference[i]->generation;
1119 if (commit->generation > min_generation)
1120 return ret;
1122 bases = paint_down_to_common(commit, nr_reference, reference, commit->generation);
1123 if (commit->object.flags & PARENT2)
1124 ret = 1;
1125 clear_commit_marks(commit, all_flags);
1126 clear_commit_marks_many(nr_reference, reference, all_flags);
1127 free_commit_list(bases);
1128 return ret;
1132 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1134 int in_merge_bases(struct commit *commit, struct commit *reference)
1136 return in_merge_bases_many(commit, 1, &reference);
1139 struct commit_list *reduce_heads(struct commit_list *heads)
1141 struct commit_list *p;
1142 struct commit_list *result = NULL, **tail = &result;
1143 struct commit **array;
1144 int num_head, i;
1146 if (!heads)
1147 return NULL;
1149 /* Uniquify */
1150 for (p = heads; p; p = p->next)
1151 p->item->object.flags &= ~STALE;
1152 for (p = heads, num_head = 0; p; p = p->next) {
1153 if (p->item->object.flags & STALE)
1154 continue;
1155 p->item->object.flags |= STALE;
1156 num_head++;
1158 array = xcalloc(num_head, sizeof(*array));
1159 for (p = heads, i = 0; p; p = p->next) {
1160 if (p->item->object.flags & STALE) {
1161 array[i++] = p->item;
1162 p->item->object.flags &= ~STALE;
1165 num_head = remove_redundant(array, num_head);
1166 for (i = 0; i < num_head; i++)
1167 tail = &commit_list_insert(array[i], tail)->next;
1168 free(array);
1169 return result;
1172 void reduce_heads_replace(struct commit_list **heads)
1174 struct commit_list *result = reduce_heads(*heads);
1175 free_commit_list(*heads);
1176 *heads = result;
1179 static const char gpg_sig_header[] = "gpgsig";
1180 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
1182 static int do_sign_commit(struct strbuf *buf, const char *keyid)
1184 struct strbuf sig = STRBUF_INIT;
1185 int inspos, copypos;
1186 const char *eoh;
1188 /* find the end of the header */
1189 eoh = strstr(buf->buf, "\n\n");
1190 if (!eoh)
1191 inspos = buf->len;
1192 else
1193 inspos = eoh - buf->buf + 1;
1195 if (!keyid || !*keyid)
1196 keyid = get_signing_key();
1197 if (sign_buffer(buf, &sig, keyid)) {
1198 strbuf_release(&sig);
1199 return -1;
1202 for (copypos = 0; sig.buf[copypos]; ) {
1203 const char *bol = sig.buf + copypos;
1204 const char *eol = strchrnul(bol, '\n');
1205 int len = (eol - bol) + !!*eol;
1207 if (!copypos) {
1208 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1209 inspos += gpg_sig_header_len;
1211 strbuf_insert(buf, inspos++, " ", 1);
1212 strbuf_insert(buf, inspos, bol, len);
1213 inspos += len;
1214 copypos += len;
1216 strbuf_release(&sig);
1217 return 0;
1220 int parse_signed_commit(const struct commit *commit,
1221 struct strbuf *payload, struct strbuf *signature)
1224 unsigned long size;
1225 const char *buffer = get_commit_buffer(commit, &size);
1226 int in_signature, saw_signature = -1;
1227 const char *line, *tail;
1229 line = buffer;
1230 tail = buffer + size;
1231 in_signature = 0;
1232 saw_signature = 0;
1233 while (line < tail) {
1234 const char *sig = NULL;
1235 const char *next = memchr(line, '\n', tail - line);
1237 next = next ? next + 1 : tail;
1238 if (in_signature && line[0] == ' ')
1239 sig = line + 1;
1240 else if (starts_with(line, gpg_sig_header) &&
1241 line[gpg_sig_header_len] == ' ')
1242 sig = line + gpg_sig_header_len + 1;
1243 if (sig) {
1244 strbuf_add(signature, sig, next - sig);
1245 saw_signature = 1;
1246 in_signature = 1;
1247 } else {
1248 if (*line == '\n')
1249 /* dump the whole remainder of the buffer */
1250 next = tail;
1251 strbuf_add(payload, line, next - line);
1252 in_signature = 0;
1254 line = next;
1256 unuse_commit_buffer(commit, buffer);
1257 return saw_signature;
1260 int remove_signature(struct strbuf *buf)
1262 const char *line = buf->buf;
1263 const char *tail = buf->buf + buf->len;
1264 int in_signature = 0;
1265 const char *sig_start = NULL;
1266 const char *sig_end = NULL;
1268 while (line < tail) {
1269 const char *next = memchr(line, '\n', tail - line);
1270 next = next ? next + 1 : tail;
1272 if (in_signature && line[0] == ' ')
1273 sig_end = next;
1274 else if (starts_with(line, gpg_sig_header) &&
1275 line[gpg_sig_header_len] == ' ') {
1276 sig_start = line;
1277 sig_end = next;
1278 in_signature = 1;
1279 } else {
1280 if (*line == '\n')
1281 /* dump the whole remainder of the buffer */
1282 next = tail;
1283 in_signature = 0;
1285 line = next;
1288 if (sig_start)
1289 strbuf_remove(buf, sig_start - buf->buf, sig_end - sig_start);
1291 return sig_start != NULL;
1294 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1296 struct merge_remote_desc *desc;
1297 struct commit_extra_header *mergetag;
1298 char *buf;
1299 unsigned long size, len;
1300 enum object_type type;
1302 desc = merge_remote_util(parent);
1303 if (!desc || !desc->obj)
1304 return;
1305 buf = read_object_file(&desc->obj->oid, &type, &size);
1306 if (!buf || type != OBJ_TAG)
1307 goto free_return;
1308 len = parse_signature(buf, size);
1309 if (size == len)
1310 goto free_return;
1312 * We could verify this signature and either omit the tag when
1313 * it does not validate, but the integrator may not have the
1314 * public key of the signer of the tag he is merging, while a
1315 * later auditor may have it while auditing, so let's not run
1316 * verify-signed-buffer here for now...
1318 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1319 * warn("warning: signed tag unverified.");
1321 mergetag = xcalloc(1, sizeof(*mergetag));
1322 mergetag->key = xstrdup("mergetag");
1323 mergetag->value = buf;
1324 mergetag->len = size;
1326 **tail = mergetag;
1327 *tail = &mergetag->next;
1328 return;
1330 free_return:
1331 free(buf);
1334 int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
1336 struct strbuf payload = STRBUF_INIT;
1337 struct strbuf signature = STRBUF_INIT;
1338 int ret = 1;
1340 sigc->result = 'N';
1342 if (parse_signed_commit(commit, &payload, &signature) <= 0)
1343 goto out;
1344 ret = check_signature(payload.buf, payload.len, signature.buf,
1345 signature.len, sigc);
1347 out:
1348 strbuf_release(&payload);
1349 strbuf_release(&signature);
1351 return ret;
1356 void append_merge_tag_headers(struct commit_list *parents,
1357 struct commit_extra_header ***tail)
1359 while (parents) {
1360 struct commit *parent = parents->item;
1361 handle_signed_tag(parent, tail);
1362 parents = parents->next;
1366 static void add_extra_header(struct strbuf *buffer,
1367 struct commit_extra_header *extra)
1369 strbuf_addstr(buffer, extra->key);
1370 if (extra->len)
1371 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1372 else
1373 strbuf_addch(buffer, '\n');
1376 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1377 const char **exclude)
1379 struct commit_extra_header *extra = NULL;
1380 unsigned long size;
1381 const char *buffer = get_commit_buffer(commit, &size);
1382 extra = read_commit_extra_header_lines(buffer, size, exclude);
1383 unuse_commit_buffer(commit, buffer);
1384 return extra;
1387 int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
1389 struct commit_extra_header *extra, *to_free;
1390 int res = 0;
1392 to_free = read_commit_extra_headers(commit, NULL);
1393 for (extra = to_free; !res && extra; extra = extra->next) {
1394 if (strcmp(extra->key, "mergetag"))
1395 continue; /* not a merge tag */
1396 res = fn(commit, extra, data);
1398 free_commit_extra_headers(to_free);
1399 return res;
1402 static inline int standard_header_field(const char *field, size_t len)
1404 return ((len == 4 && !memcmp(field, "tree", 4)) ||
1405 (len == 6 && !memcmp(field, "parent", 6)) ||
1406 (len == 6 && !memcmp(field, "author", 6)) ||
1407 (len == 9 && !memcmp(field, "committer", 9)) ||
1408 (len == 8 && !memcmp(field, "encoding", 8)));
1411 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1413 if (!exclude)
1414 return 0;
1416 while (*exclude) {
1417 size_t xlen = strlen(*exclude);
1418 if (len == xlen && !memcmp(field, *exclude, xlen))
1419 return 1;
1420 exclude++;
1422 return 0;
1425 static struct commit_extra_header *read_commit_extra_header_lines(
1426 const char *buffer, size_t size,
1427 const char **exclude)
1429 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1430 const char *line, *next, *eof, *eob;
1431 struct strbuf buf = STRBUF_INIT;
1433 for (line = buffer, eob = line + size;
1434 line < eob && *line != '\n';
1435 line = next) {
1436 next = memchr(line, '\n', eob - line);
1437 next = next ? next + 1 : eob;
1438 if (*line == ' ') {
1439 /* continuation */
1440 if (it)
1441 strbuf_add(&buf, line + 1, next - (line + 1));
1442 continue;
1444 if (it)
1445 it->value = strbuf_detach(&buf, &it->len);
1446 strbuf_reset(&buf);
1447 it = NULL;
1449 eof = memchr(line, ' ', next - line);
1450 if (!eof)
1451 eof = next;
1452 else if (standard_header_field(line, eof - line) ||
1453 excluded_header_field(line, eof - line, exclude))
1454 continue;
1456 it = xcalloc(1, sizeof(*it));
1457 it->key = xmemdupz(line, eof-line);
1458 *tail = it;
1459 tail = &it->next;
1460 if (eof + 1 < next)
1461 strbuf_add(&buf, eof + 1, next - (eof + 1));
1463 if (it)
1464 it->value = strbuf_detach(&buf, &it->len);
1465 return extra;
1468 void free_commit_extra_headers(struct commit_extra_header *extra)
1470 while (extra) {
1471 struct commit_extra_header *next = extra->next;
1472 free(extra->key);
1473 free(extra->value);
1474 free(extra);
1475 extra = next;
1479 int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1480 struct commit_list *parents, struct object_id *ret,
1481 const char *author, const char *sign_commit)
1483 struct commit_extra_header *extra = NULL, **tail = &extra;
1484 int result;
1486 append_merge_tag_headers(parents, &tail);
1487 result = commit_tree_extended(msg, msg_len, tree, parents, ret,
1488 author, sign_commit, extra);
1489 free_commit_extra_headers(extra);
1490 return result;
1493 static int find_invalid_utf8(const char *buf, int len)
1495 int offset = 0;
1496 static const unsigned int max_codepoint[] = {
1497 0x7f, 0x7ff, 0xffff, 0x10ffff
1500 while (len) {
1501 unsigned char c = *buf++;
1502 int bytes, bad_offset;
1503 unsigned int codepoint;
1504 unsigned int min_val, max_val;
1506 len--;
1507 offset++;
1509 /* Simple US-ASCII? No worries. */
1510 if (c < 0x80)
1511 continue;
1513 bad_offset = offset-1;
1516 * Count how many more high bits set: that's how
1517 * many more bytes this sequence should have.
1519 bytes = 0;
1520 while (c & 0x40) {
1521 c <<= 1;
1522 bytes++;
1526 * Must be between 1 and 3 more bytes. Longer sequences result in
1527 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1529 if (bytes < 1 || 3 < bytes)
1530 return bad_offset;
1532 /* Do we *have* that many bytes? */
1533 if (len < bytes)
1534 return bad_offset;
1537 * Place the encoded bits at the bottom of the value and compute the
1538 * valid range.
1540 codepoint = (c & 0x7f) >> bytes;
1541 min_val = max_codepoint[bytes-1] + 1;
1542 max_val = max_codepoint[bytes];
1544 offset += bytes;
1545 len -= bytes;
1547 /* And verify that they are good continuation bytes */
1548 do {
1549 codepoint <<= 6;
1550 codepoint |= *buf & 0x3f;
1551 if ((*buf++ & 0xc0) != 0x80)
1552 return bad_offset;
1553 } while (--bytes);
1555 /* Reject codepoints that are out of range for the sequence length. */
1556 if (codepoint < min_val || codepoint > max_val)
1557 return bad_offset;
1558 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1559 if ((codepoint & 0x1ff800) == 0xd800)
1560 return bad_offset;
1561 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1562 if ((codepoint & 0xfffe) == 0xfffe)
1563 return bad_offset;
1564 /* So are anything in the range U+FDD0..U+FDEF. */
1565 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
1566 return bad_offset;
1568 return -1;
1572 * This verifies that the buffer is in proper utf8 format.
1574 * If it isn't, it assumes any non-utf8 characters are Latin1,
1575 * and does the conversion.
1577 static int verify_utf8(struct strbuf *buf)
1579 int ok = 1;
1580 long pos = 0;
1582 for (;;) {
1583 int bad;
1584 unsigned char c;
1585 unsigned char replace[2];
1587 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1588 if (bad < 0)
1589 return ok;
1590 pos += bad;
1591 ok = 0;
1592 c = buf->buf[pos];
1593 strbuf_remove(buf, pos, 1);
1595 /* We know 'c' must be in the range 128-255 */
1596 replace[0] = 0xc0 + (c >> 6);
1597 replace[1] = 0x80 + (c & 0x3f);
1598 strbuf_insert(buf, pos, replace, 2);
1599 pos += 2;
1603 static const char commit_utf8_warn[] =
1604 N_("Warning: commit message did not conform to UTF-8.\n"
1605 "You may want to amend it after fixing the message, or set the config\n"
1606 "variable i18n.commitencoding to the encoding your project uses.\n");
1608 int commit_tree_extended(const char *msg, size_t msg_len,
1609 const struct object_id *tree,
1610 struct commit_list *parents, struct object_id *ret,
1611 const char *author, const char *sign_commit,
1612 struct commit_extra_header *extra)
1614 int result;
1615 int encoding_is_utf8;
1616 struct strbuf buffer;
1618 assert_oid_type(tree, OBJ_TREE);
1620 if (memchr(msg, '\0', msg_len))
1621 return error("a NUL byte in commit log message not allowed.");
1623 /* Not having i18n.commitencoding is the same as having utf-8 */
1624 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1626 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1627 strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree));
1630 * NOTE! This ordering means that the same exact tree merged with a
1631 * different order of parents will be a _different_ changeset even
1632 * if everything else stays the same.
1634 while (parents) {
1635 struct commit *parent = pop_commit(&parents);
1636 strbuf_addf(&buffer, "parent %s\n",
1637 oid_to_hex(&parent->object.oid));
1640 /* Person/date information */
1641 if (!author)
1642 author = git_author_info(IDENT_STRICT);
1643 strbuf_addf(&buffer, "author %s\n", author);
1644 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1645 if (!encoding_is_utf8)
1646 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1648 while (extra) {
1649 add_extra_header(&buffer, extra);
1650 extra = extra->next;
1652 strbuf_addch(&buffer, '\n');
1654 /* And add the comment */
1655 strbuf_add(&buffer, msg, msg_len);
1657 /* And check the encoding */
1658 if (encoding_is_utf8 && !verify_utf8(&buffer))
1659 fprintf(stderr, _(commit_utf8_warn));
1661 if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
1662 result = -1;
1663 goto out;
1666 result = write_object_file(buffer.buf, buffer.len, commit_type, ret);
1667 out:
1668 strbuf_release(&buffer);
1669 return result;
1672 define_commit_slab(merge_desc_slab, struct merge_remote_desc *);
1673 static struct merge_desc_slab merge_desc_slab = COMMIT_SLAB_INIT(1, merge_desc_slab);
1675 struct merge_remote_desc *merge_remote_util(struct commit *commit)
1677 return *merge_desc_slab_at(&merge_desc_slab, commit);
1680 void set_merge_remote_desc(struct commit *commit,
1681 const char *name, struct object *obj)
1683 struct merge_remote_desc *desc;
1684 FLEX_ALLOC_STR(desc, name, name);
1685 desc->obj = obj;
1686 *merge_desc_slab_at(&merge_desc_slab, commit) = desc;
1689 struct commit *get_merge_parent(const char *name)
1691 struct object *obj;
1692 struct commit *commit;
1693 struct object_id oid;
1694 if (get_oid(name, &oid))
1695 return NULL;
1696 obj = parse_object(&oid);
1697 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1698 if (commit && !merge_remote_util(commit))
1699 set_merge_remote_desc(commit, name, obj);
1700 return commit;
1704 * Append a commit to the end of the commit_list.
1706 * next starts by pointing to the variable that holds the head of an
1707 * empty commit_list, and is updated to point to the "next" field of
1708 * the last item on the list as new commits are appended.
1710 * Usage example:
1712 * struct commit_list *list;
1713 * struct commit_list **next = &list;
1715 * next = commit_list_append(c1, next);
1716 * next = commit_list_append(c2, next);
1717 * assert(commit_list_count(list) == 2);
1718 * return list;
1720 struct commit_list **commit_list_append(struct commit *commit,
1721 struct commit_list **next)
1723 struct commit_list *new_commit = xmalloc(sizeof(struct commit_list));
1724 new_commit->item = commit;
1725 *next = new_commit;
1726 new_commit->next = NULL;
1727 return &new_commit->next;
1730 const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1732 int key_len = strlen(key);
1733 const char *line = msg;
1735 while (line) {
1736 const char *eol = strchrnul(line, '\n');
1738 if (line == eol)
1739 return NULL;
1741 if (eol - line > key_len &&
1742 !strncmp(line, key, key_len) &&
1743 line[key_len] == ' ') {
1744 *out_len = eol - line - key_len - 1;
1745 return line + key_len + 1;
1747 line = *eol ? eol + 1 : NULL;
1749 return NULL;
1753 * Inspect the given string and determine the true "end" of the log message, in
1754 * order to find where to put a new Signed-off-by: line. Ignored are
1755 * trailing comment lines and blank lines. To support "git commit -s
1756 * --amend" on an existing commit, we also ignore "Conflicts:". To
1757 * support "git commit -v", we truncate at cut lines.
1759 * Returns the number of bytes from the tail to ignore, to be fed as
1760 * the second parameter to append_signoff().
1762 int ignore_non_trailer(const char *buf, size_t len)
1764 int boc = 0;
1765 int bol = 0;
1766 int in_old_conflicts_block = 0;
1767 size_t cutoff = wt_status_locate_end(buf, len);
1769 while (bol < cutoff) {
1770 const char *next_line = memchr(buf + bol, '\n', len - bol);
1772 if (!next_line)
1773 next_line = buf + len;
1774 else
1775 next_line++;
1777 if (buf[bol] == comment_line_char || buf[bol] == '\n') {
1778 /* is this the first of the run of comments? */
1779 if (!boc)
1780 boc = bol;
1781 /* otherwise, it is just continuing */
1782 } else if (starts_with(buf + bol, "Conflicts:\n")) {
1783 in_old_conflicts_block = 1;
1784 if (!boc)
1785 boc = bol;
1786 } else if (in_old_conflicts_block && buf[bol] == '\t') {
1787 ; /* a pathname in the conflicts block */
1788 } else if (boc) {
1789 /* the previous was not trailing comment */
1790 boc = 0;
1791 in_old_conflicts_block = 0;
1793 bol = next_line - buf;
1795 return boc ? len - boc : len - cutoff;