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