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