5 int save_commit_buffer = 1;
10 * the number of children of the associated commit
11 * that also occur in the list being sorted.
13 unsigned int indegree;
16 * reference to original list item that we will re-use
19 struct commit_list * list_item;
23 const char *commit_type = "commit";
30 { "raw", 1, CMIT_FMT_RAW },
31 { "medium", 1, CMIT_FMT_MEDIUM },
32 { "short", 1, CMIT_FMT_SHORT },
33 { "email", 1, CMIT_FMT_EMAIL },
34 { "full", 5, CMIT_FMT_FULL },
35 { "fuller", 5, CMIT_FMT_FULLER },
36 { "oneline", 1, CMIT_FMT_ONELINE },
39 enum cmit_fmt get_commit_format(const char *arg)
44 return CMIT_FMT_DEFAULT;
47 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
48 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len))
52 die("invalid --pretty format: %s", arg);
55 static struct commit *check_commit(struct object *obj,
56 const unsigned char *sha1,
59 if (obj->type != TYPE_COMMIT) {
61 error("Object %s is a %s, not a commit",
62 sha1_to_hex(sha1), typename(obj->type));
65 return (struct commit *) obj;
68 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
71 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
75 return check_commit(obj, sha1, quiet);
78 struct commit *lookup_commit_reference(const unsigned char *sha1)
80 return lookup_commit_reference_gently(sha1, 0);
83 struct commit *lookup_commit(const unsigned char *sha1)
85 struct object *obj = lookup_object(sha1);
87 struct commit *ret = alloc_commit_node();
88 created_object(sha1, &ret->object);
89 ret->object.type = TYPE_COMMIT;
93 obj->type = TYPE_COMMIT;
94 return check_commit(obj, sha1, 0);
97 static unsigned long parse_commit_date(const char *buf)
101 if (memcmp(buf, "author", 6))
103 while (*buf++ != '\n')
105 if (memcmp(buf, "committer", 9))
107 while (*buf++ != '>')
109 date = strtoul(buf, NULL, 10);
110 if (date == ULONG_MAX)
115 static struct commit_graft **commit_graft;
116 static int commit_graft_alloc, commit_graft_nr;
118 static int commit_graft_pos(const unsigned char *sha1)
122 hi = commit_graft_nr;
124 int mi = (lo + hi) / 2;
125 struct commit_graft *graft = commit_graft[mi];
126 int cmp = memcmp(sha1, graft->sha1, 20);
137 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
139 int pos = commit_graft_pos(graft->sha1);
145 free(commit_graft[pos]);
146 commit_graft[pos] = graft;
151 if (commit_graft_alloc <= ++commit_graft_nr) {
152 commit_graft_alloc = alloc_nr(commit_graft_alloc);
153 commit_graft = xrealloc(commit_graft,
154 sizeof(*commit_graft) *
157 if (pos < commit_graft_nr)
158 memmove(commit_graft + pos + 1,
160 (commit_graft_nr - pos - 1) *
161 sizeof(*commit_graft));
162 commit_graft[pos] = graft;
166 struct commit_graft *read_graft_line(char *buf, int len)
168 /* The format is just "Commit Parent1 Parent2 ...\n" */
170 struct commit_graft *graft = NULL;
172 if (buf[len-1] == '\n')
174 if (buf[0] == '#' || buf[0] == '\0')
176 if ((len + 1) % 41) {
178 error("bad graft data: %s", buf);
182 i = (len + 1) / 41 - 1;
183 graft = xmalloc(sizeof(*graft) + 20 * i);
184 graft->nr_parent = i;
185 if (get_sha1_hex(buf, graft->sha1))
187 for (i = 40; i < len; i += 41) {
190 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
196 int read_graft_file(const char *graft_file)
198 FILE *fp = fopen(graft_file, "r");
202 while (fgets(buf, sizeof(buf), fp)) {
203 /* The format is just "Commit Parent1 Parent2 ...\n" */
204 int len = strlen(buf);
205 struct commit_graft *graft = read_graft_line(buf, len);
208 if (register_commit_graft(graft, 1))
209 error("duplicate graft data: %s", buf);
215 static void prepare_commit_graft(void)
217 static int commit_graft_prepared;
220 if (commit_graft_prepared)
222 graft_file = get_graft_file();
223 read_graft_file(graft_file);
224 commit_graft_prepared = 1;
227 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
230 prepare_commit_graft();
231 pos = commit_graft_pos(sha1);
234 return commit_graft[pos];
237 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
240 char *bufptr = buffer;
241 unsigned char parent[20];
242 struct commit_list **pptr;
243 struct commit_graft *graft;
246 if (item->object.parsed)
248 item->object.parsed = 1;
250 if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5))
251 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
252 if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0)
253 return error("bad tree pointer in commit %s",
254 sha1_to_hex(item->object.sha1));
255 item->tree = lookup_tree(parent);
258 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
259 pptr = &item->parents;
261 graft = lookup_commit_graft(item->object.sha1);
262 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
263 struct commit *new_parent;
265 if (tail <= bufptr + 48 ||
266 get_sha1_hex(bufptr + 7, parent) ||
268 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
272 new_parent = lookup_commit(parent);
274 pptr = &commit_list_insert(new_parent, pptr)->next;
280 struct commit *new_parent;
281 for (i = 0; i < graft->nr_parent; i++) {
282 new_parent = lookup_commit(graft->parent[i]);
285 pptr = &commit_list_insert(new_parent, pptr)->next;
289 item->date = parse_commit_date(bufptr);
291 if (track_object_refs) {
293 struct commit_list *p;
294 struct object_refs *refs = alloc_object_refs(n_refs);
296 refs->ref[i++] = &item->tree->object;
297 for (p = item->parents; p; p = p->next)
298 refs->ref[i++] = &p->item->object;
299 set_object_refs(&item->object, refs);
305 int parse_commit(struct commit *item)
312 if (item->object.parsed)
314 buffer = read_sha1_file(item->object.sha1, type, &size);
316 return error("Could not read %s",
317 sha1_to_hex(item->object.sha1));
318 if (strcmp(type, commit_type)) {
320 return error("Object %s not a commit",
321 sha1_to_hex(item->object.sha1));
323 ret = parse_commit_buffer(item, buffer, size);
324 if (save_commit_buffer && !ret) {
325 item->buffer = buffer;
332 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
334 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
335 new_list->item = item;
336 new_list->next = *list_p;
341 void free_commit_list(struct commit_list *list)
344 struct commit_list *temp = list;
350 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
352 struct commit_list **pp = list;
353 struct commit_list *p;
354 while ((p = *pp) != NULL) {
355 if (p->item->date < item->date) {
360 return commit_list_insert(item, pp);
364 void sort_by_date(struct commit_list **list)
366 struct commit_list *ret = NULL;
368 insert_by_date((*list)->item, &ret);
369 *list = (*list)->next;
374 struct commit *pop_most_recent_commit(struct commit_list **list,
377 struct commit *ret = (*list)->item;
378 struct commit_list *parents = ret->parents;
379 struct commit_list *old = *list;
381 *list = (*list)->next;
385 struct commit *commit = parents->item;
386 parse_commit(commit);
387 if (!(commit->object.flags & mark)) {
388 commit->object.flags |= mark;
389 insert_by_date(commit, list);
391 parents = parents->next;
396 void clear_commit_marks(struct commit *commit, unsigned int mark)
398 struct commit_list *parents;
400 parents = commit->parents;
401 commit->object.flags &= ~mark;
403 struct commit *parent = parents->item;
404 if (parent && parent->object.parsed &&
405 (parent->object.flags & mark))
406 clear_commit_marks(parent, mark);
407 parents = parents->next;
412 * Generic support for pretty-printing the header
414 static int get_one_line(const char *msg, unsigned long len)
429 static int is_rfc2047_special(char ch)
431 return ((ch & 0x80) || (ch == '=') || (ch == '?') || (ch == '_'));
434 static int add_rfc2047(char *buf, const char *line, int len)
438 static const char q_utf8[] = "=?utf-8?q?";
440 for (i = needquote = 0; !needquote && i < len; i++) {
441 unsigned ch = line[i];
445 (ch == '=' && line[i+1] == '?'))
449 return sprintf(buf, "%.*s", len, line);
451 memcpy(bp, q_utf8, sizeof(q_utf8)-1);
452 bp += sizeof(q_utf8)-1;
453 for (i = 0; i < len; i++) {
454 unsigned ch = line[i] & 0xFF;
455 if (is_rfc2047_special(ch)) {
456 sprintf(bp, "=%02X", ch);
469 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
475 const char *filler = " ";
477 if (fmt == CMIT_FMT_ONELINE)
479 date = strchr(line, '>');
482 namelen = ++date - line;
483 time = strtoul(date, &date, 10);
484 tz = strtol(date, NULL, 10);
486 if (fmt == CMIT_FMT_EMAIL) {
487 char *name_tail = strchr(line, '<');
488 int display_name_length;
491 while (line < name_tail && isspace(name_tail[-1]))
493 display_name_length = name_tail - line;
495 strcpy(buf, "From: ");
497 ret += add_rfc2047(buf + ret, line, display_name_length);
498 memcpy(buf + ret, name_tail, namelen - display_name_length);
499 ret += namelen - display_name_length;
503 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
504 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
505 filler, namelen, line);
508 case CMIT_FMT_MEDIUM:
509 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
512 ret += sprintf(buf + ret, "Date: %s\n",
513 show_rfc2822_date(time, tz));
515 case CMIT_FMT_FULLER:
516 ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz));
525 static int is_empty_line(const char *line, int *len_p)
528 while (len && isspace(line[len-1]))
534 static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
536 struct commit_list *parent = commit->parents;
539 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
540 !parent || !parent->next)
543 offset = sprintf(buf, "Merge:");
546 struct commit *p = parent->item;
547 const char *hex = abbrev
548 ? find_unique_abbrev(p->object.sha1, abbrev)
549 : sha1_to_hex(p->object.sha1);
550 const char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
551 parent = parent->next;
553 offset += sprintf(buf + offset, " %s%s", hex, dots);
555 buf[offset++] = '\n';
559 unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, unsigned long len, char *buf, unsigned long space, int abbrev, const char *subject, const char *after_subject)
561 int hdr = 1, body = 0;
562 unsigned long offset = 0;
564 int parents_shown = 0;
565 const char *msg = commit->buffer;
566 int plain_non_ascii = 0;
568 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
571 /* After-subject is used to pass in Content-Type: multipart
572 * MIME header; in that case we do not have to do the
573 * plaintext content type even if the commit message has
574 * non 7-bit ASCII character. Otherwise, check if we need
575 * to say this is not a 7-bit ASCII.
577 if (fmt == CMIT_FMT_EMAIL && !after_subject) {
580 for (in_body = i = 0; (ch = msg[i]) && i < len; i++) {
582 /* author could be non 7-bit ASCII but
583 * the log may so; skip over the
587 i + 1 < len && msg[i+1] == '\n')
590 else if (ch & 0x80) {
598 const char *line = msg;
599 int linelen = get_one_line(msg, len);
605 * We want some slop for indentation and a possible
606 * final "...". Thus the "+ 20".
608 if (offset + linelen + 20 > space) {
609 memcpy(buf + offset, " ...\n", 8);
619 if ((fmt != CMIT_FMT_ONELINE) && !subject)
620 buf[offset++] = '\n';
623 if (fmt == CMIT_FMT_RAW) {
624 memcpy(buf + offset, line, linelen);
628 if (!memcmp(line, "parent ", 7)) {
630 die("bad parent line in commit");
634 if (!parents_shown) {
635 offset += add_merge_info(fmt, buf + offset,
641 * MEDIUM == DEFAULT shows only author with dates.
642 * FULL shows both authors but not dates.
643 * FULLER shows both authors and dates.
645 if (!memcmp(line, "author ", 7))
646 offset += add_user_info("Author", fmt,
649 if (!memcmp(line, "committer ", 10) &&
650 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
651 offset += add_user_info("Commit", fmt,
657 if (is_empty_line(line, &linelen)) {
662 if (fmt == CMIT_FMT_SHORT)
669 int slen = strlen(subject);
670 memcpy(buf + offset, subject, slen);
672 offset += add_rfc2047(buf + offset, line, linelen);
675 memset(buf + offset, ' ', indent);
676 memcpy(buf + offset + indent, line, linelen);
677 offset += linelen + indent;
679 buf[offset++] = '\n';
680 if (fmt == CMIT_FMT_ONELINE)
682 if (subject && plain_non_ascii) {
683 static const char header[] =
684 "Content-Type: text/plain; charset=UTF-8\n"
685 "Content-Transfer-Encoding: 8bit\n";
686 memcpy(buf + offset, header, sizeof(header)-1);
687 offset += sizeof(header)-1;
690 int slen = strlen(after_subject);
691 if (slen > space - offset - 1)
692 slen = space - offset - 1;
693 memcpy(buf + offset, after_subject, slen);
695 after_subject = NULL;
699 while (offset && isspace(buf[offset-1]))
701 /* Make sure there is an EOLN for the non-oneline case */
702 if (fmt != CMIT_FMT_ONELINE)
703 buf[offset++] = '\n';
708 struct commit *pop_commit(struct commit_list **stack)
710 struct commit_list *top = *stack;
711 struct commit *item = top ? top->item : NULL;
720 int count_parents(struct commit * commit)
723 struct commit_list * parents = commit->parents;
724 for (count=0;parents; parents=parents->next,count++)
729 void topo_sort_default_setter(struct commit *c, void *data)
734 void *topo_sort_default_getter(struct commit *c)
740 * Performs an in-place topological sort on the list supplied.
742 void sort_in_topological_order(struct commit_list ** list, int lifo)
744 sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
745 topo_sort_default_getter);
748 void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
749 topo_sort_set_fn_t setter,
750 topo_sort_get_fn_t getter)
752 struct commit_list * next = *list;
753 struct commit_list * work = NULL, **insert;
754 struct commit_list ** pptr = list;
755 struct sort_node * nodes;
756 struct sort_node * next_nodes;
759 /* determine the size of the list */
767 /* allocate an array to help sort the list */
768 nodes = xcalloc(count, sizeof(*nodes));
769 /* link the list to the array */
773 next_nodes->list_item = next;
774 setter(next->item, next_nodes);
778 /* update the indegree */
781 struct commit_list * parents = next->item->parents;
783 struct commit * parent=parents->item;
784 struct sort_node * pn = (struct sort_node *) getter(parent);
788 parents=parents->next;
795 * tips are nodes not reachable from any other node in the list
797 * the tips serve as a starting set for the work queue.
802 struct sort_node * node = (struct sort_node *) getter(next->item);
804 if (node->indegree == 0) {
805 insert = &commit_list_insert(next->item, insert)->next;
810 /* process the list in topological order */
814 struct commit * work_item = pop_commit(&work);
815 struct sort_node * work_node = (struct sort_node *) getter(work_item);
816 struct commit_list * parents = work_item->parents;
819 struct commit * parent=parents->item;
820 struct sort_node * pn = (struct sort_node *) getter(parent);
824 * parents are only enqueued for emission
825 * when all their children have been emitted thereby
826 * guaranteeing topological order.
831 insert_by_date(parent, &work);
833 commit_list_insert(parent, &work);
836 parents=parents->next;
839 * work_item is a commit all of whose children
840 * have already been emitted. we can emit it now.
842 *pptr = work_node->list_item;
843 pptr = &(*pptr)->next;
845 setter(work_item, NULL);
850 /* merge-rebase stuff */
852 /* bits #0..7 in revision.h */
853 #define PARENT1 (1u<< 8)
854 #define PARENT2 (1u<< 9)
855 #define UNINTERESTING (1u<<10)
857 static struct commit *interesting(struct commit_list *list)
860 struct commit *commit = list->item;
862 if (commit->object.flags & UNINTERESTING)
870 * A pathological example of how this thing works.
872 * Suppose we had this commit graph, where chronologically
873 * the timestamp on the commit are A <= B <= C <= D <= E <= F
874 * and we are trying to figure out the merge base for E and F
885 * First we push E and F to list to be processed. E gets bit 1
886 * and F gets bit 2. The list becomes:
888 * list=F(2) E(1), result=empty
890 * Then we pop F, the newest commit, from the list. Its flag is 2.
891 * We scan its parents, mark them reachable from the side that F is
892 * reachable from, and push them to the list:
894 * list=E(1) D(2) A(2), result=empty
896 * Next pop E and do the same.
898 * list=D(2) B(1) A(2), result=empty
900 * Next pop D and do the same.
902 * list=C(2) B(1) A(2), result=empty
904 * Next pop C and do the same.
906 * list=B(1) A(2), result=empty
908 * Now it is B's turn. We mark its parent, C, reachable from B's side,
909 * and push it to the list:
911 * list=C(3) A(2), result=empty
913 * Now pop C and notice it has flags==3. It is placed on the result list,
914 * and the list now contains:
916 * list=A(2), result=C(3)
918 * We pop A and do the same.
920 * list=B(3), result=C(3)
922 * Next, we pop B and something very interesting happens. It has flags==3
923 * so it is also placed on the result list, and its parents are marked
924 * uninteresting, retroactively, and placed back on the list:
926 * list=C(7), result=C(7) B(3)
928 * Now, list does not have any interesting commit. So we find the newest
929 * commit from the result list that is not marked uninteresting. Which is
933 * Another pathological example how this thing used to fail to mark an
934 * ancestor of a merge base as UNINTERESTING before we introduced the
935 * postprocessing phase (mark_reachable_commits).
951 * list A B C D E F G H
952 * G1 H2 - - - - - - 1 2
953 * H2 E1 B1 - 1 - - 1 - 1 2
954 * F2 E1 B1 A2 2 1 - - 1 2 1 2
955 * E3 B1 A2 2 1 - - 3 2 1 2
956 * B1 A2 2 1 - - 3 2 1 2
957 * C1 A2 2 1 1 - 3 2 1 2
958 * D1 A2 2 1 1 1 3 2 1 2
963 * At this point, unfortunately, everybody in the list is
964 * uninteresting, so we fail to complete the following two
965 * steps to fully marking uninteresting commits.
970 * and we ended up showing E as an interesting merge base.
971 * The postprocessing phase re-injects C and continues traversal
972 * to contaminate D and E.
975 static void mark_reachable_commits(struct commit_list *result,
976 struct commit_list *list)
978 struct commit_list *tmp;
981 * Postprocess to fully contaminate the well.
983 for (tmp = result; tmp; tmp = tmp->next) {
984 struct commit *c = tmp->item;
985 /* Reinject uninteresting ones to list,
986 * so we can scan their parents.
988 if (c->object.flags & UNINTERESTING)
989 commit_list_insert(c, &list);
992 struct commit *c = list->item;
993 struct commit_list *parents;
999 /* Anything taken out of the list is uninteresting, so
1000 * mark all its parents uninteresting. We do not
1001 * parse new ones (we already parsed all the relevant
1004 parents = c->parents;
1006 struct commit *p = parents->item;
1007 parents = parents->next;
1008 if (!(p->object.flags & UNINTERESTING)) {
1009 p->object.flags |= UNINTERESTING;
1010 commit_list_insert(p, &list);
1016 struct commit_list *get_merge_bases(struct commit *rev1, struct commit *rev2)
1018 struct commit_list *list = NULL;
1019 struct commit_list *result = NULL;
1020 struct commit_list *tmp = NULL;
1023 return commit_list_insert(rev1, &result);
1028 rev1->object.flags |= PARENT1;
1029 rev2->object.flags |= PARENT2;
1030 insert_by_date(rev1, &list);
1031 insert_by_date(rev2, &list);
1033 while (interesting(list)) {
1034 struct commit *commit = list->item;
1035 struct commit_list *parents;
1036 int flags = commit->object.flags
1037 & (PARENT1 | PARENT2 | UNINTERESTING);
1042 if (flags == (PARENT1 | PARENT2)) {
1043 insert_by_date(commit, &result);
1045 /* Mark parents of a found merge uninteresting */
1046 flags |= UNINTERESTING;
1048 parents = commit->parents;
1050 struct commit *p = parents->item;
1051 parents = parents->next;
1052 if ((p->object.flags & flags) == flags)
1055 p->object.flags |= flags;
1056 insert_by_date(p, &list);
1063 if (result->next && list)
1064 mark_reachable_commits(result, list);
1066 /* cull duplicates */
1067 for (tmp = result, list = NULL; tmp; ) {
1068 struct commit *commit = tmp->item;
1069 struct commit_list *next = tmp->next;
1070 if (commit->object.flags & UNINTERESTING) {
1078 commit->object.flags |= UNINTERESTING;
1087 struct commit_list *get_merge_bases_clean(struct commit *rev1,
1088 struct commit *rev2)
1090 unsigned int flags1 = rev1->object.flags;
1091 unsigned int flags2 = rev2->object.flags;
1092 struct commit_list *result = get_merge_bases(rev1, rev2);
1094 clear_commit_marks(rev1, PARENT1 | PARENT2 | UNINTERESTING);
1095 clear_commit_marks(rev2, PARENT1 | PARENT2 | UNINTERESTING);
1096 rev1->object.flags = flags1;
1097 rev2->object.flags = flags2;