]> git.ipfire.org Git - thirdparty/git.git/blame - commit.c
git-push through git protocol
[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
f7e68b29
JH
467/* High bit set, or ISO-2022-INT */
468static int non_ascii(int ch)
469{
470 ch = (ch & 0xff);
471 return ((ch & 0x80) || (ch == 0x1b));
472}
473
cdd406e3
JH
474static int is_rfc2047_special(char ch)
475{
f7e68b29 476 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
cdd406e3
JH
477}
478
f7e68b29
JH
479static int add_rfc2047(char *buf, const char *line, int len,
480 const char *encoding)
cdd406e3
JH
481{
482 char *bp = buf;
483 int i, needquote;
f7e68b29
JH
484 char q_encoding[128];
485 const char *q_encoding_fmt = "=?%s?q?";
cdd406e3
JH
486
487 for (i = needquote = 0; !needquote && i < len; i++) {
f7e68b29
JH
488 int ch = line[i];
489 if (non_ascii(ch))
cdd406e3
JH
490 needquote++;
491 if ((i + 1 < len) &&
492 (ch == '=' && line[i+1] == '?'))
493 needquote++;
494 }
495 if (!needquote)
496 return sprintf(buf, "%.*s", len, line);
497
f7e68b29
JH
498 i = snprintf(q_encoding, sizeof(q_encoding), q_encoding_fmt, encoding);
499 if (sizeof(q_encoding) < i)
500 die("Insanely long encoding name %s", encoding);
501 memcpy(bp, q_encoding, i);
502 bp += i;
cdd406e3 503 for (i = 0; i < len; i++) {
0da46771 504 unsigned ch = line[i] & 0xFF;
cdd406e3
JH
505 if (is_rfc2047_special(ch)) {
506 sprintf(bp, "=%02X", ch);
507 bp += 3;
508 }
509 else if (ch == ' ')
510 *bp++ = '_';
511 else
512 *bp++ = ch;
513 }
514 memcpy(bp, "?=", 2);
515 bp += 2;
516 return bp - buf;
517}
518
3dfb9278 519static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
f7e68b29
JH
520 const char *line, int relative_date,
521 const char *encoding)
e3bc7a3b
LT
522{
523 char *date;
5de36bfe 524 int namelen;
e3bc7a3b 525 unsigned long time;
000182ea 526 int tz, ret;
ff56fe1c 527 const char *filler = " ";
e3bc7a3b 528
d87449c5
JH
529 if (fmt == CMIT_FMT_ONELINE)
530 return 0;
e3bc7a3b
LT
531 date = strchr(line, '>');
532 if (!date)
533 return 0;
534 namelen = ++date - line;
535 time = strtoul(date, &date, 10);
536 tz = strtol(date, NULL, 10);
537
3eefc189 538 if (fmt == CMIT_FMT_EMAIL) {
cdd406e3
JH
539 char *name_tail = strchr(line, '<');
540 int display_name_length;
541 if (!name_tail)
542 return 0;
543 while (line < name_tail && isspace(name_tail[-1]))
544 name_tail--;
545 display_name_length = name_tail - line;
3eefc189 546 filler = "";
cdd406e3
JH
547 strcpy(buf, "From: ");
548 ret = strlen(buf);
f7e68b29
JH
549 ret += add_rfc2047(buf + ret, line, display_name_length,
550 encoding);
cdd406e3
JH
551 memcpy(buf + ret, name_tail, namelen - display_name_length);
552 ret += namelen - display_name_length;
553 buf[ret++] = '\n';
554 }
555 else {
556 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
557 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
558 filler, namelen, line);
3eefc189 559 }
ff56fe1c
JH
560 switch (fmt) {
561 case CMIT_FMT_MEDIUM:
3dfb9278
JF
562 ret += sprintf(buf + ret, "Date: %s\n",
563 show_date(time, tz, relative_date));
ff56fe1c 564 break;
3eefc189 565 case CMIT_FMT_EMAIL:
2a387043
JH
566 ret += sprintf(buf + ret, "Date: %s\n",
567 show_rfc2822_date(time, tz));
3eefc189 568 break;
ff56fe1c 569 case CMIT_FMT_FULLER:
3dfb9278
JF
570 ret += sprintf(buf + ret, "%sDate: %s\n", what,
571 show_date(time, tz, relative_date));
ff56fe1c
JH
572 break;
573 default:
574 /* notin' */
575 break;
576 }
000182ea
LT
577 return ret;
578}
579
3eefc189 580static int is_empty_line(const char *line, int *len_p)
000182ea 581{
3eefc189 582 int len = *len_p;
000182ea
LT
583 while (len && isspace(line[len-1]))
584 len--;
3eefc189 585 *len_p = len;
000182ea 586 return !len;
e3bc7a3b
LT
587}
588
f2d42275 589static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
28342a5d 590{
f2d42275
JH
591 struct commit_list *parent = commit->parents;
592 int offset;
d87449c5 593
3eefc189
JH
594 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
595 !parent || !parent->next)
f2d42275
JH
596 return 0;
597
598 offset = sprintf(buf, "Merge:");
599
600 while (parent) {
601 struct commit *p = parent->item;
14763e7b
EW
602 const char *hex = NULL;
603 const char *dots;
604 if (abbrev)
605 hex = find_unique_abbrev(p->object.sha1, abbrev);
606 if (!hex)
607 hex = sha1_to_hex(p->object.sha1);
608 dots = (abbrev && strlen(hex) != 40) ? "..." : "";
f2d42275
JH
609 parent = parent->next;
610
6bfb27a0 611 offset += sprintf(buf + offset, " %s%s", hex, dots);
28342a5d 612 }
f2d42275 613 buf[offset++] = '\n';
28342a5d
LT
614 return offset;
615}
616
52883fbd
JH
617static char *get_header(const struct commit *commit, const char *key)
618{
619 int key_len = strlen(key);
620 const char *line = commit->buffer;
621
622 for (;;) {
623 const char *eol = strchr(line, '\n'), *next;
624
625 if (line == eol)
626 return NULL;
627 if (!eol) {
628 eol = line + strlen(line);
629 next = NULL;
630 } else
631 next = eol + 1;
632 if (!strncmp(line, key, key_len) && line[key_len] == ' ') {
633 int len = eol - line - key_len;
634 char *ret = xmalloc(len);
635 memcpy(ret, line + key_len + 1, len - 1);
636 ret[len - 1] = '\0';
637 return ret;
638 }
639 line = next;
640 }
641}
642
53af9816
JH
643static char *replace_encoding_header(char *buf, char *encoding)
644{
645 char *encoding_header = strstr(buf, "\nencoding ");
646 char *end_of_encoding_header;
647 int encoding_header_pos;
648 int encoding_header_len;
649 int new_len;
650 int need_len;
651 int buflen = strlen(buf) + 1;
652
653 if (!encoding_header)
654 return buf; /* should not happen but be defensive */
655 encoding_header++;
656 end_of_encoding_header = strchr(encoding_header, '\n');
657 if (!end_of_encoding_header)
658 return buf; /* should not happen but be defensive */
659 end_of_encoding_header++;
660
661 encoding_header_len = end_of_encoding_header - encoding_header;
662 encoding_header_pos = encoding_header - buf;
663
664 if (is_encoding_utf8(encoding)) {
665 /* we have re-coded to UTF-8; drop the header */
666 memmove(encoding_header, end_of_encoding_header,
667 buflen - (encoding_header_pos + encoding_header_len));
668 return buf;
669 }
670 new_len = strlen(encoding);
671 need_len = new_len + strlen("encoding \n");
672 if (encoding_header_len < need_len) {
673 buf = xrealloc(buf, buflen + (need_len - encoding_header_len));
674 encoding_header = buf + encoding_header_pos;
675 end_of_encoding_header = encoding_header + encoding_header_len;
676 }
677 memmove(end_of_encoding_header + (need_len - encoding_header_len),
678 end_of_encoding_header,
679 buflen - (encoding_header_pos + encoding_header_len));
680 memcpy(encoding_header + 9, encoding, strlen(encoding));
681 encoding_header[9 + new_len] = '\n';
682 return buf;
683}
684
f7e68b29
JH
685static char *logmsg_reencode(const struct commit *commit,
686 char *output_encoding)
52883fbd 687{
d2c11a38 688 char *encoding;
52883fbd 689 char *out;
f7e68b29 690 char *utf8 = "utf-8";
52883fbd 691
f7e68b29 692 if (!*output_encoding)
52883fbd 693 return NULL;
d2c11a38 694 encoding = get_header(commit, "encoding");
e90068a9 695 if (!encoding)
f7e68b29 696 encoding = utf8;
e90068a9
JH
697 if (!strcmp(encoding, output_encoding))
698 out = strdup(commit->buffer);
699 else
700 out = reencode_string(commit->buffer,
701 output_encoding, encoding);
53af9816
JH
702 if (out)
703 out = replace_encoding_header(out, output_encoding);
704
f7e68b29
JH
705 if (encoding != utf8)
706 free(encoding);
52883fbd
JH
707 if (!out)
708 return NULL;
709 return out;
710}
711
712unsigned long pretty_print_commit(enum cmit_fmt fmt,
713 const struct commit *commit,
714 unsigned long len,
715 char *buf, unsigned long space,
3dfb9278 716 int abbrev, const char *subject,
52883fbd
JH
717 const char *after_subject,
718 int relative_date)
e3bc7a3b 719{
f3a47405 720 int hdr = 1, body = 0, seen_title = 0;
e3bc7a3b 721 unsigned long offset = 0;
3eefc189 722 int indent = 4;
f2d42275 723 int parents_shown = 0;
3815f423 724 const char *msg = commit->buffer;
c831da66 725 int plain_non_ascii = 0;
f7e68b29
JH
726 char *reencoded;
727 char *encoding;
52883fbd 728
f7e68b29
JH
729 encoding = (git_log_output_encoding
730 ? git_log_output_encoding
731 : git_commit_encoding);
732 if (!encoding)
733 encoding = "utf-8";
734 reencoded = logmsg_reencode(commit, encoding);
d2c11a38
JH
735 if (reencoded)
736 msg = reencoded;
3eefc189 737
3eefc189
JH
738 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
739 indent = 0;
e3bc7a3b 740
c831da66
JH
741 /* After-subject is used to pass in Content-Type: multipart
742 * MIME header; in that case we do not have to do the
743 * plaintext content type even if the commit message has
744 * non 7-bit ASCII character. Otherwise, check if we need
745 * to say this is not a 7-bit ASCII.
746 */
747 if (fmt == CMIT_FMT_EMAIL && !after_subject) {
0da46771
JH
748 int i, ch, in_body;
749
750 for (in_body = i = 0; (ch = msg[i]) && i < len; i++) {
751 if (!in_body) {
752 /* author could be non 7-bit ASCII but
d2c11a38 753 * the log may be so; skip over the
0da46771
JH
754 * header part first.
755 */
756 if (ch == '\n' &&
757 i + 1 < len && msg[i+1] == '\n')
758 in_body = 1;
759 }
f7e68b29 760 else if (non_ascii(ch)) {
c831da66 761 plain_non_ascii = 1;
0da46771
JH
762 break;
763 }
764 }
c831da66
JH
765 }
766
e3bc7a3b
LT
767 for (;;) {
768 const char *line = msg;
769 int linelen = get_one_line(msg, len);
770
771 if (!linelen)
772 break;
773
774 /*
775 * We want some slop for indentation and a possible
776 * final "...". Thus the "+ 20".
777 */
778 if (offset + linelen + 20 > space) {
779 memcpy(buf + offset, " ...\n", 8);
780 offset += 8;
781 break;
782 }
783
784 msg += linelen;
785 len -= linelen;
e3bc7a3b 786 if (hdr) {
000182ea
LT
787 if (linelen == 1) {
788 hdr = 0;
3eefc189 789 if ((fmt != CMIT_FMT_ONELINE) && !subject)
d87449c5 790 buf[offset++] = '\n';
000182ea
LT
791 continue;
792 }
793 if (fmt == CMIT_FMT_RAW) {
794 memcpy(buf + offset, line, linelen);
795 offset += linelen;
796 continue;
797 }
28342a5d
LT
798 if (!memcmp(line, "parent ", 7)) {
799 if (linelen != 48)
800 die("bad parent line in commit");
f2d42275 801 continue;
28342a5d 802 }
ff56fe1c 803
f2d42275
JH
804 if (!parents_shown) {
805 offset += add_merge_info(fmt, buf + offset,
806 commit, abbrev);
807 parents_shown = 1;
808 continue;
809 }
ff56fe1c
JH
810 /*
811 * MEDIUM == DEFAULT shows only author with dates.
812 * FULL shows both authors but not dates.
813 * FULLER shows both authors and dates.
814 */
e3bc7a3b 815 if (!memcmp(line, "author ", 7))
ff56fe1c
JH
816 offset += add_user_info("Author", fmt,
817 buf + offset,
3dfb9278 818 line + 7,
f7e68b29
JH
819 relative_date,
820 encoding);
ff56fe1c
JH
821 if (!memcmp(line, "committer ", 10) &&
822 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
823 offset += add_user_info("Commit", fmt,
824 buf + offset,
3dfb9278 825 line + 10,
f7e68b29
JH
826 relative_date,
827 encoding);
e3bc7a3b
LT
828 continue;
829 }
000182ea 830
19b3bd3e
RS
831 if (!subject)
832 body = 1;
833
3eefc189 834 if (is_empty_line(line, &linelen)) {
f3a47405
LH
835 if (!seen_title)
836 continue;
000182ea
LT
837 if (!body)
838 continue;
3eefc189
JH
839 if (subject)
840 continue;
000182ea
LT
841 if (fmt == CMIT_FMT_SHORT)
842 break;
000182ea 843 }
d87449c5 844
f3a47405 845 seen_title = 1;
3eefc189 846 if (subject) {
53f420ef
JH
847 int slen = strlen(subject);
848 memcpy(buf + offset, subject, slen);
849 offset += slen;
f7e68b29
JH
850 offset += add_rfc2047(buf + offset, line, linelen,
851 encoding);
cdd406e3
JH
852 }
853 else {
854 memset(buf + offset, ' ', indent);
855 memcpy(buf + offset + indent, line, linelen);
856 offset += linelen + indent;
3eefc189 857 }
3eefc189 858 buf[offset++] = '\n';
d87449c5
JH
859 if (fmt == CMIT_FMT_ONELINE)
860 break;
c831da66 861 if (subject && plain_non_ascii) {
f7e68b29
JH
862 int sz;
863 char header[512];
864 const char *header_fmt =
865 "Content-Type: text/plain; charset=%s\n"
cdd406e3 866 "Content-Transfer-Encoding: 8bit\n";
f7e68b29
JH
867 sz = snprintf(header, sizeof(header), header_fmt,
868 encoding);
869 if (sizeof(header) < sz)
870 die("Encoding name %s too long", encoding);
871 memcpy(buf + offset, header, sz);
872 offset += sz;
cdd406e3 873 }
698ce6f8
JS
874 if (after_subject) {
875 int slen = strlen(after_subject);
876 if (slen > space - offset - 1)
877 slen = space - offset - 1;
878 memcpy(buf + offset, after_subject, slen);
879 offset += slen;
880 after_subject = NULL;
881 }
3eefc189 882 subject = NULL;
d87449c5 883 }
40c2fe00
LT
884 while (offset && isspace(buf[offset-1]))
885 offset--;
886 /* Make sure there is an EOLN for the non-oneline case */
887 if (fmt != CMIT_FMT_ONELINE)
888 buf[offset++] = '\n';
19b3bd3e
RS
889 /*
890 * make sure there is another EOLN to separate the headers from whatever
891 * body the caller appends if we haven't already written a body
892 */
893 if (fmt == CMIT_FMT_EMAIL && !body)
894 buf[offset++] = '\n';
e3bc7a3b 895 buf[offset] = '\0';
52883fbd
JH
896
897 free(reencoded);
e3bc7a3b
LT
898 return offset;
899}
a3437b8c
JS
900
901struct commit *pop_commit(struct commit_list **stack)
902{
903 struct commit_list *top = *stack;
904 struct commit *item = top ? top->item : NULL;
905
906 if (top) {
907 *stack = top->next;
908 free(top);
909 }
910 return item;
911}
912
913int count_parents(struct commit * commit)
914{
96f1e58f 915 int count;
a3437b8c 916 struct commit_list * parents = commit->parents;
96f1e58f
DR
917 for (count = 0; parents; parents = parents->next,count++)
918 ;
a3437b8c
JS
919 return count;
920}
921
6b6dcfc2
FK
922void topo_sort_default_setter(struct commit *c, void *data)
923{
d3ff6f55 924 c->util = data;
6b6dcfc2
FK
925}
926
927void *topo_sort_default_getter(struct commit *c)
928{
d3ff6f55 929 return c->util;
6b6dcfc2
FK
930}
931
ab580ace
JS
932/*
933 * Performs an in-place topological sort on the list supplied.
934 */
4c8725f1 935void sort_in_topological_order(struct commit_list ** list, int lifo)
6b6dcfc2
FK
936{
937 sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
938 topo_sort_default_getter);
939}
940
941void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
942 topo_sort_set_fn_t setter,
943 topo_sort_get_fn_t getter)
ab580ace
JS
944{
945 struct commit_list * next = *list;
2ed02887 946 struct commit_list * work = NULL, **insert;
ab580ace
JS
947 struct commit_list ** pptr = list;
948 struct sort_node * nodes;
949 struct sort_node * next_nodes;
950 int count = 0;
951
952 /* determine the size of the list */
953 while (next) {
954 next = next->next;
955 count++;
956 }
7d6fb370
EW
957
958 if (!count)
959 return;
ab580ace
JS
960 /* allocate an array to help sort the list */
961 nodes = xcalloc(count, sizeof(*nodes));
962 /* link the list to the array */
963 next_nodes = nodes;
964 next=*list;
965 while (next) {
966 next_nodes->list_item = next;
6b6dcfc2 967 setter(next->item, next_nodes);
ab580ace
JS
968 next_nodes++;
969 next = next->next;
970 }
971 /* update the indegree */
972 next=*list;
973 while (next) {
974 struct commit_list * parents = next->item->parents;
975 while (parents) {
976 struct commit * parent=parents->item;
6b6dcfc2
FK
977 struct sort_node * pn = (struct sort_node *) getter(parent);
978
ab580ace
JS
979 if (pn)
980 pn->indegree++;
981 parents=parents->next;
982 }
983 next=next->next;
984 }
985 /*
986 * find the tips
987 *
988 * tips are nodes not reachable from any other node in the list
989 *
990 * the tips serve as a starting set for the work queue.
991 */
992 next=*list;
2ed02887 993 insert = &work;
ab580ace 994 while (next) {
6b6dcfc2 995 struct sort_node * node = (struct sort_node *) getter(next->item);
ab580ace
JS
996
997 if (node->indegree == 0) {
2ed02887 998 insert = &commit_list_insert(next->item, insert)->next;
ab580ace
JS
999 }
1000 next=next->next;
1001 }
4c8725f1 1002
ab580ace 1003 /* process the list in topological order */
4c8725f1
JH
1004 if (!lifo)
1005 sort_by_date(&work);
ab580ace
JS
1006 while (work) {
1007 struct commit * work_item = pop_commit(&work);
6b6dcfc2 1008 struct sort_node * work_node = (struct sort_node *) getter(work_item);
ab580ace
JS
1009 struct commit_list * parents = work_item->parents;
1010
1011 while (parents) {
1012 struct commit * parent=parents->item;
6b6dcfc2
FK
1013 struct sort_node * pn = (struct sort_node *) getter(parent);
1014
ab580ace 1015 if (pn) {
6b6dcfc2 1016 /*
ab580ace
JS
1017 * parents are only enqueued for emission
1018 * when all their children have been emitted thereby
1019 * guaranteeing topological order.
1020 */
1021 pn->indegree--;
4c8725f1
JH
1022 if (!pn->indegree) {
1023 if (!lifo)
1024 insert_by_date(parent, &work);
1025 else
1026 commit_list_insert(parent, &work);
1027 }
ab580ace
JS
1028 }
1029 parents=parents->next;
1030 }
1031 /*
1032 * work_item is a commit all of whose children
1033 * have already been emitted. we can emit it now.
1034 */
1035 *pptr = work_node->list_item;
1036 pptr = &(*pptr)->next;
1037 *pptr = NULL;
6b6dcfc2 1038 setter(work_item, NULL);
ab580ace
JS
1039 }
1040 free(nodes);
1041}
7c6f8aaf 1042
1295228d 1043/* merge-base stuff */
7c6f8aaf 1044
577ed5c2
JH
1045/* bits #0..15 in revision.h */
1046#define PARENT1 (1u<<16)
1047#define PARENT2 (1u<<17)
1048#define STALE (1u<<18)
1049#define RESULT (1u<<19)
7c6f8aaf 1050
1295228d
JH
1051static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
1052
7c6f8aaf
JS
1053static struct commit *interesting(struct commit_list *list)
1054{
1055 while (list) {
1056 struct commit *commit = list->item;
1057 list = list->next;
542ccefe 1058 if (commit->object.flags & STALE)
7c6f8aaf
JS
1059 continue;
1060 return commit;
1061 }
1062 return NULL;
1063}
1064
f3249438 1065static struct commit_list *merge_bases(struct commit *one, struct commit *two)
7c6f8aaf
JS
1066{
1067 struct commit_list *list = NULL;
1068 struct commit_list *result = NULL;
7c6f8aaf 1069
f3249438
JH
1070 if (one == two)
1071 /* We do not mark this even with RESULT so we do not
1072 * have to clean it up.
1073 */
1074 return commit_list_insert(one, &result);
7c6f8aaf 1075
f3249438
JH
1076 parse_commit(one);
1077 parse_commit(two);
7c6f8aaf 1078
f3249438
JH
1079 one->object.flags |= PARENT1;
1080 two->object.flags |= PARENT2;
1081 insert_by_date(one, &list);
1082 insert_by_date(two, &list);
7c6f8aaf
JS
1083
1084 while (interesting(list)) {
f3249438 1085 struct commit *commit;
7c6f8aaf 1086 struct commit_list *parents;
f3249438
JH
1087 struct commit_list *n;
1088 int flags;
7c6f8aaf 1089
f3249438
JH
1090 commit = list->item;
1091 n = list->next;
1092 free(list);
1093 list = n;
7c6f8aaf 1094
f3249438
JH
1095 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
1096 if (flags == (PARENT1 | PARENT2)) {
1097 if (!(commit->object.flags & RESULT)) {
1098 commit->object.flags |= RESULT;
1099 insert_by_date(commit, &result);
1100 }
542ccefe
JH
1101 /* Mark parents of a found merge stale */
1102 flags |= STALE;
7c6f8aaf
JS
1103 }
1104 parents = commit->parents;
1105 while (parents) {
1106 struct commit *p = parents->item;
1107 parents = parents->next;
1108 if ((p->object.flags & flags) == flags)
1109 continue;
1110 parse_commit(p);
1111 p->object.flags |= flags;
1112 insert_by_date(p, &list);
1113 }
1114 }
1115
f3249438 1116 /* Clean up the result to remove stale ones */
1295228d 1117 free_commit_list(list);
f3249438
JH
1118 list = result; result = NULL;
1119 while (list) {
1120 struct commit_list *n = list->next;
1121 if (!(list->item->object.flags & STALE))
1122 insert_by_date(list->item, &result);
1123 free(list);
1124 list = n;
1125 }
1126 return result;
1127}
7c6f8aaf 1128
f3249438
JH
1129struct commit_list *get_merge_bases(struct commit *one,
1130 struct commit *two,
1131 int cleanup)
1132{
f3249438
JH
1133 struct commit_list *list;
1134 struct commit **rslt;
1135 struct commit_list *result;
1136 int cnt, i, j;
1137
1138 result = merge_bases(one, two);
1139 if (one == two)
1140 return result;
1141 if (!result || !result->next) {
1142 if (cleanup) {
1143 clear_commit_marks(one, all_flags);
1144 clear_commit_marks(two, all_flags);
7c6f8aaf 1145 }
f3249438 1146 return result;
7c6f8aaf
JS
1147 }
1148
f3249438
JH
1149 /* There are more than one */
1150 cnt = 0;
1151 list = result;
1152 while (list) {
1153 list = list->next;
1154 cnt++;
1155 }
1156 rslt = xcalloc(cnt, sizeof(*rslt));
1157 for (list = result, i = 0; list; list = list->next)
1158 rslt[i++] = list->item;
1159 free_commit_list(result);
1160
1161 clear_commit_marks(one, all_flags);
1162 clear_commit_marks(two, all_flags);
1163 for (i = 0; i < cnt - 1; i++) {
1164 for (j = i+1; j < cnt; j++) {
1165 if (!rslt[i] || !rslt[j])
1166 continue;
1167 result = merge_bases(rslt[i], rslt[j]);
1168 clear_commit_marks(rslt[i], all_flags);
1169 clear_commit_marks(rslt[j], all_flags);
1170 for (list = result; list; list = list->next) {
1171 if (rslt[i] == list->item)
1172 rslt[i] = NULL;
1173 if (rslt[j] == list->item)
1174 rslt[j] = NULL;
1175 }
1176 }
58ecf5c1 1177 }
7c6f8aaf 1178
f3249438
JH
1179 /* Surviving ones in rslt[] are the independent results */
1180 result = NULL;
1181 for (i = 0; i < cnt; i++) {
1182 if (rslt[i])
1183 insert_by_date(rslt[i], &result);
1184 }
1185 free(rslt);
7c6f8aaf
JS
1186 return result;
1187}
2ecd2bbc
JH
1188
1189int in_merge_bases(struct commit *rev1, struct commit *rev2)
1190{
1191 struct commit_list *bases, *b;
1192 int ret = 0;
1193
1194 bases = get_merge_bases(rev1, rev2, 1);
1195 for (b = bases; b; b = b->next) {
1196 if (!hashcmp(rev1->object.sha1, b->item->object.sha1)) {
1197 ret = 1;
1198 break;
1199 }
1200 }
1201
1202 free_commit_list(bases);
1203 return ret;
1204}