Merge branch 'jk/fetch-all-peeled-fix'
[git.git] / commit.c
blob0c3b75aeffea75956adf2190e763b0d753c848c5
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_gently(struct commit *item, int quiet_on_missing)
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 (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));
444 ret = parse_commit_buffer(item, buffer, size, 0);
445 if (save_commit_buffer && !ret) {
446 set_commit_buffer(item, buffer, size);
447 return 0;
449 free(buffer);
450 return ret;
453 void parse_commit_or_die(struct commit *item)
455 if (parse_commit(item))
456 die("unable to parse commit %s",
457 item ? oid_to_hex(&item->object.oid) : "(null)");
460 int find_commit_subject(const char *commit_buffer, const char **subject)
462 const char *eol;
463 const char *p = commit_buffer;
465 while (*p && (*p != '\n' || p[1] != '\n'))
466 p++;
467 if (*p) {
468 p = skip_blank_lines(p + 2);
469 eol = strchrnul(p, '\n');
470 } else
471 eol = p;
473 *subject = p;
475 return eol - p;
478 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
480 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
481 new_list->item = item;
482 new_list->next = *list_p;
483 *list_p = new_list;
484 return new_list;
487 unsigned commit_list_count(const struct commit_list *l)
489 unsigned c = 0;
490 for (; l; l = l->next )
491 c++;
492 return c;
495 struct commit_list *copy_commit_list(struct commit_list *list)
497 struct commit_list *head = NULL;
498 struct commit_list **pp = &head;
499 while (list) {
500 pp = commit_list_append(list->item, pp);
501 list = list->next;
503 return head;
506 void free_commit_list(struct commit_list *list)
508 while (list)
509 pop_commit(&list);
512 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
514 struct commit_list **pp = list;
515 struct commit_list *p;
516 while ((p = *pp) != NULL) {
517 if (p->item->date < item->date) {
518 break;
520 pp = &p->next;
522 return commit_list_insert(item, pp);
525 static int commit_list_compare_by_date(const void *a, const void *b)
527 timestamp_t a_date = ((const struct commit_list *)a)->item->date;
528 timestamp_t b_date = ((const struct commit_list *)b)->item->date;
529 if (a_date < b_date)
530 return 1;
531 if (a_date > b_date)
532 return -1;
533 return 0;
536 static void *commit_list_get_next(const void *a)
538 return ((const struct commit_list *)a)->next;
541 static void commit_list_set_next(void *a, void *next)
543 ((struct commit_list *)a)->next = next;
546 void commit_list_sort_by_date(struct commit_list **list)
548 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
549 commit_list_compare_by_date);
552 struct commit *pop_most_recent_commit(struct commit_list **list,
553 unsigned int mark)
555 struct commit *ret = pop_commit(list);
556 struct commit_list *parents = ret->parents;
558 while (parents) {
559 struct commit *commit = parents->item;
560 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
561 commit->object.flags |= mark;
562 commit_list_insert_by_date(commit, list);
564 parents = parents->next;
566 return ret;
569 static void clear_commit_marks_1(struct commit_list **plist,
570 struct commit *commit, unsigned int mark)
572 while (commit) {
573 struct commit_list *parents;
575 if (!(mark & commit->object.flags))
576 return;
578 commit->object.flags &= ~mark;
580 parents = commit->parents;
581 if (!parents)
582 return;
584 while ((parents = parents->next))
585 commit_list_insert(parents->item, plist);
587 commit = commit->parents->item;
591 void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
593 struct commit_list *list = NULL;
595 while (nr--) {
596 clear_commit_marks_1(&list, *commit, mark);
597 commit++;
599 while (list)
600 clear_commit_marks_1(&list, pop_commit(&list), mark);
603 void clear_commit_marks(struct commit *commit, unsigned int mark)
605 clear_commit_marks_many(1, &commit, mark);
608 struct commit *pop_commit(struct commit_list **stack)
610 struct commit_list *top = *stack;
611 struct commit *item = top ? top->item : NULL;
613 if (top) {
614 *stack = top->next;
615 free(top);
617 return item;
621 * Topological sort support
624 /* count number of children that have not been emitted */
625 define_commit_slab(indegree_slab, int);
627 /* record author-date for each commit object */
628 define_commit_slab(author_date_slab, unsigned long);
630 static void record_author_date(struct author_date_slab *author_date,
631 struct commit *commit)
633 const char *buffer = get_commit_buffer(commit, NULL);
634 struct ident_split ident;
635 const char *ident_line;
636 size_t ident_len;
637 char *date_end;
638 timestamp_t date;
640 ident_line = find_commit_header(buffer, "author", &ident_len);
641 if (!ident_line)
642 goto fail_exit; /* no author line */
643 if (split_ident_line(&ident, ident_line, ident_len) ||
644 !ident.date_begin || !ident.date_end)
645 goto fail_exit; /* malformed "author" line */
647 date = parse_timestamp(ident.date_begin, &date_end, 10);
648 if (date_end != ident.date_end)
649 goto fail_exit; /* malformed date */
650 *(author_date_slab_at(author_date, commit)) = date;
652 fail_exit:
653 unuse_commit_buffer(commit, buffer);
656 static int compare_commits_by_author_date(const void *a_, const void *b_,
657 void *cb_data)
659 const struct commit *a = a_, *b = b_;
660 struct author_date_slab *author_date = cb_data;
661 timestamp_t a_date = *(author_date_slab_at(author_date, a));
662 timestamp_t b_date = *(author_date_slab_at(author_date, b));
664 /* newer commits with larger date first */
665 if (a_date < b_date)
666 return 1;
667 else if (a_date > b_date)
668 return -1;
669 return 0;
672 int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused)
674 const struct commit *a = a_, *b = b_;
676 /* newer commits first */
677 if (a->generation < b->generation)
678 return 1;
679 else if (a->generation > b->generation)
680 return -1;
682 /* use date as a heuristic when generations are equal */
683 if (a->date < b->date)
684 return 1;
685 else if (a->date > b->date)
686 return -1;
687 return 0;
690 int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
692 const struct commit *a = a_, *b = b_;
693 /* newer commits with larger date first */
694 if (a->date < b->date)
695 return 1;
696 else if (a->date > b->date)
697 return -1;
698 return 0;
702 * Performs an in-place topological sort on the list supplied.
704 void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
706 struct commit_list *next, *orig = *list;
707 struct commit_list **pptr;
708 struct indegree_slab indegree;
709 struct prio_queue queue;
710 struct commit *commit;
711 struct author_date_slab author_date;
713 if (!orig)
714 return;
715 *list = NULL;
717 init_indegree_slab(&indegree);
718 memset(&queue, '\0', sizeof(queue));
720 switch (sort_order) {
721 default: /* REV_SORT_IN_GRAPH_ORDER */
722 queue.compare = NULL;
723 break;
724 case REV_SORT_BY_COMMIT_DATE:
725 queue.compare = compare_commits_by_commit_date;
726 break;
727 case REV_SORT_BY_AUTHOR_DATE:
728 init_author_date_slab(&author_date);
729 queue.compare = compare_commits_by_author_date;
730 queue.cb_data = &author_date;
731 break;
734 /* Mark them and clear the indegree */
735 for (next = orig; next; next = next->next) {
736 struct commit *commit = next->item;
737 *(indegree_slab_at(&indegree, commit)) = 1;
738 /* also record the author dates, if needed */
739 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
740 record_author_date(&author_date, commit);
743 /* update the indegree */
744 for (next = orig; next; next = next->next) {
745 struct commit_list *parents = next->item->parents;
746 while (parents) {
747 struct commit *parent = parents->item;
748 int *pi = indegree_slab_at(&indegree, parent);
750 if (*pi)
751 (*pi)++;
752 parents = parents->next;
757 * find the tips
759 * tips are nodes not reachable from any other node in the list
761 * the tips serve as a starting set for the work queue.
763 for (next = orig; next; next = next->next) {
764 struct commit *commit = next->item;
766 if (*(indegree_slab_at(&indegree, commit)) == 1)
767 prio_queue_put(&queue, commit);
771 * This is unfortunate; the initial tips need to be shown
772 * in the order given from the revision traversal machinery.
774 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
775 prio_queue_reverse(&queue);
777 /* We no longer need the commit list */
778 free_commit_list(orig);
780 pptr = list;
781 *list = NULL;
782 while ((commit = prio_queue_get(&queue)) != NULL) {
783 struct commit_list *parents;
785 for (parents = commit->parents; parents ; parents = parents->next) {
786 struct commit *parent = parents->item;
787 int *pi = indegree_slab_at(&indegree, parent);
789 if (!*pi)
790 continue;
793 * parents are only enqueued for emission
794 * when all their children have been emitted thereby
795 * guaranteeing topological order.
797 if (--(*pi) == 1)
798 prio_queue_put(&queue, parent);
801 * all children of commit have already been
802 * emitted. we can emit it now.
804 *(indegree_slab_at(&indegree, commit)) = 0;
806 pptr = &commit_list_insert(commit, pptr)->next;
809 clear_indegree_slab(&indegree);
810 clear_prio_queue(&queue);
811 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
812 clear_author_date_slab(&author_date);
815 /* merge-base stuff */
817 /* Remember to update object flag allocation in object.h */
818 #define PARENT1 (1u<<16)
819 #define PARENT2 (1u<<17)
820 #define STALE (1u<<18)
821 #define RESULT (1u<<19)
823 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
825 static int queue_has_nonstale(struct prio_queue *queue)
827 int i;
828 for (i = 0; i < queue->nr; i++) {
829 struct commit *commit = queue->array[i].data;
830 if (!(commit->object.flags & STALE))
831 return 1;
833 return 0;
836 /* all input commits in one and twos[] must have been parsed! */
837 static struct commit_list *paint_down_to_common(struct commit *one, int n,
838 struct commit **twos,
839 int min_generation)
841 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
842 struct commit_list *result = NULL;
843 int i;
844 uint32_t last_gen = GENERATION_NUMBER_INFINITY;
846 one->object.flags |= PARENT1;
847 if (!n) {
848 commit_list_append(one, &result);
849 return result;
851 prio_queue_put(&queue, one);
853 for (i = 0; i < n; i++) {
854 twos[i]->object.flags |= PARENT2;
855 prio_queue_put(&queue, twos[i]);
858 while (queue_has_nonstale(&queue)) {
859 struct commit *commit = prio_queue_get(&queue);
860 struct commit_list *parents;
861 int flags;
863 if (commit->generation > last_gen)
864 BUG("bad generation skip %8x > %8x at %s",
865 commit->generation, last_gen,
866 oid_to_hex(&commit->object.oid));
867 last_gen = commit->generation;
869 if (commit->generation < min_generation)
870 break;
872 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
873 if (flags == (PARENT1 | PARENT2)) {
874 if (!(commit->object.flags & RESULT)) {
875 commit->object.flags |= RESULT;
876 commit_list_insert_by_date(commit, &result);
878 /* Mark parents of a found merge stale */
879 flags |= STALE;
881 parents = commit->parents;
882 while (parents) {
883 struct commit *p = parents->item;
884 parents = parents->next;
885 if ((p->object.flags & flags) == flags)
886 continue;
887 if (parse_commit(p))
888 return NULL;
889 p->object.flags |= flags;
890 prio_queue_put(&queue, p);
894 clear_prio_queue(&queue);
895 return result;
898 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
900 struct commit_list *list = NULL;
901 struct commit_list *result = NULL;
902 int i;
904 for (i = 0; i < n; i++) {
905 if (one == twos[i])
907 * We do not mark this even with RESULT so we do not
908 * have to clean it up.
910 return commit_list_insert(one, &result);
913 if (parse_commit(one))
914 return NULL;
915 for (i = 0; i < n; i++) {
916 if (parse_commit(twos[i]))
917 return NULL;
920 list = paint_down_to_common(one, n, twos, 0);
922 while (list) {
923 struct commit *commit = pop_commit(&list);
924 if (!(commit->object.flags & STALE))
925 commit_list_insert_by_date(commit, &result);
927 return result;
930 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
932 struct commit_list *i, *j, *k, *ret = NULL;
934 if (!in)
935 return ret;
937 commit_list_insert(in->item, &ret);
939 for (i = in->next; i; i = i->next) {
940 struct commit_list *new_commits = NULL, *end = NULL;
942 for (j = ret; j; j = j->next) {
943 struct commit_list *bases;
944 bases = get_merge_bases(i->item, j->item);
945 if (!new_commits)
946 new_commits = bases;
947 else
948 end->next = bases;
949 for (k = bases; k; k = k->next)
950 end = k;
952 ret = new_commits;
954 return ret;
957 static int remove_redundant(struct commit **array, int cnt)
960 * Some commit in the array may be an ancestor of
961 * another commit. Move such commit to the end of
962 * the array, and return the number of commits that
963 * are independent from each other.
965 struct commit **work;
966 unsigned char *redundant;
967 int *filled_index;
968 int i, j, filled;
970 work = xcalloc(cnt, sizeof(*work));
971 redundant = xcalloc(cnt, 1);
972 ALLOC_ARRAY(filled_index, cnt - 1);
974 for (i = 0; i < cnt; i++)
975 parse_commit(array[i]);
976 for (i = 0; i < cnt; i++) {
977 struct commit_list *common;
978 uint32_t min_generation = array[i]->generation;
980 if (redundant[i])
981 continue;
982 for (j = filled = 0; j < cnt; j++) {
983 if (i == j || redundant[j])
984 continue;
985 filled_index[filled] = j;
986 work[filled++] = array[j];
988 if (array[j]->generation < min_generation)
989 min_generation = array[j]->generation;
991 common = paint_down_to_common(array[i], filled, work,
992 min_generation);
993 if (array[i]->object.flags & PARENT2)
994 redundant[i] = 1;
995 for (j = 0; j < filled; j++)
996 if (work[j]->object.flags & PARENT1)
997 redundant[filled_index[j]] = 1;
998 clear_commit_marks(array[i], all_flags);
999 clear_commit_marks_many(filled, work, all_flags);
1000 free_commit_list(common);
1003 /* Now collect the result */
1004 COPY_ARRAY(work, array, cnt);
1005 for (i = filled = 0; i < cnt; i++)
1006 if (!redundant[i])
1007 array[filled++] = work[i];
1008 for (j = filled, i = 0; i < cnt; i++)
1009 if (redundant[i])
1010 array[j++] = work[i];
1011 free(work);
1012 free(redundant);
1013 free(filled_index);
1014 return filled;
1017 static struct commit_list *get_merge_bases_many_0(struct commit *one,
1018 int n,
1019 struct commit **twos,
1020 int cleanup)
1022 struct commit_list *list;
1023 struct commit **rslt;
1024 struct commit_list *result;
1025 int cnt, i;
1027 result = merge_bases_many(one, n, twos);
1028 for (i = 0; i < n; i++) {
1029 if (one == twos[i])
1030 return result;
1032 if (!result || !result->next) {
1033 if (cleanup) {
1034 clear_commit_marks(one, all_flags);
1035 clear_commit_marks_many(n, twos, all_flags);
1037 return result;
1040 /* There are more than one */
1041 cnt = commit_list_count(result);
1042 rslt = xcalloc(cnt, sizeof(*rslt));
1043 for (list = result, i = 0; list; list = list->next)
1044 rslt[i++] = list->item;
1045 free_commit_list(result);
1047 clear_commit_marks(one, all_flags);
1048 clear_commit_marks_many(n, twos, all_flags);
1050 cnt = remove_redundant(rslt, cnt);
1051 result = NULL;
1052 for (i = 0; i < cnt; i++)
1053 commit_list_insert_by_date(rslt[i], &result);
1054 free(rslt);
1055 return result;
1058 struct commit_list *get_merge_bases_many(struct commit *one,
1059 int n,
1060 struct commit **twos)
1062 return get_merge_bases_many_0(one, n, twos, 1);
1065 struct commit_list *get_merge_bases_many_dirty(struct commit *one,
1066 int n,
1067 struct commit **twos)
1069 return get_merge_bases_many_0(one, n, twos, 0);
1072 struct commit_list *get_merge_bases(struct commit *one, struct commit *two)
1074 return get_merge_bases_many_0(one, 1, &two, 1);
1078 * Is "commit" a descendant of one of the elements on the "with_commit" list?
1080 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
1082 if (!with_commit)
1083 return 1;
1084 while (with_commit) {
1085 struct commit *other;
1087 other = with_commit->item;
1088 with_commit = with_commit->next;
1089 if (in_merge_bases(other, commit))
1090 return 1;
1092 return 0;
1096 * Is "commit" an ancestor of one of the "references"?
1098 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
1100 struct commit_list *bases;
1101 int ret = 0, i;
1102 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
1104 if (parse_commit(commit))
1105 return ret;
1106 for (i = 0; i < nr_reference; i++) {
1107 if (parse_commit(reference[i]))
1108 return ret;
1109 if (reference[i]->generation < min_generation)
1110 min_generation = reference[i]->generation;
1113 if (commit->generation > min_generation)
1114 return ret;
1116 bases = paint_down_to_common(commit, nr_reference, reference, commit->generation);
1117 if (commit->object.flags & PARENT2)
1118 ret = 1;
1119 clear_commit_marks(commit, all_flags);
1120 clear_commit_marks_many(nr_reference, reference, all_flags);
1121 free_commit_list(bases);
1122 return ret;
1126 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1128 int in_merge_bases(struct commit *commit, struct commit *reference)
1130 return in_merge_bases_many(commit, 1, &reference);
1133 struct commit_list *reduce_heads(struct commit_list *heads)
1135 struct commit_list *p;
1136 struct commit_list *result = NULL, **tail = &result;
1137 struct commit **array;
1138 int num_head, i;
1140 if (!heads)
1141 return NULL;
1143 /* Uniquify */
1144 for (p = heads; p; p = p->next)
1145 p->item->object.flags &= ~STALE;
1146 for (p = heads, num_head = 0; p; p = p->next) {
1147 if (p->item->object.flags & STALE)
1148 continue;
1149 p->item->object.flags |= STALE;
1150 num_head++;
1152 array = xcalloc(num_head, sizeof(*array));
1153 for (p = heads, i = 0; p; p = p->next) {
1154 if (p->item->object.flags & STALE) {
1155 array[i++] = p->item;
1156 p->item->object.flags &= ~STALE;
1159 num_head = remove_redundant(array, num_head);
1160 for (i = 0; i < num_head; i++)
1161 tail = &commit_list_insert(array[i], tail)->next;
1162 free(array);
1163 return result;
1166 void reduce_heads_replace(struct commit_list **heads)
1168 struct commit_list *result = reduce_heads(*heads);
1169 free_commit_list(*heads);
1170 *heads = result;
1173 static const char gpg_sig_header[] = "gpgsig";
1174 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
1176 static int do_sign_commit(struct strbuf *buf, const char *keyid)
1178 struct strbuf sig = STRBUF_INIT;
1179 int inspos, copypos;
1180 const char *eoh;
1182 /* find the end of the header */
1183 eoh = strstr(buf->buf, "\n\n");
1184 if (!eoh)
1185 inspos = buf->len;
1186 else
1187 inspos = eoh - buf->buf + 1;
1189 if (!keyid || !*keyid)
1190 keyid = get_signing_key();
1191 if (sign_buffer(buf, &sig, keyid)) {
1192 strbuf_release(&sig);
1193 return -1;
1196 for (copypos = 0; sig.buf[copypos]; ) {
1197 const char *bol = sig.buf + copypos;
1198 const char *eol = strchrnul(bol, '\n');
1199 int len = (eol - bol) + !!*eol;
1201 if (!copypos) {
1202 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1203 inspos += gpg_sig_header_len;
1205 strbuf_insert(buf, inspos++, " ", 1);
1206 strbuf_insert(buf, inspos, bol, len);
1207 inspos += len;
1208 copypos += len;
1210 strbuf_release(&sig);
1211 return 0;
1214 int parse_signed_commit(const struct commit *commit,
1215 struct strbuf *payload, struct strbuf *signature)
1218 unsigned long size;
1219 const char *buffer = get_commit_buffer(commit, &size);
1220 int in_signature, saw_signature = -1;
1221 const char *line, *tail;
1223 line = buffer;
1224 tail = buffer + size;
1225 in_signature = 0;
1226 saw_signature = 0;
1227 while (line < tail) {
1228 const char *sig = NULL;
1229 const char *next = memchr(line, '\n', tail - line);
1231 next = next ? next + 1 : tail;
1232 if (in_signature && line[0] == ' ')
1233 sig = line + 1;
1234 else if (starts_with(line, gpg_sig_header) &&
1235 line[gpg_sig_header_len] == ' ')
1236 sig = line + gpg_sig_header_len + 1;
1237 if (sig) {
1238 strbuf_add(signature, sig, next - sig);
1239 saw_signature = 1;
1240 in_signature = 1;
1241 } else {
1242 if (*line == '\n')
1243 /* dump the whole remainder of the buffer */
1244 next = tail;
1245 strbuf_add(payload, line, next - line);
1246 in_signature = 0;
1248 line = next;
1250 unuse_commit_buffer(commit, buffer);
1251 return saw_signature;
1254 int remove_signature(struct strbuf *buf)
1256 const char *line = buf->buf;
1257 const char *tail = buf->buf + buf->len;
1258 int in_signature = 0;
1259 const char *sig_start = NULL;
1260 const char *sig_end = NULL;
1262 while (line < tail) {
1263 const char *next = memchr(line, '\n', tail - line);
1264 next = next ? next + 1 : tail;
1266 if (in_signature && line[0] == ' ')
1267 sig_end = next;
1268 else if (starts_with(line, gpg_sig_header) &&
1269 line[gpg_sig_header_len] == ' ') {
1270 sig_start = line;
1271 sig_end = next;
1272 in_signature = 1;
1273 } else {
1274 if (*line == '\n')
1275 /* dump the whole remainder of the buffer */
1276 next = tail;
1277 in_signature = 0;
1279 line = next;
1282 if (sig_start)
1283 strbuf_remove(buf, sig_start - buf->buf, sig_end - sig_start);
1285 return sig_start != NULL;
1288 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1290 struct merge_remote_desc *desc;
1291 struct commit_extra_header *mergetag;
1292 char *buf;
1293 unsigned long size, len;
1294 enum object_type type;
1296 desc = merge_remote_util(parent);
1297 if (!desc || !desc->obj)
1298 return;
1299 buf = read_object_file(&desc->obj->oid, &type, &size);
1300 if (!buf || type != OBJ_TAG)
1301 goto free_return;
1302 len = parse_signature(buf, size);
1303 if (size == len)
1304 goto free_return;
1306 * We could verify this signature and either omit the tag when
1307 * it does not validate, but the integrator may not have the
1308 * public key of the signer of the tag he is merging, while a
1309 * later auditor may have it while auditing, so let's not run
1310 * verify-signed-buffer here for now...
1312 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1313 * warn("warning: signed tag unverified.");
1315 mergetag = xcalloc(1, sizeof(*mergetag));
1316 mergetag->key = xstrdup("mergetag");
1317 mergetag->value = buf;
1318 mergetag->len = size;
1320 **tail = mergetag;
1321 *tail = &mergetag->next;
1322 return;
1324 free_return:
1325 free(buf);
1328 int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
1330 struct strbuf payload = STRBUF_INIT;
1331 struct strbuf signature = STRBUF_INIT;
1332 int ret = 1;
1334 sigc->result = 'N';
1336 if (parse_signed_commit(commit, &payload, &signature) <= 0)
1337 goto out;
1338 ret = check_signature(payload.buf, payload.len, signature.buf,
1339 signature.len, sigc);
1341 out:
1342 strbuf_release(&payload);
1343 strbuf_release(&signature);
1345 return ret;
1350 void append_merge_tag_headers(struct commit_list *parents,
1351 struct commit_extra_header ***tail)
1353 while (parents) {
1354 struct commit *parent = parents->item;
1355 handle_signed_tag(parent, tail);
1356 parents = parents->next;
1360 static void add_extra_header(struct strbuf *buffer,
1361 struct commit_extra_header *extra)
1363 strbuf_addstr(buffer, extra->key);
1364 if (extra->len)
1365 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1366 else
1367 strbuf_addch(buffer, '\n');
1370 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1371 const char **exclude)
1373 struct commit_extra_header *extra = NULL;
1374 unsigned long size;
1375 const char *buffer = get_commit_buffer(commit, &size);
1376 extra = read_commit_extra_header_lines(buffer, size, exclude);
1377 unuse_commit_buffer(commit, buffer);
1378 return extra;
1381 int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
1383 struct commit_extra_header *extra, *to_free;
1384 int res = 0;
1386 to_free = read_commit_extra_headers(commit, NULL);
1387 for (extra = to_free; !res && extra; extra = extra->next) {
1388 if (strcmp(extra->key, "mergetag"))
1389 continue; /* not a merge tag */
1390 res = fn(commit, extra, data);
1392 free_commit_extra_headers(to_free);
1393 return res;
1396 static inline int standard_header_field(const char *field, size_t len)
1398 return ((len == 4 && !memcmp(field, "tree", 4)) ||
1399 (len == 6 && !memcmp(field, "parent", 6)) ||
1400 (len == 6 && !memcmp(field, "author", 6)) ||
1401 (len == 9 && !memcmp(field, "committer", 9)) ||
1402 (len == 8 && !memcmp(field, "encoding", 8)));
1405 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1407 if (!exclude)
1408 return 0;
1410 while (*exclude) {
1411 size_t xlen = strlen(*exclude);
1412 if (len == xlen && !memcmp(field, *exclude, xlen))
1413 return 1;
1414 exclude++;
1416 return 0;
1419 static struct commit_extra_header *read_commit_extra_header_lines(
1420 const char *buffer, size_t size,
1421 const char **exclude)
1423 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1424 const char *line, *next, *eof, *eob;
1425 struct strbuf buf = STRBUF_INIT;
1427 for (line = buffer, eob = line + size;
1428 line < eob && *line != '\n';
1429 line = next) {
1430 next = memchr(line, '\n', eob - line);
1431 next = next ? next + 1 : eob;
1432 if (*line == ' ') {
1433 /* continuation */
1434 if (it)
1435 strbuf_add(&buf, line + 1, next - (line + 1));
1436 continue;
1438 if (it)
1439 it->value = strbuf_detach(&buf, &it->len);
1440 strbuf_reset(&buf);
1441 it = NULL;
1443 eof = memchr(line, ' ', next - line);
1444 if (!eof)
1445 eof = next;
1446 else if (standard_header_field(line, eof - line) ||
1447 excluded_header_field(line, eof - line, exclude))
1448 continue;
1450 it = xcalloc(1, sizeof(*it));
1451 it->key = xmemdupz(line, eof-line);
1452 *tail = it;
1453 tail = &it->next;
1454 if (eof + 1 < next)
1455 strbuf_add(&buf, eof + 1, next - (eof + 1));
1457 if (it)
1458 it->value = strbuf_detach(&buf, &it->len);
1459 return extra;
1462 void free_commit_extra_headers(struct commit_extra_header *extra)
1464 while (extra) {
1465 struct commit_extra_header *next = extra->next;
1466 free(extra->key);
1467 free(extra->value);
1468 free(extra);
1469 extra = next;
1473 int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1474 struct commit_list *parents, struct object_id *ret,
1475 const char *author, const char *sign_commit)
1477 struct commit_extra_header *extra = NULL, **tail = &extra;
1478 int result;
1480 append_merge_tag_headers(parents, &tail);
1481 result = commit_tree_extended(msg, msg_len, tree, parents, ret,
1482 author, sign_commit, extra);
1483 free_commit_extra_headers(extra);
1484 return result;
1487 static int find_invalid_utf8(const char *buf, int len)
1489 int offset = 0;
1490 static const unsigned int max_codepoint[] = {
1491 0x7f, 0x7ff, 0xffff, 0x10ffff
1494 while (len) {
1495 unsigned char c = *buf++;
1496 int bytes, bad_offset;
1497 unsigned int codepoint;
1498 unsigned int min_val, max_val;
1500 len--;
1501 offset++;
1503 /* Simple US-ASCII? No worries. */
1504 if (c < 0x80)
1505 continue;
1507 bad_offset = offset-1;
1510 * Count how many more high bits set: that's how
1511 * many more bytes this sequence should have.
1513 bytes = 0;
1514 while (c & 0x40) {
1515 c <<= 1;
1516 bytes++;
1520 * Must be between 1 and 3 more bytes. Longer sequences result in
1521 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1523 if (bytes < 1 || 3 < bytes)
1524 return bad_offset;
1526 /* Do we *have* that many bytes? */
1527 if (len < bytes)
1528 return bad_offset;
1531 * Place the encoded bits at the bottom of the value and compute the
1532 * valid range.
1534 codepoint = (c & 0x7f) >> bytes;
1535 min_val = max_codepoint[bytes-1] + 1;
1536 max_val = max_codepoint[bytes];
1538 offset += bytes;
1539 len -= bytes;
1541 /* And verify that they are good continuation bytes */
1542 do {
1543 codepoint <<= 6;
1544 codepoint |= *buf & 0x3f;
1545 if ((*buf++ & 0xc0) != 0x80)
1546 return bad_offset;
1547 } while (--bytes);
1549 /* Reject codepoints that are out of range for the sequence length. */
1550 if (codepoint < min_val || codepoint > max_val)
1551 return bad_offset;
1552 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1553 if ((codepoint & 0x1ff800) == 0xd800)
1554 return bad_offset;
1555 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1556 if ((codepoint & 0xfffe) == 0xfffe)
1557 return bad_offset;
1558 /* So are anything in the range U+FDD0..U+FDEF. */
1559 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
1560 return bad_offset;
1562 return -1;
1566 * This verifies that the buffer is in proper utf8 format.
1568 * If it isn't, it assumes any non-utf8 characters are Latin1,
1569 * and does the conversion.
1571 static int verify_utf8(struct strbuf *buf)
1573 int ok = 1;
1574 long pos = 0;
1576 for (;;) {
1577 int bad;
1578 unsigned char c;
1579 unsigned char replace[2];
1581 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1582 if (bad < 0)
1583 return ok;
1584 pos += bad;
1585 ok = 0;
1586 c = buf->buf[pos];
1587 strbuf_remove(buf, pos, 1);
1589 /* We know 'c' must be in the range 128-255 */
1590 replace[0] = 0xc0 + (c >> 6);
1591 replace[1] = 0x80 + (c & 0x3f);
1592 strbuf_insert(buf, pos, replace, 2);
1593 pos += 2;
1597 static const char commit_utf8_warn[] =
1598 N_("Warning: commit message did not conform to UTF-8.\n"
1599 "You may want to amend it after fixing the message, or set the config\n"
1600 "variable i18n.commitencoding to the encoding your project uses.\n");
1602 int commit_tree_extended(const char *msg, size_t msg_len,
1603 const struct object_id *tree,
1604 struct commit_list *parents, struct object_id *ret,
1605 const char *author, const char *sign_commit,
1606 struct commit_extra_header *extra)
1608 int result;
1609 int encoding_is_utf8;
1610 struct strbuf buffer;
1612 assert_oid_type(tree, OBJ_TREE);
1614 if (memchr(msg, '\0', msg_len))
1615 return error("a NUL byte in commit log message not allowed.");
1617 /* Not having i18n.commitencoding is the same as having utf-8 */
1618 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1620 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1621 strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree));
1624 * NOTE! This ordering means that the same exact tree merged with a
1625 * different order of parents will be a _different_ changeset even
1626 * if everything else stays the same.
1628 while (parents) {
1629 struct commit *parent = pop_commit(&parents);
1630 strbuf_addf(&buffer, "parent %s\n",
1631 oid_to_hex(&parent->object.oid));
1634 /* Person/date information */
1635 if (!author)
1636 author = git_author_info(IDENT_STRICT);
1637 strbuf_addf(&buffer, "author %s\n", author);
1638 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1639 if (!encoding_is_utf8)
1640 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1642 while (extra) {
1643 add_extra_header(&buffer, extra);
1644 extra = extra->next;
1646 strbuf_addch(&buffer, '\n');
1648 /* And add the comment */
1649 strbuf_add(&buffer, msg, msg_len);
1651 /* And check the encoding */
1652 if (encoding_is_utf8 && !verify_utf8(&buffer))
1653 fprintf(stderr, _(commit_utf8_warn));
1655 if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
1656 result = -1;
1657 goto out;
1660 result = write_object_file(buffer.buf, buffer.len, commit_type, ret);
1661 out:
1662 strbuf_release(&buffer);
1663 return result;
1666 define_commit_slab(merge_desc_slab, struct merge_remote_desc *);
1667 static struct merge_desc_slab merge_desc_slab = COMMIT_SLAB_INIT(1, merge_desc_slab);
1669 struct merge_remote_desc *merge_remote_util(struct commit *commit)
1671 return *merge_desc_slab_at(&merge_desc_slab, commit);
1674 void set_merge_remote_desc(struct commit *commit,
1675 const char *name, struct object *obj)
1677 struct merge_remote_desc *desc;
1678 FLEX_ALLOC_STR(desc, name, name);
1679 desc->obj = obj;
1680 *merge_desc_slab_at(&merge_desc_slab, commit) = desc;
1683 struct commit *get_merge_parent(const char *name)
1685 struct object *obj;
1686 struct commit *commit;
1687 struct object_id oid;
1688 if (get_oid(name, &oid))
1689 return NULL;
1690 obj = parse_object(&oid);
1691 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1692 if (commit && !merge_remote_util(commit))
1693 set_merge_remote_desc(commit, name, obj);
1694 return commit;
1698 * Append a commit to the end of the commit_list.
1700 * next starts by pointing to the variable that holds the head of an
1701 * empty commit_list, and is updated to point to the "next" field of
1702 * the last item on the list as new commits are appended.
1704 * Usage example:
1706 * struct commit_list *list;
1707 * struct commit_list **next = &list;
1709 * next = commit_list_append(c1, next);
1710 * next = commit_list_append(c2, next);
1711 * assert(commit_list_count(list) == 2);
1712 * return list;
1714 struct commit_list **commit_list_append(struct commit *commit,
1715 struct commit_list **next)
1717 struct commit_list *new_commit = xmalloc(sizeof(struct commit_list));
1718 new_commit->item = commit;
1719 *next = new_commit;
1720 new_commit->next = NULL;
1721 return &new_commit->next;
1724 const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1726 int key_len = strlen(key);
1727 const char *line = msg;
1729 while (line) {
1730 const char *eol = strchrnul(line, '\n');
1732 if (line == eol)
1733 return NULL;
1735 if (eol - line > key_len &&
1736 !strncmp(line, key, key_len) &&
1737 line[key_len] == ' ') {
1738 *out_len = eol - line - key_len - 1;
1739 return line + key_len + 1;
1741 line = *eol ? eol + 1 : NULL;
1743 return NULL;
1747 * Inspect the given string and determine the true "end" of the log message, in
1748 * order to find where to put a new Signed-off-by: line. Ignored are
1749 * trailing comment lines and blank lines. To support "git commit -s
1750 * --amend" on an existing commit, we also ignore "Conflicts:". To
1751 * support "git commit -v", we truncate at cut lines.
1753 * Returns the number of bytes from the tail to ignore, to be fed as
1754 * the second parameter to append_signoff().
1756 int ignore_non_trailer(const char *buf, size_t len)
1758 int boc = 0;
1759 int bol = 0;
1760 int in_old_conflicts_block = 0;
1761 size_t cutoff = wt_status_locate_end(buf, len);
1763 while (bol < cutoff) {
1764 const char *next_line = memchr(buf + bol, '\n', len - bol);
1766 if (!next_line)
1767 next_line = buf + len;
1768 else
1769 next_line++;
1771 if (buf[bol] == comment_line_char || buf[bol] == '\n') {
1772 /* is this the first of the run of comments? */
1773 if (!boc)
1774 boc = bol;
1775 /* otherwise, it is just continuing */
1776 } else if (starts_with(buf + bol, "Conflicts:\n")) {
1777 in_old_conflicts_block = 1;
1778 if (!boc)
1779 boc = bol;
1780 } else if (in_old_conflicts_block && buf[bol] == '\t') {
1781 ; /* a pathname in the conflicts block */
1782 } else if (boc) {
1783 /* the previous was not trailing comment */
1784 boc = 0;
1785 in_old_conflicts_block = 0;
1787 bol = next_line - buf;
1789 return boc ? len - boc : len - cutoff;