]> git.ipfire.org Git - thirdparty/git.git/blob - commit.c
e915b2b9a1295c88e64d903e9352c3e85ff3c6d3
[thirdparty/git.git] / commit.c
1 #define USE_THE_REPOSITORY_VARIABLE
2
3 #include "git-compat-util.h"
4 #include "tag.h"
5 #include "commit.h"
6 #include "commit-graph.h"
7 #include "environment.h"
8 #include "gettext.h"
9 #include "hex.h"
10 #include "repository.h"
11 #include "object-name.h"
12 #include "object-store.h"
13 #include "utf8.h"
14 #include "diff.h"
15 #include "revision.h"
16 #include "notes.h"
17 #include "alloc.h"
18 #include "gpg-interface.h"
19 #include "mergesort.h"
20 #include "commit-slab.h"
21 #include "prio-queue.h"
22 #include "hash-lookup.h"
23 #include "wt-status.h"
24 #include "advice.h"
25 #include "refs.h"
26 #include "commit-reach.h"
27 #include "setup.h"
28 #include "shallow.h"
29 #include "tree.h"
30 #include "hook.h"
31 #include "parse.h"
32 #include "object-file.h"
33 #include "object-file-convert.h"
34
35 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
36
37 int save_commit_buffer = 1;
38 int no_graft_file_deprecated_advice;
39
40 const char *commit_type = "commit";
41
42 struct commit *lookup_commit_reference_gently(struct repository *r,
43 const struct object_id *oid, int quiet)
44 {
45 struct object *obj = deref_tag(r,
46 parse_object(r, oid),
47 NULL, 0);
48
49 if (!obj)
50 return NULL;
51 return object_as_type(obj, OBJ_COMMIT, quiet);
52 }
53
54 struct commit *lookup_commit_reference(struct repository *r, const struct object_id *oid)
55 {
56 return lookup_commit_reference_gently(r, oid, 0);
57 }
58
59 struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name)
60 {
61 struct commit *c = lookup_commit_reference(the_repository, oid);
62 if (!c)
63 die(_("could not parse %s"), ref_name);
64 if (!oideq(oid, &c->object.oid)) {
65 warning(_("%s %s is not a commit!"),
66 ref_name, oid_to_hex(oid));
67 }
68 return c;
69 }
70
71 struct commit *lookup_commit_object(struct repository *r,
72 const struct object_id *oid)
73 {
74 struct object *obj = parse_object(r, oid);
75 return obj ? object_as_type(obj, OBJ_COMMIT, 0) : NULL;
76
77 }
78
79 struct commit *lookup_commit(struct repository *r, const struct object_id *oid)
80 {
81 struct object *obj = lookup_object(r, oid);
82 if (!obj)
83 return create_object(r, oid, alloc_commit_node(r));
84 return object_as_type(obj, OBJ_COMMIT, 0);
85 }
86
87 struct commit *lookup_commit_reference_by_name(const char *name)
88 {
89 return lookup_commit_reference_by_name_gently(name, 0);
90 }
91
92 struct commit *lookup_commit_reference_by_name_gently(const char *name,
93 int quiet)
94 {
95 struct object_id oid;
96 struct commit *commit;
97
98 if (repo_get_oid_committish(the_repository, name, &oid))
99 return NULL;
100 commit = lookup_commit_reference_gently(the_repository, &oid, quiet);
101 if (repo_parse_commit(the_repository, commit))
102 return NULL;
103 return commit;
104 }
105
106 static timestamp_t parse_commit_date(const char *buf, const char *tail)
107 {
108 const char *dateptr;
109 const char *eol;
110
111 if (buf + 6 >= tail)
112 return 0;
113 if (memcmp(buf, "author", 6))
114 return 0;
115 while (buf < tail && *buf++ != '\n')
116 /* nada */;
117 if (buf + 9 >= tail)
118 return 0;
119 if (memcmp(buf, "committer", 9))
120 return 0;
121
122 /*
123 * Jump to end-of-line so that we can walk backwards to find the
124 * end-of-email ">". This is more forgiving of malformed cases
125 * because unexpected characters tend to be in the name and email
126 * fields.
127 */
128 eol = memchr(buf, '\n', tail - buf);
129 if (!eol)
130 return 0;
131 dateptr = eol;
132 while (dateptr > buf && dateptr[-1] != '>')
133 dateptr--;
134 if (dateptr == buf)
135 return 0;
136
137 /*
138 * Trim leading whitespace, but make sure we have at least one
139 * non-whitespace character, as parse_timestamp() will otherwise walk
140 * right past the newline we found in "eol" when skipping whitespace
141 * itself.
142 *
143 * In theory it would be sufficient to allow any character not matched
144 * by isspace(), but there's a catch: our isspace() does not
145 * necessarily match the behavior of parse_timestamp(), as the latter
146 * is implemented by system routines which match more exotic control
147 * codes, or even locale-dependent sequences.
148 *
149 * Since we expect the timestamp to be a number, we can check for that.
150 * Anything else (e.g., a non-numeric token like "foo") would just
151 * cause parse_timestamp() to return 0 anyway.
152 */
153 while (dateptr < eol && isspace(*dateptr))
154 dateptr++;
155 if (!isdigit(*dateptr) && *dateptr != '-')
156 return 0;
157
158 /*
159 * We know there is at least one digit (or dash), so we'll begin
160 * parsing there and stop at worst case at eol.
161 *
162 * Note that we may feed parse_timestamp() extra characters here if the
163 * commit is malformed, and it will parse as far as it can. For
164 * example, "123foo456" would return "123". That might be questionable
165 * (versus returning "0"), but it would help in a hypothetical case
166 * like "123456+0100", where the whitespace from the timezone is
167 * missing. Since such syntactic errors may be baked into history and
168 * hard to correct now, let's err on trying to make our best guess
169 * here, rather than insist on perfect syntax.
170 */
171 return parse_timestamp(dateptr, NULL, 10);
172 }
173
174 static const struct object_id *commit_graft_oid_access(size_t index, const void *table)
175 {
176 const struct commit_graft * const *commit_graft_table = table;
177 return &commit_graft_table[index]->oid;
178 }
179
180 int commit_graft_pos(struct repository *r, const struct object_id *oid)
181 {
182 return oid_pos(oid, r->parsed_objects->grafts,
183 r->parsed_objects->grafts_nr,
184 commit_graft_oid_access);
185 }
186
187 void unparse_commit(struct repository *r, const struct object_id *oid)
188 {
189 struct commit *c = lookup_commit(r, oid);
190
191 if (!c->object.parsed)
192 return;
193 free_commit_list(c->parents);
194 c->parents = NULL;
195 c->object.parsed = 0;
196 }
197
198 int register_commit_graft(struct repository *r, struct commit_graft *graft,
199 int ignore_dups)
200 {
201 int pos = commit_graft_pos(r, &graft->oid);
202
203 if (0 <= pos) {
204 if (ignore_dups)
205 free(graft);
206 else {
207 free(r->parsed_objects->grafts[pos]);
208 r->parsed_objects->grafts[pos] = graft;
209 }
210 return 1;
211 }
212 pos = -pos - 1;
213 ALLOC_GROW(r->parsed_objects->grafts,
214 r->parsed_objects->grafts_nr + 1,
215 r->parsed_objects->grafts_alloc);
216 r->parsed_objects->grafts_nr++;
217 if (pos < r->parsed_objects->grafts_nr)
218 memmove(r->parsed_objects->grafts + pos + 1,
219 r->parsed_objects->grafts + pos,
220 (r->parsed_objects->grafts_nr - pos - 1) *
221 sizeof(*r->parsed_objects->grafts));
222 r->parsed_objects->grafts[pos] = graft;
223 unparse_commit(r, &graft->oid);
224 return 0;
225 }
226
227 struct commit_graft *read_graft_line(struct strbuf *line)
228 {
229 /* The format is just "Commit Parent1 Parent2 ...\n" */
230 int i, phase;
231 const char *tail = NULL;
232 struct commit_graft *graft = NULL;
233 struct object_id dummy_oid, *oid;
234
235 strbuf_rtrim(line);
236 if (!line->len || line->buf[0] == '#')
237 return NULL;
238 /*
239 * phase 0 verifies line, counts hashes in line and allocates graft
240 * phase 1 fills graft
241 */
242 for (phase = 0; phase < 2; phase++) {
243 oid = graft ? &graft->oid : &dummy_oid;
244 if (parse_oid_hex(line->buf, oid, &tail))
245 goto bad_graft_data;
246 for (i = 0; *tail != '\0'; i++) {
247 oid = graft ? &graft->parent[i] : &dummy_oid;
248 if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
249 goto bad_graft_data;
250 }
251 if (!graft) {
252 graft = xmalloc(st_add(sizeof(*graft),
253 st_mult(sizeof(struct object_id), i)));
254 graft->nr_parent = i;
255 }
256 }
257 return graft;
258
259 bad_graft_data:
260 error("bad graft data: %s", line->buf);
261 assert(!graft);
262 return NULL;
263 }
264
265 static int read_graft_file(struct repository *r, const char *graft_file)
266 {
267 FILE *fp = fopen_or_warn(graft_file, "r");
268 struct strbuf buf = STRBUF_INIT;
269 if (!fp)
270 return -1;
271 if (!no_graft_file_deprecated_advice &&
272 advice_enabled(ADVICE_GRAFT_FILE_DEPRECATED))
273 advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
274 "and will be removed in a future Git version.\n"
275 "\n"
276 "Please use \"git replace --convert-graft-file\"\n"
277 "to convert the grafts into replace refs.\n"
278 "\n"
279 "Turn this message off by running\n"
280 "\"git config set advice.graftFileDeprecated false\""));
281 while (!strbuf_getwholeline(&buf, fp, '\n')) {
282 /* The format is just "Commit Parent1 Parent2 ...\n" */
283 struct commit_graft *graft = read_graft_line(&buf);
284 if (!graft)
285 continue;
286 if (register_commit_graft(r, graft, 1))
287 error("duplicate graft data: %s", buf.buf);
288 }
289 fclose(fp);
290 strbuf_release(&buf);
291 return 0;
292 }
293
294 void prepare_commit_graft(struct repository *r)
295 {
296 const char *graft_file;
297
298 if (r->parsed_objects->commit_graft_prepared)
299 return;
300 if (!startup_info->have_repository)
301 return;
302
303 graft_file = repo_get_graft_file(r);
304 read_graft_file(r, graft_file);
305 /* make sure shallows are read */
306 is_repository_shallow(r);
307 r->parsed_objects->commit_graft_prepared = 1;
308 }
309
310 struct commit_graft *lookup_commit_graft(struct repository *r, const struct object_id *oid)
311 {
312 int pos;
313 prepare_commit_graft(r);
314 pos = commit_graft_pos(r, oid);
315 if (pos < 0)
316 return NULL;
317 return r->parsed_objects->grafts[pos];
318 }
319
320 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
321 {
322 int i, ret;
323 for (i = ret = 0; i < the_repository->parsed_objects->grafts_nr && !ret; i++)
324 ret = fn(the_repository->parsed_objects->grafts[i], cb_data);
325 return ret;
326 }
327
328 struct commit_buffer {
329 void *buffer;
330 unsigned long size;
331 };
332 define_commit_slab(buffer_slab, struct commit_buffer);
333
334 struct buffer_slab *allocate_commit_buffer_slab(void)
335 {
336 struct buffer_slab *bs = xmalloc(sizeof(*bs));
337 init_buffer_slab(bs);
338 return bs;
339 }
340
341 void free_commit_buffer_slab(struct buffer_slab *bs)
342 {
343 clear_buffer_slab(bs);
344 free(bs);
345 }
346
347 void set_commit_buffer(struct repository *r, struct commit *commit, void *buffer, unsigned long size)
348 {
349 struct commit_buffer *v = buffer_slab_at(
350 r->parsed_objects->buffer_slab, commit);
351 v->buffer = buffer;
352 v->size = size;
353 }
354
355 const void *get_cached_commit_buffer(struct repository *r, const struct commit *commit, unsigned long *sizep)
356 {
357 struct commit_buffer *v = buffer_slab_peek(
358 r->parsed_objects->buffer_slab, commit);
359 if (!v) {
360 if (sizep)
361 *sizep = 0;
362 return NULL;
363 }
364 if (sizep)
365 *sizep = v->size;
366 return v->buffer;
367 }
368
369 const void *repo_get_commit_buffer(struct repository *r,
370 const struct commit *commit,
371 unsigned long *sizep)
372 {
373 const void *ret = get_cached_commit_buffer(r, commit, sizep);
374 if (!ret) {
375 enum object_type type;
376 unsigned long size;
377 ret = repo_read_object_file(r, &commit->object.oid, &type, &size);
378 if (!ret)
379 die("cannot read commit object %s",
380 oid_to_hex(&commit->object.oid));
381 if (type != OBJ_COMMIT)
382 die("expected commit for %s, got %s",
383 oid_to_hex(&commit->object.oid), type_name(type));
384 if (sizep)
385 *sizep = size;
386 }
387 return ret;
388 }
389
390 void repo_unuse_commit_buffer(struct repository *r,
391 const struct commit *commit,
392 const void *buffer)
393 {
394 struct commit_buffer *v = buffer_slab_peek(
395 r->parsed_objects->buffer_slab, commit);
396 if (!(v && v->buffer == buffer))
397 free((void *)buffer);
398 }
399
400 void free_commit_buffer(struct parsed_object_pool *pool, struct commit *commit)
401 {
402 struct commit_buffer *v = buffer_slab_peek(
403 pool->buffer_slab, commit);
404 if (v) {
405 FREE_AND_NULL(v->buffer);
406 v->size = 0;
407 }
408 }
409
410 static inline void set_commit_tree(struct commit *c, struct tree *t)
411 {
412 c->maybe_tree = t;
413 }
414
415 struct tree *repo_get_commit_tree(struct repository *r,
416 const struct commit *commit)
417 {
418 if (commit->maybe_tree || !commit->object.parsed)
419 return commit->maybe_tree;
420
421 if (commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
422 return get_commit_tree_in_graph(r, commit);
423
424 return NULL;
425 }
426
427 struct object_id *get_commit_tree_oid(const struct commit *commit)
428 {
429 struct tree *tree = repo_get_commit_tree(the_repository, commit);
430 return tree ? &tree->object.oid : NULL;
431 }
432
433 void release_commit_memory(struct parsed_object_pool *pool, struct commit *c)
434 {
435 set_commit_tree(c, NULL);
436 free_commit_buffer(pool, c);
437 c->index = 0;
438 free_commit_list(c->parents);
439
440 c->object.parsed = 0;
441 }
442
443 const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
444 {
445 struct commit_buffer *v = buffer_slab_peek(
446 the_repository->parsed_objects->buffer_slab, commit);
447 void *ret;
448
449 if (!v) {
450 if (sizep)
451 *sizep = 0;
452 return NULL;
453 }
454 ret = v->buffer;
455 if (sizep)
456 *sizep = v->size;
457
458 v->buffer = NULL;
459 v->size = 0;
460 return ret;
461 }
462
463 int parse_commit_buffer(struct repository *r, struct commit *item, const void *buffer, unsigned long size, int check_graph)
464 {
465 const char *tail = buffer;
466 const char *bufptr = buffer;
467 struct object_id parent;
468 struct commit_list **pptr;
469 struct commit_graft *graft;
470 const int tree_entry_len = the_hash_algo->hexsz + 5;
471 const int parent_entry_len = the_hash_algo->hexsz + 7;
472 struct tree *tree;
473
474 if (item->object.parsed)
475 return 0;
476 /*
477 * Presumably this is leftover from an earlier failed parse;
478 * clear it out in preparation for us re-parsing (we'll hit the
479 * same error, but that's good, since it lets our caller know
480 * the result cannot be trusted.
481 */
482 free_commit_list(item->parents);
483 item->parents = NULL;
484
485 tail += size;
486 if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
487 bufptr[tree_entry_len] != '\n')
488 return error("bogus commit object %s", oid_to_hex(&item->object.oid));
489 if (get_oid_hex(bufptr + 5, &parent) < 0)
490 return error("bad tree pointer in commit %s",
491 oid_to_hex(&item->object.oid));
492 tree = lookup_tree(r, &parent);
493 if (!tree)
494 return error("bad tree pointer %s in commit %s",
495 oid_to_hex(&parent),
496 oid_to_hex(&item->object.oid));
497 set_commit_tree(item, tree);
498 bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
499 pptr = &item->parents;
500
501 graft = lookup_commit_graft(r, &item->object.oid);
502 if (graft)
503 r->parsed_objects->substituted_parent = 1;
504 while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
505 struct commit *new_parent;
506
507 if (tail <= bufptr + parent_entry_len + 1 ||
508 get_oid_hex(bufptr + 7, &parent) ||
509 bufptr[parent_entry_len] != '\n')
510 return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
511 bufptr += parent_entry_len + 1;
512 /*
513 * The clone is shallow if nr_parent < 0, and we must
514 * not traverse its real parents even when we unhide them.
515 */
516 if (graft && (graft->nr_parent < 0 || !grafts_keep_true_parents))
517 continue;
518 new_parent = lookup_commit(r, &parent);
519 if (!new_parent)
520 return error("bad parent %s in commit %s",
521 oid_to_hex(&parent),
522 oid_to_hex(&item->object.oid));
523 pptr = &commit_list_insert(new_parent, pptr)->next;
524 }
525 if (graft) {
526 int i;
527 struct commit *new_parent;
528 for (i = 0; i < graft->nr_parent; i++) {
529 new_parent = lookup_commit(r,
530 &graft->parent[i]);
531 if (!new_parent)
532 return error("bad graft parent %s in commit %s",
533 oid_to_hex(&graft->parent[i]),
534 oid_to_hex(&item->object.oid));
535 pptr = &commit_list_insert(new_parent, pptr)->next;
536 }
537 }
538 item->date = parse_commit_date(bufptr, tail);
539
540 if (check_graph)
541 load_commit_graph_info(r, item);
542
543 item->object.parsed = 1;
544 return 0;
545 }
546
547 int repo_parse_commit_internal(struct repository *r,
548 struct commit *item,
549 int quiet_on_missing,
550 int use_commit_graph)
551 {
552 enum object_type type;
553 void *buffer;
554 unsigned long size;
555 struct object_info oi = {
556 .typep = &type,
557 .sizep = &size,
558 .contentp = &buffer,
559 };
560 /*
561 * Git does not support partial clones that exclude commits, so set
562 * OBJECT_INFO_SKIP_FETCH_OBJECT to fail fast when an object is missing.
563 */
564 int flags = OBJECT_INFO_LOOKUP_REPLACE | OBJECT_INFO_SKIP_FETCH_OBJECT |
565 OBJECT_INFO_DIE_IF_CORRUPT;
566 int ret;
567
568 if (!item)
569 return -1;
570 if (item->object.parsed)
571 return 0;
572 if (use_commit_graph && parse_commit_in_graph(r, item)) {
573 static int commit_graph_paranoia = -1;
574
575 if (commit_graph_paranoia == -1)
576 commit_graph_paranoia = git_env_bool(GIT_COMMIT_GRAPH_PARANOIA, 0);
577
578 if (commit_graph_paranoia && !has_object(r, &item->object.oid, 0)) {
579 unparse_commit(r, &item->object.oid);
580 return quiet_on_missing ? -1 :
581 error(_("commit %s exists in commit-graph but not in the object database"),
582 oid_to_hex(&item->object.oid));
583 }
584
585 return 0;
586 }
587
588 if (oid_object_info_extended(r, &item->object.oid, &oi, flags) < 0)
589 return quiet_on_missing ? -1 :
590 error("Could not read %s",
591 oid_to_hex(&item->object.oid));
592 if (type != OBJ_COMMIT) {
593 free(buffer);
594 return error("Object %s not a commit",
595 oid_to_hex(&item->object.oid));
596 }
597
598 ret = parse_commit_buffer(r, item, buffer, size, 0);
599 if (save_commit_buffer && !ret &&
600 !get_cached_commit_buffer(r, item, NULL)) {
601 set_commit_buffer(r, item, buffer, size);
602 return 0;
603 }
604 free(buffer);
605 return ret;
606 }
607
608 int repo_parse_commit_gently(struct repository *r,
609 struct commit *item, int quiet_on_missing)
610 {
611 return repo_parse_commit_internal(r, item, quiet_on_missing, 1);
612 }
613
614 void parse_commit_or_die(struct commit *item)
615 {
616 if (repo_parse_commit(the_repository, item))
617 die("unable to parse commit %s",
618 item ? oid_to_hex(&item->object.oid) : "(null)");
619 }
620
621 int find_commit_subject(const char *commit_buffer, const char **subject)
622 {
623 const char *eol;
624 const char *p = commit_buffer;
625
626 while (*p && (*p != '\n' || p[1] != '\n'))
627 p++;
628 if (*p) {
629 p = skip_blank_lines(p + 2);
630 eol = strchrnul(p, '\n');
631 } else
632 eol = p;
633
634 *subject = p;
635
636 return eol - p;
637 }
638
639 size_t commit_subject_length(const char *body)
640 {
641 const char *p = body;
642 while (*p) {
643 const char *next = skip_blank_lines(p);
644 if (next != p)
645 break;
646 p = strchrnul(p, '\n');
647 if (*p)
648 p++;
649 }
650 return p - body;
651 }
652
653 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
654 {
655 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
656 new_list->item = item;
657 new_list->next = *list_p;
658 *list_p = new_list;
659 return new_list;
660 }
661
662 int commit_list_contains(struct commit *item, struct commit_list *list)
663 {
664 while (list) {
665 if (list->item == item)
666 return 1;
667 list = list->next;
668 }
669
670 return 0;
671 }
672
673 unsigned commit_list_count(const struct commit_list *l)
674 {
675 unsigned c = 0;
676 for (; l; l = l->next )
677 c++;
678 return c;
679 }
680
681 struct commit_list *copy_commit_list(const struct commit_list *list)
682 {
683 struct commit_list *head = NULL;
684 struct commit_list **pp = &head;
685 while (list) {
686 pp = commit_list_append(list->item, pp);
687 list = list->next;
688 }
689 return head;
690 }
691
692 struct commit_list *reverse_commit_list(struct commit_list *list)
693 {
694 struct commit_list *next = NULL, *current, *backup;
695 for (current = list; current; current = backup) {
696 backup = current->next;
697 current->next = next;
698 next = current;
699 }
700 return next;
701 }
702
703 void free_commit_list(struct commit_list *list)
704 {
705 while (list)
706 pop_commit(&list);
707 }
708
709 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
710 {
711 struct commit_list **pp = list;
712 struct commit_list *p;
713 while ((p = *pp) != NULL) {
714 if (p->item->date < item->date) {
715 break;
716 }
717 pp = &p->next;
718 }
719 return commit_list_insert(item, pp);
720 }
721
722 static int commit_list_compare_by_date(const struct commit_list *a,
723 const struct commit_list *b)
724 {
725 timestamp_t a_date = a->item->date;
726 timestamp_t b_date = b->item->date;
727 if (a_date < b_date)
728 return 1;
729 if (a_date > b_date)
730 return -1;
731 return 0;
732 }
733
734 DEFINE_LIST_SORT(static, commit_list_sort, struct commit_list, next);
735
736 void commit_list_sort_by_date(struct commit_list **list)
737 {
738 commit_list_sort(list, commit_list_compare_by_date);
739 }
740
741 struct commit *pop_most_recent_commit(struct commit_list **list,
742 unsigned int mark)
743 {
744 struct commit *ret = pop_commit(list);
745 struct commit_list *parents = ret->parents;
746
747 while (parents) {
748 struct commit *commit = parents->item;
749 if (!repo_parse_commit(the_repository, commit) && !(commit->object.flags & mark)) {
750 commit->object.flags |= mark;
751 commit_list_insert_by_date(commit, list);
752 }
753 parents = parents->next;
754 }
755 return ret;
756 }
757
758 static void clear_commit_marks_1(struct commit_list **plist,
759 struct commit *commit, unsigned int mark)
760 {
761 while (commit) {
762 struct commit_list *parents;
763
764 if (!(mark & commit->object.flags))
765 return;
766
767 commit->object.flags &= ~mark;
768
769 parents = commit->parents;
770 if (!parents)
771 return;
772
773 while ((parents = parents->next)) {
774 if (parents->item->object.flags & mark)
775 commit_list_insert(parents->item, plist);
776 }
777
778 commit = commit->parents->item;
779 }
780 }
781
782 void clear_commit_marks_many(size_t nr, struct commit **commit, unsigned int mark)
783 {
784 for (size_t i = 0; i < nr; i++)
785 clear_commit_marks(commit[i], mark);
786 }
787
788 void clear_commit_marks(struct commit *commit, unsigned int mark)
789 {
790 struct commit_list *list = NULL;
791
792 clear_commit_marks_1(&list, commit, mark);
793 while (list)
794 clear_commit_marks_1(&list, pop_commit(&list), mark);
795 }
796
797 struct commit *pop_commit(struct commit_list **stack)
798 {
799 struct commit_list *top = *stack;
800 struct commit *item = top ? top->item : NULL;
801
802 if (top) {
803 *stack = top->next;
804 free(top);
805 }
806 return item;
807 }
808
809 /*
810 * Topological sort support
811 */
812
813 /* count number of children that have not been emitted */
814 define_commit_slab(indegree_slab, int);
815
816 define_commit_slab(author_date_slab, timestamp_t);
817
818 void record_author_date(struct author_date_slab *author_date,
819 struct commit *commit)
820 {
821 const char *buffer = repo_get_commit_buffer(the_repository, commit,
822 NULL);
823 struct ident_split ident;
824 const char *ident_line;
825 size_t ident_len;
826 char *date_end;
827 timestamp_t date;
828
829 ident_line = find_commit_header(buffer, "author", &ident_len);
830 if (!ident_line)
831 goto fail_exit; /* no author line */
832 if (split_ident_line(&ident, ident_line, ident_len) ||
833 !ident.date_begin || !ident.date_end)
834 goto fail_exit; /* malformed "author" line */
835
836 date = parse_timestamp(ident.date_begin, &date_end, 10);
837 if (date_end != ident.date_end)
838 goto fail_exit; /* malformed date */
839 *(author_date_slab_at(author_date, commit)) = date;
840
841 fail_exit:
842 repo_unuse_commit_buffer(the_repository, commit, buffer);
843 }
844
845 int compare_commits_by_author_date(const void *a_, const void *b_,
846 void *cb_data)
847 {
848 const struct commit *a = a_, *b = b_;
849 struct author_date_slab *author_date = cb_data;
850 timestamp_t a_date = *(author_date_slab_at(author_date, a));
851 timestamp_t b_date = *(author_date_slab_at(author_date, b));
852
853 /* newer commits with larger date first */
854 if (a_date < b_date)
855 return 1;
856 else if (a_date > b_date)
857 return -1;
858 return 0;
859 }
860
861 int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_,
862 void *unused UNUSED)
863 {
864 const struct commit *a = a_, *b = b_;
865 const timestamp_t generation_a = commit_graph_generation(a),
866 generation_b = commit_graph_generation(b);
867
868 /* newer commits first */
869 if (generation_a < generation_b)
870 return 1;
871 else if (generation_a > generation_b)
872 return -1;
873
874 /* use date as a heuristic when generations are equal */
875 if (a->date < b->date)
876 return 1;
877 else if (a->date > b->date)
878 return -1;
879 return 0;
880 }
881
882 int compare_commits_by_commit_date(const void *a_, const void *b_,
883 void *unused UNUSED)
884 {
885 const struct commit *a = a_, *b = b_;
886 /* newer commits with larger date first */
887 if (a->date < b->date)
888 return 1;
889 else if (a->date > b->date)
890 return -1;
891 return 0;
892 }
893
894 /*
895 * Performs an in-place topological sort on the list supplied.
896 */
897 void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
898 {
899 struct commit_list *next, *orig = *list;
900 struct commit_list **pptr;
901 struct indegree_slab indegree;
902 struct prio_queue queue;
903 struct commit *commit;
904 struct author_date_slab author_date;
905
906 if (!orig)
907 return;
908 *list = NULL;
909
910 init_indegree_slab(&indegree);
911 memset(&queue, '\0', sizeof(queue));
912
913 switch (sort_order) {
914 default: /* REV_SORT_IN_GRAPH_ORDER */
915 queue.compare = NULL;
916 break;
917 case REV_SORT_BY_COMMIT_DATE:
918 queue.compare = compare_commits_by_commit_date;
919 break;
920 case REV_SORT_BY_AUTHOR_DATE:
921 init_author_date_slab(&author_date);
922 queue.compare = compare_commits_by_author_date;
923 queue.cb_data = &author_date;
924 break;
925 }
926
927 /* Mark them and clear the indegree */
928 for (next = orig; next; next = next->next) {
929 struct commit *commit = next->item;
930 *(indegree_slab_at(&indegree, commit)) = 1;
931 /* also record the author dates, if needed */
932 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
933 record_author_date(&author_date, commit);
934 }
935
936 /* update the indegree */
937 for (next = orig; next; next = next->next) {
938 struct commit_list *parents = next->item->parents;
939 while (parents) {
940 struct commit *parent = parents->item;
941 int *pi = indegree_slab_at(&indegree, parent);
942
943 if (*pi)
944 (*pi)++;
945 parents = parents->next;
946 }
947 }
948
949 /*
950 * find the tips
951 *
952 * tips are nodes not reachable from any other node in the list
953 *
954 * the tips serve as a starting set for the work queue.
955 */
956 for (next = orig; next; next = next->next) {
957 struct commit *commit = next->item;
958
959 if (*(indegree_slab_at(&indegree, commit)) == 1)
960 prio_queue_put(&queue, commit);
961 }
962
963 /*
964 * This is unfortunate; the initial tips need to be shown
965 * in the order given from the revision traversal machinery.
966 */
967 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
968 prio_queue_reverse(&queue);
969
970 /* We no longer need the commit list */
971 free_commit_list(orig);
972
973 pptr = list;
974 *list = NULL;
975 while ((commit = prio_queue_get(&queue)) != NULL) {
976 struct commit_list *parents;
977
978 for (parents = commit->parents; parents ; parents = parents->next) {
979 struct commit *parent = parents->item;
980 int *pi = indegree_slab_at(&indegree, parent);
981
982 if (!*pi)
983 continue;
984
985 /*
986 * parents are only enqueued for emission
987 * when all their children have been emitted thereby
988 * guaranteeing topological order.
989 */
990 if (--(*pi) == 1)
991 prio_queue_put(&queue, parent);
992 }
993 /*
994 * all children of commit have already been
995 * emitted. we can emit it now.
996 */
997 *(indegree_slab_at(&indegree, commit)) = 0;
998
999 pptr = &commit_list_insert(commit, pptr)->next;
1000 }
1001
1002 clear_indegree_slab(&indegree);
1003 clear_prio_queue(&queue);
1004 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
1005 clear_author_date_slab(&author_date);
1006 }
1007
1008 struct rev_collect {
1009 struct commit **commit;
1010 int nr;
1011 int alloc;
1012 unsigned int initial : 1;
1013 };
1014
1015 static void add_one_commit(struct object_id *oid, struct rev_collect *revs)
1016 {
1017 struct commit *commit;
1018
1019 if (is_null_oid(oid))
1020 return;
1021
1022 commit = lookup_commit(the_repository, oid);
1023 if (!commit ||
1024 (commit->object.flags & TMP_MARK) ||
1025 repo_parse_commit(the_repository, commit))
1026 return;
1027
1028 ALLOC_GROW(revs->commit, revs->nr + 1, revs->alloc);
1029 revs->commit[revs->nr++] = commit;
1030 commit->object.flags |= TMP_MARK;
1031 }
1032
1033 static int collect_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1034 const char *ident UNUSED,
1035 timestamp_t timestamp UNUSED, int tz UNUSED,
1036 const char *message UNUSED, void *cbdata)
1037 {
1038 struct rev_collect *revs = cbdata;
1039
1040 if (revs->initial) {
1041 revs->initial = 0;
1042 add_one_commit(ooid, revs);
1043 }
1044 add_one_commit(noid, revs);
1045 return 0;
1046 }
1047
1048 struct commit *get_fork_point(const char *refname, struct commit *commit)
1049 {
1050 struct object_id oid;
1051 struct rev_collect revs;
1052 struct commit_list *bases = NULL;
1053 int i;
1054 struct commit *ret = NULL;
1055 char *full_refname;
1056
1057 switch (repo_dwim_ref(the_repository, refname, strlen(refname), &oid,
1058 &full_refname, 0)) {
1059 case 0:
1060 die("No such ref: '%s'", refname);
1061 case 1:
1062 break; /* good */
1063 default:
1064 die("Ambiguous refname: '%s'", refname);
1065 }
1066
1067 memset(&revs, 0, sizeof(revs));
1068 revs.initial = 1;
1069 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1070 full_refname, collect_one_reflog_ent, &revs);
1071
1072 if (!revs.nr)
1073 add_one_commit(&oid, &revs);
1074
1075 for (i = 0; i < revs.nr; i++)
1076 revs.commit[i]->object.flags &= ~TMP_MARK;
1077
1078 if (repo_get_merge_bases_many(the_repository, commit, revs.nr,
1079 revs.commit, &bases) < 0)
1080 exit(128);
1081
1082 /*
1083 * There should be one and only one merge base, when we found
1084 * a common ancestor among reflog entries.
1085 */
1086 if (!bases || bases->next)
1087 goto cleanup_return;
1088
1089 /* And the found one must be one of the reflog entries */
1090 for (i = 0; i < revs.nr; i++)
1091 if (&bases->item->object == &revs.commit[i]->object)
1092 break; /* found */
1093 if (revs.nr <= i)
1094 goto cleanup_return;
1095
1096 ret = bases->item;
1097
1098 cleanup_return:
1099 free(revs.commit);
1100 free_commit_list(bases);
1101 free(full_refname);
1102 return ret;
1103 }
1104
1105 /*
1106 * Indexed by hash algorithm identifier.
1107 */
1108 static const char *gpg_sig_headers[] = {
1109 NULL,
1110 "gpgsig",
1111 "gpgsig-sha256",
1112 };
1113
1114 int add_header_signature(struct strbuf *buf, struct strbuf *sig, const struct git_hash_algo *algo)
1115 {
1116 int inspos, copypos;
1117 const char *eoh;
1118 const char *gpg_sig_header = gpg_sig_headers[hash_algo_by_ptr(algo)];
1119 int gpg_sig_header_len = strlen(gpg_sig_header);
1120
1121 /* find the end of the header */
1122 eoh = strstr(buf->buf, "\n\n");
1123 if (!eoh)
1124 inspos = buf->len;
1125 else
1126 inspos = eoh - buf->buf + 1;
1127
1128 for (copypos = 0; sig->buf[copypos]; ) {
1129 const char *bol = sig->buf + copypos;
1130 const char *eol = strchrnul(bol, '\n');
1131 int len = (eol - bol) + !!*eol;
1132
1133 if (!copypos) {
1134 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1135 inspos += gpg_sig_header_len;
1136 }
1137 strbuf_insertstr(buf, inspos++, " ");
1138 strbuf_insert(buf, inspos, bol, len);
1139 inspos += len;
1140 copypos += len;
1141 }
1142 return 0;
1143 }
1144
1145 static int sign_commit_to_strbuf(struct strbuf *sig, struct strbuf *buf, const char *keyid)
1146 {
1147 char *keyid_to_free = NULL;
1148 int ret = 0;
1149 if (!keyid || !*keyid)
1150 keyid = keyid_to_free = get_signing_key();
1151 if (sign_buffer(buf, sig, keyid))
1152 ret = -1;
1153 free(keyid_to_free);
1154 return ret;
1155 }
1156
1157 int parse_signed_commit(const struct commit *commit,
1158 struct strbuf *payload, struct strbuf *signature,
1159 const struct git_hash_algo *algop)
1160 {
1161 unsigned long size;
1162 const char *buffer = repo_get_commit_buffer(the_repository, commit,
1163 &size);
1164 int ret = parse_buffer_signed_by_header(buffer, size, payload, signature, algop);
1165
1166 repo_unuse_commit_buffer(the_repository, commit, buffer);
1167 return ret;
1168 }
1169
1170 int parse_buffer_signed_by_header(const char *buffer,
1171 unsigned long size,
1172 struct strbuf *payload,
1173 struct strbuf *signature,
1174 const struct git_hash_algo *algop)
1175 {
1176 int in_signature = 0, saw_signature = 0, other_signature = 0;
1177 const char *line, *tail, *p;
1178 const char *gpg_sig_header = gpg_sig_headers[hash_algo_by_ptr(algop)];
1179
1180 line = buffer;
1181 tail = buffer + size;
1182 while (line < tail) {
1183 const char *sig = NULL;
1184 const char *next = memchr(line, '\n', tail - line);
1185
1186 next = next ? next + 1 : tail;
1187 if (in_signature && line[0] == ' ')
1188 sig = line + 1;
1189 else if (skip_prefix(line, gpg_sig_header, &p) &&
1190 *p == ' ') {
1191 sig = line + strlen(gpg_sig_header) + 1;
1192 other_signature = 0;
1193 }
1194 else if (starts_with(line, "gpgsig"))
1195 other_signature = 1;
1196 else if (other_signature && line[0] != ' ')
1197 other_signature = 0;
1198 if (sig) {
1199 strbuf_add(signature, sig, next - sig);
1200 saw_signature = 1;
1201 in_signature = 1;
1202 } else {
1203 if (*line == '\n')
1204 /* dump the whole remainder of the buffer */
1205 next = tail;
1206 if (!other_signature)
1207 strbuf_add(payload, line, next - line);
1208 in_signature = 0;
1209 }
1210 line = next;
1211 }
1212 return saw_signature;
1213 }
1214
1215 int remove_signature(struct strbuf *buf)
1216 {
1217 const char *line = buf->buf;
1218 const char *tail = buf->buf + buf->len;
1219 int in_signature = 0;
1220 struct sigbuf {
1221 const char *start;
1222 const char *end;
1223 } sigs[2], *sigp = &sigs[0];
1224 int i;
1225 const char *orig_buf = buf->buf;
1226
1227 memset(sigs, 0, sizeof(sigs));
1228
1229 while (line < tail) {
1230 const char *next = memchr(line, '\n', tail - line);
1231 next = next ? next + 1 : tail;
1232
1233 if (in_signature && line[0] == ' ')
1234 sigp->end = next;
1235 else if (starts_with(line, "gpgsig")) {
1236 int i;
1237 for (i = 1; i < GIT_HASH_NALGOS; i++) {
1238 const char *p;
1239 if (skip_prefix(line, gpg_sig_headers[i], &p) &&
1240 *p == ' ') {
1241 sigp->start = line;
1242 sigp->end = next;
1243 in_signature = 1;
1244 }
1245 }
1246 } else {
1247 if (*line == '\n')
1248 /* dump the whole remainder of the buffer */
1249 next = tail;
1250 if (in_signature && sigp - sigs != ARRAY_SIZE(sigs))
1251 sigp++;
1252 in_signature = 0;
1253 }
1254 line = next;
1255 }
1256
1257 for (i = ARRAY_SIZE(sigs) - 1; i >= 0; i--)
1258 if (sigs[i].start)
1259 strbuf_remove(buf, sigs[i].start - orig_buf, sigs[i].end - sigs[i].start);
1260
1261 return sigs[0].start != NULL;
1262 }
1263
1264 static void handle_signed_tag(const struct commit *parent, struct commit_extra_header ***tail)
1265 {
1266 struct merge_remote_desc *desc;
1267 struct commit_extra_header *mergetag;
1268 char *buf;
1269 unsigned long size;
1270 enum object_type type;
1271 struct strbuf payload = STRBUF_INIT;
1272 struct strbuf signature = STRBUF_INIT;
1273
1274 desc = merge_remote_util(parent);
1275 if (!desc || !desc->obj)
1276 return;
1277 buf = repo_read_object_file(the_repository, &desc->obj->oid, &type,
1278 &size);
1279 if (!buf || type != OBJ_TAG)
1280 goto free_return;
1281 if (!parse_signature(buf, size, &payload, &signature))
1282 goto free_return;
1283 /*
1284 * We could verify this signature and either omit the tag when
1285 * it does not validate, but the integrator may not have the
1286 * public key of the signer of the tag being merged, while a
1287 * later auditor may have it while auditing, so let's not run
1288 * verify-signed-buffer here for now...
1289 *
1290 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1291 * warn("warning: signed tag unverified.");
1292 */
1293 CALLOC_ARRAY(mergetag, 1);
1294 mergetag->key = xstrdup("mergetag");
1295 mergetag->value = buf;
1296 mergetag->len = size;
1297
1298 **tail = mergetag;
1299 *tail = &mergetag->next;
1300 strbuf_release(&payload);
1301 strbuf_release(&signature);
1302 return;
1303
1304 free_return:
1305 free(buf);
1306 }
1307
1308 int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
1309 {
1310 struct strbuf payload = STRBUF_INIT;
1311 struct strbuf signature = STRBUF_INIT;
1312 int ret = 1;
1313
1314 sigc->result = 'N';
1315
1316 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
1317 goto out;
1318
1319 sigc->payload_type = SIGNATURE_PAYLOAD_COMMIT;
1320 sigc->payload = strbuf_detach(&payload, &sigc->payload_len);
1321 ret = check_signature(sigc, signature.buf, signature.len);
1322
1323 out:
1324 strbuf_release(&payload);
1325 strbuf_release(&signature);
1326
1327 return ret;
1328 }
1329
1330 void verify_merge_signature(struct commit *commit, int verbosity,
1331 int check_trust)
1332 {
1333 char hex[GIT_MAX_HEXSZ + 1];
1334 struct signature_check signature_check;
1335 int ret;
1336 memset(&signature_check, 0, sizeof(signature_check));
1337
1338 ret = check_commit_signature(commit, &signature_check);
1339
1340 repo_find_unique_abbrev_r(the_repository, hex, &commit->object.oid,
1341 DEFAULT_ABBREV);
1342 switch (signature_check.result) {
1343 case 'G':
1344 if (ret || (check_trust && signature_check.trust_level < TRUST_MARGINAL))
1345 die(_("Commit %s has an untrusted GPG signature, "
1346 "allegedly by %s."), hex, signature_check.signer);
1347 break;
1348 case 'B':
1349 die(_("Commit %s has a bad GPG signature "
1350 "allegedly by %s."), hex, signature_check.signer);
1351 default: /* 'N' */
1352 die(_("Commit %s does not have a GPG signature."), hex);
1353 }
1354 if (verbosity >= 0 && signature_check.result == 'G')
1355 printf(_("Commit %s has a good GPG signature by %s\n"),
1356 hex, signature_check.signer);
1357
1358 signature_check_clear(&signature_check);
1359 }
1360
1361 void append_merge_tag_headers(const struct commit_list *parents,
1362 struct commit_extra_header ***tail)
1363 {
1364 while (parents) {
1365 const struct commit *parent = parents->item;
1366 handle_signed_tag(parent, tail);
1367 parents = parents->next;
1368 }
1369 }
1370
1371 static int convert_commit_extra_headers(const struct commit_extra_header *orig,
1372 struct commit_extra_header **result)
1373 {
1374 const struct git_hash_algo *compat = the_repository->compat_hash_algo;
1375 const struct git_hash_algo *algo = the_repository->hash_algo;
1376 struct commit_extra_header *extra = NULL, **tail = &extra;
1377 struct strbuf out = STRBUF_INIT;
1378 while (orig) {
1379 struct commit_extra_header *new;
1380 CALLOC_ARRAY(new, 1);
1381 if (!strcmp(orig->key, "mergetag")) {
1382 if (convert_object_file(the_repository, &out, algo, compat,
1383 orig->value, orig->len,
1384 OBJ_TAG, 1)) {
1385 free(new);
1386 free_commit_extra_headers(extra);
1387 return -1;
1388 }
1389 new->key = xstrdup("mergetag");
1390 new->value = strbuf_detach(&out, &new->len);
1391 } else {
1392 new->key = xstrdup(orig->key);
1393 new->len = orig->len;
1394 new->value = xmemdupz(orig->value, orig->len);
1395 }
1396 *tail = new;
1397 tail = &new->next;
1398 orig = orig->next;
1399 }
1400 *result = extra;
1401 return 0;
1402 }
1403
1404 static void add_extra_header(struct strbuf *buffer,
1405 const struct commit_extra_header *extra)
1406 {
1407 strbuf_addstr(buffer, extra->key);
1408 if (extra->len)
1409 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1410 else
1411 strbuf_addch(buffer, '\n');
1412 }
1413
1414 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1415 const char **exclude)
1416 {
1417 struct commit_extra_header *extra = NULL;
1418 unsigned long size;
1419 const char *buffer = repo_get_commit_buffer(the_repository, commit,
1420 &size);
1421 extra = read_commit_extra_header_lines(buffer, size, exclude);
1422 repo_unuse_commit_buffer(the_repository, commit, buffer);
1423 return extra;
1424 }
1425
1426 int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
1427 {
1428 struct commit_extra_header *extra, *to_free;
1429 int res = 0;
1430
1431 to_free = read_commit_extra_headers(commit, NULL);
1432 for (extra = to_free; !res && extra; extra = extra->next) {
1433 if (strcmp(extra->key, "mergetag"))
1434 continue; /* not a merge tag */
1435 res = fn(commit, extra, data);
1436 }
1437 free_commit_extra_headers(to_free);
1438 return res;
1439 }
1440
1441 static inline int standard_header_field(const char *field, size_t len)
1442 {
1443 return ((len == 4 && !memcmp(field, "tree", 4)) ||
1444 (len == 6 && !memcmp(field, "parent", 6)) ||
1445 (len == 6 && !memcmp(field, "author", 6)) ||
1446 (len == 9 && !memcmp(field, "committer", 9)) ||
1447 (len == 8 && !memcmp(field, "encoding", 8)));
1448 }
1449
1450 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1451 {
1452 if (!exclude)
1453 return 0;
1454
1455 while (*exclude) {
1456 size_t xlen = strlen(*exclude);
1457 if (len == xlen && !memcmp(field, *exclude, xlen))
1458 return 1;
1459 exclude++;
1460 }
1461 return 0;
1462 }
1463
1464 static struct commit_extra_header *read_commit_extra_header_lines(
1465 const char *buffer, size_t size,
1466 const char **exclude)
1467 {
1468 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1469 const char *line, *next, *eof, *eob;
1470 struct strbuf buf = STRBUF_INIT;
1471
1472 for (line = buffer, eob = line + size;
1473 line < eob && *line != '\n';
1474 line = next) {
1475 next = memchr(line, '\n', eob - line);
1476 next = next ? next + 1 : eob;
1477 if (*line == ' ') {
1478 /* continuation */
1479 if (it)
1480 strbuf_add(&buf, line + 1, next - (line + 1));
1481 continue;
1482 }
1483 if (it)
1484 it->value = strbuf_detach(&buf, &it->len);
1485 strbuf_reset(&buf);
1486 it = NULL;
1487
1488 eof = memchr(line, ' ', next - line);
1489 if (!eof)
1490 eof = next;
1491 else if (standard_header_field(line, eof - line) ||
1492 excluded_header_field(line, eof - line, exclude))
1493 continue;
1494
1495 CALLOC_ARRAY(it, 1);
1496 it->key = xmemdupz(line, eof-line);
1497 *tail = it;
1498 tail = &it->next;
1499 if (eof + 1 < next)
1500 strbuf_add(&buf, eof + 1, next - (eof + 1));
1501 }
1502 if (it)
1503 it->value = strbuf_detach(&buf, &it->len);
1504 return extra;
1505 }
1506
1507 void free_commit_extra_headers(struct commit_extra_header *extra)
1508 {
1509 while (extra) {
1510 struct commit_extra_header *next = extra->next;
1511 free(extra->key);
1512 free(extra->value);
1513 free(extra);
1514 extra = next;
1515 }
1516 }
1517
1518 int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1519 const struct commit_list *parents, struct object_id *ret,
1520 const char *author, const char *sign_commit)
1521 {
1522 struct commit_extra_header *extra = NULL, **tail = &extra;
1523 int result;
1524
1525 append_merge_tag_headers(parents, &tail);
1526 result = commit_tree_extended(msg, msg_len, tree, parents, ret, author,
1527 NULL, sign_commit, extra);
1528 free_commit_extra_headers(extra);
1529 return result;
1530 }
1531
1532 static int find_invalid_utf8(const char *buf, int len)
1533 {
1534 int offset = 0;
1535 static const unsigned int max_codepoint[] = {
1536 0x7f, 0x7ff, 0xffff, 0x10ffff
1537 };
1538
1539 while (len) {
1540 unsigned char c = *buf++;
1541 int bytes, bad_offset;
1542 unsigned int codepoint;
1543 unsigned int min_val, max_val;
1544
1545 len--;
1546 offset++;
1547
1548 /* Simple US-ASCII? No worries. */
1549 if (c < 0x80)
1550 continue;
1551
1552 bad_offset = offset-1;
1553
1554 /*
1555 * Count how many more high bits set: that's how
1556 * many more bytes this sequence should have.
1557 */
1558 bytes = 0;
1559 while (c & 0x40) {
1560 c <<= 1;
1561 bytes++;
1562 }
1563
1564 /*
1565 * Must be between 1 and 3 more bytes. Longer sequences result in
1566 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1567 */
1568 if (bytes < 1 || 3 < bytes)
1569 return bad_offset;
1570
1571 /* Do we *have* that many bytes? */
1572 if (len < bytes)
1573 return bad_offset;
1574
1575 /*
1576 * Place the encoded bits at the bottom of the value and compute the
1577 * valid range.
1578 */
1579 codepoint = (c & 0x7f) >> bytes;
1580 min_val = max_codepoint[bytes-1] + 1;
1581 max_val = max_codepoint[bytes];
1582
1583 offset += bytes;
1584 len -= bytes;
1585
1586 /* And verify that they are good continuation bytes */
1587 do {
1588 codepoint <<= 6;
1589 codepoint |= *buf & 0x3f;
1590 if ((*buf++ & 0xc0) != 0x80)
1591 return bad_offset;
1592 } while (--bytes);
1593
1594 /* Reject codepoints that are out of range for the sequence length. */
1595 if (codepoint < min_val || codepoint > max_val)
1596 return bad_offset;
1597 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1598 if ((codepoint & 0x1ff800) == 0xd800)
1599 return bad_offset;
1600 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1601 if ((codepoint & 0xfffe) == 0xfffe)
1602 return bad_offset;
1603 /* So are anything in the range U+FDD0..U+FDEF. */
1604 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
1605 return bad_offset;
1606 }
1607 return -1;
1608 }
1609
1610 /*
1611 * This verifies that the buffer is in proper utf8 format.
1612 *
1613 * If it isn't, it assumes any non-utf8 characters are Latin1,
1614 * and does the conversion.
1615 */
1616 static int verify_utf8(struct strbuf *buf)
1617 {
1618 int ok = 1;
1619 long pos = 0;
1620
1621 for (;;) {
1622 int bad;
1623 unsigned char c;
1624 unsigned char replace[2];
1625
1626 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1627 if (bad < 0)
1628 return ok;
1629 pos += bad;
1630 ok = 0;
1631 c = buf->buf[pos];
1632 strbuf_remove(buf, pos, 1);
1633
1634 /* We know 'c' must be in the range 128-255 */
1635 replace[0] = 0xc0 + (c >> 6);
1636 replace[1] = 0x80 + (c & 0x3f);
1637 strbuf_insert(buf, pos, replace, 2);
1638 pos += 2;
1639 }
1640 }
1641
1642 static const char commit_utf8_warn[] =
1643 N_("Warning: commit message did not conform to UTF-8.\n"
1644 "You may want to amend it after fixing the message, or set the config\n"
1645 "variable i18n.commitEncoding to the encoding your project uses.\n");
1646
1647 static void write_commit_tree(struct strbuf *buffer, const char *msg, size_t msg_len,
1648 const struct object_id *tree,
1649 const struct object_id *parents, size_t parents_len,
1650 const char *author, const char *committer,
1651 const struct commit_extra_header *extra)
1652 {
1653 int encoding_is_utf8;
1654 size_t i;
1655
1656 /* Not having i18n.commitencoding is the same as having utf-8 */
1657 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1658
1659 strbuf_grow(buffer, 8192); /* should avoid reallocs for the headers */
1660 strbuf_addf(buffer, "tree %s\n", oid_to_hex(tree));
1661
1662 /*
1663 * NOTE! This ordering means that the same exact tree merged with a
1664 * different order of parents will be a _different_ changeset even
1665 * if everything else stays the same.
1666 */
1667 for (i = 0; i < parents_len; i++)
1668 strbuf_addf(buffer, "parent %s\n", oid_to_hex(&parents[i]));
1669
1670 /* Person/date information */
1671 if (!author)
1672 author = git_author_info(IDENT_STRICT);
1673 strbuf_addf(buffer, "author %s\n", author);
1674 if (!committer)
1675 committer = git_committer_info(IDENT_STRICT);
1676 strbuf_addf(buffer, "committer %s\n", committer);
1677 if (!encoding_is_utf8)
1678 strbuf_addf(buffer, "encoding %s\n", git_commit_encoding);
1679
1680 while (extra) {
1681 add_extra_header(buffer, extra);
1682 extra = extra->next;
1683 }
1684 strbuf_addch(buffer, '\n');
1685
1686 /* And add the comment */
1687 strbuf_add(buffer, msg, msg_len);
1688 }
1689
1690 int commit_tree_extended(const char *msg, size_t msg_len,
1691 const struct object_id *tree,
1692 const struct commit_list *parents, struct object_id *ret,
1693 const char *author, const char *committer,
1694 const char *sign_commit,
1695 const struct commit_extra_header *extra)
1696 {
1697 struct repository *r = the_repository;
1698 int result = 0;
1699 int encoding_is_utf8;
1700 struct strbuf buffer = STRBUF_INIT, compat_buffer = STRBUF_INIT;
1701 struct strbuf sig = STRBUF_INIT, compat_sig = STRBUF_INIT;
1702 struct object_id *parent_buf = NULL, *compat_oid = NULL;
1703 struct object_id compat_oid_buf;
1704 size_t i, nparents;
1705
1706 /* Not having i18n.commitencoding is the same as having utf-8 */
1707 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1708
1709 assert_oid_type(tree, OBJ_TREE);
1710
1711 if (memchr(msg, '\0', msg_len))
1712 return error("a NUL byte in commit log message not allowed.");
1713
1714 nparents = commit_list_count(parents);
1715 CALLOC_ARRAY(parent_buf, nparents);
1716 i = 0;
1717 for (const struct commit_list *p = parents; p; p = p->next)
1718 oidcpy(&parent_buf[i++], &p->item->object.oid);
1719
1720 write_commit_tree(&buffer, msg, msg_len, tree, parent_buf, nparents, author, committer, extra);
1721 if (sign_commit && sign_commit_to_strbuf(&sig, &buffer, sign_commit)) {
1722 result = -1;
1723 goto out;
1724 }
1725 if (r->compat_hash_algo) {
1726 struct commit_extra_header *compat_extra = NULL;
1727 struct object_id mapped_tree;
1728 struct object_id *mapped_parents;
1729
1730 CALLOC_ARRAY(mapped_parents, nparents);
1731
1732 if (repo_oid_to_algop(r, tree, r->compat_hash_algo, &mapped_tree)) {
1733 result = -1;
1734 free(mapped_parents);
1735 goto out;
1736 }
1737 for (i = 0; i < nparents; i++)
1738 if (repo_oid_to_algop(r, &parent_buf[i], r->compat_hash_algo, &mapped_parents[i])) {
1739 result = -1;
1740 free(mapped_parents);
1741 goto out;
1742 }
1743 if (convert_commit_extra_headers(extra, &compat_extra)) {
1744 result = -1;
1745 free(mapped_parents);
1746 goto out;
1747 }
1748 write_commit_tree(&compat_buffer, msg, msg_len, &mapped_tree,
1749 mapped_parents, nparents, author, committer, compat_extra);
1750 free_commit_extra_headers(compat_extra);
1751 free(mapped_parents);
1752
1753 if (sign_commit && sign_commit_to_strbuf(&compat_sig, &compat_buffer, sign_commit)) {
1754 result = -1;
1755 goto out;
1756 }
1757 }
1758
1759 if (sign_commit) {
1760 struct sig_pairs {
1761 struct strbuf *sig;
1762 const struct git_hash_algo *algo;
1763 } bufs [2] = {
1764 { &compat_sig, r->compat_hash_algo },
1765 { &sig, r->hash_algo },
1766 };
1767
1768 /*
1769 * We write algorithms in the order they were implemented in
1770 * Git to produce a stable hash when multiple algorithms are
1771 * used.
1772 */
1773 if (r->compat_hash_algo && hash_algo_by_ptr(bufs[0].algo) > hash_algo_by_ptr(bufs[1].algo))
1774 SWAP(bufs[0], bufs[1]);
1775
1776 /*
1777 * We traverse each algorithm in order, and apply the signature
1778 * to each buffer.
1779 */
1780 for (size_t i = 0; i < ARRAY_SIZE(bufs); i++) {
1781 if (!bufs[i].algo)
1782 continue;
1783 add_header_signature(&buffer, bufs[i].sig, bufs[i].algo);
1784 if (r->compat_hash_algo)
1785 add_header_signature(&compat_buffer, bufs[i].sig, bufs[i].algo);
1786 }
1787 }
1788
1789 /* And check the encoding. */
1790 if (encoding_is_utf8 && (!verify_utf8(&buffer) || !verify_utf8(&compat_buffer)))
1791 fprintf(stderr, _(commit_utf8_warn));
1792
1793 if (r->compat_hash_algo) {
1794 hash_object_file(r->compat_hash_algo, compat_buffer.buf, compat_buffer.len,
1795 OBJ_COMMIT, &compat_oid_buf);
1796 compat_oid = &compat_oid_buf;
1797 }
1798
1799 result = write_object_file_flags(buffer.buf, buffer.len, OBJ_COMMIT,
1800 ret, compat_oid, 0);
1801 out:
1802 free(parent_buf);
1803 strbuf_release(&buffer);
1804 strbuf_release(&compat_buffer);
1805 strbuf_release(&sig);
1806 strbuf_release(&compat_sig);
1807 return result;
1808 }
1809
1810 define_commit_slab(merge_desc_slab, struct merge_remote_desc *);
1811 static struct merge_desc_slab merge_desc_slab = COMMIT_SLAB_INIT(1, merge_desc_slab);
1812
1813 struct merge_remote_desc *merge_remote_util(const struct commit *commit)
1814 {
1815 return *merge_desc_slab_at(&merge_desc_slab, commit);
1816 }
1817
1818 void set_merge_remote_desc(struct commit *commit,
1819 const char *name, struct object *obj)
1820 {
1821 struct merge_remote_desc *desc;
1822 FLEX_ALLOC_STR(desc, name, name);
1823 desc->obj = obj;
1824 *merge_desc_slab_at(&merge_desc_slab, commit) = desc;
1825 }
1826
1827 struct commit *get_merge_parent(const char *name)
1828 {
1829 struct object *obj;
1830 struct commit *commit;
1831 struct object_id oid;
1832 if (repo_get_oid(the_repository, name, &oid))
1833 return NULL;
1834 obj = parse_object(the_repository, &oid);
1835 commit = (struct commit *)repo_peel_to_type(the_repository, name, 0,
1836 obj, OBJ_COMMIT);
1837 if (commit && !merge_remote_util(commit))
1838 set_merge_remote_desc(commit, name, obj);
1839 return commit;
1840 }
1841
1842 /*
1843 * Append a commit to the end of the commit_list.
1844 *
1845 * next starts by pointing to the variable that holds the head of an
1846 * empty commit_list, and is updated to point to the "next" field of
1847 * the last item on the list as new commits are appended.
1848 *
1849 * Usage example:
1850 *
1851 * struct commit_list *list;
1852 * struct commit_list **next = &list;
1853 *
1854 * next = commit_list_append(c1, next);
1855 * next = commit_list_append(c2, next);
1856 * assert(commit_list_count(list) == 2);
1857 * return list;
1858 */
1859 struct commit_list **commit_list_append(struct commit *commit,
1860 struct commit_list **next)
1861 {
1862 struct commit_list *new_commit = xmalloc(sizeof(struct commit_list));
1863 new_commit->item = commit;
1864 *next = new_commit;
1865 new_commit->next = NULL;
1866 return &new_commit->next;
1867 }
1868
1869 const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1870 {
1871 int key_len = strlen(key);
1872 const char *line = msg;
1873
1874 while (line) {
1875 const char *eol = strchrnul(line, '\n');
1876
1877 if (line == eol)
1878 return NULL;
1879
1880 if (eol - line > key_len &&
1881 !strncmp(line, key, key_len) &&
1882 line[key_len] == ' ') {
1883 *out_len = eol - line - key_len - 1;
1884 return line + key_len + 1;
1885 }
1886 line = *eol ? eol + 1 : NULL;
1887 }
1888 return NULL;
1889 }
1890
1891 /*
1892 * Inspect the given string and determine the true "end" of the log message, in
1893 * order to find where to put a new Signed-off-by trailer. Ignored are
1894 * trailing comment lines and blank lines. To support "git commit -s
1895 * --amend" on an existing commit, we also ignore "Conflicts:". To
1896 * support "git commit -v", we truncate at cut lines.
1897 *
1898 * Returns the number of bytes from the tail to ignore, to be fed as
1899 * the second parameter to append_signoff().
1900 */
1901 size_t ignored_log_message_bytes(const char *buf, size_t len)
1902 {
1903 size_t boc = 0;
1904 size_t bol = 0;
1905 int in_old_conflicts_block = 0;
1906 size_t cutoff = wt_status_locate_end(buf, len);
1907
1908 while (bol < cutoff) {
1909 const char *next_line = memchr(buf + bol, '\n', len - bol);
1910
1911 if (!next_line)
1912 next_line = buf + len;
1913 else
1914 next_line++;
1915
1916 if (starts_with_mem(buf + bol, cutoff - bol, comment_line_str) ||
1917 buf[bol] == '\n') {
1918 /* is this the first of the run of comments? */
1919 if (!boc)
1920 boc = bol;
1921 /* otherwise, it is just continuing */
1922 } else if (starts_with(buf + bol, "Conflicts:\n")) {
1923 in_old_conflicts_block = 1;
1924 if (!boc)
1925 boc = bol;
1926 } else if (in_old_conflicts_block && buf[bol] == '\t') {
1927 ; /* a pathname in the conflicts block */
1928 } else if (boc) {
1929 /* the previous was not trailing comment */
1930 boc = 0;
1931 in_old_conflicts_block = 0;
1932 }
1933 bol = next_line - buf;
1934 }
1935 return boc ? len - boc : len - cutoff;
1936 }
1937
1938 int run_commit_hook(int editor_is_used, const char *index_file,
1939 int *invoked_hook, const char *name, ...)
1940 {
1941 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
1942 va_list args;
1943 const char *arg;
1944
1945 strvec_pushf(&opt.env, "GIT_INDEX_FILE=%s", index_file);
1946
1947 /*
1948 * Let the hook know that no editor will be launched.
1949 */
1950 if (!editor_is_used)
1951 strvec_push(&opt.env, "GIT_EDITOR=:");
1952
1953 va_start(args, name);
1954 while ((arg = va_arg(args, const char *)))
1955 strvec_push(&opt.args, arg);
1956 va_end(args);
1957
1958 opt.invoked_hook = invoked_hook;
1959 return run_hooks_opt(the_repository, name, &opt);
1960 }