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