]> git.ipfire.org Git - thirdparty/git.git/blame - commit.c
treewide: remove unnecessary cache.h inclusion from several sources
[thirdparty/git.git] / commit.c
CommitLineData
8f1d2e6f 1#include "cache.h"
961784ee 2#include "tag.h"
175785e5 3#include "commit.h"
177722b3 4#include "commit-graph.h"
f394e093 5#include "gettext.h"
41771fa4 6#include "hex.h"
6a1a79fd 7#include "repository.h"
cbd53a21 8#include "object-store.h"
ed09aef0 9#include "pkt-line.h"
52883fbd 10#include "utf8.h"
199c45bf
JH
11#include "diff.h"
12#include "revision.h"
a97a7468 13#include "notes.h"
14ba97f8 14#include "alloc.h"
ba3c69a9 15#include "gpg-interface.h"
46905893 16#include "mergesort.h"
a84b794a 17#include "commit-slab.h"
da24b104 18#include "prio-queue.h"
bc626927 19#include "hash-lookup.h"
d76650b8 20#include "wt-status.h"
f9f99b3f 21#include "advice.h"
103148aa 22#include "refs.h"
02931217 23#include "commit-reach.h"
49697cb7 24#include "run-command.h"
120ad2b0 25#include "shallow.h"
f443246b 26#include "hook.h"
175785e5 27
82a75299
JH
28static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
29
60ab26de 30int save_commit_buffer = 1;
ab628588 31int no_graft_file_deprecated_advice;
60ab26de 32
175785e5
DB
33const char *commit_type = "commit";
34
d9a05e74 35struct commit *lookup_commit_reference_gently(struct repository *r,
21e1ee8f 36 const struct object_id *oid, int quiet)
961784ee 37{
d9a05e74
SB
38 struct object *obj = deref_tag(r,
39 parse_object(r, oid),
109cd76d 40 NULL, 0);
961784ee
LT
41
42 if (!obj)
43 return NULL;
6da43d93 44 return object_as_type(obj, OBJ_COMMIT, quiet);
f76412ed
JH
45}
46
1f6c72fe 47struct commit *lookup_commit_reference(struct repository *r, const struct object_id *oid)
f76412ed 48{
1f6c72fe 49 return lookup_commit_reference_gently(r, oid, 0);
961784ee
LT
50}
51
bc83266a 52struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name)
baf18fc2 53{
2122f675 54 struct commit *c = lookup_commit_reference(the_repository, oid);
baf18fc2
NTND
55 if (!c)
56 die(_("could not parse %s"), ref_name);
9001dc2a 57 if (!oideq(oid, &c->object.oid)) {
baf18fc2 58 warning(_("%s %s is not a commit!"),
bc83266a 59 ref_name, oid_to_hex(oid));
baf18fc2
NTND
60 }
61 return c;
62}
63
b8dbfd03
PW
64struct 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
bacf1687 72struct commit *lookup_commit(struct repository *r, const struct object_id *oid)
175785e5 73{
d0229abd 74 struct object *obj = lookup_object(r, oid);
d36f51c1 75 if (!obj)
a378509e 76 return create_object(r, oid, alloc_commit_node(r));
6da43d93 77 return object_as_type(obj, OBJ_COMMIT, 0);
175785e5
DB
78}
79
a6fa5992
PN
80struct commit *lookup_commit_reference_by_name(const char *name)
81{
7683e2e6 82 struct object_id oid;
a6fa5992
PN
83 struct commit *commit;
84
e82caf38 85 if (get_oid_committish(name, &oid))
a6fa5992 86 return NULL;
2122f675 87 commit = lookup_commit_reference(the_repository, &oid);
5e7d4d3e 88 if (parse_commit(commit))
a6fa5992
PN
89 return NULL;
90 return commit;
91}
92
dddbad72 93static timestamp_t parse_commit_date(const char *buf, const char *tail)
175785e5 94{
0a617799 95 const char *dateptr;
175785e5 96
0a617799
MK
97 if (buf + 6 >= tail)
98 return 0;
175785e5
DB
99 if (memcmp(buf, "author", 6))
100 return 0;
0a617799 101 while (buf < tail && *buf++ != '\n')
175785e5 102 /* nada */;
0a617799
MK
103 if (buf + 9 >= tail)
104 return 0;
175785e5
DB
105 if (memcmp(buf, "committer", 9))
106 return 0;
0a617799 107 while (buf < tail && *buf++ != '>')
175785e5 108 /* nada */;
0a617799
MK
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;
1aeb7e75
JS
116 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
117 return parse_timestamp(dateptr, NULL, 10);
175785e5
DB
118}
119
8380dcd7 120static const struct object_id *commit_graft_oid_access(size_t index, const void *table)
0a9136f3 121{
8380dcd7 122 const struct commit_graft * const *commit_graft_table = table;
45ee13b9 123 return &commit_graft_table[index]->oid;
0a9136f3
DD
124}
125
98c431b6 126int commit_graft_pos(struct repository *r, const struct object_id *oid)
5da5c8f4 127{
45ee13b9
JK
128 return oid_pos(oid, r->parsed_objects->grafts,
129 r->parsed_objects->grafts_nr,
130 commit_graft_oid_access);
5da5c8f4
JH
131}
132
4d4e49ff
JT
133static 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
a3b78e83
BW
144int register_commit_graft(struct repository *r, struct commit_graft *graft,
145 int ignore_dups)
5040f17e 146{
98c431b6 147 int pos = commit_graft_pos(r, &graft->oid);
a6080a0a 148
5040f17e
JH
149 if (0 <= pos) {
150 if (ignore_dups)
151 free(graft);
152 else {
a3b78e83
BW
153 free(r->parsed_objects->grafts[pos]);
154 r->parsed_objects->grafts[pos] = graft;
5040f17e
JH
155 }
156 return 1;
157 }
158 pos = -pos - 1;
a3b78e83
BW
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;
4d4e49ff 169 unparse_commit(r, &graft->oid);
5040f17e
JH
170 return 0;
171}
172
9a934032 173struct commit_graft *read_graft_line(struct strbuf *line)
5040f17e
JH
174{
175 /* The format is just "Commit Parent1 Parent2 ...\n" */
cfa5bf16
PO
176 int i, phase;
177 const char *tail = NULL;
5040f17e 178 struct commit_graft *graft = NULL;
cfa5bf16 179 struct object_id dummy_oid, *oid;
5040f17e 180
9a934032
PO
181 strbuf_rtrim(line);
182 if (!line->len || line->buf[0] == '#')
5bc4ce58 183 return NULL;
cfa5bf16
PO
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))
5040f17e 191 goto bad_graft_data;
cfa5bf16
PO
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 }
5040f17e
JH
202 }
203 return graft;
df5d43be
RT
204
205bad_graft_data:
9a934032 206 error("bad graft data: %s", line->buf);
cfa5bf16 207 assert(!graft);
df5d43be 208 return NULL;
5040f17e
JH
209}
210
d0e5dd0e 211static int read_graft_file(struct repository *r, const char *graft_file)
5da5c8f4 212{
e9d983f1 213 FILE *fp = fopen_or_warn(graft_file, "r");
e228c173 214 struct strbuf buf = STRBUF_INIT;
5040f17e
JH
215 if (!fp)
216 return -1;
ab628588
ÆAB
217 if (!no_graft_file_deprecated_advice &&
218 advice_enabled(ADVICE_GRAFT_FILE_DEPRECATED))
f9f99b3f
JS
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\""));
e228c173 227 while (!strbuf_getwholeline(&buf, fp, '\n')) {
5da5c8f4 228 /* The format is just "Commit Parent1 Parent2 ...\n" */
9a934032 229 struct commit_graft *graft = read_graft_line(&buf);
5bc4ce58
JH
230 if (!graft)
231 continue;
d0e5dd0e 232 if (register_commit_graft(r, graft, 1))
e228c173 233 error("duplicate graft data: %s", buf.buf);
5da5c8f4
JH
234 }
235 fclose(fp);
e228c173 236 strbuf_release(&buf);
5040f17e
JH
237 return 0;
238}
239
20fd6d57 240void prepare_commit_graft(struct repository *r)
5040f17e 241{
5040f17e
JH
242 char *graft_file;
243
2f6c767f 244 if (r->parsed_objects->commit_graft_prepared)
5040f17e 245 return;
14a9bd28
JK
246 if (!startup_info->have_repository)
247 return;
248
2f6c767f
SB
249 graft_file = get_graft_file(r);
250 read_graft_file(r, graft_file);
ed09aef0 251 /* make sure shallows are read */
2f6c767f
SB
252 is_repository_shallow(r);
253 r->parsed_objects->commit_graft_prepared = 1;
5da5c8f4
JH
254}
255
b9dbddf6 256struct commit_graft *lookup_commit_graft(struct repository *r, const struct object_id *oid)
5da5c8f4
JH
257{
258 int pos;
b9dbddf6 259 prepare_commit_graft(r);
98c431b6 260 pos = commit_graft_pos(r, oid);
5da5c8f4
JH
261 if (pos < 0)
262 return NULL;
b9dbddf6 263 return r->parsed_objects->grafts[pos];
5da5c8f4
JH
264}
265
09d46644 266int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
ed09aef0 267{
09d46644 268 int i, ret;
6a1a79fd
JN
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);
09d46644 271 return ret;
ed09aef0
JS
272}
273
2a69ff09
JT
274void reset_commit_grafts(struct repository *r)
275{
276 int i;
277
4d4e49ff
JT
278 for (i = 0; i < r->parsed_objects->grafts_nr; i++) {
279 unparse_commit(r, &r->parsed_objects->grafts[i]->oid);
2a69ff09 280 free(r->parsed_objects->grafts[i]);
4d4e49ff 281 }
2a69ff09
JT
282 r->parsed_objects->grafts_nr = 0;
283 r->parsed_objects->commit_graft_prepared = 0;
284}
285
8597ea3a
JK
286struct commit_buffer {
287 void *buffer;
288 unsigned long size;
289};
290define_commit_slab(buffer_slab, struct commit_buffer);
c1b3c71f 291
65ea9d4b 292struct buffer_slab *allocate_commit_buffer_slab(void)
66c2827e 293{
65ea9d4b
SB
294 struct buffer_slab *bs = xmalloc(sizeof(*bs));
295 init_buffer_slab(bs);
296 return bs;
297}
298
299void free_commit_buffer_slab(struct buffer_slab *bs)
300{
301 clear_buffer_slab(bs);
302 free(bs);
303}
c1b3c71f 304
1a40fc45 305void set_commit_buffer(struct repository *r, struct commit *commit, void *buffer, unsigned long size)
66c2827e 306{
65ea9d4b 307 struct commit_buffer *v = buffer_slab_at(
1a40fc45 308 r->parsed_objects->buffer_slab, commit);
8597ea3a
JK
309 v->buffer = buffer;
310 v->size = size;
66c2827e
JK
311}
312
4ff7e5c9 313const void *get_cached_commit_buffer(struct repository *r, const struct commit *commit, unsigned long *sizep)
152ff1cc 314{
65ea9d4b 315 struct commit_buffer *v = buffer_slab_peek(
4ff7e5c9 316 r->parsed_objects->buffer_slab, commit);
862e730e
JH
317 if (!v) {
318 if (sizep)
319 *sizep = 0;
320 return NULL;
321 }
8597ea3a
JK
322 if (sizep)
323 *sizep = v->size;
324 return v->buffer;
152ff1cc
JK
325}
326
07de3fd8
SB
327const void *repo_get_commit_buffer(struct repository *r,
328 const struct commit *commit,
329 unsigned long *sizep)
152ff1cc 330{
07de3fd8 331 const void *ret = get_cached_commit_buffer(r, commit, sizep);
152ff1cc
JK
332 if (!ret) {
333 enum object_type type;
334 unsigned long size;
07de3fd8 335 ret = repo_read_object_file(r, &commit->object.oid, &type, &size);
152ff1cc
JK
336 if (!ret)
337 die("cannot read commit object %s",
f2fd0760 338 oid_to_hex(&commit->object.oid));
152ff1cc
JK
339 if (type != OBJ_COMMIT)
340 die("expected commit for %s, got %s",
debca9d2 341 oid_to_hex(&commit->object.oid), type_name(type));
8597ea3a
JK
342 if (sizep)
343 *sizep = size;
152ff1cc
JK
344 }
345 return ret;
346}
347
70315373
SB
348void repo_unuse_commit_buffer(struct repository *r,
349 const struct commit *commit,
350 const void *buffer)
152ff1cc 351{
65ea9d4b 352 struct commit_buffer *v = buffer_slab_peek(
70315373 353 r->parsed_objects->buffer_slab, commit);
862e730e 354 if (!(v && v->buffer == buffer))
152ff1cc
JK
355 free((void *)buffer);
356}
357
6a7895fd 358void free_commit_buffer(struct parsed_object_pool *pool, struct commit *commit)
0fb370da 359{
65ea9d4b 360 struct commit_buffer *v = buffer_slab_peek(
6a7895fd 361 pool->buffer_slab, commit);
862e730e 362 if (v) {
6a83d902 363 FREE_AND_NULL(v->buffer);
862e730e
JH
364 v->size = 0;
365 }
0fb370da
JK
366}
367
a133c40b
NTND
368static inline void set_commit_tree(struct commit *c, struct tree *t)
369{
370 c->maybe_tree = t;
371}
372
301b8c7f
NTND
373struct tree *repo_get_commit_tree(struct repository *r,
374 const struct commit *commit)
5bb03de1 375{
7b8a21db
DS
376 if (commit->maybe_tree || !commit->object.parsed)
377 return commit->maybe_tree;
378
c49c82aa 379 if (commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
ea2dab1a 380 return get_commit_tree_in_graph(r, commit);
7b8a21db 381
83487663 382 return NULL;
5bb03de1
DS
383}
384
385struct object_id *get_commit_tree_oid(const struct commit *commit)
386{
806278de
TB
387 struct tree *tree = get_commit_tree(commit);
388 return tree ? &tree->object.oid : NULL;
5bb03de1
DS
389}
390
6a7895fd 391void release_commit_memory(struct parsed_object_pool *pool, struct commit *c)
14ba97f8 392{
a133c40b 393 set_commit_tree(c, NULL);
6a7895fd 394 free_commit_buffer(pool, c);
9784f973 395 c->index = 0;
14ba97f8 396 free_commit_list(c->parents);
14ba97f8
SB
397
398 c->object.parsed = 0;
399}
400
8597ea3a 401const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
0fb370da 402{
65ea9d4b
SB
403 struct commit_buffer *v = buffer_slab_peek(
404 the_repository->parsed_objects->buffer_slab, commit);
8597ea3a
JK
405 void *ret;
406
862e730e
JH
407 if (!v) {
408 if (sizep)
409 *sizep = 0;
410 return NULL;
411 }
8597ea3a
JK
412 ret = v->buffer;
413 if (sizep)
414 *sizep = v->size;
415
416 v->buffer = NULL;
417 v->size = 0;
0fb370da
JK
418 return ret;
419}
420
fd8030c7 421int parse_commit_buffer(struct repository *r, struct commit *item, const void *buffer, unsigned long size, int check_graph)
175785e5 422{
cf7b1cad
NTND
423 const char *tail = buffer;
424 const char *bufptr = buffer;
7683e2e6 425 struct object_id parent;
6c88be16 426 struct commit_list **pptr;
5da5c8f4 427 struct commit_graft *graft;
2770ccbd 428 const int tree_entry_len = the_hash_algo->hexsz + 5;
429 const int parent_entry_len = the_hash_algo->hexsz + 7;
12736d2f 430 struct tree *tree;
bd2c39f5 431
175785e5
DB
432 if (item->object.parsed)
433 return 0;
bf20fe4c
ÆAB
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;
228c78fb 442
3b44f15a 443 tail += size;
7683e2e6 444 if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
445 bufptr[tree_entry_len] != '\n')
f2fd0760 446 return error("bogus commit object %s", oid_to_hex(&item->object.oid));
26ea3e7d 447 if (get_oid_hex(bufptr + 5, &parent) < 0)
bd2afde8 448 return error("bad tree pointer in commit %s",
f2fd0760 449 oid_to_hex(&item->object.oid));
12736d2f
JK
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);
7683e2e6 456 bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
6c88be16 457 pptr = &item->parents;
5da5c8f4 458
fd8030c7 459 graft = lookup_commit_graft(r, &item->object.oid);
ce16364e
TB
460 if (graft)
461 r->parsed_objects->substituted_parent = 1;
7683e2e6 462 while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
f7cc77d7
LT
463 struct commit *new_parent;
464
7683e2e6 465 if (tail <= bufptr + parent_entry_len + 1 ||
26ea3e7d 466 get_oid_hex(bufptr + 7, &parent) ||
7683e2e6 467 bufptr[parent_entry_len] != '\n')
f2fd0760 468 return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
7683e2e6 469 bufptr += parent_entry_len + 1;
7f3140cd
JS
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))
5da5c8f4 475 continue;
fd8030c7 476 new_parent = lookup_commit(r, &parent);
c78fe004
JK
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;
5da5c8f4
JH
482 }
483 if (graft) {
484 int i;
485 struct commit *new_parent;
486 for (i = 0; i < graft->nr_parent; i++) {
fd8030c7 487 new_parent = lookup_commit(r,
c1f5eb49 488 &graft->parent[i]);
5da5c8f4 489 if (!new_parent)
c78fe004
JK
490 return error("bad graft parent %s in commit %s",
491 oid_to_hex(&graft->parent[i]),
492 oid_to_hex(&item->object.oid));
5da5c8f4 493 pptr = &commit_list_insert(new_parent, pptr)->next;
5da5c8f4 494 }
175785e5 495 }
0a617799 496 item->date = parse_commit_date(bufptr, tail);
27dedf0c 497
e2838d85 498 if (check_graph)
c7944050 499 load_commit_graph_info(r, item);
e2838d85 500
228c78fb 501 item->object.parsed = 1;
175785e5
DB
502 return 0;
503}
504
9e5252ab
SB
505int repo_parse_commit_internal(struct repository *r,
506 struct commit *item,
507 int quiet_on_missing,
508 int use_commit_graph)
bd2c39f5 509{
21666f1a 510 enum object_type type;
bd2c39f5
NP
511 void *buffer;
512 unsigned long size;
7e2ad1cd
JT
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;
bd2c39f5
NP
524 int ret;
525
9786f68b
MK
526 if (!item)
527 return -1;
bd2c39f5
NP
528 if (item->object.parsed)
529 return 0;
9e5252ab 530 if (use_commit_graph && parse_commit_in_graph(r, item))
177722b3 531 return 0;
7e2ad1cd
JT
532
533 if (oid_object_info_extended(r, &item->object.oid, &oi, flags) < 0)
9cc2b07a
JK
534 return quiet_on_missing ? -1 :
535 error("Could not read %s",
f2fd0760 536 oid_to_hex(&item->object.oid));
21666f1a 537 if (type != OBJ_COMMIT) {
bd2c39f5
NP
538 free(buffer);
539 return error("Object %s not a commit",
f2fd0760 540 oid_to_hex(&item->object.oid));
bd2c39f5 541 }
9b19adac 542
9e5252ab 543 ret = parse_commit_buffer(r, item, buffer, size, 0);
60ab26de 544 if (save_commit_buffer && !ret) {
9e5252ab 545 set_commit_buffer(r, item, buffer, size);
3ff1fbbb
LT
546 return 0;
547 }
bd2c39f5
NP
548 free(buffer);
549 return ret;
550}
551
9e5252ab
SB
552int repo_parse_commit_gently(struct repository *r,
553 struct commit *item, int quiet_on_missing)
9b19adac 554{
9e5252ab 555 return repo_parse_commit_internal(r, item, quiet_on_missing, 1);
9b19adac
DS
556}
557
7059dccc
JK
558void parse_commit_or_die(struct commit *item)
559{
560 if (parse_commit(item))
561 die("unable to parse commit %s",
f2fd0760 562 item ? oid_to_hex(&item->object.oid) : "(null)");
7059dccc
JK
563}
564
11af2aae
CC
565int 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) {
4e1b06da 573 p = skip_blank_lines(p + 2);
9c9b03b1 574 eol = strchrnul(p, '\n');
11af2aae
CC
575 } else
576 eol = p;
577
578 *subject = p;
579
580 return eol - p;
581}
582
6e0e2887
CM
583size_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
ac5155ef 597struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
dd97f850 598{
812666c8 599 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
dd97f850
DB
600 new_list->item = item;
601 new_list->next = *list_p;
602 *list_p = new_list;
ac5155ef 603 return new_list;
dd97f850
DB
604}
605
597b2c39
DS
606int 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
65319475
MV
617unsigned 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
53d00b39
TR
625struct 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) {
cb979dbd 630 pp = commit_list_append(list->item, pp);
53d00b39
TR
631 list = list->next;
632 }
633 return head;
634}
635
b0ca1205
EN
636struct 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
175785e5
DB
647void free_commit_list(struct commit_list *list)
648{
e510ab89
RS
649 while (list)
650 pop_commit(&list);
175785e5 651}
dd97f850 652
47e44ed1 653struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
dd97f850
DB
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 }
f755494c 663 return commit_list_insert(item, pp);
dd97f850
DB
664}
665
c0fb5774
RS
666static int commit_list_compare_by_date(const struct commit_list *a,
667 const struct commit_list *b)
46905893 668{
c0fb5774
RS
669 timestamp_t a_date = a->item->date;
670 timestamp_t b_date = b->item->date;
46905893
RS
671 if (a_date < b_date)
672 return 1;
673 if (a_date > b_date)
674 return -1;
675 return 0;
676}
677
c0fb5774 678DEFINE_LIST_SORT(static, commit_list_sort, struct commit_list, next);
a6080a0a 679
47e44ed1 680void commit_list_sort_by_date(struct commit_list **list)
dd97f850 681{
c0fb5774 682 commit_list_sort(list, commit_list_compare_by_date);
dd97f850
DB
683}
684
58e28af6
DB
685struct commit *pop_most_recent_commit(struct commit_list **list,
686 unsigned int mark)
dd97f850 687{
e510ab89 688 struct commit *ret = pop_commit(list);
dd97f850 689 struct commit_list *parents = ret->parents;
dd97f850
DB
690
691 while (parents) {
4056c091 692 struct commit *commit = parents->item;
dec38c81 693 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
58e28af6 694 commit->object.flags |= mark;
47e44ed1 695 commit_list_insert_by_date(commit, list);
4056c091 696 }
dd97f850
DB
697 parents = parents->next;
698 }
699 return ret;
700}
e3bc7a3b 701
941ba8db
NTND
702static void clear_commit_marks_1(struct commit_list **plist,
703 struct commit *commit, unsigned int mark)
f8f9c73c 704{
60fcc2e6
JS
705 while (commit) {
706 struct commit_list *parents;
f8f9c73c 707
60fcc2e6
JS
708 if (!(mark & commit->object.flags))
709 return;
58ecf5c1 710
60fcc2e6
JS
711 commit->object.flags &= ~mark;
712
713 parents = commit->parents;
714 if (!parents)
715 return;
716
4cb39fcf
RS
717 while ((parents = parents->next)) {
718 if (parents->item->object.flags & mark)
719 commit_list_insert(parents->item, plist);
720 }
60fcc2e6
JS
721
722 commit = commit->parents->item;
f8f9c73c
JH
723 }
724}
725
e895cb51 726void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
941ba8db
NTND
727{
728 struct commit_list *list = NULL;
e895cb51
JH
729
730 while (nr--) {
07f7d55a 731 clear_commit_marks_1(&list, *commit, mark);
e895cb51
JH
732 commit++;
733 }
941ba8db
NTND
734 while (list)
735 clear_commit_marks_1(&list, pop_commit(&list), mark);
736}
737
e895cb51
JH
738void clear_commit_marks(struct commit *commit, unsigned int mark)
739{
740 clear_commit_marks_many(1, &commit, mark);
741}
742
a3437b8c
JS
743struct 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
a84b794a
JH
755/*
756 * Topological sort support
757 */
96c4f4a3 758
a84b794a
JH
759/* count number of children that have not been emitted */
760define_commit_slab(indegree_slab, int);
96c4f4a3 761
18207030 762define_commit_slab(author_date_slab, timestamp_t);
81c6b38b 763
5284fc5c
DS
764void record_author_date(struct author_date_slab *author_date,
765 struct commit *commit)
81c6b38b 766{
8597ea3a 767 const char *buffer = get_commit_buffer(commit, NULL);
81c6b38b 768 struct ident_split ident;
ea5517f0
JK
769 const char *ident_line;
770 size_t ident_len;
81c6b38b 771 char *date_end;
dddbad72 772 timestamp_t date;
81c6b38b 773
ea5517f0
JK
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 */
81c6b38b 780
1aeb7e75 781 date = parse_timestamp(ident.date_begin, &date_end, 10);
81c6b38b
JH
782 if (date_end != ident.date_end)
783 goto fail_exit; /* malformed date */
784 *(author_date_slab_at(author_date, commit)) = date;
785
786fail_exit:
ba41c1c9 787 unuse_commit_buffer(commit, buffer);
81c6b38b
JH
788}
789
5284fc5c
DS
790int compare_commits_by_author_date(const void *a_, const void *b_,
791 void *cb_data)
81c6b38b
JH
792{
793 const struct commit *a = a_, *b = b_;
794 struct author_date_slab *author_date = cb_data;
dddbad72
JS
795 timestamp_t a_date = *(author_date_slab_at(author_date, a));
796 timestamp_t b_date = *(author_date_slab_at(author_date, b));
81c6b38b
JH
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
17587122
JK
806int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_,
807 void *unused UNUSED)
3afc679b
DS
808{
809 const struct commit *a = a_, *b = b_;
d7f92784
AK
810 const timestamp_t generation_a = commit_graph_generation(a),
811 generation_b = commit_graph_generation(b);
3afc679b
DS
812
813 /* newer commits first */
c752ad09 814 if (generation_a < generation_b)
3afc679b 815 return 1;
c752ad09 816 else if (generation_a > generation_b)
3afc679b
DS
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
17587122
JK
827int compare_commits_by_commit_date(const void *a_, const void *b_,
828 void *unused UNUSED)
da24b104
JH
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
ab580ace
JS
839/*
840 * Performs an in-place topological sort on the list supplied.
841 */
da24b104 842void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
6b6dcfc2 843{
23c17d4a 844 struct commit_list *next, *orig = *list;
23c17d4a 845 struct commit_list **pptr;
a84b794a 846 struct indegree_slab indegree;
da24b104
JH
847 struct prio_queue queue;
848 struct commit *commit;
81c6b38b 849 struct author_date_slab author_date;
a6080a0a 850
23c17d4a 851 if (!orig)
7d6fb370 852 return;
23c17d4a
LT
853 *list = NULL;
854
a84b794a 855 init_indegree_slab(&indegree);
da24b104 856 memset(&queue, '\0', sizeof(queue));
81c6b38b 857
da24b104
JH
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;
81c6b38b
JH
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;
da24b104 870 }
96c4f4a3 871
23c17d4a
LT
872 /* Mark them and clear the indegree */
873 for (next = orig; next; next = next->next) {
874 struct commit *commit = next->item;
a84b794a 875 *(indegree_slab_at(&indegree, commit)) = 1;
81c6b38b
JH
876 /* also record the author dates, if needed */
877 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
878 record_author_date(&author_date, commit);
ab580ace 879 }
23c17d4a 880
ab580ace 881 /* update the indegree */
23c17d4a 882 for (next = orig; next; next = next->next) {
da24b104 883 struct commit_list *parents = next->item->parents;
ab580ace 884 while (parents) {
23c17d4a 885 struct commit *parent = parents->item;
a84b794a 886 int *pi = indegree_slab_at(&indegree, parent);
6b6dcfc2 887
96c4f4a3
JK
888 if (*pi)
889 (*pi)++;
23c17d4a 890 parents = parents->next;
ab580ace 891 }
ab580ace 892 }
23c17d4a 893
a6080a0a 894 /*
674d1727
PH
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 */
23c17d4a
LT
901 for (next = orig; next; next = next->next) {
902 struct commit *commit = next->item;
ab580ace 903
a84b794a 904 if (*(indegree_slab_at(&indegree, commit)) == 1)
da24b104 905 prio_queue_put(&queue, commit);
ab580ace 906 }
4c8725f1 907
da24b104
JH
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);
23c17d4a
LT
917
918 pptr = list;
919 *list = NULL;
da24b104
JH
920 while ((commit = prio_queue_get(&queue)) != NULL) {
921 struct commit_list *parents;
23c17d4a 922
23c17d4a 923 for (parents = commit->parents; parents ; parents = parents->next) {
cd2b8ae9 924 struct commit *parent = parents->item;
a84b794a 925 int *pi = indegree_slab_at(&indegree, parent);
23c17d4a 926
96c4f4a3 927 if (!*pi)
23c17d4a
LT
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 */
da24b104
JH
935 if (--(*pi) == 1)
936 prio_queue_put(&queue, parent);
ab580ace
JS
937 }
938 /*
da24b104
JH
939 * all children of commit have already been
940 * emitted. we can emit it now.
674d1727 941 */
a84b794a 942 *(indegree_slab_at(&indegree, commit)) = 0;
da24b104
JH
943
944 pptr = &commit_list_insert(commit, pptr)->next;
ab580ace 945 }
96c4f4a3 946
a84b794a 947 clear_indegree_slab(&indegree);
da24b104 948 clear_prio_queue(&queue);
81c6b38b
JH
949 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
950 clear_author_date_slab(&author_date);
ab580ace 951}
7c6f8aaf 952
103148aa
PK
953struct rev_collect {
954 struct commit **commit;
955 int nr;
956 int alloc;
957 unsigned int initial : 1;
958};
959
960static 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
978static int collect_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
5cf88fd8
ÆAB
979 const char *ident UNUSED,
980 timestamp_t timestamp UNUSED, int tz UNUSED,
981 const char *message UNUSED, void *cbdata)
103148aa
PK
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
993struct 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;
f08132f8
JH
1000 char *full_refname;
1001
f24c30e0 1002 switch (dwim_ref(refname, strlen(refname), &oid, &full_refname, 0)) {
f08132f8
JH
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 }
103148aa
PK
1010
1011 memset(&revs, 0, sizeof(revs));
1012 revs.initial = 1;
f08132f8 1013 for_each_reflog_ent(full_refname, collect_one_reflog_ent, &revs);
103148aa 1014
f08132f8 1015 if (!revs.nr)
103148aa
PK
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
1039cleanup_return:
0c10ed19 1040 free(revs.commit);
103148aa 1041 free_commit_list(bases);
f08132f8 1042 free(full_refname);
103148aa
PK
1043 return ret;
1044}
1045
42d4e1d1 1046/*
1047 * Indexed by hash algorithm identifier.
1048 */
1049static const char *gpg_sig_headers[] = {
1050 NULL,
1051 "gpgsig",
1052 "gpgsig-sha256",
1053};
ba3c69a9 1054
937032e1 1055int sign_with_header(struct strbuf *buf, const char *keyid)
ba3c69a9
JH
1056{
1057 struct strbuf sig = STRBUF_INIT;
1058 int inspos, copypos;
3324dd8f 1059 const char *eoh;
42d4e1d1 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);
ba3c69a9
JH
1062
1063 /* find the end of the header */
3324dd8f
JS
1064 eoh = strstr(buf->buf, "\n\n");
1065 if (!eoh)
1066 inspos = buf->len;
1067 else
1068 inspos = eoh - buf->buf + 1;
ba3c69a9
JH
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 }
a91cc7fa 1086 strbuf_insertstr(buf, inspos++, " ");
ba3c69a9
JH
1087 strbuf_insert(buf, inspos, bol, len);
1088 inspos += len;
1089 copypos += len;
1090 }
1091 strbuf_release(&sig);
1092 return 0;
1093}
1094
937032e1 1095
1096
218aa3a6 1097int parse_signed_commit(const struct commit *commit,
1fb5cf0d 1098 struct strbuf *payload, struct strbuf *signature,
1099 const struct git_hash_algo *algop)
0c37f1fc
JH
1100{
1101 unsigned long size;
218aa3a6 1102 const char *buffer = get_commit_buffer(commit, &size);
937032e1 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
1109int 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{
1fb5cf0d 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)];
0c37f1fc
JH
1118
1119 line = buffer;
1120 tail = buffer + size;
0c37f1fc
JH
1121 while (line < tail) {
1122 const char *sig = NULL;
218aa3a6 1123 const char *next = memchr(line, '\n', tail - line);
0c37f1fc
JH
1124
1125 next = next ? next + 1 : tail;
1126 if (in_signature && line[0] == ' ')
1127 sig = line + 1;
1fb5cf0d 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;
0c37f1fc
JH
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;
1fb5cf0d 1145 if (!other_signature)
1146 strbuf_add(payload, line, next - line);
0c37f1fc
JH
1147 in_signature = 0;
1148 }
1149 line = next;
1150 }
0c37f1fc
JH
1151 return saw_signature;
1152}
1153
0b05ab6f
CC
1154int 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;
1fb5cf0d 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));
0b05ab6f
CC
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] == ' ')
1fb5cf0d 1173 sigp->end = next;
42d4e1d1 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 == ' ') {
1fb5cf0d 1180 sigp->start = line;
1181 sigp->end = next;
42d4e1d1 1182 in_signature = 1;
1183 }
1184 }
0b05ab6f
CC
1185 } else {
1186 if (*line == '\n')
1187 /* dump the whole remainder of the buffer */
1188 next = tail;
1fb5cf0d 1189 if (in_signature && sigp - sigs != ARRAY_SIZE(sigs))
1190 sigp++;
0b05ab6f
CC
1191 in_signature = 0;
1192 }
1193 line = next;
1194 }
1195
1fb5cf0d 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);
0b05ab6f 1199
1fb5cf0d 1200 return sigs[0].start != NULL;
0b05ab6f
CC
1201}
1202
5231c633
JH
1203static 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;
482c1191 1208 unsigned long size;
5231c633 1209 enum object_type type;
482c1191 1210 struct strbuf payload = STRBUF_INIT;
1211 struct strbuf signature = STRBUF_INIT;
5231c633
JH
1212
1213 desc = merge_remote_util(parent);
1214 if (!desc || !desc->obj)
1215 return;
b4f5aca4 1216 buf = read_object_file(&desc->obj->oid, &type, &size);
5231c633
JH
1217 if (!buf || type != OBJ_TAG)
1218 goto free_return;
482c1191 1219 if (!parse_signature(buf, size, &payload, &signature))
5231c633
JH
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
0e20b229 1224 * public key of the signer of the tag being merged, while a
5231c633
JH
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 */
ca56dadb 1231 CALLOC_ARRAY(mergetag, 1);
5231c633
JH
1232 mergetag->key = xstrdup("mergetag");
1233 mergetag->value = buf;
1234 mergetag->len = size;
1235
1236 **tail = mergetag;
1237 *tail = &mergetag->next;
482c1191 1238 strbuf_release(&payload);
1239 strbuf_release(&signature);
5231c633
JH
1240 return;
1241
1242free_return:
1243 free(buf);
1244}
1245
434060ec 1246int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
ffb6d7d5
SG
1247{
1248 struct strbuf payload = STRBUF_INIT;
1249 struct strbuf signature = STRBUF_INIT;
434060ec 1250 int ret = 1;
ffb6d7d5
SG
1251
1252 sigc->result = 'N';
1253
1fb5cf0d 1254 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
ffb6d7d5 1255 goto out;
02769437 1256
6393c956 1257 sigc->payload_type = SIGNATURE_PAYLOAD_COMMIT;
02769437
FS
1258 sigc->payload = strbuf_detach(&payload, &sigc->payload_len);
1259 ret = check_signature(sigc, signature.buf, signature.len);
ffb6d7d5
SG
1260
1261 out:
ffb6d7d5
SG
1262 strbuf_release(&payload);
1263 strbuf_release(&signature);
434060ec 1264
1265 return ret;
ffb6d7d5
SG
1266}
1267
54887b46
HJI
1268void verify_merge_signature(struct commit *commit, int verbosity,
1269 int check_trust)
edc4d47d
JK
1270{
1271 char hex[GIT_MAX_HEXSZ + 1];
1272 struct signature_check signature_check;
54887b46 1273 int ret;
edc4d47d
JK
1274 memset(&signature_check, 0, sizeof(signature_check));
1275
54887b46 1276 ret = check_commit_signature(commit, &signature_check);
edc4d47d
JK
1277
1278 find_unique_abbrev_r(hex, &commit->object.oid, DEFAULT_ABBREV);
1279 switch (signature_check.result) {
1280 case 'G':
54887b46
HJI
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);
edc4d47d 1284 break;
edc4d47d
JK
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);
ffb6d7d5 1294
edc4d47d
JK
1295 signature_check_clear(&signature_check);
1296}
ffb6d7d5 1297
5231c633
JH
1298void 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
1308static void add_extra_header(struct strbuf *buffer,
1309 struct commit_extra_header *extra)
1310{
1311 strbuf_addstr(buffer, extra->key);
ed7a42a0
JH
1312 if (extra->len)
1313 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1314 else
1315 strbuf_addch(buffer, '\n');
1316}
1317
c871a1d1
JH
1318struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1319 const char **exclude)
ed7a42a0
JH
1320{
1321 struct commit_extra_header *extra = NULL;
1322 unsigned long size;
218aa3a6
JK
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);
ed7a42a0
JH
1326 return extra;
1327}
1328
fef461ea 1329int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
063da62b
CC
1330{
1331 struct commit_extra_header *extra, *to_free;
fef461ea 1332 int res = 0;
063da62b
CC
1333
1334 to_free = read_commit_extra_headers(commit, NULL);
fef461ea 1335 for (extra = to_free; !res && extra; extra = extra->next) {
063da62b
CC
1336 if (strcmp(extra->key, "mergetag"))
1337 continue; /* not a merge tag */
fef461ea 1338 res = fn(commit, extra, data);
063da62b
CC
1339 }
1340 free_commit_extra_headers(to_free);
fef461ea 1341 return res;
063da62b
CC
1342}
1343
ed7a42a0
JH
1344static inline int standard_header_field(const char *field, size_t len)
1345{
b072504c
RS
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)));
ed7a42a0
JH
1351}
1352
c871a1d1
JH
1353static 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);
b072504c 1360 if (len == xlen && !memcmp(field, *exclude, xlen))
c871a1d1
JH
1361 return 1;
1362 exclude++;
1363 }
1364 return 0;
1365}
1366
82a75299
JH
1367static struct commit_extra_header *read_commit_extra_header_lines(
1368 const char *buffer, size_t size,
1369 const char **exclude)
ed7a42a0
JH
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
50a01cc4
RS
1391 eof = memchr(line, ' ', next - line);
1392 if (!eof)
ed7a42a0 1393 eof = next;
b072504c
RS
1394 else if (standard_header_field(line, eof - line) ||
1395 excluded_header_field(line, eof - line, exclude))
ed7a42a0
JH
1396 continue;
1397
ca56dadb 1398 CALLOC_ARRAY(it, 1);
ed7a42a0
JH
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;
5231c633
JH
1408}
1409
1410void 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
5078f344
PO
1421int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1422 struct commit_list *parents, struct object_id *ret,
ba3c69a9 1423 const char *author, const char *sign_commit)
5231c633
JH
1424{
1425 struct commit_extra_header *extra = NULL, **tail = &extra;
1426 int result;
1427
1428 append_merge_tag_headers(parents, &tail);
e8cbe211
PW
1429 result = commit_tree_extended(msg, msg_len, tree, parents, ret, author,
1430 NULL, sign_commit, extra);
5231c633
JH
1431 free_commit_extra_headers(extra);
1432 return result;
1433}
1434
08a94a14
LT
1435static int find_invalid_utf8(const char *buf, int len)
1436{
1437 int offset = 0;
e82bd6cc 1438 static const unsigned int max_codepoint[] = {
1439 0x7f, 0x7ff, 0xffff, 0x10ffff
1440 };
08a94a14
LT
1441
1442 while (len) {
1443 unsigned char c = *buf++;
1444 int bytes, bad_offset;
28110d4b 1445 unsigned int codepoint;
e82bd6cc 1446 unsigned int min_val, max_val;
08a94a14
LT
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
28110d4b 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)
08a94a14
LT
1472 return bad_offset;
1473
1474 /* Do we *have* that many bytes? */
1475 if (len < bytes)
1476 return bad_offset;
1477
e82bd6cc 1478 /*
1479 * Place the encoded bits at the bottom of the value and compute the
1480 * valid range.
1481 */
28110d4b 1482 codepoint = (c & 0x7f) >> bytes;
e82bd6cc 1483 min_val = max_codepoint[bytes-1] + 1;
1484 max_val = max_codepoint[bytes];
28110d4b 1485
08a94a14
LT
1486 offset += bytes;
1487 len -= bytes;
1488
1489 /* And verify that they are good continuation bytes */
1490 do {
28110d4b 1491 codepoint <<= 6;
1492 codepoint |= *buf & 0x3f;
08a94a14
LT
1493 if ((*buf++ & 0xc0) != 0x80)
1494 return bad_offset;
1495 } while (--bytes);
1496
e82bd6cc 1497 /* Reject codepoints that are out of range for the sequence length. */
1498 if (codepoint < min_val || codepoint > max_val)
28110d4b 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;
81050ac6 1503 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
dc773a67 1504 if ((codepoint & 0xfffe) == 0xfffe)
81050ac6
PK
1505 return bad_offset;
1506 /* So are anything in the range U+FDD0..U+FDEF. */
1507 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
28110d4b 1508 return bad_offset;
08a94a14
LT
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.
08a94a14
LT
1518 */
1519static 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
40d52ff7 1545static const char commit_utf8_warn[] =
4fa4b315
VA
1546N_("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"
b4eda05d 1548 "variable i18n.commitEncoding to the encoding your project uses.\n");
40d52ff7 1549
3ffefb54 1550int commit_tree_extended(const char *msg, size_t msg_len,
5078f344
PO
1551 const struct object_id *tree,
1552 struct commit_list *parents, struct object_id *ret,
e8cbe211
PW
1553 const char *author, const char *committer,
1554 const char *sign_commit,
ba3c69a9 1555 struct commit_extra_header *extra)
40d52ff7
JK
1556{
1557 int result;
1558 int encoding_is_utf8;
1559 struct strbuf buffer;
1560
e816caa0 1561 assert_oid_type(tree, OBJ_TREE);
40d52ff7 1562
3ffefb54 1563 if (memchr(msg, '\0', msg_len))
37576c14
NTND
1564 return error("a NUL byte in commit log message not allowed.");
1565
40d52ff7
JK
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 */
5078f344 1570 strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree));
40d52ff7
JK
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) {
e510ab89 1578 struct commit *parent = pop_commit(&parents);
40d52ff7 1579 strbuf_addf(&buffer, "parent %s\n",
f2fd0760 1580 oid_to_hex(&parent->object.oid));
40d52ff7
JK
1581 }
1582
1583 /* Person/date information */
1584 if (!author)
f9bc573f 1585 author = git_author_info(IDENT_STRICT);
40d52ff7 1586 strbuf_addf(&buffer, "author %s\n", author);
e8cbe211
PW
1587 if (!committer)
1588 committer = git_committer_info(IDENT_STRICT);
1589 strbuf_addf(&buffer, "committer %s\n", committer);
40d52ff7
JK
1590 if (!encoding_is_utf8)
1591 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
5231c633
JH
1592
1593 while (extra) {
1594 add_extra_header(&buffer, extra);
1595 extra = extra->next;
1596 }
40d52ff7
JK
1597 strbuf_addch(&buffer, '\n');
1598
1599 /* And add the comment */
3ffefb54 1600 strbuf_add(&buffer, msg, msg_len);
40d52ff7
JK
1601
1602 /* And check the encoding */
08a94a14 1603 if (encoding_is_utf8 && !verify_utf8(&buffer))
4fa4b315 1604 fprintf(stderr, _(commit_utf8_warn));
40d52ff7 1605
937032e1 1606 if (sign_commit && sign_with_header(&buffer, sign_commit)) {
e505146d
RS
1607 result = -1;
1608 goto out;
1609 }
ba3c69a9 1610
c80d226a 1611 result = write_object_file(buffer.buf, buffer.len, OBJ_COMMIT, ret);
e505146d 1612out:
40d52ff7
JK
1613 strbuf_release(&buffer);
1614 return result;
1615}
ae8e4c9c 1616
e2e5ac23
NTND
1617define_commit_slab(merge_desc_slab, struct merge_remote_desc *);
1618static struct merge_desc_slab merge_desc_slab = COMMIT_SLAB_INIT(1, merge_desc_slab);
1619
1620struct merge_remote_desc *merge_remote_util(struct commit *commit)
1621{
1622 return *merge_desc_slab_at(&merge_desc_slab, commit);
1623}
1624
beb518c9
RS
1625void set_merge_remote_desc(struct commit *commit,
1626 const char *name, struct object *obj)
1627{
1628 struct merge_remote_desc *desc;
5447a76a 1629 FLEX_ALLOC_STR(desc, name, name);
beb518c9 1630 desc->obj = obj;
e2e5ac23 1631 *merge_desc_slab_at(&merge_desc_slab, commit) = desc;
beb518c9
RS
1632}
1633
ae8e4c9c
JH
1634struct commit *get_merge_parent(const char *name)
1635{
1636 struct object *obj;
1637 struct commit *commit;
7683e2e6 1638 struct object_id oid;
e82caf38 1639 if (get_oid(name, &oid))
ae8e4c9c 1640 return NULL;
109cd76d 1641 obj = parse_object(the_repository, &oid);
ae8e4c9c 1642 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
e2e5ac23 1643 if (commit && !merge_remote_util(commit))
beb518c9 1644 set_merge_remote_desc(commit, name, obj);
ae8e4c9c
JH
1645 return commit;
1646}
89b5f1d9
RS
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 */
1665struct commit_list **commit_list_append(struct commit *commit,
1666 struct commit_list **next)
1667{
258c03eb
BW
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;
89b5f1d9 1673}
efc7df45 1674
cfc5cf42
JC
1675const char *find_header_mem(const char *msg, size_t len,
1676 const char *key, size_t *out_len)
fe6eb7f2
JK
1677{
1678 int key_len = strlen(key);
1679 const char *line = msg;
1680
cfc5cf42
JC
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) {
fe6eb7f2
JK
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}
0ed8a4e1 1704
cfc5cf42
JC
1705const 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}
8c384589 1709/*
710714aa 1710 * Inspect the given string and determine the true "end" of the log message, in
3abd4a67 1711 * order to find where to put a new Signed-off-by trailer. Ignored are
d76650b8
BM
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.
8c384589
CC
1715 *
1716 * Returns the number of bytes from the tail to ignore, to be fed as
1717 * the second parameter to append_signoff().
1718 */
66e83d9b 1719size_t ignore_non_trailer(const char *buf, size_t len)
8c384589 1720{
66e83d9b
JK
1721 size_t boc = 0;
1722 size_t bol = 0;
8c384589 1723 int in_old_conflicts_block = 0;
d76650b8 1724 size_t cutoff = wt_status_locate_end(buf, len);
8c384589 1725
d76650b8 1726 while (bol < cutoff) {
710714aa 1727 const char *next_line = memchr(buf + bol, '\n', len - bol);
8c384589 1728
710714aa
JT
1729 if (!next_line)
1730 next_line = buf + len;
8c384589
CC
1731 else
1732 next_line++;
1733
710714aa 1734 if (buf[bol] == comment_line_char || buf[bol] == '\n') {
8c384589
CC
1735 /* is this the first of the run of comments? */
1736 if (!boc)
1737 boc = bol;
1738 /* otherwise, it is just continuing */
710714aa 1739 } else if (starts_with(buf + bol, "Conflicts:\n")) {
8c384589
CC
1740 in_old_conflicts_block = 1;
1741 if (!boc)
1742 boc = bol;
710714aa 1743 } else if (in_old_conflicts_block && buf[bol] == '\t') {
8c384589
CC
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 }
710714aa 1750 bol = next_line - buf;
8c384589 1751 }
d76650b8 1752 return boc ? len - boc : len - cutoff;
8c384589 1753}
49697cb7
PW
1754
1755int run_commit_hook(int editor_is_used, const char *index_file,
a8cc5943 1756 int *invoked_hook, const char *name, ...)
49697cb7 1757{
f443246b 1758 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
49697cb7 1759 va_list args;
f443246b 1760 const char *arg;
49697cb7 1761
f443246b 1762 strvec_pushf(&opt.env, "GIT_INDEX_FILE=%s", index_file);
49697cb7
PW
1763
1764 /*
1765 * Let the hook know that no editor will be launched.
1766 */
1767 if (!editor_is_used)
f443246b 1768 strvec_push(&opt.env, "GIT_EDITOR=:");
49697cb7
PW
1769
1770 va_start(args, name);
f443246b
ES
1771 while ((arg = va_arg(args, const char *)))
1772 strvec_push(&opt.args, arg);
49697cb7 1773 va_end(args);
49697cb7 1774
4369e3a1 1775 opt.invoked_hook = invoked_hook;
f443246b 1776 return run_hooks_opt(name, &opt);
49697cb7 1777}