gitview: annotation support
[git/dscho.git] / commit.c
blob952095faa70dd8f5166f106a83382e50e131897a
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "pkt-line.h"
5 #include "utf8.h"
6 #include "interpolate.h"
7 #include "diff.h"
8 #include "revision.h"
10 int save_commit_buffer = 1;
12 struct sort_node
15 * the number of children of the associated commit
16 * that also occur in the list being sorted.
18 unsigned int indegree;
21 * reference to original list item that we will re-use
22 * on output.
24 struct commit_list * list_item;
28 const char *commit_type = "commit";
30 struct cmt_fmt_map {
31 const char *n;
32 size_t cmp_len;
33 enum cmit_fmt v;
34 } cmt_fmts[] = {
35 { "raw", 1, CMIT_FMT_RAW },
36 { "medium", 1, CMIT_FMT_MEDIUM },
37 { "short", 1, CMIT_FMT_SHORT },
38 { "email", 1, CMIT_FMT_EMAIL },
39 { "full", 5, CMIT_FMT_FULL },
40 { "fuller", 5, CMIT_FMT_FULLER },
41 { "oneline", 1, CMIT_FMT_ONELINE },
42 { "format:", 7, CMIT_FMT_USERFORMAT},
45 static char *user_format;
47 enum cmit_fmt get_commit_format(const char *arg)
49 int i;
51 if (!arg || !*arg)
52 return CMIT_FMT_DEFAULT;
53 if (*arg == '=')
54 arg++;
55 if (!prefixcmp(arg, "format:")) {
56 if (user_format)
57 free(user_format);
58 user_format = xstrdup(arg + 7);
59 return CMIT_FMT_USERFORMAT;
61 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
62 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
63 !strncmp(arg, cmt_fmts[i].n, strlen(arg)))
64 return cmt_fmts[i].v;
67 die("invalid --pretty format: %s", arg);
70 static struct commit *check_commit(struct object *obj,
71 const unsigned char *sha1,
72 int quiet)
74 if (obj->type != OBJ_COMMIT) {
75 if (!quiet)
76 error("Object %s is a %s, not a commit",
77 sha1_to_hex(sha1), typename(obj->type));
78 return NULL;
80 return (struct commit *) obj;
83 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
84 int quiet)
86 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
88 if (!obj)
89 return NULL;
90 return check_commit(obj, sha1, quiet);
93 struct commit *lookup_commit_reference(const unsigned char *sha1)
95 return lookup_commit_reference_gently(sha1, 0);
98 struct commit *lookup_commit(const unsigned char *sha1)
100 struct object *obj = lookup_object(sha1);
101 if (!obj) {
102 struct commit *ret = alloc_commit_node();
103 created_object(sha1, &ret->object);
104 ret->object.type = OBJ_COMMIT;
105 return ret;
107 if (!obj->type)
108 obj->type = OBJ_COMMIT;
109 return check_commit(obj, sha1, 0);
112 static unsigned long parse_commit_date(const char *buf)
114 unsigned long date;
116 if (memcmp(buf, "author", 6))
117 return 0;
118 while (*buf++ != '\n')
119 /* nada */;
120 if (memcmp(buf, "committer", 9))
121 return 0;
122 while (*buf++ != '>')
123 /* nada */;
124 date = strtoul(buf, NULL, 10);
125 if (date == ULONG_MAX)
126 date = 0;
127 return date;
130 static struct commit_graft **commit_graft;
131 static int commit_graft_alloc, commit_graft_nr;
133 static int commit_graft_pos(const unsigned char *sha1)
135 int lo, hi;
136 lo = 0;
137 hi = commit_graft_nr;
138 while (lo < hi) {
139 int mi = (lo + hi) / 2;
140 struct commit_graft *graft = commit_graft[mi];
141 int cmp = hashcmp(sha1, graft->sha1);
142 if (!cmp)
143 return mi;
144 if (cmp < 0)
145 hi = mi;
146 else
147 lo = mi + 1;
149 return -lo - 1;
152 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
154 int pos = commit_graft_pos(graft->sha1);
156 if (0 <= pos) {
157 if (ignore_dups)
158 free(graft);
159 else {
160 free(commit_graft[pos]);
161 commit_graft[pos] = graft;
163 return 1;
165 pos = -pos - 1;
166 if (commit_graft_alloc <= ++commit_graft_nr) {
167 commit_graft_alloc = alloc_nr(commit_graft_alloc);
168 commit_graft = xrealloc(commit_graft,
169 sizeof(*commit_graft) *
170 commit_graft_alloc);
172 if (pos < commit_graft_nr)
173 memmove(commit_graft + pos + 1,
174 commit_graft + pos,
175 (commit_graft_nr - pos - 1) *
176 sizeof(*commit_graft));
177 commit_graft[pos] = graft;
178 return 0;
181 struct commit_graft *read_graft_line(char *buf, int len)
183 /* The format is just "Commit Parent1 Parent2 ...\n" */
184 int i;
185 struct commit_graft *graft = NULL;
187 if (buf[len-1] == '\n')
188 buf[--len] = 0;
189 if (buf[0] == '#' || buf[0] == '\0')
190 return NULL;
191 if ((len + 1) % 41) {
192 bad_graft_data:
193 error("bad graft data: %s", buf);
194 free(graft);
195 return NULL;
197 i = (len + 1) / 41 - 1;
198 graft = xmalloc(sizeof(*graft) + 20 * i);
199 graft->nr_parent = i;
200 if (get_sha1_hex(buf, graft->sha1))
201 goto bad_graft_data;
202 for (i = 40; i < len; i += 41) {
203 if (buf[i] != ' ')
204 goto bad_graft_data;
205 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
206 goto bad_graft_data;
208 return graft;
211 int read_graft_file(const char *graft_file)
213 FILE *fp = fopen(graft_file, "r");
214 char buf[1024];
215 if (!fp)
216 return -1;
217 while (fgets(buf, sizeof(buf), fp)) {
218 /* The format is just "Commit Parent1 Parent2 ...\n" */
219 int len = strlen(buf);
220 struct commit_graft *graft = read_graft_line(buf, len);
221 if (!graft)
222 continue;
223 if (register_commit_graft(graft, 1))
224 error("duplicate graft data: %s", buf);
226 fclose(fp);
227 return 0;
230 static void prepare_commit_graft(void)
232 static int commit_graft_prepared;
233 char *graft_file;
235 if (commit_graft_prepared)
236 return;
237 graft_file = get_graft_file();
238 read_graft_file(graft_file);
239 /* make sure shallows are read */
240 is_repository_shallow();
241 commit_graft_prepared = 1;
244 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
246 int pos;
247 prepare_commit_graft();
248 pos = commit_graft_pos(sha1);
249 if (pos < 0)
250 return NULL;
251 return commit_graft[pos];
254 int write_shallow_commits(int fd, int use_pack_protocol)
256 int i, count = 0;
257 for (i = 0; i < commit_graft_nr; i++)
258 if (commit_graft[i]->nr_parent < 0) {
259 const char *hex =
260 sha1_to_hex(commit_graft[i]->sha1);
261 count++;
262 if (use_pack_protocol)
263 packet_write(fd, "shallow %s", hex);
264 else {
265 if (write_in_full(fd, hex, 40) != 40)
266 break;
267 if (write_in_full(fd, "\n", 1) != 1)
268 break;
271 return count;
274 int unregister_shallow(const unsigned char *sha1)
276 int pos = commit_graft_pos(sha1);
277 if (pos < 0)
278 return -1;
279 if (pos + 1 < commit_graft_nr)
280 memcpy(commit_graft + pos, commit_graft + pos + 1,
281 sizeof(struct commit_graft *)
282 * (commit_graft_nr - pos - 1));
283 commit_graft_nr--;
284 return 0;
287 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
289 char *tail = buffer;
290 char *bufptr = buffer;
291 unsigned char parent[20];
292 struct commit_list **pptr;
293 struct commit_graft *graft;
294 unsigned n_refs = 0;
296 if (item->object.parsed)
297 return 0;
298 item->object.parsed = 1;
299 tail += size;
300 if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5))
301 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
302 if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0)
303 return error("bad tree pointer in commit %s",
304 sha1_to_hex(item->object.sha1));
305 item->tree = lookup_tree(parent);
306 if (item->tree)
307 n_refs++;
308 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
309 pptr = &item->parents;
311 graft = lookup_commit_graft(item->object.sha1);
312 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
313 struct commit *new_parent;
315 if (tail <= bufptr + 48 ||
316 get_sha1_hex(bufptr + 7, parent) ||
317 bufptr[47] != '\n')
318 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
319 bufptr += 48;
320 if (graft)
321 continue;
322 new_parent = lookup_commit(parent);
323 if (new_parent) {
324 pptr = &commit_list_insert(new_parent, pptr)->next;
325 n_refs++;
328 if (graft) {
329 int i;
330 struct commit *new_parent;
331 for (i = 0; i < graft->nr_parent; i++) {
332 new_parent = lookup_commit(graft->parent[i]);
333 if (!new_parent)
334 continue;
335 pptr = &commit_list_insert(new_parent, pptr)->next;
336 n_refs++;
339 item->date = parse_commit_date(bufptr);
341 if (track_object_refs) {
342 unsigned i = 0;
343 struct commit_list *p;
344 struct object_refs *refs = alloc_object_refs(n_refs);
345 if (item->tree)
346 refs->ref[i++] = &item->tree->object;
347 for (p = item->parents; p; p = p->next)
348 refs->ref[i++] = &p->item->object;
349 set_object_refs(&item->object, refs);
352 return 0;
355 int parse_commit(struct commit *item)
357 enum object_type type;
358 void *buffer;
359 unsigned long size;
360 int ret;
362 if (item->object.parsed)
363 return 0;
364 buffer = read_sha1_file(item->object.sha1, &type, &size);
365 if (!buffer)
366 return error("Could not read %s",
367 sha1_to_hex(item->object.sha1));
368 if (type != OBJ_COMMIT) {
369 free(buffer);
370 return error("Object %s not a commit",
371 sha1_to_hex(item->object.sha1));
373 ret = parse_commit_buffer(item, buffer, size);
374 if (save_commit_buffer && !ret) {
375 item->buffer = buffer;
376 return 0;
378 free(buffer);
379 return ret;
382 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
384 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
385 new_list->item = item;
386 new_list->next = *list_p;
387 *list_p = new_list;
388 return new_list;
391 void free_commit_list(struct commit_list *list)
393 while (list) {
394 struct commit_list *temp = list;
395 list = temp->next;
396 free(temp);
400 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
402 struct commit_list **pp = list;
403 struct commit_list *p;
404 while ((p = *pp) != NULL) {
405 if (p->item->date < item->date) {
406 break;
408 pp = &p->next;
410 return commit_list_insert(item, pp);
414 void sort_by_date(struct commit_list **list)
416 struct commit_list *ret = NULL;
417 while (*list) {
418 insert_by_date((*list)->item, &ret);
419 *list = (*list)->next;
421 *list = ret;
424 struct commit *pop_most_recent_commit(struct commit_list **list,
425 unsigned int mark)
427 struct commit *ret = (*list)->item;
428 struct commit_list *parents = ret->parents;
429 struct commit_list *old = *list;
431 *list = (*list)->next;
432 free(old);
434 while (parents) {
435 struct commit *commit = parents->item;
436 parse_commit(commit);
437 if (!(commit->object.flags & mark)) {
438 commit->object.flags |= mark;
439 insert_by_date(commit, list);
441 parents = parents->next;
443 return ret;
446 void clear_commit_marks(struct commit *commit, unsigned int mark)
448 struct commit_list *parents;
450 commit->object.flags &= ~mark;
451 parents = commit->parents;
452 while (parents) {
453 struct commit *parent = parents->item;
455 /* Have we already cleared this? */
456 if (mark & parent->object.flags)
457 clear_commit_marks(parent, mark);
458 parents = parents->next;
463 * Generic support for pretty-printing the header
465 static int get_one_line(const char *msg, unsigned long len)
467 int ret = 0;
469 while (len--) {
470 char c = *msg++;
471 if (!c)
472 break;
473 ret++;
474 if (c == '\n')
475 break;
477 return ret;
480 /* High bit set, or ISO-2022-INT */
481 static int non_ascii(int ch)
483 ch = (ch & 0xff);
484 return ((ch & 0x80) || (ch == 0x1b));
487 static int is_rfc2047_special(char ch)
489 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
492 static int add_rfc2047(char *buf, const char *line, int len,
493 const char *encoding)
495 char *bp = buf;
496 int i, needquote;
497 char q_encoding[128];
498 const char *q_encoding_fmt = "=?%s?q?";
500 for (i = needquote = 0; !needquote && i < len; i++) {
501 int ch = line[i];
502 if (non_ascii(ch))
503 needquote++;
504 if ((i + 1 < len) &&
505 (ch == '=' && line[i+1] == '?'))
506 needquote++;
508 if (!needquote)
509 return sprintf(buf, "%.*s", len, line);
511 i = snprintf(q_encoding, sizeof(q_encoding), q_encoding_fmt, encoding);
512 if (sizeof(q_encoding) < i)
513 die("Insanely long encoding name %s", encoding);
514 memcpy(bp, q_encoding, i);
515 bp += i;
516 for (i = 0; i < len; i++) {
517 unsigned ch = line[i] & 0xFF;
518 if (is_rfc2047_special(ch)) {
519 sprintf(bp, "=%02X", ch);
520 bp += 3;
522 else if (ch == ' ')
523 *bp++ = '_';
524 else
525 *bp++ = ch;
527 memcpy(bp, "?=", 2);
528 bp += 2;
529 return bp - buf;
532 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
533 const char *line, int relative_date,
534 const char *encoding)
536 char *date;
537 int namelen;
538 unsigned long time;
539 int tz, ret;
540 const char *filler = " ";
542 if (fmt == CMIT_FMT_ONELINE)
543 return 0;
544 date = strchr(line, '>');
545 if (!date)
546 return 0;
547 namelen = ++date - line;
548 time = strtoul(date, &date, 10);
549 tz = strtol(date, NULL, 10);
551 if (fmt == CMIT_FMT_EMAIL) {
552 char *name_tail = strchr(line, '<');
553 int display_name_length;
554 if (!name_tail)
555 return 0;
556 while (line < name_tail && isspace(name_tail[-1]))
557 name_tail--;
558 display_name_length = name_tail - line;
559 filler = "";
560 strcpy(buf, "From: ");
561 ret = strlen(buf);
562 ret += add_rfc2047(buf + ret, line, display_name_length,
563 encoding);
564 memcpy(buf + ret, name_tail, namelen - display_name_length);
565 ret += namelen - display_name_length;
566 buf[ret++] = '\n';
568 else {
569 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
570 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
571 filler, namelen, line);
573 switch (fmt) {
574 case CMIT_FMT_MEDIUM:
575 ret += sprintf(buf + ret, "Date: %s\n",
576 show_date(time, tz, relative_date));
577 break;
578 case CMIT_FMT_EMAIL:
579 ret += sprintf(buf + ret, "Date: %s\n",
580 show_rfc2822_date(time, tz));
581 break;
582 case CMIT_FMT_FULLER:
583 ret += sprintf(buf + ret, "%sDate: %s\n", what,
584 show_date(time, tz, relative_date));
585 break;
586 default:
587 /* notin' */
588 break;
590 return ret;
593 static int is_empty_line(const char *line, int *len_p)
595 int len = *len_p;
596 while (len && isspace(line[len-1]))
597 len--;
598 *len_p = len;
599 return !len;
602 static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
604 struct commit_list *parent = commit->parents;
605 int offset;
607 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
608 !parent || !parent->next)
609 return 0;
611 offset = sprintf(buf, "Merge:");
613 while (parent) {
614 struct commit *p = parent->item;
615 const char *hex = NULL;
616 const char *dots;
617 if (abbrev)
618 hex = find_unique_abbrev(p->object.sha1, abbrev);
619 if (!hex)
620 hex = sha1_to_hex(p->object.sha1);
621 dots = (abbrev && strlen(hex) != 40) ? "..." : "";
622 parent = parent->next;
624 offset += sprintf(buf + offset, " %s%s", hex, dots);
626 buf[offset++] = '\n';
627 return offset;
630 static char *get_header(const struct commit *commit, const char *key)
632 int key_len = strlen(key);
633 const char *line = commit->buffer;
635 for (;;) {
636 const char *eol = strchr(line, '\n'), *next;
638 if (line == eol)
639 return NULL;
640 if (!eol) {
641 eol = line + strlen(line);
642 next = NULL;
643 } else
644 next = eol + 1;
645 if (!strncmp(line, key, key_len) && line[key_len] == ' ') {
646 int len = eol - line - key_len;
647 char *ret = xmalloc(len);
648 memcpy(ret, line + key_len + 1, len - 1);
649 ret[len - 1] = '\0';
650 return ret;
652 line = next;
656 static char *replace_encoding_header(char *buf, const char *encoding)
658 char *encoding_header = strstr(buf, "\nencoding ");
659 char *header_end = strstr(buf, "\n\n");
660 char *end_of_encoding_header;
661 int encoding_header_pos;
662 int encoding_header_len;
663 int new_len;
664 int need_len;
665 int buflen = strlen(buf) + 1;
667 if (!header_end)
668 header_end = buf + buflen;
669 if (!encoding_header || encoding_header >= header_end)
670 return buf;
671 encoding_header++;
672 end_of_encoding_header = strchr(encoding_header, '\n');
673 if (!end_of_encoding_header)
674 return buf; /* should not happen but be defensive */
675 end_of_encoding_header++;
677 encoding_header_len = end_of_encoding_header - encoding_header;
678 encoding_header_pos = encoding_header - buf;
680 if (is_encoding_utf8(encoding)) {
681 /* we have re-coded to UTF-8; drop the header */
682 memmove(encoding_header, end_of_encoding_header,
683 buflen - (encoding_header_pos + encoding_header_len));
684 return buf;
686 new_len = strlen(encoding);
687 need_len = new_len + strlen("encoding \n");
688 if (encoding_header_len < need_len) {
689 buf = xrealloc(buf, buflen + (need_len - encoding_header_len));
690 encoding_header = buf + encoding_header_pos;
691 end_of_encoding_header = encoding_header + encoding_header_len;
693 memmove(end_of_encoding_header + (need_len - encoding_header_len),
694 end_of_encoding_header,
695 buflen - (encoding_header_pos + encoding_header_len));
696 memcpy(encoding_header + 9, encoding, strlen(encoding));
697 encoding_header[9 + new_len] = '\n';
698 return buf;
701 static char *logmsg_reencode(const struct commit *commit,
702 const char *output_encoding)
704 static const char *utf8 = "utf-8";
705 const char *use_encoding;
706 char *encoding;
707 char *out;
709 if (!*output_encoding)
710 return NULL;
711 encoding = get_header(commit, "encoding");
712 use_encoding = encoding ? encoding : utf8;
713 if (!strcmp(use_encoding, output_encoding))
714 out = xstrdup(commit->buffer);
715 else
716 out = reencode_string(commit->buffer,
717 output_encoding, use_encoding);
718 if (out)
719 out = replace_encoding_header(out, output_encoding);
721 free(encoding);
722 return out;
725 static char *xstrndup(const char *text, int len)
727 char *result = xmalloc(len + 1);
728 memcpy(result, text, len);
729 result[len] = '\0';
730 return result;
733 static void fill_person(struct interp *table, const char *msg, int len)
735 int start, end, tz = 0;
736 unsigned long date;
737 char *ep;
739 /* parse name */
740 for (end = 0; end < len && msg[end] != '<'; end++)
741 ; /* do nothing */
742 start = end + 1;
743 while (end > 0 && isspace(msg[end - 1]))
744 end--;
745 table[0].value = xstrndup(msg, end);
747 if (start >= len)
748 return;
750 /* parse email */
751 for (end = start + 1; end < len && msg[end] != '>'; end++)
752 ; /* do nothing */
754 if (end >= len)
755 return;
757 table[1].value = xstrndup(msg + start, end - start);
759 /* parse date */
760 for (start = end + 1; start < len && isspace(msg[start]); start++)
761 ; /* do nothing */
762 if (start >= len)
763 return;
764 date = strtoul(msg + start, &ep, 10);
765 if (msg + start == ep)
766 return;
768 table[5].value = xstrndup(msg + start, ep - (msg + start));
770 /* parse tz */
771 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
772 ; /* do nothing */
773 if (start + 1 < len) {
774 tz = strtoul(msg + start + 1, NULL, 10);
775 if (msg[start] == '-')
776 tz = -tz;
779 interp_set_entry(table, 2, show_date(date, tz, 0));
780 interp_set_entry(table, 3, show_rfc2822_date(date, tz));
781 interp_set_entry(table, 4, show_date(date, tz, 1));
784 static long format_commit_message(const struct commit *commit,
785 const char *msg, char *buf, unsigned long space)
787 struct interp table[] = {
788 { "%H" }, /* commit hash */
789 { "%h" }, /* abbreviated commit hash */
790 { "%T" }, /* tree hash */
791 { "%t" }, /* abbreviated tree hash */
792 { "%P" }, /* parent hashes */
793 { "%p" }, /* abbreviated parent hashes */
794 { "%an" }, /* author name */
795 { "%ae" }, /* author email */
796 { "%ad" }, /* author date */
797 { "%aD" }, /* author date, RFC2822 style */
798 { "%ar" }, /* author date, relative */
799 { "%at" }, /* author date, UNIX timestamp */
800 { "%cn" }, /* committer name */
801 { "%ce" }, /* committer email */
802 { "%cd" }, /* committer date */
803 { "%cD" }, /* committer date, RFC2822 style */
804 { "%cr" }, /* committer date, relative */
805 { "%ct" }, /* committer date, UNIX timestamp */
806 { "%e" }, /* encoding */
807 { "%s" }, /* subject */
808 { "%b" }, /* body */
809 { "%Cred" }, /* red */
810 { "%Cgreen" }, /* green */
811 { "%Cblue" }, /* blue */
812 { "%Creset" }, /* reset color */
813 { "%n" }, /* newline */
814 { "%m" }, /* left/right/bottom */
816 enum interp_index {
817 IHASH = 0, IHASH_ABBREV,
818 ITREE, ITREE_ABBREV,
819 IPARENTS, IPARENTS_ABBREV,
820 IAUTHOR_NAME, IAUTHOR_EMAIL,
821 IAUTHOR_DATE, IAUTHOR_DATE_RFC2822, IAUTHOR_DATE_RELATIVE,
822 IAUTHOR_TIMESTAMP,
823 ICOMMITTER_NAME, ICOMMITTER_EMAIL,
824 ICOMMITTER_DATE, ICOMMITTER_DATE_RFC2822,
825 ICOMMITTER_DATE_RELATIVE, ICOMMITTER_TIMESTAMP,
826 IENCODING,
827 ISUBJECT,
828 IBODY,
829 IRED, IGREEN, IBLUE, IRESET_COLOR,
830 INEWLINE,
831 ILEFT_RIGHT,
833 struct commit_list *p;
834 char parents[1024];
835 int i;
836 enum { HEADER, SUBJECT, BODY } state;
838 if (ILEFT_RIGHT + 1 != ARRAY_SIZE(table))
839 die("invalid interp table!");
841 /* these are independent of the commit */
842 interp_set_entry(table, IRED, "\033[31m");
843 interp_set_entry(table, IGREEN, "\033[32m");
844 interp_set_entry(table, IBLUE, "\033[34m");
845 interp_set_entry(table, IRESET_COLOR, "\033[m");
846 interp_set_entry(table, INEWLINE, "\n");
848 /* these depend on the commit */
849 if (!commit->object.parsed)
850 parse_object(commit->object.sha1);
851 interp_set_entry(table, IHASH, sha1_to_hex(commit->object.sha1));
852 interp_set_entry(table, IHASH_ABBREV,
853 find_unique_abbrev(commit->object.sha1,
854 DEFAULT_ABBREV));
855 interp_set_entry(table, ITREE, sha1_to_hex(commit->tree->object.sha1));
856 interp_set_entry(table, ITREE_ABBREV,
857 find_unique_abbrev(commit->tree->object.sha1,
858 DEFAULT_ABBREV));
859 interp_set_entry(table, ILEFT_RIGHT,
860 (commit->object.flags & BOUNDARY)
861 ? "-"
862 : (commit->object.flags & SYMMETRIC_LEFT)
863 ? "<"
864 : ">");
866 parents[1] = 0;
867 for (i = 0, p = commit->parents;
868 p && i < sizeof(parents) - 1;
869 p = p->next)
870 i += snprintf(parents + i, sizeof(parents) - i - 1, " %s",
871 sha1_to_hex(p->item->object.sha1));
872 interp_set_entry(table, IPARENTS, parents + 1);
874 parents[1] = 0;
875 for (i = 0, p = commit->parents;
876 p && i < sizeof(parents) - 1;
877 p = p->next)
878 i += snprintf(parents + i, sizeof(parents) - i - 1, " %s",
879 find_unique_abbrev(p->item->object.sha1,
880 DEFAULT_ABBREV));
881 interp_set_entry(table, IPARENTS_ABBREV, parents + 1);
883 for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
884 int eol;
885 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
886 ; /* do nothing */
888 if (state == SUBJECT) {
889 table[ISUBJECT].value = xstrndup(msg + i, eol - i);
890 i = eol;
892 if (i == eol) {
893 state++;
894 /* strip empty lines */
895 while (msg[eol + 1] == '\n')
896 eol++;
897 } else if (!prefixcmp(msg + i, "author "))
898 fill_person(table + IAUTHOR_NAME,
899 msg + i + 7, eol - i - 7);
900 else if (!prefixcmp(msg + i, "committer "))
901 fill_person(table + ICOMMITTER_NAME,
902 msg + i + 10, eol - i - 10);
903 else if (!prefixcmp(msg + i, "encoding "))
904 table[IENCODING].value =
905 xstrndup(msg + i + 9, eol - i - 9);
906 i = eol;
908 if (msg[i])
909 table[IBODY].value = xstrdup(msg + i);
910 for (i = 0; i < ARRAY_SIZE(table); i++)
911 if (!table[i].value)
912 interp_set_entry(table, i, "<unknown>");
914 interpolate(buf, space, user_format, table, ARRAY_SIZE(table));
915 interp_clear_table(table, ARRAY_SIZE(table));
917 return strlen(buf);
920 unsigned long pretty_print_commit(enum cmit_fmt fmt,
921 const struct commit *commit,
922 unsigned long len,
923 char *buf, unsigned long space,
924 int abbrev, const char *subject,
925 const char *after_subject,
926 int relative_date)
928 int hdr = 1, body = 0, seen_title = 0;
929 unsigned long offset = 0;
930 int indent = 4;
931 int parents_shown = 0;
932 const char *msg = commit->buffer;
933 int plain_non_ascii = 0;
934 char *reencoded;
935 const char *encoding;
937 if (fmt == CMIT_FMT_USERFORMAT)
938 return format_commit_message(commit, msg, buf, space);
940 encoding = (git_log_output_encoding
941 ? git_log_output_encoding
942 : git_commit_encoding);
943 if (!encoding)
944 encoding = "utf-8";
945 reencoded = logmsg_reencode(commit, encoding);
946 if (reencoded)
947 msg = reencoded;
949 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
950 indent = 0;
952 /* After-subject is used to pass in Content-Type: multipart
953 * MIME header; in that case we do not have to do the
954 * plaintext content type even if the commit message has
955 * non 7-bit ASCII character. Otherwise, check if we need
956 * to say this is not a 7-bit ASCII.
958 if (fmt == CMIT_FMT_EMAIL && !after_subject) {
959 int i, ch, in_body;
961 for (in_body = i = 0; (ch = msg[i]) && i < len; i++) {
962 if (!in_body) {
963 /* author could be non 7-bit ASCII but
964 * the log may be so; skip over the
965 * header part first.
967 if (ch == '\n' &&
968 i + 1 < len && msg[i+1] == '\n')
969 in_body = 1;
971 else if (non_ascii(ch)) {
972 plain_non_ascii = 1;
973 break;
978 for (;;) {
979 const char *line = msg;
980 int linelen = get_one_line(msg, len);
982 if (!linelen)
983 break;
986 * We want some slop for indentation and a possible
987 * final "...". Thus the "+ 20".
989 if (offset + linelen + 20 > space) {
990 memcpy(buf + offset, " ...\n", 8);
991 offset += 8;
992 break;
995 msg += linelen;
996 len -= linelen;
997 if (hdr) {
998 if (linelen == 1) {
999 hdr = 0;
1000 if ((fmt != CMIT_FMT_ONELINE) && !subject)
1001 buf[offset++] = '\n';
1002 continue;
1004 if (fmt == CMIT_FMT_RAW) {
1005 memcpy(buf + offset, line, linelen);
1006 offset += linelen;
1007 continue;
1009 if (!memcmp(line, "parent ", 7)) {
1010 if (linelen != 48)
1011 die("bad parent line in commit");
1012 continue;
1015 if (!parents_shown) {
1016 offset += add_merge_info(fmt, buf + offset,
1017 commit, abbrev);
1018 parents_shown = 1;
1019 continue;
1022 * MEDIUM == DEFAULT shows only author with dates.
1023 * FULL shows both authors but not dates.
1024 * FULLER shows both authors and dates.
1026 if (!memcmp(line, "author ", 7))
1027 offset += add_user_info("Author", fmt,
1028 buf + offset,
1029 line + 7,
1030 relative_date,
1031 encoding);
1032 if (!memcmp(line, "committer ", 10) &&
1033 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
1034 offset += add_user_info("Commit", fmt,
1035 buf + offset,
1036 line + 10,
1037 relative_date,
1038 encoding);
1039 continue;
1042 if (!subject)
1043 body = 1;
1045 if (is_empty_line(line, &linelen)) {
1046 if (!seen_title)
1047 continue;
1048 if (!body)
1049 continue;
1050 if (subject)
1051 continue;
1052 if (fmt == CMIT_FMT_SHORT)
1053 break;
1056 seen_title = 1;
1057 if (subject) {
1058 int slen = strlen(subject);
1059 memcpy(buf + offset, subject, slen);
1060 offset += slen;
1061 offset += add_rfc2047(buf + offset, line, linelen,
1062 encoding);
1064 else {
1065 memset(buf + offset, ' ', indent);
1066 memcpy(buf + offset + indent, line, linelen);
1067 offset += linelen + indent;
1069 buf[offset++] = '\n';
1070 if (fmt == CMIT_FMT_ONELINE)
1071 break;
1072 if (subject && plain_non_ascii) {
1073 int sz;
1074 char header[512];
1075 const char *header_fmt =
1076 "Content-Type: text/plain; charset=%s\n"
1077 "Content-Transfer-Encoding: 8bit\n";
1078 sz = snprintf(header, sizeof(header), header_fmt,
1079 encoding);
1080 if (sizeof(header) < sz)
1081 die("Encoding name %s too long", encoding);
1082 memcpy(buf + offset, header, sz);
1083 offset += sz;
1085 if (after_subject) {
1086 int slen = strlen(after_subject);
1087 if (slen > space - offset - 1)
1088 slen = space - offset - 1;
1089 memcpy(buf + offset, after_subject, slen);
1090 offset += slen;
1091 after_subject = NULL;
1093 subject = NULL;
1095 while (offset && isspace(buf[offset-1]))
1096 offset--;
1097 /* Make sure there is an EOLN for the non-oneline case */
1098 if (fmt != CMIT_FMT_ONELINE)
1099 buf[offset++] = '\n';
1101 * make sure there is another EOLN to separate the headers from whatever
1102 * body the caller appends if we haven't already written a body
1104 if (fmt == CMIT_FMT_EMAIL && !body)
1105 buf[offset++] = '\n';
1106 buf[offset] = '\0';
1108 free(reencoded);
1109 return offset;
1112 struct commit *pop_commit(struct commit_list **stack)
1114 struct commit_list *top = *stack;
1115 struct commit *item = top ? top->item : NULL;
1117 if (top) {
1118 *stack = top->next;
1119 free(top);
1121 return item;
1124 int count_parents(struct commit * commit)
1126 int count;
1127 struct commit_list * parents = commit->parents;
1128 for (count = 0; parents; parents = parents->next,count++)
1130 return count;
1133 void topo_sort_default_setter(struct commit *c, void *data)
1135 c->util = data;
1138 void *topo_sort_default_getter(struct commit *c)
1140 return c->util;
1144 * Performs an in-place topological sort on the list supplied.
1146 void sort_in_topological_order(struct commit_list ** list, int lifo)
1148 sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
1149 topo_sort_default_getter);
1152 void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
1153 topo_sort_set_fn_t setter,
1154 topo_sort_get_fn_t getter)
1156 struct commit_list * next = *list;
1157 struct commit_list * work = NULL, **insert;
1158 struct commit_list ** pptr = list;
1159 struct sort_node * nodes;
1160 struct sort_node * next_nodes;
1161 int count = 0;
1163 /* determine the size of the list */
1164 while (next) {
1165 next = next->next;
1166 count++;
1169 if (!count)
1170 return;
1171 /* allocate an array to help sort the list */
1172 nodes = xcalloc(count, sizeof(*nodes));
1173 /* link the list to the array */
1174 next_nodes = nodes;
1175 next=*list;
1176 while (next) {
1177 next_nodes->list_item = next;
1178 setter(next->item, next_nodes);
1179 next_nodes++;
1180 next = next->next;
1182 /* update the indegree */
1183 next=*list;
1184 while (next) {
1185 struct commit_list * parents = next->item->parents;
1186 while (parents) {
1187 struct commit * parent=parents->item;
1188 struct sort_node * pn = (struct sort_node *) getter(parent);
1190 if (pn)
1191 pn->indegree++;
1192 parents=parents->next;
1194 next=next->next;
1197 * find the tips
1199 * tips are nodes not reachable from any other node in the list
1201 * the tips serve as a starting set for the work queue.
1203 next=*list;
1204 insert = &work;
1205 while (next) {
1206 struct sort_node * node = (struct sort_node *) getter(next->item);
1208 if (node->indegree == 0) {
1209 insert = &commit_list_insert(next->item, insert)->next;
1211 next=next->next;
1214 /* process the list in topological order */
1215 if (!lifo)
1216 sort_by_date(&work);
1217 while (work) {
1218 struct commit * work_item = pop_commit(&work);
1219 struct sort_node * work_node = (struct sort_node *) getter(work_item);
1220 struct commit_list * parents = work_item->parents;
1222 while (parents) {
1223 struct commit * parent=parents->item;
1224 struct sort_node * pn = (struct sort_node *) getter(parent);
1226 if (pn) {
1228 * parents are only enqueued for emission
1229 * when all their children have been emitted thereby
1230 * guaranteeing topological order.
1232 pn->indegree--;
1233 if (!pn->indegree) {
1234 if (!lifo)
1235 insert_by_date(parent, &work);
1236 else
1237 commit_list_insert(parent, &work);
1240 parents=parents->next;
1243 * work_item is a commit all of whose children
1244 * have already been emitted. we can emit it now.
1246 *pptr = work_node->list_item;
1247 pptr = &(*pptr)->next;
1248 *pptr = NULL;
1249 setter(work_item, NULL);
1251 free(nodes);
1254 /* merge-base stuff */
1256 /* bits #0..15 in revision.h */
1257 #define PARENT1 (1u<<16)
1258 #define PARENT2 (1u<<17)
1259 #define STALE (1u<<18)
1260 #define RESULT (1u<<19)
1262 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
1264 static struct commit *interesting(struct commit_list *list)
1266 while (list) {
1267 struct commit *commit = list->item;
1268 list = list->next;
1269 if (commit->object.flags & STALE)
1270 continue;
1271 return commit;
1273 return NULL;
1276 static struct commit_list *merge_bases(struct commit *one, struct commit *two)
1278 struct commit_list *list = NULL;
1279 struct commit_list *result = NULL;
1281 if (one == two)
1282 /* We do not mark this even with RESULT so we do not
1283 * have to clean it up.
1285 return commit_list_insert(one, &result);
1287 parse_commit(one);
1288 parse_commit(two);
1290 one->object.flags |= PARENT1;
1291 two->object.flags |= PARENT2;
1292 insert_by_date(one, &list);
1293 insert_by_date(two, &list);
1295 while (interesting(list)) {
1296 struct commit *commit;
1297 struct commit_list *parents;
1298 struct commit_list *n;
1299 int flags;
1301 commit = list->item;
1302 n = list->next;
1303 free(list);
1304 list = n;
1306 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
1307 if (flags == (PARENT1 | PARENT2)) {
1308 if (!(commit->object.flags & RESULT)) {
1309 commit->object.flags |= RESULT;
1310 insert_by_date(commit, &result);
1312 /* Mark parents of a found merge stale */
1313 flags |= STALE;
1315 parents = commit->parents;
1316 while (parents) {
1317 struct commit *p = parents->item;
1318 parents = parents->next;
1319 if ((p->object.flags & flags) == flags)
1320 continue;
1321 parse_commit(p);
1322 p->object.flags |= flags;
1323 insert_by_date(p, &list);
1327 /* Clean up the result to remove stale ones */
1328 free_commit_list(list);
1329 list = result; result = NULL;
1330 while (list) {
1331 struct commit_list *n = list->next;
1332 if (!(list->item->object.flags & STALE))
1333 insert_by_date(list->item, &result);
1334 free(list);
1335 list = n;
1337 return result;
1340 struct commit_list *get_merge_bases(struct commit *one,
1341 struct commit *two,
1342 int cleanup)
1344 struct commit_list *list;
1345 struct commit **rslt;
1346 struct commit_list *result;
1347 int cnt, i, j;
1349 result = merge_bases(one, two);
1350 if (one == two)
1351 return result;
1352 if (!result || !result->next) {
1353 if (cleanup) {
1354 clear_commit_marks(one, all_flags);
1355 clear_commit_marks(two, all_flags);
1357 return result;
1360 /* There are more than one */
1361 cnt = 0;
1362 list = result;
1363 while (list) {
1364 list = list->next;
1365 cnt++;
1367 rslt = xcalloc(cnt, sizeof(*rslt));
1368 for (list = result, i = 0; list; list = list->next)
1369 rslt[i++] = list->item;
1370 free_commit_list(result);
1372 clear_commit_marks(one, all_flags);
1373 clear_commit_marks(two, all_flags);
1374 for (i = 0; i < cnt - 1; i++) {
1375 for (j = i+1; j < cnt; j++) {
1376 if (!rslt[i] || !rslt[j])
1377 continue;
1378 result = merge_bases(rslt[i], rslt[j]);
1379 clear_commit_marks(rslt[i], all_flags);
1380 clear_commit_marks(rslt[j], all_flags);
1381 for (list = result; list; list = list->next) {
1382 if (rslt[i] == list->item)
1383 rslt[i] = NULL;
1384 if (rslt[j] == list->item)
1385 rslt[j] = NULL;
1390 /* Surviving ones in rslt[] are the independent results */
1391 result = NULL;
1392 for (i = 0; i < cnt; i++) {
1393 if (rslt[i])
1394 insert_by_date(rslt[i], &result);
1396 free(rslt);
1397 return result;
1400 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
1402 struct commit_list *bases, *b;
1403 int ret = 0;
1405 if (num == 1)
1406 bases = get_merge_bases(commit, *reference, 1);
1407 else
1408 die("not yet");
1409 for (b = bases; b; b = b->next) {
1410 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
1411 ret = 1;
1412 break;
1416 free_commit_list(bases);
1417 return ret;