7 int save_commit_buffer = 1;
12 * the number of children of the associated commit
13 * that also occur in the list being sorted.
15 unsigned int indegree;
18 * reference to original list item that we will re-use
21 struct commit_list * list_item;
25 const char *commit_type = "commit";
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 },
41 enum cmit_fmt get_commit_format(const char *arg)
46 return CMIT_FMT_DEFAULT;
49 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
50 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len))
54 die("invalid --pretty format: %s", arg);
57 static struct commit *check_commit(struct object *obj,
58 const unsigned char *sha1,
61 if (obj->type != OBJ_COMMIT) {
63 error("Object %s is a %s, not a commit",
64 sha1_to_hex(sha1), typename(obj->type));
67 return (struct commit *) obj;
70 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
73 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
77 return check_commit(obj, sha1, quiet);
80 struct commit *lookup_commit_reference(const unsigned char *sha1)
82 return lookup_commit_reference_gently(sha1, 0);
85 struct commit *lookup_commit(const unsigned char *sha1)
87 struct object *obj = lookup_object(sha1);
89 struct commit *ret = alloc_commit_node();
90 created_object(sha1, &ret->object);
91 ret->object.type = OBJ_COMMIT;
95 obj->type = OBJ_COMMIT;
96 return check_commit(obj, sha1, 0);
99 static unsigned long parse_commit_date(const char *buf)
103 if (memcmp(buf, "author", 6))
105 while (*buf++ != '\n')
107 if (memcmp(buf, "committer", 9))
109 while (*buf++ != '>')
111 date = strtoul(buf, NULL, 10);
112 if (date == ULONG_MAX)
117 static struct commit_graft **commit_graft;
118 static int commit_graft_alloc, commit_graft_nr;
120 static int commit_graft_pos(const unsigned char *sha1)
124 hi = commit_graft_nr;
126 int mi = (lo + hi) / 2;
127 struct commit_graft *graft = commit_graft[mi];
128 int cmp = hashcmp(sha1, graft->sha1);
139 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
141 int pos = commit_graft_pos(graft->sha1);
147 free(commit_graft[pos]);
148 commit_graft[pos] = graft;
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) *
159 if (pos < commit_graft_nr)
160 memmove(commit_graft + pos + 1,
162 (commit_graft_nr - pos - 1) *
163 sizeof(*commit_graft));
164 commit_graft[pos] = graft;
168 struct commit_graft *read_graft_line(char *buf, int len)
170 /* The format is just "Commit Parent1 Parent2 ...\n" */
172 struct commit_graft *graft = NULL;
174 if (buf[len-1] == '\n')
176 if (buf[0] == '#' || buf[0] == '\0')
178 if ((len + 1) % 41) {
180 error("bad graft data: %s", buf);
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))
189 for (i = 40; i < len; i += 41) {
192 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
198 int read_graft_file(const char *graft_file)
200 FILE *fp = fopen(graft_file, "r");
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);
210 if (register_commit_graft(graft, 1))
211 error("duplicate graft data: %s", buf);
217 static void prepare_commit_graft(void)
219 static int commit_graft_prepared;
222 if (commit_graft_prepared)
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;
231 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
234 prepare_commit_graft();
235 pos = commit_graft_pos(sha1);
238 return commit_graft[pos];
241 int write_shallow_commits(int fd, int use_pack_protocol)
244 for (i = 0; i < commit_graft_nr; i++)
245 if (commit_graft[i]->nr_parent < 0) {
247 sha1_to_hex(commit_graft[i]->sha1);
249 if (use_pack_protocol)
250 packet_write(fd, "shallow %s", hex);
252 if (write_in_full(fd, hex, 40) != 40)
254 if (write_in_full(fd, "\n", 1) != 1)
261 int unregister_shallow(const unsigned char *sha1)
263 int pos = commit_graft_pos(sha1);
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));
274 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
277 char *bufptr = buffer;
278 unsigned char parent[20];
279 struct commit_list **pptr;
280 struct commit_graft *graft;
283 if (item->object.parsed)
285 item->object.parsed = 1;
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);
295 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
296 pptr = &item->parents;
298 graft = lookup_commit_graft(item->object.sha1);
299 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
300 struct commit *new_parent;
302 if (tail <= bufptr + 48 ||
303 get_sha1_hex(bufptr + 7, parent) ||
305 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
309 new_parent = lookup_commit(parent);
311 pptr = &commit_list_insert(new_parent, pptr)->next;
317 struct commit *new_parent;
318 for (i = 0; i < graft->nr_parent; i++) {
319 new_parent = lookup_commit(graft->parent[i]);
322 pptr = &commit_list_insert(new_parent, pptr)->next;
326 item->date = parse_commit_date(bufptr);
328 if (track_object_refs) {
330 struct commit_list *p;
331 struct object_refs *refs = alloc_object_refs(n_refs);
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);
342 int parse_commit(struct commit *item)
349 if (item->object.parsed)
351 buffer = read_sha1_file(item->object.sha1, type, &size);
353 return error("Could not read %s",
354 sha1_to_hex(item->object.sha1));
355 if (strcmp(type, commit_type)) {
357 return error("Object %s not a commit",
358 sha1_to_hex(item->object.sha1));
360 ret = parse_commit_buffer(item, buffer, size);
361 if (save_commit_buffer && !ret) {
362 item->buffer = buffer;
369 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
371 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
372 new_list->item = item;
373 new_list->next = *list_p;
378 void free_commit_list(struct commit_list *list)
381 struct commit_list *temp = list;
387 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
389 struct commit_list **pp = list;
390 struct commit_list *p;
391 while ((p = *pp) != NULL) {
392 if (p->item->date < item->date) {
397 return commit_list_insert(item, pp);
401 void sort_by_date(struct commit_list **list)
403 struct commit_list *ret = NULL;
405 insert_by_date((*list)->item, &ret);
406 *list = (*list)->next;
411 struct commit *pop_most_recent_commit(struct commit_list **list,
414 struct commit *ret = (*list)->item;
415 struct commit_list *parents = ret->parents;
416 struct commit_list *old = *list;
418 *list = (*list)->next;
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);
428 parents = parents->next;
433 void clear_commit_marks(struct commit *commit, unsigned int mark)
435 struct commit_list *parents;
437 commit->object.flags &= ~mark;
438 parents = commit->parents;
440 struct commit *parent = parents->item;
442 /* Have we already cleared this? */
443 if (mark & parent->object.flags)
444 clear_commit_marks(parent, mark);
445 parents = parents->next;
450 * Generic support for pretty-printing the header
452 static int get_one_line(const char *msg, unsigned long len)
467 static int is_rfc2047_special(char ch)
469 return ((ch & 0x80) || (ch == '=') || (ch == '?') || (ch == '_'));
472 static int add_rfc2047(char *buf, const char *line, int len)
476 static const char q_utf8[] = "=?utf-8?q?";
478 for (i = needquote = 0; !needquote && i < len; i++) {
479 unsigned ch = line[i];
483 (ch == '=' && line[i+1] == '?'))
487 return sprintf(buf, "%.*s", len, line);
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);
507 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
508 const char *line, int relative_date)
514 const char *filler = " ";
516 if (fmt == CMIT_FMT_ONELINE)
518 date = strchr(line, '>');
521 namelen = ++date - line;
522 time = strtoul(date, &date, 10);
523 tz = strtol(date, NULL, 10);
525 if (fmt == CMIT_FMT_EMAIL) {
526 char *name_tail = strchr(line, '<');
527 int display_name_length;
530 while (line < name_tail && isspace(name_tail[-1]))
532 display_name_length = name_tail - line;
534 strcpy(buf, "From: ");
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;
542 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
543 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
544 filler, namelen, line);
547 case CMIT_FMT_MEDIUM:
548 ret += sprintf(buf + ret, "Date: %s\n",
549 show_date(time, tz, relative_date));
552 ret += sprintf(buf + ret, "Date: %s\n",
553 show_rfc2822_date(time, tz));
555 case CMIT_FMT_FULLER:
556 ret += sprintf(buf + ret, "%sDate: %s\n", what,
557 show_date(time, tz, relative_date));
566 static int is_empty_line(const char *line, int *len_p)
569 while (len && isspace(line[len-1]))
575 static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
577 struct commit_list *parent = commit->parents;
580 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
581 !parent || !parent->next)
584 offset = sprintf(buf, "Merge:");
587 struct commit *p = parent->item;
588 const char *hex = NULL;
591 hex = find_unique_abbrev(p->object.sha1, abbrev);
593 hex = sha1_to_hex(p->object.sha1);
594 dots = (abbrev && strlen(hex) != 40) ? "..." : "";
595 parent = parent->next;
597 offset += sprintf(buf + offset, " %s%s", hex, dots);
599 buf[offset++] = '\n';
603 static char *get_header(const struct commit *commit, const char *key)
605 int key_len = strlen(key);
606 const char *line = commit->buffer;
609 const char *eol = strchr(line, '\n'), *next;
614 eol = line + strlen(line);
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);
629 static char *replace_encoding_header(char *buf, char *encoding)
631 char *encoding_header = strstr(buf, "\nencoding ");
632 char *end_of_encoding_header;
633 int encoding_header_pos;
634 int encoding_header_len;
637 int buflen = strlen(buf) + 1;
639 if (!encoding_header)
640 return buf; /* should not happen but be defensive */
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++;
647 encoding_header_len = end_of_encoding_header - encoding_header;
648 encoding_header_pos = encoding_header - buf;
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));
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;
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';
671 static char *logmsg_reencode(const struct commit *commit)
675 char *output_encoding = (git_log_output_encoding
676 ? git_log_output_encoding
677 : git_commit_encoding);
679 if (!output_encoding)
680 output_encoding = "utf-8";
681 else if (!*output_encoding)
683 encoding = get_header(commit, "encoding");
686 if (!strcmp(encoding, output_encoding))
687 out = strdup(commit->buffer);
689 out = reencode_string(commit->buffer,
690 output_encoding, encoding);
692 out = replace_encoding_header(out, output_encoding);
700 unsigned long pretty_print_commit(enum cmit_fmt fmt,
701 const struct commit *commit,
703 char *buf, unsigned long space,
704 int abbrev, const char *subject,
705 const char *after_subject,
708 int hdr = 1, body = 0, seen_title = 0;
709 unsigned long offset = 0;
711 int parents_shown = 0;
712 const char *msg = commit->buffer;
713 int plain_non_ascii = 0;
714 char *reencoded = logmsg_reencode(commit);
719 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
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.
728 if (fmt == CMIT_FMT_EMAIL && !after_subject) {
731 for (in_body = i = 0; (ch = msg[i]) && i < len; i++) {
733 /* author could be non 7-bit ASCII but
734 * the log may be so; skip over the
738 i + 1 < len && msg[i+1] == '\n')
741 else if (ch & 0x80) {
749 const char *line = msg;
750 int linelen = get_one_line(msg, len);
756 * We want some slop for indentation and a possible
757 * final "...". Thus the "+ 20".
759 if (offset + linelen + 20 > space) {
760 memcpy(buf + offset, " ...\n", 8);
770 if ((fmt != CMIT_FMT_ONELINE) && !subject)
771 buf[offset++] = '\n';
774 if (fmt == CMIT_FMT_RAW) {
775 memcpy(buf + offset, line, linelen);
779 if (!memcmp(line, "parent ", 7)) {
781 die("bad parent line in commit");
785 if (!parents_shown) {
786 offset += add_merge_info(fmt, buf + offset,
792 * MEDIUM == DEFAULT shows only author with dates.
793 * FULL shows both authors but not dates.
794 * FULLER shows both authors and dates.
796 if (!memcmp(line, "author ", 7))
797 offset += add_user_info("Author", fmt,
801 if (!memcmp(line, "committer ", 10) &&
802 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
803 offset += add_user_info("Commit", fmt,
813 if (is_empty_line(line, &linelen)) {
820 if (fmt == CMIT_FMT_SHORT)
826 int slen = strlen(subject);
827 memcpy(buf + offset, subject, slen);
829 offset += add_rfc2047(buf + offset, line, linelen);
832 memset(buf + offset, ' ', indent);
833 memcpy(buf + offset + indent, line, linelen);
834 offset += linelen + indent;
836 buf[offset++] = '\n';
837 if (fmt == CMIT_FMT_ONELINE)
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;
847 int slen = strlen(after_subject);
848 if (slen > space - offset - 1)
849 slen = space - offset - 1;
850 memcpy(buf + offset, after_subject, slen);
852 after_subject = NULL;
856 while (offset && isspace(buf[offset-1]))
858 /* Make sure there is an EOLN for the non-oneline case */
859 if (fmt != CMIT_FMT_ONELINE)
860 buf[offset++] = '\n';
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
865 if (fmt == CMIT_FMT_EMAIL && !body)
866 buf[offset++] = '\n';
873 struct commit *pop_commit(struct commit_list **stack)
875 struct commit_list *top = *stack;
876 struct commit *item = top ? top->item : NULL;
885 int count_parents(struct commit * commit)
888 struct commit_list * parents = commit->parents;
889 for (count = 0; parents; parents = parents->next,count++)
894 void topo_sort_default_setter(struct commit *c, void *data)
899 void *topo_sort_default_getter(struct commit *c)
905 * Performs an in-place topological sort on the list supplied.
907 void sort_in_topological_order(struct commit_list ** list, int lifo)
909 sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
910 topo_sort_default_getter);
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)
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;
924 /* determine the size of the list */
932 /* allocate an array to help sort the list */
933 nodes = xcalloc(count, sizeof(*nodes));
934 /* link the list to the array */
938 next_nodes->list_item = next;
939 setter(next->item, next_nodes);
943 /* update the indegree */
946 struct commit_list * parents = next->item->parents;
948 struct commit * parent=parents->item;
949 struct sort_node * pn = (struct sort_node *) getter(parent);
953 parents=parents->next;
960 * tips are nodes not reachable from any other node in the list
962 * the tips serve as a starting set for the work queue.
967 struct sort_node * node = (struct sort_node *) getter(next->item);
969 if (node->indegree == 0) {
970 insert = &commit_list_insert(next->item, insert)->next;
975 /* process the list in topological order */
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;
984 struct commit * parent=parents->item;
985 struct sort_node * pn = (struct sort_node *) getter(parent);
989 * parents are only enqueued for emission
990 * when all their children have been emitted thereby
991 * guaranteeing topological order.
996 insert_by_date(parent, &work);
998 commit_list_insert(parent, &work);
1001 parents=parents->next;
1004 * work_item is a commit all of whose children
1005 * have already been emitted. we can emit it now.
1007 *pptr = work_node->list_item;
1008 pptr = &(*pptr)->next;
1010 setter(work_item, NULL);
1015 /* merge-base stuff */
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)
1023 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
1025 static struct commit *interesting(struct commit_list *list)
1028 struct commit *commit = list->item;
1030 if (commit->object.flags & STALE)
1037 static struct commit_list *merge_bases(struct commit *one, struct commit *two)
1039 struct commit_list *list = NULL;
1040 struct commit_list *result = NULL;
1043 /* We do not mark this even with RESULT so we do not
1044 * have to clean it up.
1046 return commit_list_insert(one, &result);
1051 one->object.flags |= PARENT1;
1052 two->object.flags |= PARENT2;
1053 insert_by_date(one, &list);
1054 insert_by_date(two, &list);
1056 while (interesting(list)) {
1057 struct commit *commit;
1058 struct commit_list *parents;
1059 struct commit_list *n;
1062 commit = list->item;
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);
1073 /* Mark parents of a found merge stale */
1076 parents = commit->parents;
1078 struct commit *p = parents->item;
1079 parents = parents->next;
1080 if ((p->object.flags & flags) == flags)
1083 p->object.flags |= flags;
1084 insert_by_date(p, &list);
1088 /* Clean up the result to remove stale ones */
1089 free_commit_list(list);
1090 list = result; result = NULL;
1092 struct commit_list *n = list->next;
1093 if (!(list->item->object.flags & STALE))
1094 insert_by_date(list->item, &result);
1101 struct commit_list *get_merge_bases(struct commit *one,
1105 struct commit_list *list;
1106 struct commit **rslt;
1107 struct commit_list *result;
1110 result = merge_bases(one, two);
1113 if (!result || !result->next) {
1115 clear_commit_marks(one, all_flags);
1116 clear_commit_marks(two, all_flags);
1121 /* There are more than one */
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);
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])
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)
1145 if (rslt[j] == list->item)
1151 /* Surviving ones in rslt[] are the independent results */
1153 for (i = 0; i < cnt; i++) {
1155 insert_by_date(rslt[i], &result);
1161 int in_merge_bases(struct commit *rev1, struct commit *rev2)
1163 struct commit_list *bases, *b;
1166 bases = get_merge_bases(rev1, rev2, 1);
1167 for (b = bases; b; b = b->next) {
1168 if (!hashcmp(rev1->object.sha1, b->item->object.sha1)) {
1174 free_commit_list(bases);