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