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