commit-slab: introduce a macro to define a slab for new type
[git/git.git] / commit.c
CommitLineData
8f1d2e6f 1#include "cache.h"
961784ee 2#include "tag.h"
175785e5 3#include "commit.h"
ed09aef0 4#include "pkt-line.h"
52883fbd 5#include "utf8.h"
199c45bf
JH
6#include "diff.h"
7#include "revision.h"
a97a7468 8#include "notes.h"
ba3c69a9 9#include "gpg-interface.h"
46905893 10#include "mergesort.h"
a84b794a 11#include "commit-slab.h"
175785e5 12
82a75299
JH
13static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
14
60ab26de
LT
15int save_commit_buffer = 1;
16
175785e5 17const char *commit_type = "commit";
96c4f4a3 18static int commit_count;
175785e5 19
f76412ed
JH
20static struct commit *check_commit(struct object *obj,
21 const unsigned char *sha1,
22 int quiet)
961784ee 23{
1974632c 24 if (obj->type != OBJ_COMMIT) {
f76412ed
JH
25 if (!quiet)
26 error("Object %s is a %s, not a commit",
885a86ab 27 sha1_to_hex(sha1), typename(obj->type));
961784ee
LT
28 return NULL;
29 }
30 return (struct commit *) obj;
31}
32
f76412ed
JH
33struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
34 int quiet)
961784ee 35{
9534f40b 36 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
961784ee
LT
37
38 if (!obj)
39 return NULL;
f76412ed
JH
40 return check_commit(obj, sha1, quiet);
41}
42
43struct commit *lookup_commit_reference(const unsigned char *sha1)
44{
45 return lookup_commit_reference_gently(sha1, 0);
961784ee
LT
46}
47
baf18fc2
NTND
48struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name)
49{
50 struct commit *c = lookup_commit_reference(sha1);
51 if (!c)
52 die(_("could not parse %s"), ref_name);
53 if (hashcmp(sha1, c->object.sha1)) {
54 warning(_("%s %s is not a commit!"),
55 ref_name, sha1_to_hex(sha1));
56 }
57 return c;
58}
59
5d6ccf5c 60struct commit *lookup_commit(const unsigned char *sha1)
175785e5
DB
61{
62 struct object *obj = lookup_object(sha1);
96c4f4a3
JK
63 if (!obj) {
64 struct commit *c = alloc_commit_node();
65 c->index = commit_count++;
66 return create_object(sha1, OBJ_COMMIT, c);
67 }
d1af002d 68 if (!obj->type)
1974632c 69 obj->type = OBJ_COMMIT;
f76412ed 70 return check_commit(obj, sha1, 0);
175785e5
DB
71}
72
a6fa5992
PN
73struct commit *lookup_commit_reference_by_name(const char *name)
74{
75 unsigned char sha1[20];
76 struct commit *commit;
77
cd74e473 78 if (get_sha1_committish(name, sha1))
a6fa5992
PN
79 return NULL;
80 commit = lookup_commit_reference(sha1);
81 if (!commit || parse_commit(commit))
82 return NULL;
83 return commit;
84}
85
0a617799 86static unsigned long parse_commit_date(const char *buf, const char *tail)
175785e5 87{
0a617799 88 const char *dateptr;
175785e5 89
0a617799
MK
90 if (buf + 6 >= tail)
91 return 0;
175785e5
DB
92 if (memcmp(buf, "author", 6))
93 return 0;
0a617799 94 while (buf < tail && *buf++ != '\n')
175785e5 95 /* nada */;
0a617799
MK
96 if (buf + 9 >= tail)
97 return 0;
175785e5
DB
98 if (memcmp(buf, "committer", 9))
99 return 0;
0a617799 100 while (buf < tail && *buf++ != '>')
175785e5 101 /* nada */;
0a617799
MK
102 if (buf >= tail)
103 return 0;
104 dateptr = buf;
105 while (buf < tail && *buf++ != '\n')
106 /* nada */;
107 if (buf >= tail)
108 return 0;
109 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
f2909743 110 return strtoul(dateptr, NULL, 10);
175785e5
DB
111}
112
5040f17e 113static struct commit_graft **commit_graft;
5da5c8f4
JH
114static int commit_graft_alloc, commit_graft_nr;
115
116static int commit_graft_pos(const unsigned char *sha1)
117{
118 int lo, hi;
119 lo = 0;
120 hi = commit_graft_nr;
121 while (lo < hi) {
122 int mi = (lo + hi) / 2;
123 struct commit_graft *graft = commit_graft[mi];
a89fccd2 124 int cmp = hashcmp(sha1, graft->sha1);
5da5c8f4
JH
125 if (!cmp)
126 return mi;
127 if (cmp < 0)
128 hi = mi;
129 else
130 lo = mi + 1;
131 }
132 return -lo - 1;
133}
134
5040f17e
JH
135int register_commit_graft(struct commit_graft *graft, int ignore_dups)
136{
137 int pos = commit_graft_pos(graft->sha1);
a6080a0a 138
5040f17e
JH
139 if (0 <= pos) {
140 if (ignore_dups)
141 free(graft);
142 else {
143 free(commit_graft[pos]);
144 commit_graft[pos] = graft;
145 }
146 return 1;
147 }
148 pos = -pos - 1;
149 if (commit_graft_alloc <= ++commit_graft_nr) {
150 commit_graft_alloc = alloc_nr(commit_graft_alloc);
151 commit_graft = xrealloc(commit_graft,
152 sizeof(*commit_graft) *
153 commit_graft_alloc);
154 }
155 if (pos < commit_graft_nr)
156 memmove(commit_graft + pos + 1,
157 commit_graft + pos,
158 (commit_graft_nr - pos - 1) *
159 sizeof(*commit_graft));
160 commit_graft[pos] = graft;
161 return 0;
162}
163
164struct commit_graft *read_graft_line(char *buf, int len)
165{
166 /* The format is just "Commit Parent1 Parent2 ...\n" */
167 int i;
168 struct commit_graft *graft = NULL;
169
fe0a3cb2
JH
170 while (len && isspace(buf[len-1]))
171 buf[--len] = '\0';
360204c3 172 if (buf[0] == '#' || buf[0] == '\0')
5bc4ce58 173 return NULL;
df5d43be
RT
174 if ((len + 1) % 41)
175 goto bad_graft_data;
5040f17e
JH
176 i = (len + 1) / 41 - 1;
177 graft = xmalloc(sizeof(*graft) + 20 * i);
178 graft->nr_parent = i;
179 if (get_sha1_hex(buf, graft->sha1))
180 goto bad_graft_data;
181 for (i = 40; i < len; i += 41) {
182 if (buf[i] != ' ')
183 goto bad_graft_data;
184 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
185 goto bad_graft_data;
186 }
187 return graft;
df5d43be
RT
188
189bad_graft_data:
190 error("bad graft data: %s", buf);
191 free(graft);
192 return NULL;
5040f17e
JH
193}
194
c5ae6439 195static int read_graft_file(const char *graft_file)
5da5c8f4 196{
5da5c8f4
JH
197 FILE *fp = fopen(graft_file, "r");
198 char buf[1024];
5040f17e
JH
199 if (!fp)
200 return -1;
5da5c8f4
JH
201 while (fgets(buf, sizeof(buf), fp)) {
202 /* The format is just "Commit Parent1 Parent2 ...\n" */
203 int len = strlen(buf);
5040f17e 204 struct commit_graft *graft = read_graft_line(buf, len);
5bc4ce58
JH
205 if (!graft)
206 continue;
5040f17e 207 if (register_commit_graft(graft, 1))
5da5c8f4 208 error("duplicate graft data: %s", buf);
5da5c8f4
JH
209 }
210 fclose(fp);
5040f17e
JH
211 return 0;
212}
213
214static void prepare_commit_graft(void)
215{
216 static int commit_graft_prepared;
217 char *graft_file;
218
219 if (commit_graft_prepared)
220 return;
221 graft_file = get_graft_file();
222 read_graft_file(graft_file);
ed09aef0
JS
223 /* make sure shallows are read */
224 is_repository_shallow();
5040f17e 225 commit_graft_prepared = 1;
5da5c8f4
JH
226}
227
45163382 228struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
5da5c8f4
JH
229{
230 int pos;
5040f17e 231 prepare_commit_graft();
5da5c8f4
JH
232 pos = commit_graft_pos(sha1);
233 if (pos < 0)
234 return NULL;
235 return commit_graft[pos];
236}
237
09d46644 238int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
ed09aef0 239{
09d46644
NTND
240 int i, ret;
241 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
242 ret = fn(commit_graft[i], cb_data);
243 return ret;
ed09aef0
JS
244}
245
f53514bc
JS
246int unregister_shallow(const unsigned char *sha1)
247{
248 int pos = commit_graft_pos(sha1);
249 if (pos < 0)
250 return -1;
251 if (pos + 1 < commit_graft_nr)
65d41d48 252 memmove(commit_graft + pos, commit_graft + pos + 1,
f53514bc
JS
253 sizeof(struct commit_graft *)
254 * (commit_graft_nr - pos - 1));
255 commit_graft_nr--;
256 return 0;
257}
258
cf7b1cad 259int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
175785e5 260{
cf7b1cad
NTND
261 const char *tail = buffer;
262 const char *bufptr = buffer;
175785e5 263 unsigned char parent[20];
6c88be16 264 struct commit_list **pptr;
5da5c8f4 265 struct commit_graft *graft;
bd2c39f5 266
175785e5
DB
267 if (item->object.parsed)
268 return 0;
269 item->object.parsed = 1;
3b44f15a 270 tail += size;
0a617799 271 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
f7cc77d7 272 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
0a617799 273 if (get_sha1_hex(bufptr + 5, parent) < 0)
bd2afde8
JH
274 return error("bad tree pointer in commit %s",
275 sha1_to_hex(item->object.sha1));
175785e5 276 item->tree = lookup_tree(parent);
175785e5 277 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
6c88be16 278 pptr = &item->parents;
5da5c8f4
JH
279
280 graft = lookup_commit_graft(item->object.sha1);
3b44f15a 281 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
f7cc77d7
LT
282 struct commit *new_parent;
283
3b44f15a
JH
284 if (tail <= bufptr + 48 ||
285 get_sha1_hex(bufptr + 7, parent) ||
286 bufptr[47] != '\n')
f7cc77d7 287 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
5da5c8f4 288 bufptr += 48;
7f3140cd
JS
289 /*
290 * The clone is shallow if nr_parent < 0, and we must
291 * not traverse its real parents even when we unhide them.
292 */
293 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
5da5c8f4 294 continue;
f7cc77d7 295 new_parent = lookup_commit(parent);
39468def 296 if (new_parent)
6c88be16 297 pptr = &commit_list_insert(new_parent, pptr)->next;
5da5c8f4
JH
298 }
299 if (graft) {
300 int i;
301 struct commit *new_parent;
302 for (i = 0; i < graft->nr_parent; i++) {
303 new_parent = lookup_commit(graft->parent[i]);
304 if (!new_parent)
305 continue;
306 pptr = &commit_list_insert(new_parent, pptr)->next;
5da5c8f4 307 }
175785e5 308 }
0a617799 309 item->date = parse_commit_date(bufptr, tail);
27dedf0c 310
175785e5
DB
311 return 0;
312}
313
bd2c39f5
NP
314int parse_commit(struct commit *item)
315{
21666f1a 316 enum object_type type;
bd2c39f5
NP
317 void *buffer;
318 unsigned long size;
319 int ret;
320
9786f68b
MK
321 if (!item)
322 return -1;
bd2c39f5
NP
323 if (item->object.parsed)
324 return 0;
21666f1a 325 buffer = read_sha1_file(item->object.sha1, &type, &size);
bd2c39f5
NP
326 if (!buffer)
327 return error("Could not read %s",
328 sha1_to_hex(item->object.sha1));
21666f1a 329 if (type != OBJ_COMMIT) {
bd2c39f5
NP
330 free(buffer);
331 return error("Object %s not a commit",
332 sha1_to_hex(item->object.sha1));
333 }
334 ret = parse_commit_buffer(item, buffer, size);
60ab26de 335 if (save_commit_buffer && !ret) {
3ff1fbbb
LT
336 item->buffer = buffer;
337 return 0;
338 }
bd2c39f5
NP
339 free(buffer);
340 return ret;
341}
342
11af2aae
CC
343int find_commit_subject(const char *commit_buffer, const char **subject)
344{
345 const char *eol;
346 const char *p = commit_buffer;
347
348 while (*p && (*p != '\n' || p[1] != '\n'))
349 p++;
350 if (*p) {
351 p += 2;
352 for (eol = p; *eol && *eol != '\n'; eol++)
353 ; /* do nothing */
354 } else
355 eol = p;
356
357 *subject = p;
358
359 return eol - p;
360}
361
ac5155ef 362struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
dd97f850 363{
812666c8 364 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
dd97f850
DB
365 new_list->item = item;
366 new_list->next = *list_p;
367 *list_p = new_list;
ac5155ef 368 return new_list;
dd97f850
DB
369}
370
65319475
MV
371unsigned commit_list_count(const struct commit_list *l)
372{
373 unsigned c = 0;
374 for (; l; l = l->next )
375 c++;
376 return c;
377}
378
175785e5
DB
379void free_commit_list(struct commit_list *list)
380{
381 while (list) {
382 struct commit_list *temp = list;
383 list = temp->next;
384 free(temp);
385 }
386}
dd97f850 387
47e44ed1 388struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
dd97f850
DB
389{
390 struct commit_list **pp = list;
391 struct commit_list *p;
392 while ((p = *pp) != NULL) {
393 if (p->item->date < item->date) {
394 break;
395 }
396 pp = &p->next;
397 }
f755494c 398 return commit_list_insert(item, pp);
dd97f850
DB
399}
400
46905893
RS
401static int commit_list_compare_by_date(const void *a, const void *b)
402{
403 unsigned long a_date = ((const struct commit_list *)a)->item->date;
404 unsigned long b_date = ((const struct commit_list *)b)->item->date;
405 if (a_date < b_date)
406 return 1;
407 if (a_date > b_date)
408 return -1;
409 return 0;
410}
411
412static void *commit_list_get_next(const void *a)
413{
414 return ((const struct commit_list *)a)->next;
415}
416
417static void commit_list_set_next(void *a, void *next)
418{
419 ((struct commit_list *)a)->next = next;
420}
a6080a0a 421
47e44ed1 422void commit_list_sort_by_date(struct commit_list **list)
dd97f850 423{
7365c95d
JH
424 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
425 commit_list_compare_by_date);
dd97f850
DB
426}
427
58e28af6
DB
428struct commit *pop_most_recent_commit(struct commit_list **list,
429 unsigned int mark)
dd97f850
DB
430{
431 struct commit *ret = (*list)->item;
432 struct commit_list *parents = ret->parents;
433 struct commit_list *old = *list;
434
435 *list = (*list)->next;
436 free(old);
437
438 while (parents) {
4056c091 439 struct commit *commit = parents->item;
dec38c81 440 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
58e28af6 441 commit->object.flags |= mark;
47e44ed1 442 commit_list_insert_by_date(commit, list);
4056c091 443 }
dd97f850
DB
444 parents = parents->next;
445 }
446 return ret;
447}
e3bc7a3b 448
941ba8db
NTND
449static void clear_commit_marks_1(struct commit_list **plist,
450 struct commit *commit, unsigned int mark)
f8f9c73c 451{
60fcc2e6
JS
452 while (commit) {
453 struct commit_list *parents;
f8f9c73c 454
60fcc2e6
JS
455 if (!(mark & commit->object.flags))
456 return;
58ecf5c1 457
60fcc2e6
JS
458 commit->object.flags &= ~mark;
459
460 parents = commit->parents;
461 if (!parents)
462 return;
463
464 while ((parents = parents->next))
941ba8db 465 commit_list_insert(parents->item, plist);
60fcc2e6
JS
466
467 commit = commit->parents->item;
f8f9c73c
JH
468 }
469}
470
941ba8db
NTND
471void clear_commit_marks(struct commit *commit, unsigned int mark)
472{
473 struct commit_list *list = NULL;
474 commit_list_insert(commit, &list);
475 while (list)
476 clear_commit_marks_1(&list, pop_commit(&list), mark);
477}
478
86a0a408
RS
479void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
480{
481 struct object *object;
482 struct commit *commit;
483 unsigned int i;
484
485 for (i = 0; i < a->nr; i++) {
486 object = a->objects[i].item;
487 commit = lookup_commit_reference_gently(object->sha1, 1);
488 if (commit)
489 clear_commit_marks(commit, mark);
490 }
491}
492
a3437b8c
JS
493struct commit *pop_commit(struct commit_list **stack)
494{
495 struct commit_list *top = *stack;
496 struct commit *item = top ? top->item : NULL;
497
498 if (top) {
499 *stack = top->next;
500 free(top);
501 }
502 return item;
503}
504
a84b794a
JH
505/*
506 * Topological sort support
507 */
96c4f4a3 508
a84b794a
JH
509/* count number of children that have not been emitted */
510define_commit_slab(indegree_slab, int);
96c4f4a3 511
ab580ace
JS
512/*
513 * Performs an in-place topological sort on the list supplied.
514 */
4c8725f1 515void sort_in_topological_order(struct commit_list ** list, int lifo)
6b6dcfc2 516{
23c17d4a
LT
517 struct commit_list *next, *orig = *list;
518 struct commit_list *work, **insert;
519 struct commit_list **pptr;
a84b794a 520 struct indegree_slab indegree;
a6080a0a 521
23c17d4a 522 if (!orig)
7d6fb370 523 return;
23c17d4a
LT
524 *list = NULL;
525
a84b794a 526 init_indegree_slab(&indegree);
96c4f4a3 527
23c17d4a
LT
528 /* Mark them and clear the indegree */
529 for (next = orig; next; next = next->next) {
530 struct commit *commit = next->item;
a84b794a 531 *(indegree_slab_at(&indegree, commit)) = 1;
ab580ace 532 }
23c17d4a 533
ab580ace 534 /* update the indegree */
23c17d4a 535 for (next = orig; next; next = next->next) {
ab580ace
JS
536 struct commit_list * parents = next->item->parents;
537 while (parents) {
23c17d4a 538 struct commit *parent = parents->item;
a84b794a 539 int *pi = indegree_slab_at(&indegree, parent);
6b6dcfc2 540
96c4f4a3
JK
541 if (*pi)
542 (*pi)++;
23c17d4a 543 parents = parents->next;
ab580ace 544 }
ab580ace 545 }
23c17d4a 546
a6080a0a 547 /*
674d1727
PH
548 * find the tips
549 *
550 * tips are nodes not reachable from any other node in the list
551 *
552 * the tips serve as a starting set for the work queue.
553 */
23c17d4a 554 work = NULL;
2ed02887 555 insert = &work;
23c17d4a
LT
556 for (next = orig; next; next = next->next) {
557 struct commit *commit = next->item;
ab580ace 558
a84b794a 559 if (*(indegree_slab_at(&indegree, commit)) == 1)
23c17d4a 560 insert = &commit_list_insert(commit, insert)->next;
ab580ace 561 }
4c8725f1 562
ab580ace 563 /* process the list in topological order */
4c8725f1 564 if (!lifo)
47e44ed1 565 commit_list_sort_by_date(&work);
23c17d4a
LT
566
567 pptr = list;
568 *list = NULL;
ab580ace 569 while (work) {
23c17d4a
LT
570 struct commit *commit;
571 struct commit_list *parents, *work_item;
ab580ace 572
23c17d4a
LT
573 work_item = work;
574 work = work_item->next;
575 work_item->next = NULL;
576
577 commit = work_item->item;
578 for (parents = commit->parents; parents ; parents = parents->next) {
cd2b8ae9 579 struct commit *parent = parents->item;
a84b794a 580 int *pi = indegree_slab_at(&indegree, parent);
23c17d4a 581
96c4f4a3 582 if (!*pi)
23c17d4a
LT
583 continue;
584
585 /*
586 * parents are only enqueued for emission
587 * when all their children have been emitted thereby
588 * guaranteeing topological order.
589 */
96c4f4a3 590 if (--(*pi) == 1) {
23c17d4a 591 if (!lifo)
47e44ed1 592 commit_list_insert_by_date(parent, &work);
23c17d4a
LT
593 else
594 commit_list_insert(parent, &work);
ab580ace 595 }
ab580ace
JS
596 }
597 /*
674d1727
PH
598 * work_item is a commit all of whose children
599 * have already been emitted. we can emit it now.
600 */
a84b794a 601 *(indegree_slab_at(&indegree, commit)) = 0;
23c17d4a
LT
602 *pptr = work_item;
603 pptr = &work_item->next;
ab580ace 604 }
96c4f4a3 605
a84b794a 606 clear_indegree_slab(&indegree);
ab580ace 607}
7c6f8aaf 608
1295228d 609/* merge-base stuff */
7c6f8aaf 610
577ed5c2
JH
611/* bits #0..15 in revision.h */
612#define PARENT1 (1u<<16)
613#define PARENT2 (1u<<17)
614#define STALE (1u<<18)
615#define RESULT (1u<<19)
7c6f8aaf 616
1295228d
JH
617static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
618
7c6f8aaf
JS
619static struct commit *interesting(struct commit_list *list)
620{
621 while (list) {
622 struct commit *commit = list->item;
623 list = list->next;
542ccefe 624 if (commit->object.flags & STALE)
7c6f8aaf
JS
625 continue;
626 return commit;
627 }
628 return NULL;
629}
630
d866924a 631/* all input commits in one and twos[] must have been parsed! */
da1f5156 632static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
7c6f8aaf
JS
633{
634 struct commit_list *list = NULL;
635 struct commit_list *result = NULL;
6a938648 636 int i;
7c6f8aaf 637
f3249438 638 one->object.flags |= PARENT1;
47e44ed1 639 commit_list_insert_by_date(one, &list);
d866924a
JH
640 if (!n)
641 return list;
6a938648
JH
642 for (i = 0; i < n; i++) {
643 twos[i]->object.flags |= PARENT2;
47e44ed1 644 commit_list_insert_by_date(twos[i], &list);
6a938648 645 }
7c6f8aaf
JS
646
647 while (interesting(list)) {
f3249438 648 struct commit *commit;
7c6f8aaf 649 struct commit_list *parents;
f203d696 650 struct commit_list *next;
f3249438 651 int flags;
7c6f8aaf 652
f3249438 653 commit = list->item;
f203d696 654 next = list->next;
f3249438 655 free(list);
f203d696 656 list = next;
7c6f8aaf 657
f3249438
JH
658 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
659 if (flags == (PARENT1 | PARENT2)) {
660 if (!(commit->object.flags & RESULT)) {
661 commit->object.flags |= RESULT;
47e44ed1 662 commit_list_insert_by_date(commit, &result);
f3249438 663 }
542ccefe
JH
664 /* Mark parents of a found merge stale */
665 flags |= STALE;
7c6f8aaf
JS
666 }
667 parents = commit->parents;
668 while (parents) {
669 struct commit *p = parents->item;
670 parents = parents->next;
671 if ((p->object.flags & flags) == flags)
672 continue;
172947e6
MK
673 if (parse_commit(p))
674 return NULL;
7c6f8aaf 675 p->object.flags |= flags;
47e44ed1 676 commit_list_insert_by_date(p, &list);
7c6f8aaf
JS
677 }
678 }
679
1295228d 680 free_commit_list(list);
da1f5156
JH
681 return result;
682}
683
684static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
685{
686 struct commit_list *list = NULL;
687 struct commit_list *result = NULL;
688 int i;
689
690 for (i = 0; i < n; i++) {
691 if (one == twos[i])
692 /*
693 * We do not mark this even with RESULT so we do not
694 * have to clean it up.
695 */
696 return commit_list_insert(one, &result);
697 }
698
699 if (parse_commit(one))
700 return NULL;
701 for (i = 0; i < n; i++) {
702 if (parse_commit(twos[i]))
703 return NULL;
704 }
705
706 list = paint_down_to_common(one, n, twos);
707
f3249438 708 while (list) {
f203d696 709 struct commit_list *next = list->next;
f3249438 710 if (!(list->item->object.flags & STALE))
47e44ed1 711 commit_list_insert_by_date(list->item, &result);
f3249438 712 free(list);
f203d696 713 list = next;
f3249438
JH
714 }
715 return result;
716}
7c6f8aaf 717
5240c9d7
MV
718struct commit_list *get_octopus_merge_bases(struct commit_list *in)
719{
720 struct commit_list *i, *j, *k, *ret = NULL;
721 struct commit_list **pptr = &ret;
722
723 for (i = in; i; i = i->next) {
724 if (!ret)
725 pptr = &commit_list_insert(i->item, pptr)->next;
726 else {
727 struct commit_list *new = NULL, *end = NULL;
728
729 for (j = ret; j; j = j->next) {
730 struct commit_list *bases;
731 bases = get_merge_bases(i->item, j->item, 1);
732 if (!new)
733 new = bases;
734 else
735 end->next = bases;
736 for (k = bases; k; k = k->next)
737 end = k;
738 }
739 ret = new;
740 }
741 }
742 return ret;
743}
744
94f0ced0
JH
745static int remove_redundant(struct commit **array, int cnt)
746{
747 /*
748 * Some commit in the array may be an ancestor of
749 * another commit. Move such commit to the end of
750 * the array, and return the number of commits that
751 * are independent from each other.
752 */
753 struct commit **work;
754 unsigned char *redundant;
755 int *filled_index;
756 int i, j, filled;
757
758 work = xcalloc(cnt, sizeof(*work));
759 redundant = xcalloc(cnt, 1);
760 filled_index = xmalloc(sizeof(*filled_index) * (cnt - 1));
761
d866924a
JH
762 for (i = 0; i < cnt; i++)
763 parse_commit(array[i]);
94f0ced0
JH
764 for (i = 0; i < cnt; i++) {
765 struct commit_list *common;
766
767 if (redundant[i])
768 continue;
769 for (j = filled = 0; j < cnt; j++) {
770 if (i == j || redundant[j])
771 continue;
772 filled_index[filled] = j;
773 work[filled++] = array[j];
774 }
775 common = paint_down_to_common(array[i], filled, work);
776 if (array[i]->object.flags & PARENT2)
777 redundant[i] = 1;
778 for (j = 0; j < filled; j++)
779 if (work[j]->object.flags & PARENT1)
780 redundant[filled_index[j]] = 1;
781 clear_commit_marks(array[i], all_flags);
782 for (j = 0; j < filled; j++)
783 clear_commit_marks(work[j], all_flags);
784 free_commit_list(common);
785 }
786
787 /* Now collect the result */
788 memcpy(work, array, sizeof(*array) * cnt);
789 for (i = filled = 0; i < cnt; i++)
790 if (!redundant[i])
791 array[filled++] = work[i];
792 for (j = filled, i = 0; i < cnt; i++)
793 if (redundant[i])
794 array[j++] = work[i];
795 free(work);
796 free(redundant);
797 free(filled_index);
798 return filled;
799}
800
6a938648
JH
801struct commit_list *get_merge_bases_many(struct commit *one,
802 int n,
803 struct commit **twos,
804 int cleanup)
f3249438 805{
f3249438
JH
806 struct commit_list *list;
807 struct commit **rslt;
808 struct commit_list *result;
94f0ced0 809 int cnt, i;
f3249438 810
6a938648
JH
811 result = merge_bases_many(one, n, twos);
812 for (i = 0; i < n; i++) {
813 if (one == twos[i])
814 return result;
815 }
f3249438
JH
816 if (!result || !result->next) {
817 if (cleanup) {
818 clear_commit_marks(one, all_flags);
6a938648
JH
819 for (i = 0; i < n; i++)
820 clear_commit_marks(twos[i], all_flags);
7c6f8aaf 821 }
f3249438 822 return result;
7c6f8aaf
JS
823 }
824
f3249438
JH
825 /* There are more than one */
826 cnt = 0;
827 list = result;
828 while (list) {
829 list = list->next;
830 cnt++;
831 }
832 rslt = xcalloc(cnt, sizeof(*rslt));
833 for (list = result, i = 0; list; list = list->next)
834 rslt[i++] = list->item;
835 free_commit_list(result);
836
837 clear_commit_marks(one, all_flags);
6a938648
JH
838 for (i = 0; i < n; i++)
839 clear_commit_marks(twos[i], all_flags);
7c6f8aaf 840
94f0ced0 841 cnt = remove_redundant(rslt, cnt);
f3249438 842 result = NULL;
94f0ced0
JH
843 for (i = 0; i < cnt; i++)
844 commit_list_insert_by_date(rslt[i], &result);
f3249438 845 free(rslt);
7c6f8aaf
JS
846 return result;
847}
2ecd2bbc 848
6a938648
JH
849struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
850 int cleanup)
851{
852 return get_merge_bases_many(one, 1, &two, cleanup);
853}
854
a20efee9 855/*
41ccfdd9 856 * Is "commit" a descendant of one of the elements on the "with_commit" list?
a20efee9 857 */
7fcdb36e
JG
858int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
859{
860 if (!with_commit)
861 return 1;
862 while (with_commit) {
863 struct commit *other;
864
865 other = with_commit->item;
866 with_commit = with_commit->next;
a20efee9 867 if (in_merge_bases(other, commit))
7fcdb36e
JG
868 return 1;
869 }
870 return 0;
871}
872
a20efee9
JH
873/*
874 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
875 */
876int in_merge_bases(struct commit *commit, struct commit *reference)
2ecd2bbc 877{
6440fdba 878 struct commit_list *bases;
2ecd2bbc
JH
879 int ret = 0;
880
6440fdba
JH
881 if (parse_commit(commit) || parse_commit(reference))
882 return ret;
2ecd2bbc 883
6440fdba
JH
884 bases = paint_down_to_common(commit, 1, &reference);
885 if (commit->object.flags & PARENT2)
886 ret = 1;
b0f9e9ee
TR
887 clear_commit_marks(commit, all_flags);
888 clear_commit_marks(reference, all_flags);
2ecd2bbc
JH
889 free_commit_list(bases);
890 return ret;
891}
98cf9c3b
JH
892
893struct commit_list *reduce_heads(struct commit_list *heads)
894{
895 struct commit_list *p;
896 struct commit_list *result = NULL, **tail = &result;
f37d3c75
JH
897 struct commit **array;
898 int num_head, i;
98cf9c3b
JH
899
900 if (!heads)
901 return NULL;
902
f37d3c75
JH
903 /* Uniquify */
904 for (p = heads; p; p = p->next)
905 p->item->object.flags &= ~STALE;
906 for (p = heads, num_head = 0; p; p = p->next) {
907 if (p->item->object.flags & STALE)
711f6b29 908 continue;
f37d3c75
JH
909 p->item->object.flags |= STALE;
910 num_head++;
911 }
912 array = xcalloc(sizeof(*array), num_head);
913 for (p = heads, i = 0; p; p = p->next) {
914 if (p->item->object.flags & STALE) {
915 array[i++] = p->item;
916 p->item->object.flags &= ~STALE;
98cf9c3b 917 }
98cf9c3b 918 }
f37d3c75
JH
919 num_head = remove_redundant(array, num_head);
920 for (i = 0; i < num_head; i++)
921 tail = &commit_list_insert(array[i], tail)->next;
98cf9c3b
JH
922 return result;
923}
40d52ff7 924
ba3c69a9
JH
925static const char gpg_sig_header[] = "gpgsig";
926static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
927
928static int do_sign_commit(struct strbuf *buf, const char *keyid)
929{
930 struct strbuf sig = STRBUF_INIT;
931 int inspos, copypos;
932
933 /* find the end of the header */
934 inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
935
936 if (!keyid || !*keyid)
937 keyid = get_signing_key();
938 if (sign_buffer(buf, &sig, keyid)) {
939 strbuf_release(&sig);
940 return -1;
941 }
942
943 for (copypos = 0; sig.buf[copypos]; ) {
944 const char *bol = sig.buf + copypos;
945 const char *eol = strchrnul(bol, '\n');
946 int len = (eol - bol) + !!*eol;
947
948 if (!copypos) {
949 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
950 inspos += gpg_sig_header_len;
951 }
952 strbuf_insert(buf, inspos++, " ", 1);
953 strbuf_insert(buf, inspos, bol, len);
954 inspos += len;
955 copypos += len;
956 }
957 strbuf_release(&sig);
958 return 0;
959}
960
0c37f1fc
JH
961int parse_signed_commit(const unsigned char *sha1,
962 struct strbuf *payload, struct strbuf *signature)
963{
964 unsigned long size;
965 enum object_type type;
966 char *buffer = read_sha1_file(sha1, &type, &size);
967 int in_signature, saw_signature = -1;
968 char *line, *tail;
969
970 if (!buffer || type != OBJ_COMMIT)
971 goto cleanup;
972
973 line = buffer;
974 tail = buffer + size;
975 in_signature = 0;
976 saw_signature = 0;
977 while (line < tail) {
978 const char *sig = NULL;
979 char *next = memchr(line, '\n', tail - line);
980
981 next = next ? next + 1 : tail;
982 if (in_signature && line[0] == ' ')
983 sig = line + 1;
984 else if (!prefixcmp(line, gpg_sig_header) &&
985 line[gpg_sig_header_len] == ' ')
986 sig = line + gpg_sig_header_len + 1;
987 if (sig) {
988 strbuf_add(signature, sig, next - sig);
989 saw_signature = 1;
990 in_signature = 1;
991 } else {
992 if (*line == '\n')
993 /* dump the whole remainder of the buffer */
994 next = tail;
995 strbuf_add(payload, line, next - line);
996 in_signature = 0;
997 }
998 line = next;
999 }
1000 cleanup:
1001 free(buffer);
1002 return saw_signature;
1003}
1004
5231c633
JH
1005static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1006{
1007 struct merge_remote_desc *desc;
1008 struct commit_extra_header *mergetag;
1009 char *buf;
1010 unsigned long size, len;
1011 enum object_type type;
1012
1013 desc = merge_remote_util(parent);
1014 if (!desc || !desc->obj)
1015 return;
1016 buf = read_sha1_file(desc->obj->sha1, &type, &size);
1017 if (!buf || type != OBJ_TAG)
1018 goto free_return;
1019 len = parse_signature(buf, size);
1020 if (size == len)
1021 goto free_return;
1022 /*
1023 * We could verify this signature and either omit the tag when
1024 * it does not validate, but the integrator may not have the
1025 * public key of the signer of the tag he is merging, while a
1026 * later auditor may have it while auditing, so let's not run
1027 * verify-signed-buffer here for now...
1028 *
1029 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1030 * warn("warning: signed tag unverified.");
1031 */
1032 mergetag = xcalloc(1, sizeof(*mergetag));
1033 mergetag->key = xstrdup("mergetag");
1034 mergetag->value = buf;
1035 mergetag->len = size;
1036
1037 **tail = mergetag;
1038 *tail = &mergetag->next;
1039 return;
1040
1041free_return:
1042 free(buf);
1043}
1044
1045void append_merge_tag_headers(struct commit_list *parents,
1046 struct commit_extra_header ***tail)
1047{
1048 while (parents) {
1049 struct commit *parent = parents->item;
1050 handle_signed_tag(parent, tail);
1051 parents = parents->next;
1052 }
1053}
1054
1055static void add_extra_header(struct strbuf *buffer,
1056 struct commit_extra_header *extra)
1057{
1058 strbuf_addstr(buffer, extra->key);
ed7a42a0
JH
1059 if (extra->len)
1060 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1061 else
1062 strbuf_addch(buffer, '\n');
1063}
1064
c871a1d1
JH
1065struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1066 const char **exclude)
ed7a42a0
JH
1067{
1068 struct commit_extra_header *extra = NULL;
1069 unsigned long size;
1070 enum object_type type;
1071 char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
1072 if (buffer && type == OBJ_COMMIT)
c871a1d1 1073 extra = read_commit_extra_header_lines(buffer, size, exclude);
ed7a42a0
JH
1074 free(buffer);
1075 return extra;
1076}
1077
1078static inline int standard_header_field(const char *field, size_t len)
1079{
1080 return ((len == 4 && !memcmp(field, "tree ", 5)) ||
1081 (len == 6 && !memcmp(field, "parent ", 7)) ||
1082 (len == 6 && !memcmp(field, "author ", 7)) ||
1083 (len == 9 && !memcmp(field, "committer ", 10)) ||
1084 (len == 8 && !memcmp(field, "encoding ", 9)));
1085}
1086
c871a1d1
JH
1087static int excluded_header_field(const char *field, size_t len, const char **exclude)
1088{
1089 if (!exclude)
1090 return 0;
1091
1092 while (*exclude) {
1093 size_t xlen = strlen(*exclude);
1094 if (len == xlen &&
1095 !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
1096 return 1;
1097 exclude++;
1098 }
1099 return 0;
1100}
1101
82a75299
JH
1102static struct commit_extra_header *read_commit_extra_header_lines(
1103 const char *buffer, size_t size,
1104 const char **exclude)
ed7a42a0
JH
1105{
1106 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1107 const char *line, *next, *eof, *eob;
1108 struct strbuf buf = STRBUF_INIT;
1109
1110 for (line = buffer, eob = line + size;
1111 line < eob && *line != '\n';
1112 line = next) {
1113 next = memchr(line, '\n', eob - line);
1114 next = next ? next + 1 : eob;
1115 if (*line == ' ') {
1116 /* continuation */
1117 if (it)
1118 strbuf_add(&buf, line + 1, next - (line + 1));
1119 continue;
1120 }
1121 if (it)
1122 it->value = strbuf_detach(&buf, &it->len);
1123 strbuf_reset(&buf);
1124 it = NULL;
1125
1126 eof = strchr(line, ' ');
1127 if (next <= eof)
1128 eof = next;
1129
c871a1d1
JH
1130 if (standard_header_field(line, eof - line) ||
1131 excluded_header_field(line, eof - line, exclude))
ed7a42a0
JH
1132 continue;
1133
1134 it = xcalloc(1, sizeof(*it));
1135 it->key = xmemdupz(line, eof-line);
1136 *tail = it;
1137 tail = &it->next;
1138 if (eof + 1 < next)
1139 strbuf_add(&buf, eof + 1, next - (eof + 1));
1140 }
1141 if (it)
1142 it->value = strbuf_detach(&buf, &it->len);
1143 return extra;
5231c633
JH
1144}
1145
1146void free_commit_extra_headers(struct commit_extra_header *extra)
1147{
1148 while (extra) {
1149 struct commit_extra_header *next = extra->next;
1150 free(extra->key);
1151 free(extra->value);
1152 free(extra);
1153 extra = next;
1154 }
1155}
1156
f35ccd9b 1157int commit_tree(const struct strbuf *msg, unsigned char *tree,
5231c633 1158 struct commit_list *parents, unsigned char *ret,
ba3c69a9 1159 const char *author, const char *sign_commit)
5231c633
JH
1160{
1161 struct commit_extra_header *extra = NULL, **tail = &extra;
1162 int result;
1163
1164 append_merge_tag_headers(parents, &tail);
ba3c69a9
JH
1165 result = commit_tree_extended(msg, tree, parents, ret,
1166 author, sign_commit, extra);
5231c633
JH
1167 free_commit_extra_headers(extra);
1168 return result;
1169}
1170
08a94a14
LT
1171static int find_invalid_utf8(const char *buf, int len)
1172{
1173 int offset = 0;
1174
1175 while (len) {
1176 unsigned char c = *buf++;
1177 int bytes, bad_offset;
1178
1179 len--;
1180 offset++;
1181
1182 /* Simple US-ASCII? No worries. */
1183 if (c < 0x80)
1184 continue;
1185
1186 bad_offset = offset-1;
1187
1188 /*
1189 * Count how many more high bits set: that's how
1190 * many more bytes this sequence should have.
1191 */
1192 bytes = 0;
1193 while (c & 0x40) {
1194 c <<= 1;
1195 bytes++;
1196 }
1197
1198 /* Must be between 1 and 5 more bytes */
1199 if (bytes < 1 || bytes > 5)
1200 return bad_offset;
1201
1202 /* Do we *have* that many bytes? */
1203 if (len < bytes)
1204 return bad_offset;
1205
1206 offset += bytes;
1207 len -= bytes;
1208
1209 /* And verify that they are good continuation bytes */
1210 do {
1211 if ((*buf++ & 0xc0) != 0x80)
1212 return bad_offset;
1213 } while (--bytes);
1214
1215 /* We could/should check the value and length here too */
1216 }
1217 return -1;
1218}
1219
1220/*
1221 * This verifies that the buffer is in proper utf8 format.
1222 *
1223 * If it isn't, it assumes any non-utf8 characters are Latin1,
1224 * and does the conversion.
1225 *
1226 * Fixme: we should probably also disallow overlong forms and
1227 * invalid characters. But we don't do that currently.
1228 */
1229static int verify_utf8(struct strbuf *buf)
1230{
1231 int ok = 1;
1232 long pos = 0;
1233
1234 for (;;) {
1235 int bad;
1236 unsigned char c;
1237 unsigned char replace[2];
1238
1239 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1240 if (bad < 0)
1241 return ok;
1242 pos += bad;
1243 ok = 0;
1244 c = buf->buf[pos];
1245 strbuf_remove(buf, pos, 1);
1246
1247 /* We know 'c' must be in the range 128-255 */
1248 replace[0] = 0xc0 + (c >> 6);
1249 replace[1] = 0x80 + (c & 0x3f);
1250 strbuf_insert(buf, pos, replace, 2);
1251 pos += 2;
1252 }
1253}
1254
40d52ff7 1255static const char commit_utf8_warn[] =
08a94a14 1256"Warning: commit message did not conform to UTF-8.\n"
40d52ff7
JK
1257"You may want to amend it after fixing the message, or set the config\n"
1258"variable i18n.commitencoding to the encoding your project uses.\n";
1259
f35ccd9b 1260int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
5231c633 1261 struct commit_list *parents, unsigned char *ret,
ba3c69a9
JH
1262 const char *author, const char *sign_commit,
1263 struct commit_extra_header *extra)
40d52ff7
JK
1264{
1265 int result;
1266 int encoding_is_utf8;
1267 struct strbuf buffer;
1268
1269 assert_sha1_type(tree, OBJ_TREE);
1270
37576c14
NTND
1271 if (memchr(msg->buf, '\0', msg->len))
1272 return error("a NUL byte in commit log message not allowed.");
1273
40d52ff7
JK
1274 /* Not having i18n.commitencoding is the same as having utf-8 */
1275 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1276
1277 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1278 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1279
1280 /*
1281 * NOTE! This ordering means that the same exact tree merged with a
1282 * different order of parents will be a _different_ changeset even
1283 * if everything else stays the same.
1284 */
1285 while (parents) {
1286 struct commit_list *next = parents->next;
5231c633
JH
1287 struct commit *parent = parents->item;
1288
40d52ff7 1289 strbuf_addf(&buffer, "parent %s\n",
5231c633 1290 sha1_to_hex(parent->object.sha1));
40d52ff7
JK
1291 free(parents);
1292 parents = next;
1293 }
1294
1295 /* Person/date information */
1296 if (!author)
f9bc573f 1297 author = git_author_info(IDENT_STRICT);
40d52ff7 1298 strbuf_addf(&buffer, "author %s\n", author);
f9bc573f 1299 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
40d52ff7
JK
1300 if (!encoding_is_utf8)
1301 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
5231c633
JH
1302
1303 while (extra) {
1304 add_extra_header(&buffer, extra);
1305 extra = extra->next;
1306 }
40d52ff7
JK
1307 strbuf_addch(&buffer, '\n');
1308
1309 /* And add the comment */
13f8b72d 1310 strbuf_addbuf(&buffer, msg);
40d52ff7
JK
1311
1312 /* And check the encoding */
08a94a14 1313 if (encoding_is_utf8 && !verify_utf8(&buffer))
40d52ff7
JK
1314 fprintf(stderr, commit_utf8_warn);
1315
ba3c69a9
JH
1316 if (sign_commit && do_sign_commit(&buffer, sign_commit))
1317 return -1;
1318
40d52ff7
JK
1319 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1320 strbuf_release(&buffer);
1321 return result;
1322}
ae8e4c9c
JH
1323
1324struct commit *get_merge_parent(const char *name)
1325{
1326 struct object *obj;
1327 struct commit *commit;
1328 unsigned char sha1[20];
1329 if (get_sha1(name, sha1))
1330 return NULL;
1331 obj = parse_object(sha1);
1332 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1333 if (commit && !commit->util) {
1334 struct merge_remote_desc *desc;
1335 desc = xmalloc(sizeof(*desc));
1336 desc->obj = obj;
1337 desc->name = strdup(name);
1338 commit->util = desc;
1339 }
1340 return commit;
1341}
89b5f1d9
RS
1342
1343/*
1344 * Append a commit to the end of the commit_list.
1345 *
1346 * next starts by pointing to the variable that holds the head of an
1347 * empty commit_list, and is updated to point to the "next" field of
1348 * the last item on the list as new commits are appended.
1349 *
1350 * Usage example:
1351 *
1352 * struct commit_list *list;
1353 * struct commit_list **next = &list;
1354 *
1355 * next = commit_list_append(c1, next);
1356 * next = commit_list_append(c2, next);
1357 * assert(commit_list_count(list) == 2);
1358 * return list;
1359 */
1360struct commit_list **commit_list_append(struct commit *commit,
1361 struct commit_list **next)
1362{
1363 struct commit_list *new = xmalloc(sizeof(struct commit_list));
1364 new->item = commit;
1365 *next = new;
1366 new->next = NULL;
1367 return &new->next;
1368}
efc7df45
NTND
1369
1370void print_commit_list(struct commit_list *list,
1371 const char *format_cur,
1372 const char *format_last)
1373{
1374 for ( ; list; list = list->next) {
1375 const char *format = list->next ? format_cur : format_last;
1376 printf(format, sha1_to_hex(list->item->object.sha1));
1377 }
1378}