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