markup fix in svnimport documentation.
[git/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 /*
55c3eb43
TS
10 * the number of children of the associated commit
11 * that also occur in the list being sorted.
12 */
ab580ace
JS
13 unsigned int indegree;
14
15 /*
55c3eb43
TS
16 * reference to original list item that we will re-use
17 * on output.
18 */
ab580ace
JS
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{
1974632c 59 if (obj->type != OBJ_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);
1974632c 89 ret->object.type = OBJ_COMMIT;
175785e5
DB
90 return ret;
91 }
d1af002d 92 if (!obj->type)
1974632c 93 obj->type = OBJ_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];
a89fccd2 126 int cmp = hashcmp(sha1, graft->sha1);
5da5c8f4
JH
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
f8f9c73c 400 commit->object.flags &= ~mark;
58ecf5c1 401 parents = commit->parents;
f8f9c73c 402 while (parents) {
160b7983 403 struct commit *parent = parents->item;
58ecf5c1
JH
404
405 /* Have we already cleared this? */
406 if (mark & parent->object.flags)
160b7983 407 clear_commit_marks(parent, mark);
f8f9c73c
JH
408 parents = parents->next;
409 }
410}
411
e3bc7a3b
LT
412/*
413 * Generic support for pretty-printing the header
414 */
415static int get_one_line(const char *msg, unsigned long len)
416{
417 int ret = 0;
418
419 while (len--) {
420 char c = *msg++;
684958ae
LT
421 if (!c)
422 break;
e3bc7a3b
LT
423 ret++;
424 if (c == '\n')
425 break;
e3bc7a3b
LT
426 }
427 return ret;
428}
429
cdd406e3
JH
430static int is_rfc2047_special(char ch)
431{
432 return ((ch & 0x80) || (ch == '=') || (ch == '?') || (ch == '_'));
433}
434
435static int add_rfc2047(char *buf, const char *line, int len)
436{
437 char *bp = buf;
438 int i, needquote;
439 static const char q_utf8[] = "=?utf-8?q?";
440
441 for (i = needquote = 0; !needquote && i < len; i++) {
442 unsigned ch = line[i];
443 if (ch & 0x80)
444 needquote++;
445 if ((i + 1 < len) &&
446 (ch == '=' && line[i+1] == '?'))
447 needquote++;
448 }
449 if (!needquote)
450 return sprintf(buf, "%.*s", len, line);
451
452 memcpy(bp, q_utf8, sizeof(q_utf8)-1);
453 bp += sizeof(q_utf8)-1;
454 for (i = 0; i < len; i++) {
0da46771 455 unsigned ch = line[i] & 0xFF;
cdd406e3
JH
456 if (is_rfc2047_special(ch)) {
457 sprintf(bp, "=%02X", ch);
458 bp += 3;
459 }
460 else if (ch == ' ')
461 *bp++ = '_';
462 else
463 *bp++ = ch;
464 }
465 memcpy(bp, "?=", 2);
466 bp += 2;
467 return bp - buf;
468}
469
3dfb9278
JF
470static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
471 const char *line, int relative_date)
e3bc7a3b
LT
472{
473 char *date;
5de36bfe 474 int namelen;
e3bc7a3b 475 unsigned long time;
000182ea 476 int tz, ret;
ff56fe1c 477 const char *filler = " ";
e3bc7a3b 478
d87449c5
JH
479 if (fmt == CMIT_FMT_ONELINE)
480 return 0;
e3bc7a3b
LT
481 date = strchr(line, '>');
482 if (!date)
483 return 0;
484 namelen = ++date - line;
485 time = strtoul(date, &date, 10);
486 tz = strtol(date, NULL, 10);
487
3eefc189 488 if (fmt == CMIT_FMT_EMAIL) {
cdd406e3
JH
489 char *name_tail = strchr(line, '<');
490 int display_name_length;
491 if (!name_tail)
492 return 0;
493 while (line < name_tail && isspace(name_tail[-1]))
494 name_tail--;
495 display_name_length = name_tail - line;
3eefc189 496 filler = "";
cdd406e3
JH
497 strcpy(buf, "From: ");
498 ret = strlen(buf);
499 ret += add_rfc2047(buf + ret, line, display_name_length);
500 memcpy(buf + ret, name_tail, namelen - display_name_length);
501 ret += namelen - display_name_length;
502 buf[ret++] = '\n';
503 }
504 else {
505 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
506 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
507 filler, namelen, line);
3eefc189 508 }
ff56fe1c
JH
509 switch (fmt) {
510 case CMIT_FMT_MEDIUM:
3dfb9278
JF
511 ret += sprintf(buf + ret, "Date: %s\n",
512 show_date(time, tz, relative_date));
ff56fe1c 513 break;
3eefc189 514 case CMIT_FMT_EMAIL:
2a387043
JH
515 ret += sprintf(buf + ret, "Date: %s\n",
516 show_rfc2822_date(time, tz));
3eefc189 517 break;
ff56fe1c 518 case CMIT_FMT_FULLER:
3dfb9278
JF
519 ret += sprintf(buf + ret, "%sDate: %s\n", what,
520 show_date(time, tz, relative_date));
ff56fe1c
JH
521 break;
522 default:
523 /* notin' */
524 break;
525 }
000182ea
LT
526 return ret;
527}
528
3eefc189 529static int is_empty_line(const char *line, int *len_p)
000182ea 530{
3eefc189 531 int len = *len_p;
000182ea
LT
532 while (len && isspace(line[len-1]))
533 len--;
3eefc189 534 *len_p = len;
000182ea 535 return !len;
e3bc7a3b
LT
536}
537
f2d42275 538static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
28342a5d 539{
f2d42275
JH
540 struct commit_list *parent = commit->parents;
541 int offset;
d87449c5 542
3eefc189
JH
543 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
544 !parent || !parent->next)
f2d42275
JH
545 return 0;
546
547 offset = sprintf(buf, "Merge:");
548
549 while (parent) {
550 struct commit *p = parent->item;
14763e7b
EW
551 const char *hex = NULL;
552 const char *dots;
553 if (abbrev)
554 hex = find_unique_abbrev(p->object.sha1, abbrev);
555 if (!hex)
556 hex = sha1_to_hex(p->object.sha1);
557 dots = (abbrev && strlen(hex) != 40) ? "..." : "";
f2d42275
JH
558 parent = parent->next;
559
6bfb27a0 560 offset += sprintf(buf + offset, " %s%s", hex, dots);
28342a5d 561 }
f2d42275 562 buf[offset++] = '\n';
28342a5d
LT
563 return offset;
564}
565
3dfb9278
JF
566unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
567 unsigned long len, char *buf, unsigned long space,
568 int abbrev, const char *subject,
569 const char *after_subject, int relative_date)
e3bc7a3b 570{
000182ea 571 int hdr = 1, body = 0;
e3bc7a3b 572 unsigned long offset = 0;
3eefc189 573 int indent = 4;
f2d42275 574 int parents_shown = 0;
3815f423 575 const char *msg = commit->buffer;
c831da66 576 int plain_non_ascii = 0;
3eefc189 577
3eefc189
JH
578 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
579 indent = 0;
e3bc7a3b 580
c831da66
JH
581 /* After-subject is used to pass in Content-Type: multipart
582 * MIME header; in that case we do not have to do the
583 * plaintext content type even if the commit message has
584 * non 7-bit ASCII character. Otherwise, check if we need
585 * to say this is not a 7-bit ASCII.
586 */
587 if (fmt == CMIT_FMT_EMAIL && !after_subject) {
0da46771
JH
588 int i, ch, in_body;
589
590 for (in_body = i = 0; (ch = msg[i]) && i < len; i++) {
591 if (!in_body) {
592 /* author could be non 7-bit ASCII but
593 * the log may so; skip over the
594 * header part first.
595 */
596 if (ch == '\n' &&
597 i + 1 < len && msg[i+1] == '\n')
598 in_body = 1;
599 }
600 else if (ch & 0x80) {
c831da66 601 plain_non_ascii = 1;
0da46771
JH
602 break;
603 }
604 }
c831da66
JH
605 }
606
e3bc7a3b
LT
607 for (;;) {
608 const char *line = msg;
609 int linelen = get_one_line(msg, len);
610
611 if (!linelen)
612 break;
613
614 /*
615 * We want some slop for indentation and a possible
616 * final "...". Thus the "+ 20".
617 */
618 if (offset + linelen + 20 > space) {
619 memcpy(buf + offset, " ...\n", 8);
620 offset += 8;
621 break;
622 }
623
624 msg += linelen;
625 len -= linelen;
e3bc7a3b 626 if (hdr) {
000182ea
LT
627 if (linelen == 1) {
628 hdr = 0;
3eefc189 629 if ((fmt != CMIT_FMT_ONELINE) && !subject)
d87449c5 630 buf[offset++] = '\n';
000182ea
LT
631 continue;
632 }
633 if (fmt == CMIT_FMT_RAW) {
634 memcpy(buf + offset, line, linelen);
635 offset += linelen;
636 continue;
637 }
28342a5d
LT
638 if (!memcmp(line, "parent ", 7)) {
639 if (linelen != 48)
640 die("bad parent line in commit");
f2d42275 641 continue;
28342a5d 642 }
ff56fe1c 643
f2d42275
JH
644 if (!parents_shown) {
645 offset += add_merge_info(fmt, buf + offset,
646 commit, abbrev);
647 parents_shown = 1;
648 continue;
649 }
ff56fe1c
JH
650 /*
651 * MEDIUM == DEFAULT shows only author with dates.
652 * FULL shows both authors but not dates.
653 * FULLER shows both authors and dates.
654 */
e3bc7a3b 655 if (!memcmp(line, "author ", 7))
ff56fe1c
JH
656 offset += add_user_info("Author", fmt,
657 buf + offset,
3dfb9278
JF
658 line + 7,
659 relative_date);
ff56fe1c
JH
660 if (!memcmp(line, "committer ", 10) &&
661 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
662 offset += add_user_info("Commit", fmt,
663 buf + offset,
3dfb9278
JF
664 line + 10,
665 relative_date);
e3bc7a3b
LT
666 continue;
667 }
000182ea 668
19b3bd3e
RS
669 if (!subject)
670 body = 1;
671
3eefc189 672 if (is_empty_line(line, &linelen)) {
000182ea
LT
673 if (!body)
674 continue;
3eefc189
JH
675 if (subject)
676 continue;
000182ea
LT
677 if (fmt == CMIT_FMT_SHORT)
678 break;
000182ea 679 }
d87449c5 680
3eefc189 681 if (subject) {
53f420ef
JH
682 int slen = strlen(subject);
683 memcpy(buf + offset, subject, slen);
684 offset += slen;
cdd406e3
JH
685 offset += add_rfc2047(buf + offset, line, linelen);
686 }
687 else {
688 memset(buf + offset, ' ', indent);
689 memcpy(buf + offset + indent, line, linelen);
690 offset += linelen + indent;
3eefc189 691 }
3eefc189 692 buf[offset++] = '\n';
d87449c5
JH
693 if (fmt == CMIT_FMT_ONELINE)
694 break;
c831da66 695 if (subject && plain_non_ascii) {
cdd406e3
JH
696 static const char header[] =
697 "Content-Type: text/plain; charset=UTF-8\n"
698 "Content-Transfer-Encoding: 8bit\n";
699 memcpy(buf + offset, header, sizeof(header)-1);
700 offset += sizeof(header)-1;
cdd406e3 701 }
698ce6f8
JS
702 if (after_subject) {
703 int slen = strlen(after_subject);
704 if (slen > space - offset - 1)
705 slen = space - offset - 1;
706 memcpy(buf + offset, after_subject, slen);
707 offset += slen;
708 after_subject = NULL;
709 }
3eefc189 710 subject = NULL;
d87449c5 711 }
40c2fe00
LT
712 while (offset && isspace(buf[offset-1]))
713 offset--;
714 /* Make sure there is an EOLN for the non-oneline case */
715 if (fmt != CMIT_FMT_ONELINE)
716 buf[offset++] = '\n';
19b3bd3e
RS
717 /*
718 * make sure there is another EOLN to separate the headers from whatever
719 * body the caller appends if we haven't already written a body
720 */
721 if (fmt == CMIT_FMT_EMAIL && !body)
722 buf[offset++] = '\n';
e3bc7a3b
LT
723 buf[offset] = '\0';
724 return offset;
725}
a3437b8c
JS
726
727struct commit *pop_commit(struct commit_list **stack)
728{
729 struct commit_list *top = *stack;
730 struct commit *item = top ? top->item : NULL;
731
732 if (top) {
733 *stack = top->next;
734 free(top);
735 }
736 return item;
737}
738
739int count_parents(struct commit * commit)
740{
96f1e58f 741 int count;
a3437b8c 742 struct commit_list * parents = commit->parents;
96f1e58f
DR
743 for (count = 0; parents; parents = parents->next,count++)
744 ;
a3437b8c
JS
745 return count;
746}
747
6b6dcfc2
FK
748void topo_sort_default_setter(struct commit *c, void *data)
749{
d3ff6f55 750 c->util = data;
6b6dcfc2
FK
751}
752
753void *topo_sort_default_getter(struct commit *c)
754{
d3ff6f55 755 return c->util;
6b6dcfc2
FK
756}
757
ab580ace
JS
758/*
759 * Performs an in-place topological sort on the list supplied.
760 */
4c8725f1 761void sort_in_topological_order(struct commit_list ** list, int lifo)
6b6dcfc2
FK
762{
763 sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
764 topo_sort_default_getter);
765}
766
767void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
768 topo_sort_set_fn_t setter,
769 topo_sort_get_fn_t getter)
ab580ace
JS
770{
771 struct commit_list * next = *list;
2ed02887 772 struct commit_list * work = NULL, **insert;
ab580ace
JS
773 struct commit_list ** pptr = list;
774 struct sort_node * nodes;
775 struct sort_node * next_nodes;
776 int count = 0;
777
778 /* determine the size of the list */
779 while (next) {
780 next = next->next;
781 count++;
782 }
7d6fb370
EW
783
784 if (!count)
785 return;
ab580ace
JS
786 /* allocate an array to help sort the list */
787 nodes = xcalloc(count, sizeof(*nodes));
788 /* link the list to the array */
789 next_nodes = nodes;
790 next=*list;
791 while (next) {
792 next_nodes->list_item = next;
6b6dcfc2 793 setter(next->item, next_nodes);
ab580ace
JS
794 next_nodes++;
795 next = next->next;
796 }
797 /* update the indegree */
798 next=*list;
799 while (next) {
800 struct commit_list * parents = next->item->parents;
801 while (parents) {
802 struct commit * parent=parents->item;
6b6dcfc2
FK
803 struct sort_node * pn = (struct sort_node *) getter(parent);
804
ab580ace
JS
805 if (pn)
806 pn->indegree++;
807 parents=parents->next;
808 }
809 next=next->next;
810 }
811 /*
812 * find the tips
813 *
814 * tips are nodes not reachable from any other node in the list
815 *
816 * the tips serve as a starting set for the work queue.
817 */
818 next=*list;
2ed02887 819 insert = &work;
ab580ace 820 while (next) {
6b6dcfc2 821 struct sort_node * node = (struct sort_node *) getter(next->item);
ab580ace
JS
822
823 if (node->indegree == 0) {
2ed02887 824 insert = &commit_list_insert(next->item, insert)->next;
ab580ace
JS
825 }
826 next=next->next;
827 }
4c8725f1 828
ab580ace 829 /* process the list in topological order */
4c8725f1
JH
830 if (!lifo)
831 sort_by_date(&work);
ab580ace
JS
832 while (work) {
833 struct commit * work_item = pop_commit(&work);
6b6dcfc2 834 struct sort_node * work_node = (struct sort_node *) getter(work_item);
ab580ace
JS
835 struct commit_list * parents = work_item->parents;
836
837 while (parents) {
838 struct commit * parent=parents->item;
6b6dcfc2
FK
839 struct sort_node * pn = (struct sort_node *) getter(parent);
840
ab580ace 841 if (pn) {
6b6dcfc2 842 /*
ab580ace
JS
843 * parents are only enqueued for emission
844 * when all their children have been emitted thereby
845 * guaranteeing topological order.
846 */
847 pn->indegree--;
4c8725f1
JH
848 if (!pn->indegree) {
849 if (!lifo)
850 insert_by_date(parent, &work);
851 else
852 commit_list_insert(parent, &work);
853 }
ab580ace
JS
854 }
855 parents=parents->next;
856 }
857 /*
858 * work_item is a commit all of whose children
859 * have already been emitted. we can emit it now.
860 */
861 *pptr = work_node->list_item;
862 pptr = &(*pptr)->next;
863 *pptr = NULL;
6b6dcfc2 864 setter(work_item, NULL);
ab580ace
JS
865 }
866 free(nodes);
867}
7c6f8aaf
JS
868
869/* merge-rebase stuff */
870
31609c17
RS
871/* bits #0..7 in revision.h */
872#define PARENT1 (1u<< 8)
873#define PARENT2 (1u<< 9)
542ccefe 874#define STALE (1u<<10)
f3249438 875#define RESULT (1u<<11)
7c6f8aaf
JS
876
877static struct commit *interesting(struct commit_list *list)
878{
879 while (list) {
880 struct commit *commit = list->item;
881 list = list->next;
542ccefe 882 if (commit->object.flags & STALE)
7c6f8aaf
JS
883 continue;
884 return commit;
885 }
886 return NULL;
887}
888
f3249438 889static struct commit_list *merge_bases(struct commit *one, struct commit *two)
7c6f8aaf
JS
890{
891 struct commit_list *list = NULL;
892 struct commit_list *result = NULL;
7c6f8aaf 893
f3249438
JH
894 if (one == two)
895 /* We do not mark this even with RESULT so we do not
896 * have to clean it up.
897 */
898 return commit_list_insert(one, &result);
7c6f8aaf 899
f3249438
JH
900 parse_commit(one);
901 parse_commit(two);
7c6f8aaf 902
f3249438
JH
903 one->object.flags |= PARENT1;
904 two->object.flags |= PARENT2;
905 insert_by_date(one, &list);
906 insert_by_date(two, &list);
7c6f8aaf
JS
907
908 while (interesting(list)) {
f3249438 909 struct commit *commit;
7c6f8aaf 910 struct commit_list *parents;
f3249438
JH
911 struct commit_list *n;
912 int flags;
7c6f8aaf 913
f3249438
JH
914 commit = list->item;
915 n = list->next;
916 free(list);
917 list = n;
7c6f8aaf 918
f3249438
JH
919 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
920 if (flags == (PARENT1 | PARENT2)) {
921 if (!(commit->object.flags & RESULT)) {
922 commit->object.flags |= RESULT;
923 insert_by_date(commit, &result);
924 }
542ccefe
JH
925 /* Mark parents of a found merge stale */
926 flags |= STALE;
7c6f8aaf
JS
927 }
928 parents = commit->parents;
929 while (parents) {
930 struct commit *p = parents->item;
931 parents = parents->next;
932 if ((p->object.flags & flags) == flags)
933 continue;
934 parse_commit(p);
935 p->object.flags |= flags;
936 insert_by_date(p, &list);
937 }
938 }
939
f3249438
JH
940 /* Clean up the result to remove stale ones */
941 list = result; result = NULL;
942 while (list) {
943 struct commit_list *n = list->next;
944 if (!(list->item->object.flags & STALE))
945 insert_by_date(list->item, &result);
946 free(list);
947 list = n;
948 }
949 return result;
950}
7c6f8aaf 951
f3249438
JH
952struct commit_list *get_merge_bases(struct commit *one,
953 struct commit *two,
954 int cleanup)
955{
956 const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
957 struct commit_list *list;
958 struct commit **rslt;
959 struct commit_list *result;
960 int cnt, i, j;
961
962 result = merge_bases(one, two);
963 if (one == two)
964 return result;
965 if (!result || !result->next) {
966 if (cleanup) {
967 clear_commit_marks(one, all_flags);
968 clear_commit_marks(two, all_flags);
7c6f8aaf 969 }
f3249438 970 return result;
7c6f8aaf
JS
971 }
972
f3249438
JH
973 /* There are more than one */
974 cnt = 0;
975 list = result;
976 while (list) {
977 list = list->next;
978 cnt++;
979 }
980 rslt = xcalloc(cnt, sizeof(*rslt));
981 for (list = result, i = 0; list; list = list->next)
982 rslt[i++] = list->item;
983 free_commit_list(result);
984
985 clear_commit_marks(one, all_flags);
986 clear_commit_marks(two, all_flags);
987 for (i = 0; i < cnt - 1; i++) {
988 for (j = i+1; j < cnt; j++) {
989 if (!rslt[i] || !rslt[j])
990 continue;
991 result = merge_bases(rslt[i], rslt[j]);
992 clear_commit_marks(rslt[i], all_flags);
993 clear_commit_marks(rslt[j], all_flags);
994 for (list = result; list; list = list->next) {
995 if (rslt[i] == list->item)
996 rslt[i] = NULL;
997 if (rslt[j] == list->item)
998 rslt[j] = NULL;
999 }
1000 }
58ecf5c1 1001 }
7c6f8aaf 1002
f3249438
JH
1003 /* Surviving ones in rslt[] are the independent results */
1004 result = NULL;
1005 for (i = 0; i < cnt; i++) {
1006 if (rslt[i])
1007 insert_by_date(rslt[i], &result);
1008 }
1009 free(rslt);
7c6f8aaf
JS
1010 return result;
1011}