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