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