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