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