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