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