]> git.ipfire.org Git - thirdparty/git.git/blame - commit.c
git-fmt-patch: thinkofix to show [PATCH] properly.
[thirdparty/git.git] / commit.c
CommitLineData
8f1d2e6f 1#include "cache.h"
961784ee 2#include "tag.h"
175785e5 3#include "commit.h"
175785e5 4
60ab26de
LT
5int save_commit_buffer = 1;
6
ab580ace
JS
7struct sort_node
8{
9 /*
10 * the number of children of the associated commit
11 * that also occur in the list being sorted.
12 */
13 unsigned int indegree;
14
15 /*
16 * reference to original list item that we will re-use
17 * on output.
18 */
19 struct commit_list * list_item;
20
21};
22
175785e5
DB
23const char *commit_type = "commit";
24
9b66ec04
LT
25enum cmit_fmt get_commit_format(const char *arg)
26{
27 if (!*arg)
28 return CMIT_FMT_DEFAULT;
29 if (!strcmp(arg, "=raw"))
30 return CMIT_FMT_RAW;
31 if (!strcmp(arg, "=medium"))
32 return CMIT_FMT_MEDIUM;
33 if (!strcmp(arg, "=short"))
34 return CMIT_FMT_SHORT;
35 if (!strcmp(arg, "=full"))
36 return CMIT_FMT_FULL;
ff56fe1c
JH
37 if (!strcmp(arg, "=fuller"))
38 return CMIT_FMT_FULLER;
3eefc189
JH
39 if (!strcmp(arg, "=email"))
40 return CMIT_FMT_EMAIL;
d87449c5
JH
41 if (!strcmp(arg, "=oneline"))
42 return CMIT_FMT_ONELINE;
9b66ec04
LT
43 die("invalid --pretty format");
44}
45
f76412ed
JH
46static struct commit *check_commit(struct object *obj,
47 const unsigned char *sha1,
48 int quiet)
961784ee
LT
49{
50 if (obj->type != commit_type) {
f76412ed
JH
51 if (!quiet)
52 error("Object %s is a %s, not a commit",
53 sha1_to_hex(sha1), obj->type);
961784ee
LT
54 return NULL;
55 }
56 return (struct commit *) obj;
57}
58
f76412ed
JH
59struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
60 int quiet)
961784ee 61{
9534f40b 62 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
961784ee
LT
63
64 if (!obj)
65 return NULL;
f76412ed
JH
66 return check_commit(obj, sha1, quiet);
67}
68
69struct commit *lookup_commit_reference(const unsigned char *sha1)
70{
71 return lookup_commit_reference_gently(sha1, 0);
961784ee
LT
72}
73
5d6ccf5c 74struct commit *lookup_commit(const unsigned char *sha1)
175785e5
DB
75{
76 struct object *obj = lookup_object(sha1);
77 if (!obj) {
90321c10 78 struct commit *ret = xcalloc(1, sizeof(struct commit));
175785e5 79 created_object(sha1, &ret->object);
d32987be 80 ret->object.type = commit_type;
175785e5
DB
81 return ret;
82 }
d1af002d
NP
83 if (!obj->type)
84 obj->type = commit_type;
f76412ed 85 return check_commit(obj, sha1, 0);
175785e5
DB
86}
87
88static unsigned long parse_commit_date(const char *buf)
89{
90 unsigned long date;
91
92 if (memcmp(buf, "author", 6))
93 return 0;
94 while (*buf++ != '\n')
95 /* nada */;
96 if (memcmp(buf, "committer", 9))
97 return 0;
98 while (*buf++ != '>')
99 /* nada */;
100 date = strtoul(buf, NULL, 10);
101 if (date == ULONG_MAX)
102 date = 0;
103 return date;
104}
105
5040f17e 106static struct commit_graft **commit_graft;
5da5c8f4
JH
107static int commit_graft_alloc, commit_graft_nr;
108
109static int commit_graft_pos(const unsigned char *sha1)
110{
111 int lo, hi;
112 lo = 0;
113 hi = commit_graft_nr;
114 while (lo < hi) {
115 int mi = (lo + hi) / 2;
116 struct commit_graft *graft = commit_graft[mi];
117 int cmp = memcmp(sha1, graft->sha1, 20);
118 if (!cmp)
119 return mi;
120 if (cmp < 0)
121 hi = mi;
122 else
123 lo = mi + 1;
124 }
125 return -lo - 1;
126}
127
5040f17e
JH
128int register_commit_graft(struct commit_graft *graft, int ignore_dups)
129{
130 int pos = commit_graft_pos(graft->sha1);
131
132 if (0 <= pos) {
133 if (ignore_dups)
134 free(graft);
135 else {
136 free(commit_graft[pos]);
137 commit_graft[pos] = graft;
138 }
139 return 1;
140 }
141 pos = -pos - 1;
142 if (commit_graft_alloc <= ++commit_graft_nr) {
143 commit_graft_alloc = alloc_nr(commit_graft_alloc);
144 commit_graft = xrealloc(commit_graft,
145 sizeof(*commit_graft) *
146 commit_graft_alloc);
147 }
148 if (pos < commit_graft_nr)
149 memmove(commit_graft + pos + 1,
150 commit_graft + pos,
151 (commit_graft_nr - pos - 1) *
152 sizeof(*commit_graft));
153 commit_graft[pos] = graft;
154 return 0;
155}
156
157struct commit_graft *read_graft_line(char *buf, int len)
158{
159 /* The format is just "Commit Parent1 Parent2 ...\n" */
160 int i;
161 struct commit_graft *graft = NULL;
162
163 if (buf[len-1] == '\n')
164 buf[--len] = 0;
360204c3 165 if (buf[0] == '#' || buf[0] == '\0')
5bc4ce58 166 return NULL;
5040f17e
JH
167 if ((len + 1) % 41) {
168 bad_graft_data:
169 error("bad graft data: %s", buf);
170 free(graft);
171 return NULL;
172 }
173 i = (len + 1) / 41 - 1;
174 graft = xmalloc(sizeof(*graft) + 20 * i);
175 graft->nr_parent = i;
176 if (get_sha1_hex(buf, graft->sha1))
177 goto bad_graft_data;
178 for (i = 40; i < len; i += 41) {
179 if (buf[i] != ' ')
180 goto bad_graft_data;
181 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
182 goto bad_graft_data;
183 }
184 return graft;
185}
186
187int 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);
215 commit_graft_prepared = 1;
5da5c8f4
JH
216}
217
218static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
219{
220 int pos;
5040f17e 221 prepare_commit_graft();
5da5c8f4
JH
222 pos = commit_graft_pos(sha1);
223 if (pos < 0)
224 return NULL;
225 return commit_graft[pos];
226}
227
bd2c39f5 228int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
175785e5 229{
f7cc77d7 230 char *bufptr = buffer;
175785e5 231 unsigned char parent[20];
6c88be16 232 struct commit_list **pptr;
5da5c8f4 233 struct commit_graft *graft;
27dedf0c 234 unsigned n_refs = 0;
bd2c39f5 235
175785e5
DB
236 if (item->object.parsed)
237 return 0;
238 item->object.parsed = 1;
f7cc77d7
LT
239 if (memcmp(bufptr, "tree ", 5))
240 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
241 if (get_sha1_hex(bufptr + 5, parent) < 0)
bd2afde8
JH
242 return error("bad tree pointer in commit %s",
243 sha1_to_hex(item->object.sha1));
175785e5 244 item->tree = lookup_tree(parent);
235ac407 245 if (item->tree)
27dedf0c 246 n_refs++;
175785e5 247 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
6c88be16 248 pptr = &item->parents;
5da5c8f4
JH
249
250 graft = lookup_commit_graft(item->object.sha1);
f7cc77d7
LT
251 while (!memcmp(bufptr, "parent ", 7)) {
252 struct commit *new_parent;
253
254 if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
255 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
5da5c8f4
JH
256 bufptr += 48;
257 if (graft)
258 continue;
f7cc77d7 259 new_parent = lookup_commit(parent);
235ac407 260 if (new_parent) {
6c88be16 261 pptr = &commit_list_insert(new_parent, pptr)->next;
27dedf0c 262 n_refs++;
235ac407 263 }
5da5c8f4
JH
264 }
265 if (graft) {
266 int i;
267 struct commit *new_parent;
268 for (i = 0; i < graft->nr_parent; i++) {
269 new_parent = lookup_commit(graft->parent[i]);
270 if (!new_parent)
271 continue;
272 pptr = &commit_list_insert(new_parent, pptr)->next;
27dedf0c 273 n_refs++;
5da5c8f4 274 }
175785e5
DB
275 }
276 item->date = parse_commit_date(bufptr);
27dedf0c
JH
277
278 if (track_object_refs) {
279 unsigned i = 0;
280 struct commit_list *p;
281 struct object_refs *refs = alloc_object_refs(n_refs);
282 if (item->tree)
283 refs->ref[i++] = &item->tree->object;
284 for (p = item->parents; p; p = p->next)
285 refs->ref[i++] = &p->item->object;
286 set_object_refs(&item->object, refs);
287 }
288
175785e5
DB
289 return 0;
290}
291
bd2c39f5
NP
292int parse_commit(struct commit *item)
293{
294 char type[20];
295 void *buffer;
296 unsigned long size;
297 int ret;
298
299 if (item->object.parsed)
300 return 0;
301 buffer = read_sha1_file(item->object.sha1, type, &size);
302 if (!buffer)
303 return error("Could not read %s",
304 sha1_to_hex(item->object.sha1));
305 if (strcmp(type, commit_type)) {
306 free(buffer);
307 return error("Object %s not a commit",
308 sha1_to_hex(item->object.sha1));
309 }
310 ret = parse_commit_buffer(item, buffer, size);
60ab26de 311 if (save_commit_buffer && !ret) {
3ff1fbbb
LT
312 item->buffer = buffer;
313 return 0;
314 }
bd2c39f5
NP
315 free(buffer);
316 return ret;
317}
318
ac5155ef 319struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
dd97f850 320{
812666c8 321 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
dd97f850
DB
322 new_list->item = item;
323 new_list->next = *list_p;
324 *list_p = new_list;
ac5155ef 325 return new_list;
dd97f850
DB
326}
327
175785e5
DB
328void free_commit_list(struct commit_list *list)
329{
330 while (list) {
331 struct commit_list *temp = list;
332 list = temp->next;
333 free(temp);
334 }
335}
dd97f850 336
f755494c 337struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
dd97f850
DB
338{
339 struct commit_list **pp = list;
340 struct commit_list *p;
341 while ((p = *pp) != NULL) {
342 if (p->item->date < item->date) {
343 break;
344 }
345 pp = &p->next;
346 }
f755494c 347 return commit_list_insert(item, pp);
dd97f850
DB
348}
349
350
351void sort_by_date(struct commit_list **list)
352{
353 struct commit_list *ret = NULL;
354 while (*list) {
f755494c 355 insert_by_date((*list)->item, &ret);
dd97f850
DB
356 *list = (*list)->next;
357 }
358 *list = ret;
359}
360
58e28af6
DB
361struct commit *pop_most_recent_commit(struct commit_list **list,
362 unsigned int mark)
dd97f850
DB
363{
364 struct commit *ret = (*list)->item;
365 struct commit_list *parents = ret->parents;
366 struct commit_list *old = *list;
367
368 *list = (*list)->next;
369 free(old);
370
371 while (parents) {
4056c091 372 struct commit *commit = parents->item;
58e28af6
DB
373 parse_commit(commit);
374 if (!(commit->object.flags & mark)) {
375 commit->object.flags |= mark;
f755494c 376 insert_by_date(commit, list);
4056c091 377 }
dd97f850
DB
378 parents = parents->next;
379 }
380 return ret;
381}
e3bc7a3b 382
f8f9c73c
JH
383void clear_commit_marks(struct commit *commit, unsigned int mark)
384{
385 struct commit_list *parents;
386
387 parents = commit->parents;
388 commit->object.flags &= ~mark;
389 while (parents) {
181dc776
JH
390 struct commit *parent = parents->item;
391 if (parent && parent->object.parsed &&
392 (parent->object.flags & mark))
393 clear_commit_marks(parent, mark);
f8f9c73c
JH
394 parents = parents->next;
395 }
396}
397
e3bc7a3b
LT
398/*
399 * Generic support for pretty-printing the header
400 */
401static int get_one_line(const char *msg, unsigned long len)
402{
403 int ret = 0;
404
405 while (len--) {
406 char c = *msg++;
684958ae
LT
407 if (!c)
408 break;
e3bc7a3b
LT
409 ret++;
410 if (c == '\n')
411 break;
e3bc7a3b
LT
412 }
413 return ret;
414}
415
9b66ec04 416static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
e3bc7a3b
LT
417{
418 char *date;
5de36bfe 419 int namelen;
e3bc7a3b 420 unsigned long time;
000182ea 421 int tz, ret;
ff56fe1c 422 const char *filler = " ";
e3bc7a3b 423
d87449c5
JH
424 if (fmt == CMIT_FMT_ONELINE)
425 return 0;
e3bc7a3b
LT
426 date = strchr(line, '>');
427 if (!date)
428 return 0;
429 namelen = ++date - line;
430 time = strtoul(date, &date, 10);
431 tz = strtol(date, NULL, 10);
432
3eefc189
JH
433 if (fmt == CMIT_FMT_EMAIL) {
434 what = "From";
435 filler = "";
436 }
ff56fe1c
JH
437 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
438 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
439 filler, namelen, line);
440 switch (fmt) {
441 case CMIT_FMT_MEDIUM:
000182ea 442 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
ff56fe1c 443 break;
3eefc189
JH
444 case CMIT_FMT_EMAIL:
445 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
446 break;
ff56fe1c
JH
447 case CMIT_FMT_FULLER:
448 ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz));
449 break;
450 default:
451 /* notin' */
452 break;
453 }
000182ea
LT
454 return ret;
455}
456
3eefc189 457static int is_empty_line(const char *line, int *len_p)
000182ea 458{
3eefc189 459 int len = *len_p;
000182ea
LT
460 while (len && isspace(line[len-1]))
461 len--;
3eefc189 462 *len_p = len;
000182ea 463 return !len;
e3bc7a3b
LT
464}
465
f2d42275 466static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
28342a5d 467{
f2d42275
JH
468 struct commit_list *parent = commit->parents;
469 int offset;
d87449c5 470
3eefc189
JH
471 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
472 !parent || !parent->next)
f2d42275
JH
473 return 0;
474
475 offset = sprintf(buf, "Merge:");
476
477 while (parent) {
478 struct commit *p = parent->item;
6bfb27a0
JH
479 const char *hex = abbrev
480 ? find_unique_abbrev(p->object.sha1, abbrev)
481 : sha1_to_hex(p->object.sha1);
482 char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
f2d42275
JH
483 parent = parent->next;
484
6bfb27a0 485 offset += sprintf(buf + offset, " %s%s", hex, dots);
28342a5d 486 }
f2d42275 487 buf[offset++] = '\n';
28342a5d
LT
488 return offset;
489}
490
3815f423 491unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, unsigned long len, char *buf, unsigned long space, int abbrev)
e3bc7a3b 492{
000182ea 493 int hdr = 1, body = 0;
e3bc7a3b 494 unsigned long offset = 0;
3eefc189 495 int indent = 4;
f2d42275 496 int parents_shown = 0;
3815f423 497 const char *msg = commit->buffer;
3eefc189
JH
498 const char *subject = NULL;
499
500 if (fmt == CMIT_FMT_EMAIL)
4c4b158b 501 subject = "Subject: [PATCH] ";
3eefc189
JH
502 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
503 indent = 0;
e3bc7a3b
LT
504
505 for (;;) {
506 const char *line = msg;
507 int linelen = get_one_line(msg, len);
508
509 if (!linelen)
510 break;
511
512 /*
513 * We want some slop for indentation and a possible
514 * final "...". Thus the "+ 20".
515 */
516 if (offset + linelen + 20 > space) {
517 memcpy(buf + offset, " ...\n", 8);
518 offset += 8;
519 break;
520 }
521
522 msg += linelen;
523 len -= linelen;
e3bc7a3b 524 if (hdr) {
000182ea
LT
525 if (linelen == 1) {
526 hdr = 0;
3eefc189 527 if ((fmt != CMIT_FMT_ONELINE) && !subject)
d87449c5 528 buf[offset++] = '\n';
000182ea
LT
529 continue;
530 }
531 if (fmt == CMIT_FMT_RAW) {
532 memcpy(buf + offset, line, linelen);
533 offset += linelen;
534 continue;
535 }
28342a5d
LT
536 if (!memcmp(line, "parent ", 7)) {
537 if (linelen != 48)
538 die("bad parent line in commit");
f2d42275 539 continue;
28342a5d 540 }
ff56fe1c 541
f2d42275
JH
542 if (!parents_shown) {
543 offset += add_merge_info(fmt, buf + offset,
544 commit, abbrev);
545 parents_shown = 1;
546 continue;
547 }
ff56fe1c
JH
548 /*
549 * MEDIUM == DEFAULT shows only author with dates.
550 * FULL shows both authors but not dates.
551 * FULLER shows both authors and dates.
552 */
e3bc7a3b 553 if (!memcmp(line, "author ", 7))
ff56fe1c
JH
554 offset += add_user_info("Author", fmt,
555 buf + offset,
556 line + 7);
557 if (!memcmp(line, "committer ", 10) &&
558 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
559 offset += add_user_info("Commit", fmt,
560 buf + offset,
561 line + 10);
e3bc7a3b
LT
562 continue;
563 }
000182ea 564
3eefc189 565 if (is_empty_line(line, &linelen)) {
000182ea
LT
566 if (!body)
567 continue;
3eefc189
JH
568 if (subject)
569 continue;
000182ea
LT
570 if (fmt == CMIT_FMT_SHORT)
571 break;
572 } else {
573 body = 1;
574 }
d87449c5 575
3eefc189 576 if (subject) {
53f420ef
JH
577 int slen = strlen(subject);
578 memcpy(buf + offset, subject, slen);
579 offset += slen;
3eefc189 580 }
d87449c5
JH
581 memset(buf + offset, ' ', indent);
582 memcpy(buf + offset + indent, line, linelen);
583 offset += linelen + indent;
3eefc189 584 buf[offset++] = '\n';
d87449c5
JH
585 if (fmt == CMIT_FMT_ONELINE)
586 break;
3eefc189 587 subject = NULL;
d87449c5 588 }
40c2fe00
LT
589 while (offset && isspace(buf[offset-1]))
590 offset--;
591 /* Make sure there is an EOLN for the non-oneline case */
592 if (fmt != CMIT_FMT_ONELINE)
593 buf[offset++] = '\n';
e3bc7a3b
LT
594 buf[offset] = '\0';
595 return offset;
596}
a3437b8c
JS
597
598struct commit *pop_commit(struct commit_list **stack)
599{
600 struct commit_list *top = *stack;
601 struct commit *item = top ? top->item : NULL;
602
603 if (top) {
604 *stack = top->next;
605 free(top);
606 }
607 return item;
608}
609
610int count_parents(struct commit * commit)
611{
612 int count = 0;
613 struct commit_list * parents = commit->parents;
614 for (count=0;parents; parents=parents->next,count++)
615 ;
616 return count;
617}
618
6b6dcfc2
FK
619void topo_sort_default_setter(struct commit *c, void *data)
620{
621 c->object.util = data;
622}
623
624void *topo_sort_default_getter(struct commit *c)
625{
626 return c->object.util;
627}
628
ab580ace
JS
629/*
630 * Performs an in-place topological sort on the list supplied.
631 */
4c8725f1 632void sort_in_topological_order(struct commit_list ** list, int lifo)
6b6dcfc2
FK
633{
634 sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
635 topo_sort_default_getter);
636}
637
638void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
639 topo_sort_set_fn_t setter,
640 topo_sort_get_fn_t getter)
ab580ace
JS
641{
642 struct commit_list * next = *list;
2ed02887 643 struct commit_list * work = NULL, **insert;
ab580ace
JS
644 struct commit_list ** pptr = list;
645 struct sort_node * nodes;
646 struct sort_node * next_nodes;
647 int count = 0;
648
649 /* determine the size of the list */
650 while (next) {
651 next = next->next;
652 count++;
653 }
7d6fb370
EW
654
655 if (!count)
656 return;
ab580ace
JS
657 /* allocate an array to help sort the list */
658 nodes = xcalloc(count, sizeof(*nodes));
659 /* link the list to the array */
660 next_nodes = nodes;
661 next=*list;
662 while (next) {
663 next_nodes->list_item = next;
6b6dcfc2 664 setter(next->item, next_nodes);
ab580ace
JS
665 next_nodes++;
666 next = next->next;
667 }
668 /* update the indegree */
669 next=*list;
670 while (next) {
671 struct commit_list * parents = next->item->parents;
672 while (parents) {
673 struct commit * parent=parents->item;
6b6dcfc2
FK
674 struct sort_node * pn = (struct sort_node *) getter(parent);
675
ab580ace
JS
676 if (pn)
677 pn->indegree++;
678 parents=parents->next;
679 }
680 next=next->next;
681 }
682 /*
683 * find the tips
684 *
685 * tips are nodes not reachable from any other node in the list
686 *
687 * the tips serve as a starting set for the work queue.
688 */
689 next=*list;
2ed02887 690 insert = &work;
ab580ace 691 while (next) {
6b6dcfc2 692 struct sort_node * node = (struct sort_node *) getter(next->item);
ab580ace
JS
693
694 if (node->indegree == 0) {
2ed02887 695 insert = &commit_list_insert(next->item, insert)->next;
ab580ace
JS
696 }
697 next=next->next;
698 }
4c8725f1 699
ab580ace 700 /* process the list in topological order */
4c8725f1
JH
701 if (!lifo)
702 sort_by_date(&work);
ab580ace
JS
703 while (work) {
704 struct commit * work_item = pop_commit(&work);
6b6dcfc2 705 struct sort_node * work_node = (struct sort_node *) getter(work_item);
ab580ace
JS
706 struct commit_list * parents = work_item->parents;
707
708 while (parents) {
709 struct commit * parent=parents->item;
6b6dcfc2
FK
710 struct sort_node * pn = (struct sort_node *) getter(parent);
711
ab580ace 712 if (pn) {
6b6dcfc2 713 /*
ab580ace
JS
714 * parents are only enqueued for emission
715 * when all their children have been emitted thereby
716 * guaranteeing topological order.
717 */
718 pn->indegree--;
4c8725f1
JH
719 if (!pn->indegree) {
720 if (!lifo)
721 insert_by_date(parent, &work);
722 else
723 commit_list_insert(parent, &work);
724 }
ab580ace
JS
725 }
726 parents=parents->next;
727 }
728 /*
729 * work_item is a commit all of whose children
730 * have already been emitted. we can emit it now.
731 */
732 *pptr = work_node->list_item;
733 pptr = &(*pptr)->next;
734 *pptr = NULL;
6b6dcfc2 735 setter(work_item, NULL);
ab580ace
JS
736 }
737 free(nodes);
738}