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