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 != OBJ_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 = OBJ_COMMIT;
93 obj->type = OBJ_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 void free_commit_grafts(void)
168 int pos = commit_graft_nr;
170 free(commit_graft[pos--]);
174 struct commit_graft *read_graft_line(char *buf, int len)
176 /* The format is just "Commit Parent1 Parent2 ...\n" */
178 struct commit_graft *graft = NULL;
180 if (buf[len-1] == '\n')
182 if (buf[0] == '#' || buf[0] == '\0')
184 if ((len + 1) % 41) {
186 error("bad graft data: %s", buf);
190 i = (len + 1) / 41 - 1;
191 graft = xmalloc(sizeof(*graft) + 20 * i);
192 graft->nr_parent = i;
193 if (get_sha1_hex(buf, graft->sha1))
195 for (i = 40; i < len; i += 41) {
198 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
204 int read_graft_file(const char *graft_file)
206 FILE *fp = fopen(graft_file, "r");
210 while (fgets(buf, sizeof(buf), fp)) {
211 /* The format is just "Commit Parent1 Parent2 ...\n" */
212 int len = strlen(buf);
213 struct commit_graft *graft = read_graft_line(buf, len);
216 if (register_commit_graft(graft, 1))
217 error("duplicate graft data: %s", buf);
223 static void prepare_commit_graft(void)
225 static int commit_graft_prepared;
226 static char *last_graft_file;
227 char *graft_file = get_graft_file();
229 if (last_graft_file) {
230 if (!strcmp(graft_file, last_graft_file))
232 free_commit_grafts();
235 free(last_graft_file);
236 last_graft_file = strdup(graft_file);
238 read_graft_file(graft_file);
239 commit_graft_prepared = 1;
242 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
245 prepare_commit_graft();
246 pos = commit_graft_pos(sha1);
249 return commit_graft[pos];
252 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
255 char *bufptr = buffer;
256 unsigned char parent[20];
257 struct commit_list **pptr;
258 struct commit_graft *graft;
261 if (item->object.parsed)
263 item->object.parsed = 1;
265 if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5))
266 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
267 if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0)
268 return error("bad tree pointer in commit %s",
269 sha1_to_hex(item->object.sha1));
270 item->tree = lookup_tree(parent);
273 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
274 pptr = &item->parents;
276 graft = lookup_commit_graft(item->object.sha1);
277 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
278 struct commit *new_parent;
280 if (tail <= bufptr + 48 ||
281 get_sha1_hex(bufptr + 7, parent) ||
283 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
287 new_parent = lookup_commit(parent);
289 pptr = &commit_list_insert(new_parent, pptr)->next;
295 struct commit *new_parent;
296 for (i = 0; i < graft->nr_parent; i++) {
297 new_parent = lookup_commit(graft->parent[i]);
300 pptr = &commit_list_insert(new_parent, pptr)->next;
304 item->date = parse_commit_date(bufptr);
306 if (track_object_refs) {
308 struct commit_list *p;
309 struct object_refs *refs = alloc_object_refs(n_refs);
311 refs->ref[i++] = &item->tree->object;
312 for (p = item->parents; p; p = p->next)
313 refs->ref[i++] = &p->item->object;
314 set_object_refs(&item->object, refs);
320 int parse_commit(struct commit *item)
327 if (item->object.parsed)
329 buffer = read_sha1_file(item->object.sha1, type, &size);
331 return error("Could not read %s",
332 sha1_to_hex(item->object.sha1));
333 if (strcmp(type, commit_type)) {
335 return error("Object %s not a commit",
336 sha1_to_hex(item->object.sha1));
338 ret = parse_commit_buffer(item, buffer, size);
339 if (save_commit_buffer && !ret) {
340 item->buffer = buffer;
347 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
349 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
350 new_list->item = item;
351 new_list->next = *list_p;
356 void free_commit_list(struct commit_list *list)
359 struct commit_list *temp = list;
365 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
367 struct commit_list **pp = list;
368 struct commit_list *p;
369 while ((p = *pp) != NULL) {
370 if (p->item->date < item->date) {
375 return commit_list_insert(item, pp);
379 void sort_by_date(struct commit_list **list)
381 struct commit_list *ret = NULL;
383 insert_by_date((*list)->item, &ret);
384 *list = (*list)->next;
389 struct commit *pop_most_recent_commit(struct commit_list **list,
392 struct commit *ret = (*list)->item;
393 struct commit_list *parents = ret->parents;
394 struct commit_list *old = *list;
396 *list = (*list)->next;
400 struct commit *commit = parents->item;
401 parse_commit(commit);
402 if (!(commit->object.flags & mark)) {
403 commit->object.flags |= mark;
404 insert_by_date(commit, list);
406 parents = parents->next;
411 void clear_commit_marks(struct commit *commit, unsigned int mark)
413 struct commit_list *parents;
415 commit->object.flags &= ~mark;
416 parents = commit->parents;
418 struct commit *parent = parents->item;
420 /* Have we already cleared this? */
421 if (mark & parent->object.flags)
422 clear_commit_marks(parent, mark);
423 parents = parents->next;
428 * Generic support for pretty-printing the header
430 static int get_one_line(const char *msg, unsigned long len)
445 static int is_rfc2047_special(char ch)
447 return ((ch & 0x80) || (ch == '=') || (ch == '?') || (ch == '_'));
450 static int add_rfc2047(char *buf, const char *line, int len)
454 static const char q_utf8[] = "=?utf-8?q?";
456 for (i = needquote = 0; !needquote && i < len; i++) {
457 unsigned ch = line[i];
461 (ch == '=' && line[i+1] == '?'))
465 return sprintf(buf, "%.*s", len, line);
467 memcpy(bp, q_utf8, sizeof(q_utf8)-1);
468 bp += sizeof(q_utf8)-1;
469 for (i = 0; i < len; i++) {
470 unsigned ch = line[i] & 0xFF;
471 if (is_rfc2047_special(ch)) {
472 sprintf(bp, "=%02X", ch);
485 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
491 const char *filler = " ";
493 if (fmt == CMIT_FMT_ONELINE)
495 date = strchr(line, '>');
498 namelen = ++date - line;
499 time = strtoul(date, &date, 10);
500 tz = strtol(date, NULL, 10);
502 if (fmt == CMIT_FMT_EMAIL) {
503 char *name_tail = strchr(line, '<');
504 int display_name_length;
507 while (line < name_tail && isspace(name_tail[-1]))
509 display_name_length = name_tail - line;
511 strcpy(buf, "From: ");
513 ret += add_rfc2047(buf + ret, line, display_name_length);
514 memcpy(buf + ret, name_tail, namelen - display_name_length);
515 ret += namelen - display_name_length;
519 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
520 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
521 filler, namelen, line);
524 case CMIT_FMT_MEDIUM:
525 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
528 ret += sprintf(buf + ret, "Date: %s\n",
529 show_rfc2822_date(time, tz));
531 case CMIT_FMT_FULLER:
532 ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz));
541 static int is_empty_line(const char *line, int *len_p)
544 while (len && isspace(line[len-1]))
550 static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
552 struct commit_list *parent = commit->parents;
555 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
556 !parent || !parent->next)
559 offset = sprintf(buf, "Merge:");
562 struct commit *p = parent->item;
563 const char *hex = abbrev
564 ? find_unique_abbrev(p->object.sha1, abbrev)
565 : sha1_to_hex(p->object.sha1);
566 const char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
567 parent = parent->next;
569 offset += sprintf(buf + offset, " %s%s", hex, dots);
571 buf[offset++] = '\n';
575 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)
577 int hdr = 1, body = 0;
578 unsigned long offset = 0;
580 int parents_shown = 0;
581 const char *msg = commit->buffer;
582 int plain_non_ascii = 0;
584 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
587 /* After-subject is used to pass in Content-Type: multipart
588 * MIME header; in that case we do not have to do the
589 * plaintext content type even if the commit message has
590 * non 7-bit ASCII character. Otherwise, check if we need
591 * to say this is not a 7-bit ASCII.
593 if (fmt == CMIT_FMT_EMAIL && !after_subject) {
596 for (in_body = i = 0; (ch = msg[i]) && i < len; i++) {
598 /* author could be non 7-bit ASCII but
599 * the log may so; skip over the
603 i + 1 < len && msg[i+1] == '\n')
606 else if (ch & 0x80) {
614 const char *line = msg;
615 int linelen = get_one_line(msg, len);
621 * We want some slop for indentation and a possible
622 * final "...". Thus the "+ 20".
624 if (offset + linelen + 20 > space) {
625 memcpy(buf + offset, " ...\n", 8);
635 if ((fmt != CMIT_FMT_ONELINE) && !subject)
636 buf[offset++] = '\n';
639 if (fmt == CMIT_FMT_RAW) {
640 memcpy(buf + offset, line, linelen);
644 if (!memcmp(line, "parent ", 7)) {
646 die("bad parent line in commit");
650 if (!parents_shown) {
651 offset += add_merge_info(fmt, buf + offset,
657 * MEDIUM == DEFAULT shows only author with dates.
658 * FULL shows both authors but not dates.
659 * FULLER shows both authors and dates.
661 if (!memcmp(line, "author ", 7))
662 offset += add_user_info("Author", fmt,
665 if (!memcmp(line, "committer ", 10) &&
666 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
667 offset += add_user_info("Commit", fmt,
676 if (is_empty_line(line, &linelen)) {
681 if (fmt == CMIT_FMT_SHORT)
686 int slen = strlen(subject);
687 memcpy(buf + offset, subject, slen);
689 offset += add_rfc2047(buf + offset, line, linelen);
692 memset(buf + offset, ' ', indent);
693 memcpy(buf + offset + indent, line, linelen);
694 offset += linelen + indent;
696 buf[offset++] = '\n';
697 if (fmt == CMIT_FMT_ONELINE)
699 if (subject && plain_non_ascii) {
700 static const char header[] =
701 "Content-Type: text/plain; charset=UTF-8\n"
702 "Content-Transfer-Encoding: 8bit\n";
703 memcpy(buf + offset, header, sizeof(header)-1);
704 offset += sizeof(header)-1;
707 int slen = strlen(after_subject);
708 if (slen > space - offset - 1)
709 slen = space - offset - 1;
710 memcpy(buf + offset, after_subject, slen);
712 after_subject = NULL;
716 while (offset && isspace(buf[offset-1]))
718 /* Make sure there is an EOLN for the non-oneline case */
719 if (fmt != CMIT_FMT_ONELINE)
720 buf[offset++] = '\n';
722 * make sure there is another EOLN to separate the headers from whatever
723 * body the caller appends if we haven't already written a body
725 if (fmt == CMIT_FMT_EMAIL && !body)
726 buf[offset++] = '\n';
731 struct commit *pop_commit(struct commit_list **stack)
733 struct commit_list *top = *stack;
734 struct commit *item = top ? top->item : NULL;
743 int count_parents(struct commit * commit)
746 struct commit_list * parents = commit->parents;
747 for (count=0;parents; parents=parents->next,count++)
752 void topo_sort_default_setter(struct commit *c, void *data)
757 void *topo_sort_default_getter(struct commit *c)
763 * Performs an in-place topological sort on the list supplied.
765 void sort_in_topological_order(struct commit_list ** list, int lifo)
767 sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
768 topo_sort_default_getter);
771 void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
772 topo_sort_set_fn_t setter,
773 topo_sort_get_fn_t getter)
775 struct commit_list * next = *list;
776 struct commit_list * work = NULL, **insert;
777 struct commit_list ** pptr = list;
778 struct sort_node * nodes;
779 struct sort_node * next_nodes;
782 /* determine the size of the list */
790 /* allocate an array to help sort the list */
791 nodes = xcalloc(count, sizeof(*nodes));
792 /* link the list to the array */
796 next_nodes->list_item = next;
797 setter(next->item, next_nodes);
801 /* update the indegree */
804 struct commit_list * parents = next->item->parents;
806 struct commit * parent=parents->item;
807 struct sort_node * pn = (struct sort_node *) getter(parent);
811 parents=parents->next;
818 * tips are nodes not reachable from any other node in the list
820 * the tips serve as a starting set for the work queue.
825 struct sort_node * node = (struct sort_node *) getter(next->item);
827 if (node->indegree == 0) {
828 insert = &commit_list_insert(next->item, insert)->next;
833 /* process the list in topological order */
837 struct commit * work_item = pop_commit(&work);
838 struct sort_node * work_node = (struct sort_node *) getter(work_item);
839 struct commit_list * parents = work_item->parents;
842 struct commit * parent=parents->item;
843 struct sort_node * pn = (struct sort_node *) getter(parent);
847 * parents are only enqueued for emission
848 * when all their children have been emitted thereby
849 * guaranteeing topological order.
854 insert_by_date(parent, &work);
856 commit_list_insert(parent, &work);
859 parents=parents->next;
862 * work_item is a commit all of whose children
863 * have already been emitted. we can emit it now.
865 *pptr = work_node->list_item;
866 pptr = &(*pptr)->next;
868 setter(work_item, NULL);
873 /* merge-rebase stuff */
875 /* bits #0..7 in revision.h */
876 #define PARENT1 (1u<< 8)
877 #define PARENT2 (1u<< 9)
878 #define STALE (1u<<10)
879 #define RESULT (1u<<11)
881 static struct commit *interesting(struct commit_list *list)
884 struct commit *commit = list->item;
886 if (commit->object.flags & STALE)
893 static struct commit_list *merge_bases(struct commit *one, struct commit *two)
895 struct commit_list *list = NULL;
896 struct commit_list *result = NULL;
899 /* We do not mark this even with RESULT so we do not
900 * have to clean it up.
902 return commit_list_insert(one, &result);
907 one->object.flags |= PARENT1;
908 two->object.flags |= PARENT2;
909 insert_by_date(one, &list);
910 insert_by_date(two, &list);
912 while (interesting(list)) {
913 struct commit *commit;
914 struct commit_list *parents;
915 struct commit_list *n;
923 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
924 if (flags == (PARENT1 | PARENT2)) {
925 if (!(commit->object.flags & RESULT)) {
926 commit->object.flags |= RESULT;
927 insert_by_date(commit, &result);
929 /* Mark parents of a found merge stale */
932 parents = commit->parents;
934 struct commit *p = parents->item;
935 parents = parents->next;
936 if ((p->object.flags & flags) == flags)
939 p->object.flags |= flags;
940 insert_by_date(p, &list);
944 /* Clean up the result to remove stale ones */
945 list = result; result = NULL;
947 struct commit_list *n = list->next;
948 if (!(list->item->object.flags & STALE))
949 insert_by_date(list->item, &result);
956 struct commit_list *get_merge_bases(struct commit *one,
960 const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
961 struct commit_list *list;
962 struct commit **rslt;
963 struct commit_list *result;
966 result = merge_bases(one, two);
969 if (!result || !result->next) {
971 clear_commit_marks(one, all_flags);
972 clear_commit_marks(two, all_flags);
977 /* There are more than one */
984 rslt = xcalloc(cnt, sizeof(*rslt));
985 for (list = result, i = 0; list; list = list->next)
986 rslt[i++] = list->item;
987 free_commit_list(result);
989 clear_commit_marks(one, all_flags);
990 clear_commit_marks(two, all_flags);
991 for (i = 0; i < cnt - 1; i++) {
992 for (j = i+1; j < cnt; j++) {
993 if (!rslt[i] || !rslt[j])
995 result = merge_bases(rslt[i], rslt[j]);
996 clear_commit_marks(rslt[i], all_flags);
997 clear_commit_marks(rslt[j], all_flags);
998 for (list = result; list; list = list->next) {
999 if (rslt[i] == list->item)
1001 if (rslt[j] == list->item)
1007 /* Surviving ones in rslt[] are the independent results */
1009 for (i = 0; i < cnt; i++) {
1011 insert_by_date(rslt[i], &result);