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