]> git.ipfire.org Git - thirdparty/git.git/blame - commit.c
rev-list: free commit_list in ... handler
[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
6cdfd179
EW
25struct cmt_fmt_map {
26 const char *n;
27 size_t cmp_len;
28 enum cmit_fmt v;
29} cmt_fmts[] = {
30 { "raw", 1, CMIT_FMT_RAW },
31 { "medium", 1, CMIT_FMT_MEDIUM },
32 { "short", 1, CMIT_FMT_SHORT },
328b710d 33 { "email", 1, CMIT_FMT_EMAIL },
6cdfd179
EW
34 { "full", 5, CMIT_FMT_FULL },
35 { "fuller", 5, CMIT_FMT_FULLER },
36 { "oneline", 1, CMIT_FMT_ONELINE },
37};
38
9b66ec04
LT
39enum cmit_fmt get_commit_format(const char *arg)
40{
6cdfd179
EW
41 int i;
42
43 if (!arg || !*arg)
9b66ec04 44 return CMIT_FMT_DEFAULT;
6cdfd179
EW
45 if (*arg == '=')
46 arg++;
47 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
48 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len))
49 return cmt_fmts[i].v;
50 }
51
52 die("invalid --pretty format: %s", arg);
9b66ec04
LT
53}
54
f76412ed
JH
55static struct commit *check_commit(struct object *obj,
56 const unsigned char *sha1,
57 int quiet)
961784ee 58{
885a86ab 59 if (obj->type != TYPE_COMMIT) {
f76412ed
JH
60 if (!quiet)
61 error("Object %s is a %s, not a commit",
885a86ab 62 sha1_to_hex(sha1), typename(obj->type));
961784ee
LT
63 return NULL;
64 }
65 return (struct commit *) obj;
66}
67
f76412ed
JH
68struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
69 int quiet)
961784ee 70{
9534f40b 71 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
961784ee
LT
72
73 if (!obj)
74 return NULL;
f76412ed
JH
75 return check_commit(obj, sha1, quiet);
76}
77
78struct commit *lookup_commit_reference(const unsigned char *sha1)
79{
80 return lookup_commit_reference_gently(sha1, 0);
961784ee
LT
81}
82
5d6ccf5c 83struct commit *lookup_commit(const unsigned char *sha1)
175785e5
DB
84{
85 struct object *obj = lookup_object(sha1);
86 if (!obj) {
855419f7 87 struct commit *ret = alloc_commit_node();
175785e5 88 created_object(sha1, &ret->object);
885a86ab 89 ret->object.type = TYPE_COMMIT;
175785e5
DB
90 return ret;
91 }
d1af002d 92 if (!obj->type)
885a86ab 93 obj->type = TYPE_COMMIT;
f76412ed 94 return check_commit(obj, sha1, 0);
175785e5
DB
95}
96
97static unsigned long parse_commit_date(const char *buf)
98{
99 unsigned long date;
100
101 if (memcmp(buf, "author", 6))
102 return 0;
103 while (*buf++ != '\n')
104 /* nada */;
105 if (memcmp(buf, "committer", 9))
106 return 0;
107 while (*buf++ != '>')
108 /* nada */;
109 date = strtoul(buf, NULL, 10);
110 if (date == ULONG_MAX)
111 date = 0;
112 return date;
113}
114
5040f17e 115static struct commit_graft **commit_graft;
5da5c8f4
JH
116static int commit_graft_alloc, commit_graft_nr;
117
118static int commit_graft_pos(const unsigned char *sha1)
119{
120 int lo, hi;
121 lo = 0;
122 hi = commit_graft_nr;
123 while (lo < hi) {
124 int mi = (lo + hi) / 2;
125 struct commit_graft *graft = commit_graft[mi];
126 int cmp = memcmp(sha1, graft->sha1, 20);
127 if (!cmp)
128 return mi;
129 if (cmp < 0)
130 hi = mi;
131 else
132 lo = mi + 1;
133 }
134 return -lo - 1;
135}
136
5040f17e
JH
137int register_commit_graft(struct commit_graft *graft, int ignore_dups)
138{
139 int pos = commit_graft_pos(graft->sha1);
140
141 if (0 <= pos) {
142 if (ignore_dups)
143 free(graft);
144 else {
145 free(commit_graft[pos]);
146 commit_graft[pos] = graft;
147 }
148 return 1;
149 }
150 pos = -pos - 1;
151 if (commit_graft_alloc <= ++commit_graft_nr) {
152 commit_graft_alloc = alloc_nr(commit_graft_alloc);
153 commit_graft = xrealloc(commit_graft,
154 sizeof(*commit_graft) *
155 commit_graft_alloc);
156 }
157 if (pos < commit_graft_nr)
158 memmove(commit_graft + pos + 1,
159 commit_graft + pos,
160 (commit_graft_nr - pos - 1) *
161 sizeof(*commit_graft));
162 commit_graft[pos] = graft;
163 return 0;
164}
165
166struct commit_graft *read_graft_line(char *buf, int len)
167{
168 /* The format is just "Commit Parent1 Parent2 ...\n" */
169 int i;
170 struct commit_graft *graft = NULL;
171
172 if (buf[len-1] == '\n')
173 buf[--len] = 0;
360204c3 174 if (buf[0] == '#' || buf[0] == '\0')
5bc4ce58 175 return NULL;
5040f17e
JH
176 if ((len + 1) % 41) {
177 bad_graft_data:
178 error("bad graft data: %s", buf);
179 free(graft);
180 return NULL;
181 }
182 i = (len + 1) / 41 - 1;
183 graft = xmalloc(sizeof(*graft) + 20 * i);
184 graft->nr_parent = i;
185 if (get_sha1_hex(buf, graft->sha1))
186 goto bad_graft_data;
187 for (i = 40; i < len; i += 41) {
188 if (buf[i] != ' ')
189 goto bad_graft_data;
190 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
191 goto bad_graft_data;
192 }
193 return graft;
194}
195
196int 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);
224 commit_graft_prepared = 1;
5da5c8f4
JH
225}
226
227static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
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
bd2c39f5 237int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
175785e5 238{
3b44f15a 239 char *tail = buffer;
f7cc77d7 240 char *bufptr = buffer;
175785e5 241 unsigned char parent[20];
6c88be16 242 struct commit_list **pptr;
5da5c8f4 243 struct commit_graft *graft;
27dedf0c 244 unsigned n_refs = 0;
bd2c39f5 245
175785e5
DB
246 if (item->object.parsed)
247 return 0;
248 item->object.parsed = 1;
3b44f15a
JH
249 tail += size;
250 if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5))
f7cc77d7 251 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
3b44f15a 252 if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0)
bd2afde8
JH
253 return error("bad tree pointer in commit %s",
254 sha1_to_hex(item->object.sha1));
175785e5 255 item->tree = lookup_tree(parent);
235ac407 256 if (item->tree)
27dedf0c 257 n_refs++;
175785e5 258 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
6c88be16 259 pptr = &item->parents;
5da5c8f4
JH
260
261 graft = lookup_commit_graft(item->object.sha1);
3b44f15a 262 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
f7cc77d7
LT
263 struct commit *new_parent;
264
3b44f15a
JH
265 if (tail <= bufptr + 48 ||
266 get_sha1_hex(bufptr + 7, parent) ||
267 bufptr[47] != '\n')
f7cc77d7 268 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
5da5c8f4
JH
269 bufptr += 48;
270 if (graft)
271 continue;
f7cc77d7 272 new_parent = lookup_commit(parent);
235ac407 273 if (new_parent) {
6c88be16 274 pptr = &commit_list_insert(new_parent, pptr)->next;
27dedf0c 275 n_refs++;
235ac407 276 }
5da5c8f4
JH
277 }
278 if (graft) {
279 int i;
280 struct commit *new_parent;
281 for (i = 0; i < graft->nr_parent; i++) {
282 new_parent = lookup_commit(graft->parent[i]);
283 if (!new_parent)
284 continue;
285 pptr = &commit_list_insert(new_parent, pptr)->next;
27dedf0c 286 n_refs++;
5da5c8f4 287 }
175785e5
DB
288 }
289 item->date = parse_commit_date(bufptr);
27dedf0c
JH
290
291 if (track_object_refs) {
292 unsigned i = 0;
293 struct commit_list *p;
294 struct object_refs *refs = alloc_object_refs(n_refs);
295 if (item->tree)
296 refs->ref[i++] = &item->tree->object;
297 for (p = item->parents; p; p = p->next)
298 refs->ref[i++] = &p->item->object;
299 set_object_refs(&item->object, refs);
300 }
301
175785e5
DB
302 return 0;
303}
304
bd2c39f5
NP
305int parse_commit(struct commit *item)
306{
307 char type[20];
308 void *buffer;
309 unsigned long size;
310 int ret;
311
312 if (item->object.parsed)
313 return 0;
314 buffer = read_sha1_file(item->object.sha1, type, &size);
315 if (!buffer)
316 return error("Could not read %s",
317 sha1_to_hex(item->object.sha1));
318 if (strcmp(type, commit_type)) {
319 free(buffer);
320 return error("Object %s not a commit",
321 sha1_to_hex(item->object.sha1));
322 }
323 ret = parse_commit_buffer(item, buffer, size);
60ab26de 324 if (save_commit_buffer && !ret) {
3ff1fbbb
LT
325 item->buffer = buffer;
326 return 0;
327 }
bd2c39f5
NP
328 free(buffer);
329 return ret;
330}
331
ac5155ef 332struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
dd97f850 333{
812666c8 334 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
dd97f850
DB
335 new_list->item = item;
336 new_list->next = *list_p;
337 *list_p = new_list;
ac5155ef 338 return new_list;
dd97f850
DB
339}
340
175785e5
DB
341void free_commit_list(struct commit_list *list)
342{
343 while (list) {
344 struct commit_list *temp = list;
345 list = temp->next;
346 free(temp);
347 }
348}
dd97f850 349
f755494c 350struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
dd97f850
DB
351{
352 struct commit_list **pp = list;
353 struct commit_list *p;
354 while ((p = *pp) != NULL) {
355 if (p->item->date < item->date) {
356 break;
357 }
358 pp = &p->next;
359 }
f755494c 360 return commit_list_insert(item, pp);
dd97f850
DB
361}
362
363
364void sort_by_date(struct commit_list **list)
365{
366 struct commit_list *ret = NULL;
367 while (*list) {
f755494c 368 insert_by_date((*list)->item, &ret);
dd97f850
DB
369 *list = (*list)->next;
370 }
371 *list = ret;
372}
373
58e28af6
DB
374struct commit *pop_most_recent_commit(struct commit_list **list,
375 unsigned int mark)
dd97f850
DB
376{
377 struct commit *ret = (*list)->item;
378 struct commit_list *parents = ret->parents;
379 struct commit_list *old = *list;
380
381 *list = (*list)->next;
382 free(old);
383
384 while (parents) {
4056c091 385 struct commit *commit = parents->item;
58e28af6
DB
386 parse_commit(commit);
387 if (!(commit->object.flags & mark)) {
388 commit->object.flags |= mark;
f755494c 389 insert_by_date(commit, list);
4056c091 390 }
dd97f850
DB
391 parents = parents->next;
392 }
393 return ret;
394}
e3bc7a3b 395
f8f9c73c
JH
396void clear_commit_marks(struct commit *commit, unsigned int mark)
397{
398 struct commit_list *parents;
399
400 parents = commit->parents;
401 commit->object.flags &= ~mark;
402 while (parents) {
160b7983
JH
403 struct commit *parent = parents->item;
404 if (parent && parent->object.parsed &&
405 (parent->object.flags & mark))
406 clear_commit_marks(parent, mark);
f8f9c73c
JH
407 parents = parents->next;
408 }
409}
410
e3bc7a3b
LT
411/*
412 * Generic support for pretty-printing the header
413 */
414static int get_one_line(const char *msg, unsigned long len)
415{
416 int ret = 0;
417
418 while (len--) {
419 char c = *msg++;
684958ae
LT
420 if (!c)
421 break;
e3bc7a3b
LT
422 ret++;
423 if (c == '\n')
424 break;
e3bc7a3b
LT
425 }
426 return ret;
427}
428
cdd406e3
JH
429static int is_rfc2047_special(char ch)
430{
431 return ((ch & 0x80) || (ch == '=') || (ch == '?') || (ch == '_'));
432}
433
434static int add_rfc2047(char *buf, const char *line, int len)
435{
436 char *bp = buf;
437 int i, needquote;
438 static const char q_utf8[] = "=?utf-8?q?";
439
440 for (i = needquote = 0; !needquote && i < len; i++) {
441 unsigned ch = line[i];
442 if (ch & 0x80)
443 needquote++;
444 if ((i + 1 < len) &&
445 (ch == '=' && line[i+1] == '?'))
446 needquote++;
447 }
448 if (!needquote)
449 return sprintf(buf, "%.*s", len, line);
450
451 memcpy(bp, q_utf8, sizeof(q_utf8)-1);
452 bp += sizeof(q_utf8)-1;
453 for (i = 0; i < len; i++) {
0da46771 454 unsigned ch = line[i] & 0xFF;
cdd406e3
JH
455 if (is_rfc2047_special(ch)) {
456 sprintf(bp, "=%02X", ch);
457 bp += 3;
458 }
459 else if (ch == ' ')
460 *bp++ = '_';
461 else
462 *bp++ = ch;
463 }
464 memcpy(bp, "?=", 2);
465 bp += 2;
466 return bp - buf;
467}
468
9b66ec04 469static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
e3bc7a3b
LT
470{
471 char *date;
5de36bfe 472 int namelen;
e3bc7a3b 473 unsigned long time;
000182ea 474 int tz, ret;
ff56fe1c 475 const char *filler = " ";
e3bc7a3b 476
d87449c5
JH
477 if (fmt == CMIT_FMT_ONELINE)
478 return 0;
e3bc7a3b
LT
479 date = strchr(line, '>');
480 if (!date)
481 return 0;
482 namelen = ++date - line;
483 time = strtoul(date, &date, 10);
484 tz = strtol(date, NULL, 10);
485
3eefc189 486 if (fmt == CMIT_FMT_EMAIL) {
cdd406e3
JH
487 char *name_tail = strchr(line, '<');
488 int display_name_length;
489 if (!name_tail)
490 return 0;
491 while (line < name_tail && isspace(name_tail[-1]))
492 name_tail--;
493 display_name_length = name_tail - line;
3eefc189 494 filler = "";
cdd406e3
JH
495 strcpy(buf, "From: ");
496 ret = strlen(buf);
497 ret += add_rfc2047(buf + ret, line, display_name_length);
498 memcpy(buf + ret, name_tail, namelen - display_name_length);
499 ret += namelen - display_name_length;
500 buf[ret++] = '\n';
501 }
502 else {
503 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
504 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
505 filler, namelen, line);
3eefc189 506 }
ff56fe1c
JH
507 switch (fmt) {
508 case CMIT_FMT_MEDIUM:
000182ea 509 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
ff56fe1c 510 break;
3eefc189 511 case CMIT_FMT_EMAIL:
2a387043
JH
512 ret += sprintf(buf + ret, "Date: %s\n",
513 show_rfc2822_date(time, tz));
3eefc189 514 break;
ff56fe1c
JH
515 case CMIT_FMT_FULLER:
516 ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz));
517 break;
518 default:
519 /* notin' */
520 break;
521 }
000182ea
LT
522 return ret;
523}
524
3eefc189 525static int is_empty_line(const char *line, int *len_p)
000182ea 526{
3eefc189 527 int len = *len_p;
000182ea
LT
528 while (len && isspace(line[len-1]))
529 len--;
3eefc189 530 *len_p = len;
000182ea 531 return !len;
e3bc7a3b
LT
532}
533
f2d42275 534static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
28342a5d 535{
f2d42275
JH
536 struct commit_list *parent = commit->parents;
537 int offset;
d87449c5 538
3eefc189
JH
539 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
540 !parent || !parent->next)
f2d42275
JH
541 return 0;
542
543 offset = sprintf(buf, "Merge:");
544
545 while (parent) {
546 struct commit *p = parent->item;
6bfb27a0
JH
547 const char *hex = abbrev
548 ? find_unique_abbrev(p->object.sha1, abbrev)
549 : sha1_to_hex(p->object.sha1);
554fe20d 550 const char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
f2d42275
JH
551 parent = parent->next;
552
6bfb27a0 553 offset += sprintf(buf + offset, " %s%s", hex, dots);
28342a5d 554 }
f2d42275 555 buf[offset++] = '\n';
28342a5d
LT
556 return offset;
557}
558
698ce6f8 559unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, unsigned long len, char *buf, unsigned long space, int abbrev, const char *subject, const char *after_subject)
e3bc7a3b 560{
000182ea 561 int hdr = 1, body = 0;
e3bc7a3b 562 unsigned long offset = 0;
3eefc189 563 int indent = 4;
f2d42275 564 int parents_shown = 0;
3815f423 565 const char *msg = commit->buffer;
c831da66 566 int plain_non_ascii = 0;
3eefc189 567
3eefc189
JH
568 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
569 indent = 0;
e3bc7a3b 570
c831da66
JH
571 /* After-subject is used to pass in Content-Type: multipart
572 * MIME header; in that case we do not have to do the
573 * plaintext content type even if the commit message has
574 * non 7-bit ASCII character. Otherwise, check if we need
575 * to say this is not a 7-bit ASCII.
576 */
577 if (fmt == CMIT_FMT_EMAIL && !after_subject) {
0da46771
JH
578 int i, ch, in_body;
579
580 for (in_body = i = 0; (ch = msg[i]) && i < len; i++) {
581 if (!in_body) {
582 /* author could be non 7-bit ASCII but
583 * the log may so; skip over the
584 * header part first.
585 */
586 if (ch == '\n' &&
587 i + 1 < len && msg[i+1] == '\n')
588 in_body = 1;
589 }
590 else if (ch & 0x80) {
c831da66 591 plain_non_ascii = 1;
0da46771
JH
592 break;
593 }
594 }
c831da66
JH
595 }
596
e3bc7a3b
LT
597 for (;;) {
598 const char *line = msg;
599 int linelen = get_one_line(msg, len);
600
601 if (!linelen)
602 break;
603
604 /*
605 * We want some slop for indentation and a possible
606 * final "...". Thus the "+ 20".
607 */
608 if (offset + linelen + 20 > space) {
609 memcpy(buf + offset, " ...\n", 8);
610 offset += 8;
611 break;
612 }
613
614 msg += linelen;
615 len -= linelen;
e3bc7a3b 616 if (hdr) {
000182ea
LT
617 if (linelen == 1) {
618 hdr = 0;
3eefc189 619 if ((fmt != CMIT_FMT_ONELINE) && !subject)
d87449c5 620 buf[offset++] = '\n';
000182ea
LT
621 continue;
622 }
623 if (fmt == CMIT_FMT_RAW) {
624 memcpy(buf + offset, line, linelen);
625 offset += linelen;
626 continue;
627 }
28342a5d
LT
628 if (!memcmp(line, "parent ", 7)) {
629 if (linelen != 48)
630 die("bad parent line in commit");
f2d42275 631 continue;
28342a5d 632 }
ff56fe1c 633
f2d42275
JH
634 if (!parents_shown) {
635 offset += add_merge_info(fmt, buf + offset,
636 commit, abbrev);
637 parents_shown = 1;
638 continue;
639 }
ff56fe1c
JH
640 /*
641 * MEDIUM == DEFAULT shows only author with dates.
642 * FULL shows both authors but not dates.
643 * FULLER shows both authors and dates.
644 */
e3bc7a3b 645 if (!memcmp(line, "author ", 7))
ff56fe1c
JH
646 offset += add_user_info("Author", fmt,
647 buf + offset,
648 line + 7);
649 if (!memcmp(line, "committer ", 10) &&
650 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
651 offset += add_user_info("Commit", fmt,
652 buf + offset,
653 line + 10);
e3bc7a3b
LT
654 continue;
655 }
000182ea 656
3eefc189 657 if (is_empty_line(line, &linelen)) {
000182ea
LT
658 if (!body)
659 continue;
3eefc189
JH
660 if (subject)
661 continue;
000182ea
LT
662 if (fmt == CMIT_FMT_SHORT)
663 break;
664 } else {
665 body = 1;
666 }
d87449c5 667
3eefc189 668 if (subject) {
53f420ef
JH
669 int slen = strlen(subject);
670 memcpy(buf + offset, subject, slen);
671 offset += slen;
cdd406e3
JH
672 offset += add_rfc2047(buf + offset, line, linelen);
673 }
674 else {
675 memset(buf + offset, ' ', indent);
676 memcpy(buf + offset + indent, line, linelen);
677 offset += linelen + indent;
3eefc189 678 }
3eefc189 679 buf[offset++] = '\n';
d87449c5
JH
680 if (fmt == CMIT_FMT_ONELINE)
681 break;
c831da66 682 if (subject && plain_non_ascii) {
cdd406e3
JH
683 static const char header[] =
684 "Content-Type: text/plain; charset=UTF-8\n"
685 "Content-Transfer-Encoding: 8bit\n";
686 memcpy(buf + offset, header, sizeof(header)-1);
687 offset += sizeof(header)-1;
cdd406e3 688 }
698ce6f8
JS
689 if (after_subject) {
690 int slen = strlen(after_subject);
691 if (slen > space - offset - 1)
692 slen = space - offset - 1;
693 memcpy(buf + offset, after_subject, slen);
694 offset += slen;
695 after_subject = NULL;
696 }
3eefc189 697 subject = NULL;
d87449c5 698 }
40c2fe00
LT
699 while (offset && isspace(buf[offset-1]))
700 offset--;
701 /* Make sure there is an EOLN for the non-oneline case */
702 if (fmt != CMIT_FMT_ONELINE)
703 buf[offset++] = '\n';
e3bc7a3b
LT
704 buf[offset] = '\0';
705 return offset;
706}
a3437b8c
JS
707
708struct commit *pop_commit(struct commit_list **stack)
709{
710 struct commit_list *top = *stack;
711 struct commit *item = top ? top->item : NULL;
712
713 if (top) {
714 *stack = top->next;
715 free(top);
716 }
717 return item;
718}
719
720int count_parents(struct commit * commit)
721{
722 int count = 0;
723 struct commit_list * parents = commit->parents;
724 for (count=0;parents; parents=parents->next,count++)
725 ;
726 return count;
727}
728
6b6dcfc2
FK
729void topo_sort_default_setter(struct commit *c, void *data)
730{
d3ff6f55 731 c->util = data;
6b6dcfc2
FK
732}
733
734void *topo_sort_default_getter(struct commit *c)
735{
d3ff6f55 736 return c->util;
6b6dcfc2
FK
737}
738
ab580ace
JS
739/*
740 * Performs an in-place topological sort on the list supplied.
741 */
4c8725f1 742void sort_in_topological_order(struct commit_list ** list, int lifo)
6b6dcfc2
FK
743{
744 sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
745 topo_sort_default_getter);
746}
747
748void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
749 topo_sort_set_fn_t setter,
750 topo_sort_get_fn_t getter)
ab580ace
JS
751{
752 struct commit_list * next = *list;
2ed02887 753 struct commit_list * work = NULL, **insert;
ab580ace
JS
754 struct commit_list ** pptr = list;
755 struct sort_node * nodes;
756 struct sort_node * next_nodes;
757 int count = 0;
758
759 /* determine the size of the list */
760 while (next) {
761 next = next->next;
762 count++;
763 }
7d6fb370
EW
764
765 if (!count)
766 return;
ab580ace
JS
767 /* allocate an array to help sort the list */
768 nodes = xcalloc(count, sizeof(*nodes));
769 /* link the list to the array */
770 next_nodes = nodes;
771 next=*list;
772 while (next) {
773 next_nodes->list_item = next;
6b6dcfc2 774 setter(next->item, next_nodes);
ab580ace
JS
775 next_nodes++;
776 next = next->next;
777 }
778 /* update the indegree */
779 next=*list;
780 while (next) {
781 struct commit_list * parents = next->item->parents;
782 while (parents) {
783 struct commit * parent=parents->item;
6b6dcfc2
FK
784 struct sort_node * pn = (struct sort_node *) getter(parent);
785
ab580ace
JS
786 if (pn)
787 pn->indegree++;
788 parents=parents->next;
789 }
790 next=next->next;
791 }
792 /*
793 * find the tips
794 *
795 * tips are nodes not reachable from any other node in the list
796 *
797 * the tips serve as a starting set for the work queue.
798 */
799 next=*list;
2ed02887 800 insert = &work;
ab580ace 801 while (next) {
6b6dcfc2 802 struct sort_node * node = (struct sort_node *) getter(next->item);
ab580ace
JS
803
804 if (node->indegree == 0) {
2ed02887 805 insert = &commit_list_insert(next->item, insert)->next;
ab580ace
JS
806 }
807 next=next->next;
808 }
4c8725f1 809
ab580ace 810 /* process the list in topological order */
4c8725f1
JH
811 if (!lifo)
812 sort_by_date(&work);
ab580ace
JS
813 while (work) {
814 struct commit * work_item = pop_commit(&work);
6b6dcfc2 815 struct sort_node * work_node = (struct sort_node *) getter(work_item);
ab580ace
JS
816 struct commit_list * parents = work_item->parents;
817
818 while (parents) {
819 struct commit * parent=parents->item;
6b6dcfc2
FK
820 struct sort_node * pn = (struct sort_node *) getter(parent);
821
ab580ace 822 if (pn) {
6b6dcfc2 823 /*
ab580ace
JS
824 * parents are only enqueued for emission
825 * when all their children have been emitted thereby
826 * guaranteeing topological order.
827 */
828 pn->indegree--;
4c8725f1
JH
829 if (!pn->indegree) {
830 if (!lifo)
831 insert_by_date(parent, &work);
832 else
833 commit_list_insert(parent, &work);
834 }
ab580ace
JS
835 }
836 parents=parents->next;
837 }
838 /*
839 * work_item is a commit all of whose children
840 * have already been emitted. we can emit it now.
841 */
842 *pptr = work_node->list_item;
843 pptr = &(*pptr)->next;
844 *pptr = NULL;
6b6dcfc2 845 setter(work_item, NULL);
ab580ace
JS
846 }
847 free(nodes);
848}
7c6f8aaf
JS
849
850/* merge-rebase stuff */
851
31609c17
RS
852/* bits #0..7 in revision.h */
853#define PARENT1 (1u<< 8)
854#define PARENT2 (1u<< 9)
542ccefe 855#define STALE (1u<<10)
7c6f8aaf
JS
856
857static struct commit *interesting(struct commit_list *list)
858{
859 while (list) {
860 struct commit *commit = list->item;
861 list = list->next;
542ccefe 862 if (commit->object.flags & STALE)
7c6f8aaf
JS
863 continue;
864 return commit;
865 }
866 return NULL;
867}
868
869/*
870 * A pathological example of how this thing works.
871 *
872 * Suppose we had this commit graph, where chronologically
873 * the timestamp on the commit are A <= B <= C <= D <= E <= F
874 * and we are trying to figure out the merge base for E and F
875 * commits.
876 *
877 * F
878 * / \
879 * E A D
880 * \ / /
881 * B /
882 * \ /
883 * C
884 *
885 * First we push E and F to list to be processed. E gets bit 1
886 * and F gets bit 2. The list becomes:
887 *
888 * list=F(2) E(1), result=empty
889 *
890 * Then we pop F, the newest commit, from the list. Its flag is 2.
891 * We scan its parents, mark them reachable from the side that F is
892 * reachable from, and push them to the list:
893 *
894 * list=E(1) D(2) A(2), result=empty
895 *
896 * Next pop E and do the same.
897 *
898 * list=D(2) B(1) A(2), result=empty
899 *
900 * Next pop D and do the same.
901 *
902 * list=C(2) B(1) A(2), result=empty
903 *
904 * Next pop C and do the same.
905 *
906 * list=B(1) A(2), result=empty
907 *
908 * Now it is B's turn. We mark its parent, C, reachable from B's side,
909 * and push it to the list:
910 *
911 * list=C(3) A(2), result=empty
912 *
913 * Now pop C and notice it has flags==3. It is placed on the result list,
914 * and the list now contains:
915 *
916 * list=A(2), result=C(3)
917 *
918 * We pop A and do the same.
919 *
920 * list=B(3), result=C(3)
921 *
922 * Next, we pop B and something very interesting happens. It has flags==3
923 * so it is also placed on the result list, and its parents are marked
542ccefe 924 * stale, retroactively, and placed back on the list:
7c6f8aaf
JS
925 *
926 * list=C(7), result=C(7) B(3)
927 *
928 * Now, list does not have any interesting commit. So we find the newest
542ccefe 929 * commit from the result list that is not marked stale. Which is
7c6f8aaf
JS
930 * commit B.
931 *
932 *
933 * Another pathological example how this thing used to fail to mark an
542ccefe 934 * ancestor of a merge base as STALE before we introduced the
7c6f8aaf
JS
935 * postprocessing phase (mark_reachable_commits).
936 *
937 * 2
938 * H
939 * 1 / \
940 * G A \
941 * |\ / \
942 * | B \
943 * | \ \
944 * \ C F
945 * \ \ /
946 * \ D /
947 * \ | /
948 * \| /
949 * E
950 *
951 * list A B C D E F G H
952 * G1 H2 - - - - - - 1 2
953 * H2 E1 B1 - 1 - - 1 - 1 2
954 * F2 E1 B1 A2 2 1 - - 1 2 1 2
955 * E3 B1 A2 2 1 - - 3 2 1 2
956 * B1 A2 2 1 - - 3 2 1 2
957 * C1 A2 2 1 1 - 3 2 1 2
958 * D1 A2 2 1 1 1 3 2 1 2
959 * A2 2 1 1 1 3 2 1 2
960 * B3 2 3 1 1 3 2 1 2
961 * C7 2 3 7 1 3 2 1 2
962 *
963 * At this point, unfortunately, everybody in the list is
542ccefe
JH
964 * stale, so we fail to complete the following two
965 * steps to fully marking stale commits.
7c6f8aaf
JS
966 *
967 * D7 2 3 7 7 3 2 1 2
968 * E7 2 3 7 7 7 2 1 2
969 *
970 * and we ended up showing E as an interesting merge base.
971 * The postprocessing phase re-injects C and continues traversal
972 * to contaminate D and E.
973 */
974
975static void mark_reachable_commits(struct commit_list *result,
976 struct commit_list *list)
977{
978 struct commit_list *tmp;
979
980 /*
981 * Postprocess to fully contaminate the well.
982 */
983 for (tmp = result; tmp; tmp = tmp->next) {
984 struct commit *c = tmp->item;
542ccefe 985 /* Reinject stale ones to list,
7c6f8aaf
JS
986 * so we can scan their parents.
987 */
542ccefe 988 if (c->object.flags & STALE)
7c6f8aaf
JS
989 commit_list_insert(c, &list);
990 }
991 while (list) {
992 struct commit *c = list->item;
993 struct commit_list *parents;
994
995 tmp = list;
996 list = list->next;
997 free(tmp);
998
542ccefe
JH
999 /* Anything taken out of the list is stale, so
1000 * mark all its parents stale. We do not
7c6f8aaf
JS
1001 * parse new ones (we already parsed all the relevant
1002 * ones).
1003 */
1004 parents = c->parents;
1005 while (parents) {
1006 struct commit *p = parents->item;
1007 parents = parents->next;
542ccefe
JH
1008 if (!(p->object.flags & STALE)) {
1009 p->object.flags |= STALE;
7c6f8aaf
JS
1010 commit_list_insert(p, &list);
1011 }
1012 }
1013 }
1014}
1015
c0fa8255
RS
1016struct commit_list *get_merge_bases(struct commit *rev1, struct commit *rev2,
1017 int cleanup)
7c6f8aaf
JS
1018{
1019 struct commit_list *list = NULL;
1020 struct commit_list *result = NULL;
1021 struct commit_list *tmp = NULL;
1022
1023 if (rev1 == rev2)
1024 return commit_list_insert(rev1, &result);
1025
1026 parse_commit(rev1);
1027 parse_commit(rev2);
1028
1029 rev1->object.flags |= PARENT1;
1030 rev2->object.flags |= PARENT2;
1031 insert_by_date(rev1, &list);
1032 insert_by_date(rev2, &list);
1033
1034 while (interesting(list)) {
1035 struct commit *commit = list->item;
1036 struct commit_list *parents;
1037 int flags = commit->object.flags
542ccefe 1038 & (PARENT1 | PARENT2 | STALE);
7c6f8aaf
JS
1039
1040 tmp = list;
1041 list = list->next;
1042 free(tmp);
1043 if (flags == (PARENT1 | PARENT2)) {
1044 insert_by_date(commit, &result);
1045
542ccefe
JH
1046 /* Mark parents of a found merge stale */
1047 flags |= STALE;
7c6f8aaf
JS
1048 }
1049 parents = commit->parents;
1050 while (parents) {
1051 struct commit *p = parents->item;
1052 parents = parents->next;
1053 if ((p->object.flags & flags) == flags)
1054 continue;
1055 parse_commit(p);
1056 p->object.flags |= flags;
1057 insert_by_date(p, &list);
1058 }
1059 }
1060
1061 if (!result)
2ef10801 1062 goto finish;
7c6f8aaf
JS
1063
1064 if (result->next && list)
1065 mark_reachable_commits(result, list);
1066
1067 /* cull duplicates */
1068 for (tmp = result, list = NULL; tmp; ) {
1069 struct commit *commit = tmp->item;
1070 struct commit_list *next = tmp->next;
542ccefe 1071 if (commit->object.flags & STALE) {
7c6f8aaf
JS
1072 if (list != NULL)
1073 list->next = next;
1074 free(tmp);
1075 } else {
1076 if (list == NULL)
1077 result = tmp;
1078 list = tmp;
542ccefe 1079 commit->object.flags |= STALE;
7c6f8aaf
JS
1080 }
1081
1082 tmp = next;
1083 }
1084
2ef10801 1085 finish:
160b7983
JH
1086 if (cleanup)
1087 clear_object_marks(PARENT1 | PARENT2 | STALE);
7c6f8aaf
JS
1088
1089 return result;
1090}