Allow in_merge_bases() to take more than one reference commits.
[git/git.git] / commit.c
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "pkt-line.h"
5 #include "utf8.h"
6
7 int save_commit_buffer = 1;
8
9 struct sort_node
10 {
11 /*
12 * the number of children of the associated commit
13 * that also occur in the list being sorted.
14 */
15 unsigned int indegree;
16
17 /*
18 * reference to original list item that we will re-use
19 * on output.
20 */
21 struct commit_list * list_item;
22
23 };
24
25 const char *commit_type = "commit";
26
27 struct 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 },
35 { "email", 1, CMIT_FMT_EMAIL },
36 { "full", 5, CMIT_FMT_FULL },
37 { "fuller", 5, CMIT_FMT_FULLER },
38 { "oneline", 1, CMIT_FMT_ONELINE },
39 };
40
41 enum cmit_fmt get_commit_format(const char *arg)
42 {
43 int i;
44
45 if (!arg || !*arg)
46 return CMIT_FMT_DEFAULT;
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);
55 }
56
57 static struct commit *check_commit(struct object *obj,
58 const unsigned char *sha1,
59 int quiet)
60 {
61 if (obj->type != OBJ_COMMIT) {
62 if (!quiet)
63 error("Object %s is a %s, not a commit",
64 sha1_to_hex(sha1), typename(obj->type));
65 return NULL;
66 }
67 return (struct commit *) obj;
68 }
69
70 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
71 int quiet)
72 {
73 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
74
75 if (!obj)
76 return NULL;
77 return check_commit(obj, sha1, quiet);
78 }
79
80 struct commit *lookup_commit_reference(const unsigned char *sha1)
81 {
82 return lookup_commit_reference_gently(sha1, 0);
83 }
84
85 struct commit *lookup_commit(const unsigned char *sha1)
86 {
87 struct object *obj = lookup_object(sha1);
88 if (!obj) {
89 struct commit *ret = alloc_commit_node();
90 created_object(sha1, &ret->object);
91 ret->object.type = OBJ_COMMIT;
92 return ret;
93 }
94 if (!obj->type)
95 obj->type = OBJ_COMMIT;
96 return check_commit(obj, sha1, 0);
97 }
98
99 static 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
117 static struct commit_graft **commit_graft;
118 static int commit_graft_alloc, commit_graft_nr;
119
120 static 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];
128 int cmp = hashcmp(sha1, graft->sha1);
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
139 int 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
168 struct 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;
176 if (buf[0] == '#' || buf[0] == '\0')
177 return NULL;
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
198 int read_graft_file(const char *graft_file)
199 {
200 FILE *fp = fopen(graft_file, "r");
201 char buf[1024];
202 if (!fp)
203 return -1;
204 while (fgets(buf, sizeof(buf), fp)) {
205 /* The format is just "Commit Parent1 Parent2 ...\n" */
206 int len = strlen(buf);
207 struct commit_graft *graft = read_graft_line(buf, len);
208 if (!graft)
209 continue;
210 if (register_commit_graft(graft, 1))
211 error("duplicate graft data: %s", buf);
212 }
213 fclose(fp);
214 return 0;
215 }
216
217 static 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);
226 /* make sure shallows are read */
227 is_repository_shallow();
228 commit_graft_prepared = 1;
229 }
230
231 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
232 {
233 int pos;
234 prepare_commit_graft();
235 pos = commit_graft_pos(sha1);
236 if (pos < 0)
237 return NULL;
238 return commit_graft[pos];
239 }
240
241 int 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 if (write_in_full(fd, hex, 40) != 40)
253 break;
254 if (write_in_full(fd, "\n", 1) != 1)
255 break;
256 }
257 }
258 return count;
259 }
260
261 int 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
274 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
275 {
276 char *tail = buffer;
277 char *bufptr = buffer;
278 unsigned char parent[20];
279 struct commit_list **pptr;
280 struct commit_graft *graft;
281 unsigned n_refs = 0;
282
283 if (item->object.parsed)
284 return 0;
285 item->object.parsed = 1;
286 tail += size;
287 if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5))
288 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
289 if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0)
290 return error("bad tree pointer in commit %s",
291 sha1_to_hex(item->object.sha1));
292 item->tree = lookup_tree(parent);
293 if (item->tree)
294 n_refs++;
295 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
296 pptr = &item->parents;
297
298 graft = lookup_commit_graft(item->object.sha1);
299 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
300 struct commit *new_parent;
301
302 if (tail <= bufptr + 48 ||
303 get_sha1_hex(bufptr + 7, parent) ||
304 bufptr[47] != '\n')
305 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
306 bufptr += 48;
307 if (graft)
308 continue;
309 new_parent = lookup_commit(parent);
310 if (new_parent) {
311 pptr = &commit_list_insert(new_parent, pptr)->next;
312 n_refs++;
313 }
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;
323 n_refs++;
324 }
325 }
326 item->date = parse_commit_date(bufptr);
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
339 return 0;
340 }
341
342 int 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);
361 if (save_commit_buffer && !ret) {
362 item->buffer = buffer;
363 return 0;
364 }
365 free(buffer);
366 return ret;
367 }
368
369 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
370 {
371 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
372 new_list->item = item;
373 new_list->next = *list_p;
374 *list_p = new_list;
375 return new_list;
376 }
377
378 void 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 }
386
387 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
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 }
397 return commit_list_insert(item, pp);
398 }
399
400
401 void sort_by_date(struct commit_list **list)
402 {
403 struct commit_list *ret = NULL;
404 while (*list) {
405 insert_by_date((*list)->item, &ret);
406 *list = (*list)->next;
407 }
408 *list = ret;
409 }
410
411 struct commit *pop_most_recent_commit(struct commit_list **list,
412 unsigned int mark)
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) {
422 struct commit *commit = parents->item;
423 parse_commit(commit);
424 if (!(commit->object.flags & mark)) {
425 commit->object.flags |= mark;
426 insert_by_date(commit, list);
427 }
428 parents = parents->next;
429 }
430 return ret;
431 }
432
433 void clear_commit_marks(struct commit *commit, unsigned int mark)
434 {
435 struct commit_list *parents;
436
437 commit->object.flags &= ~mark;
438 parents = commit->parents;
439 while (parents) {
440 struct commit *parent = parents->item;
441
442 /* Have we already cleared this? */
443 if (mark & parent->object.flags)
444 clear_commit_marks(parent, mark);
445 parents = parents->next;
446 }
447 }
448
449 /*
450 * Generic support for pretty-printing the header
451 */
452 static int get_one_line(const char *msg, unsigned long len)
453 {
454 int ret = 0;
455
456 while (len--) {
457 char c = *msg++;
458 if (!c)
459 break;
460 ret++;
461 if (c == '\n')
462 break;
463 }
464 return ret;
465 }
466
467 static int is_rfc2047_special(char ch)
468 {
469 return ((ch & 0x80) || (ch == '=') || (ch == '?') || (ch == '_'));
470 }
471
472 static 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++) {
492 unsigned ch = line[i] & 0xFF;
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
507 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
508 const char *line, int relative_date)
509 {
510 char *date;
511 int namelen;
512 unsigned long time;
513 int tz, ret;
514 const char *filler = " ";
515
516 if (fmt == CMIT_FMT_ONELINE)
517 return 0;
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
525 if (fmt == CMIT_FMT_EMAIL) {
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;
533 filler = "";
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);
545 }
546 switch (fmt) {
547 case CMIT_FMT_MEDIUM:
548 ret += sprintf(buf + ret, "Date: %s\n",
549 show_date(time, tz, relative_date));
550 break;
551 case CMIT_FMT_EMAIL:
552 ret += sprintf(buf + ret, "Date: %s\n",
553 show_rfc2822_date(time, tz));
554 break;
555 case CMIT_FMT_FULLER:
556 ret += sprintf(buf + ret, "%sDate: %s\n", what,
557 show_date(time, tz, relative_date));
558 break;
559 default:
560 /* notin' */
561 break;
562 }
563 return ret;
564 }
565
566 static int is_empty_line(const char *line, int *len_p)
567 {
568 int len = *len_p;
569 while (len && isspace(line[len-1]))
570 len--;
571 *len_p = len;
572 return !len;
573 }
574
575 static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
576 {
577 struct commit_list *parent = commit->parents;
578 int offset;
579
580 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
581 !parent || !parent->next)
582 return 0;
583
584 offset = sprintf(buf, "Merge:");
585
586 while (parent) {
587 struct commit *p = parent->item;
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) ? "..." : "";
595 parent = parent->next;
596
597 offset += sprintf(buf + offset, " %s%s", hex, dots);
598 }
599 buf[offset++] = '\n';
600 return offset;
601 }
602
603 static 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
629 static 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
671 static char *logmsg_reencode(const struct commit *commit)
672 {
673 char *encoding;
674 char *out;
675 char *output_encoding = (git_log_output_encoding
676 ? git_log_output_encoding
677 : git_commit_encoding);
678
679 if (!output_encoding)
680 output_encoding = "utf-8";
681 else if (!*output_encoding)
682 return NULL;
683 encoding = get_header(commit, "encoding");
684 if (!encoding)
685 return NULL;
686 if (!strcmp(encoding, output_encoding))
687 out = strdup(commit->buffer);
688 else
689 out = reencode_string(commit->buffer,
690 output_encoding, encoding);
691 if (out)
692 out = replace_encoding_header(out, output_encoding);
693
694 free(encoding);
695 if (!out)
696 return NULL;
697 return out;
698 }
699
700 unsigned long pretty_print_commit(enum cmit_fmt fmt,
701 const struct commit *commit,
702 unsigned long len,
703 char *buf, unsigned long space,
704 int abbrev, const char *subject,
705 const char *after_subject,
706 int relative_date)
707 {
708 int hdr = 1, body = 0, seen_title = 0;
709 unsigned long offset = 0;
710 int indent = 4;
711 int parents_shown = 0;
712 const char *msg = commit->buffer;
713 int plain_non_ascii = 0;
714 char *reencoded = logmsg_reencode(commit);
715
716 if (reencoded)
717 msg = reencoded;
718
719 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
720 indent = 0;
721
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) {
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
734 * the log may be so; skip over the
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) {
742 plain_non_ascii = 1;
743 break;
744 }
745 }
746 }
747
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;
767 if (hdr) {
768 if (linelen == 1) {
769 hdr = 0;
770 if ((fmt != CMIT_FMT_ONELINE) && !subject)
771 buf[offset++] = '\n';
772 continue;
773 }
774 if (fmt == CMIT_FMT_RAW) {
775 memcpy(buf + offset, line, linelen);
776 offset += linelen;
777 continue;
778 }
779 if (!memcmp(line, "parent ", 7)) {
780 if (linelen != 48)
781 die("bad parent line in commit");
782 continue;
783 }
784
785 if (!parents_shown) {
786 offset += add_merge_info(fmt, buf + offset,
787 commit, abbrev);
788 parents_shown = 1;
789 continue;
790 }
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 */
796 if (!memcmp(line, "author ", 7))
797 offset += add_user_info("Author", fmt,
798 buf + offset,
799 line + 7,
800 relative_date);
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,
805 line + 10,
806 relative_date);
807 continue;
808 }
809
810 if (!subject)
811 body = 1;
812
813 if (is_empty_line(line, &linelen)) {
814 if (!seen_title)
815 continue;
816 if (!body)
817 continue;
818 if (subject)
819 continue;
820 if (fmt == CMIT_FMT_SHORT)
821 break;
822 }
823
824 seen_title = 1;
825 if (subject) {
826 int slen = strlen(subject);
827 memcpy(buf + offset, subject, slen);
828 offset += slen;
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;
835 }
836 buf[offset++] = '\n';
837 if (fmt == CMIT_FMT_ONELINE)
838 break;
839 if (subject && plain_non_ascii) {
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;
845 }
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 }
854 subject = NULL;
855 }
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';
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';
867 buf[offset] = '\0';
868
869 free(reencoded);
870 return offset;
871 }
872
873 struct 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
885 int count_parents(struct commit * commit)
886 {
887 int count;
888 struct commit_list * parents = commit->parents;
889 for (count = 0; parents; parents = parents->next,count++)
890 ;
891 return count;
892 }
893
894 void topo_sort_default_setter(struct commit *c, void *data)
895 {
896 c->util = data;
897 }
898
899 void *topo_sort_default_getter(struct commit *c)
900 {
901 return c->util;
902 }
903
904 /*
905 * Performs an in-place topological sort on the list supplied.
906 */
907 void sort_in_topological_order(struct commit_list ** list, int lifo)
908 {
909 sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
910 topo_sort_default_getter);
911 }
912
913 void 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)
916 {
917 struct commit_list * next = *list;
918 struct commit_list * work = NULL, **insert;
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 }
929
930 if (!count)
931 return;
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;
939 setter(next->item, next_nodes);
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;
949 struct sort_node * pn = (struct sort_node *) getter(parent);
950
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;
965 insert = &work;
966 while (next) {
967 struct sort_node * node = (struct sort_node *) getter(next->item);
968
969 if (node->indegree == 0) {
970 insert = &commit_list_insert(next->item, insert)->next;
971 }
972 next=next->next;
973 }
974
975 /* process the list in topological order */
976 if (!lifo)
977 sort_by_date(&work);
978 while (work) {
979 struct commit * work_item = pop_commit(&work);
980 struct sort_node * work_node = (struct sort_node *) getter(work_item);
981 struct commit_list * parents = work_item->parents;
982
983 while (parents) {
984 struct commit * parent=parents->item;
985 struct sort_node * pn = (struct sort_node *) getter(parent);
986
987 if (pn) {
988 /*
989 * parents are only enqueued for emission
990 * when all their children have been emitted thereby
991 * guaranteeing topological order.
992 */
993 pn->indegree--;
994 if (!pn->indegree) {
995 if (!lifo)
996 insert_by_date(parent, &work);
997 else
998 commit_list_insert(parent, &work);
999 }
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;
1010 setter(work_item, NULL);
1011 }
1012 free(nodes);
1013 }
1014
1015 /* merge-base stuff */
1016
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)
1022
1023 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
1024
1025 static struct commit *interesting(struct commit_list *list)
1026 {
1027 while (list) {
1028 struct commit *commit = list->item;
1029 list = list->next;
1030 if (commit->object.flags & STALE)
1031 continue;
1032 return commit;
1033 }
1034 return NULL;
1035 }
1036
1037 static struct commit_list *merge_bases(struct commit *one, struct commit *two)
1038 {
1039 struct commit_list *list = NULL;
1040 struct commit_list *result = NULL;
1041
1042 if (one == two)
1043 /* We do not mark this even with RESULT so we do not
1044 * have to clean it up.
1045 */
1046 return commit_list_insert(one, &result);
1047
1048 parse_commit(one);
1049 parse_commit(two);
1050
1051 one->object.flags |= PARENT1;
1052 two->object.flags |= PARENT2;
1053 insert_by_date(one, &list);
1054 insert_by_date(two, &list);
1055
1056 while (interesting(list)) {
1057 struct commit *commit;
1058 struct commit_list *parents;
1059 struct commit_list *n;
1060 int flags;
1061
1062 commit = list->item;
1063 n = list->next;
1064 free(list);
1065 list = n;
1066
1067 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
1068 if (flags == (PARENT1 | PARENT2)) {
1069 if (!(commit->object.flags & RESULT)) {
1070 commit->object.flags |= RESULT;
1071 insert_by_date(commit, &result);
1072 }
1073 /* Mark parents of a found merge stale */
1074 flags |= STALE;
1075 }
1076 parents = commit->parents;
1077 while (parents) {
1078 struct commit *p = parents->item;
1079 parents = parents->next;
1080 if ((p->object.flags & flags) == flags)
1081 continue;
1082 parse_commit(p);
1083 p->object.flags |= flags;
1084 insert_by_date(p, &list);
1085 }
1086 }
1087
1088 /* Clean up the result to remove stale ones */
1089 free_commit_list(list);
1090 list = result; result = NULL;
1091 while (list) {
1092 struct commit_list *n = list->next;
1093 if (!(list->item->object.flags & STALE))
1094 insert_by_date(list->item, &result);
1095 free(list);
1096 list = n;
1097 }
1098 return result;
1099 }
1100
1101 struct commit_list *get_merge_bases(struct commit *one,
1102 struct commit *two,
1103 int cleanup)
1104 {
1105 struct commit_list *list;
1106 struct commit **rslt;
1107 struct commit_list *result;
1108 int cnt, i, j;
1109
1110 result = merge_bases(one, two);
1111 if (one == two)
1112 return result;
1113 if (!result || !result->next) {
1114 if (cleanup) {
1115 clear_commit_marks(one, all_flags);
1116 clear_commit_marks(two, all_flags);
1117 }
1118 return result;
1119 }
1120
1121 /* There are more than one */
1122 cnt = 0;
1123 list = result;
1124 while (list) {
1125 list = list->next;
1126 cnt++;
1127 }
1128 rslt = xcalloc(cnt, sizeof(*rslt));
1129 for (list = result, i = 0; list; list = list->next)
1130 rslt[i++] = list->item;
1131 free_commit_list(result);
1132
1133 clear_commit_marks(one, all_flags);
1134 clear_commit_marks(two, all_flags);
1135 for (i = 0; i < cnt - 1; i++) {
1136 for (j = i+1; j < cnt; j++) {
1137 if (!rslt[i] || !rslt[j])
1138 continue;
1139 result = merge_bases(rslt[i], rslt[j]);
1140 clear_commit_marks(rslt[i], all_flags);
1141 clear_commit_marks(rslt[j], all_flags);
1142 for (list = result; list; list = list->next) {
1143 if (rslt[i] == list->item)
1144 rslt[i] = NULL;
1145 if (rslt[j] == list->item)
1146 rslt[j] = NULL;
1147 }
1148 }
1149 }
1150
1151 /* Surviving ones in rslt[] are the independent results */
1152 result = NULL;
1153 for (i = 0; i < cnt; i++) {
1154 if (rslt[i])
1155 insert_by_date(rslt[i], &result);
1156 }
1157 free(rslt);
1158 return result;
1159 }
1160
1161 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
1162 {
1163 struct commit_list *bases, *b;
1164 int ret = 0;
1165
1166 if (num == 1)
1167 bases = get_merge_bases(commit, *reference, 1);
1168 else
1169 die("not yet");
1170 for (b = bases; b; b = b->next) {
1171 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
1172 ret = 1;
1173 break;
1174 }
1175 }
1176
1177 free_commit_list(bases);
1178 return ret;
1179 }