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";
25 enum cmit_fmt get_commit_format(const char *arg)
28 return CMIT_FMT_DEFAULT;
29 if (!strcmp(arg, "=raw"))
31 if (!strcmp(arg, "=medium"))
32 return CMIT_FMT_MEDIUM;
33 if (!strcmp(arg, "=short"))
34 return CMIT_FMT_SHORT;
35 if (!strcmp(arg, "=full"))
37 if (!strcmp(arg, "=fuller"))
38 return CMIT_FMT_FULLER;
39 if (!strcmp(arg, "=email"))
40 return CMIT_FMT_EMAIL;
41 if (!strcmp(arg, "=oneline"))
42 return CMIT_FMT_ONELINE;
43 die("invalid --pretty format");
46 static struct commit *check_commit(struct object *obj,
47 const unsigned char *sha1,
50 if (obj->type != commit_type) {
52 error("Object %s is a %s, not a commit",
53 sha1_to_hex(sha1), obj->type);
56 return (struct commit *) obj;
59 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
62 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
66 return check_commit(obj, sha1, quiet);
69 struct commit *lookup_commit_reference(const unsigned char *sha1)
71 return lookup_commit_reference_gently(sha1, 0);
74 struct commit *lookup_commit(const unsigned char *sha1)
76 struct object *obj = lookup_object(sha1);
78 struct commit *ret = xcalloc(1, sizeof(struct commit));
79 created_object(sha1, &ret->object);
80 ret->object.type = commit_type;
84 obj->type = commit_type;
85 return check_commit(obj, sha1, 0);
88 static unsigned long parse_commit_date(const char *buf)
92 if (memcmp(buf, "author", 6))
94 while (*buf++ != '\n')
96 if (memcmp(buf, "committer", 9))
100 date = strtoul(buf, NULL, 10);
101 if (date == ULONG_MAX)
106 static struct commit_graft **commit_graft;
107 static int commit_graft_alloc, commit_graft_nr;
109 static int commit_graft_pos(const unsigned char *sha1)
113 hi = commit_graft_nr;
115 int mi = (lo + hi) / 2;
116 struct commit_graft *graft = commit_graft[mi];
117 int cmp = memcmp(sha1, graft->sha1, 20);
128 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
130 int pos = commit_graft_pos(graft->sha1);
136 free(commit_graft[pos]);
137 commit_graft[pos] = graft;
142 if (commit_graft_alloc <= ++commit_graft_nr) {
143 commit_graft_alloc = alloc_nr(commit_graft_alloc);
144 commit_graft = xrealloc(commit_graft,
145 sizeof(*commit_graft) *
148 if (pos < commit_graft_nr)
149 memmove(commit_graft + pos + 1,
151 (commit_graft_nr - pos - 1) *
152 sizeof(*commit_graft));
153 commit_graft[pos] = graft;
157 struct commit_graft *read_graft_line(char *buf, int len)
159 /* The format is just "Commit Parent1 Parent2 ...\n" */
161 struct commit_graft *graft = NULL;
163 if (buf[len-1] == '\n')
165 if (buf[0] == '#' || buf[0] == '\0')
167 if ((len + 1) % 41) {
169 error("bad graft data: %s", buf);
173 i = (len + 1) / 41 - 1;
174 graft = xmalloc(sizeof(*graft) + 20 * i);
175 graft->nr_parent = i;
176 if (get_sha1_hex(buf, graft->sha1))
178 for (i = 40; i < len; i += 41) {
181 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
187 int read_graft_file(const char *graft_file)
189 FILE *fp = fopen(graft_file, "r");
193 while (fgets(buf, sizeof(buf), fp)) {
194 /* The format is just "Commit Parent1 Parent2 ...\n" */
195 int len = strlen(buf);
196 struct commit_graft *graft = read_graft_line(buf, len);
199 if (register_commit_graft(graft, 1))
200 error("duplicate graft data: %s", buf);
206 static void prepare_commit_graft(void)
208 static int commit_graft_prepared;
211 if (commit_graft_prepared)
213 graft_file = get_graft_file();
214 read_graft_file(graft_file);
215 commit_graft_prepared = 1;
218 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
221 prepare_commit_graft();
222 pos = commit_graft_pos(sha1);
225 return commit_graft[pos];
228 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
230 char *bufptr = buffer;
231 unsigned char parent[20];
232 struct commit_list **pptr;
233 struct commit_graft *graft;
236 if (item->object.parsed)
238 item->object.parsed = 1;
239 if (memcmp(bufptr, "tree ", 5))
240 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
241 if (get_sha1_hex(bufptr + 5, parent) < 0)
242 return error("bad tree pointer in commit %s",
243 sha1_to_hex(item->object.sha1));
244 item->tree = lookup_tree(parent);
247 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
248 pptr = &item->parents;
250 graft = lookup_commit_graft(item->object.sha1);
251 while (!memcmp(bufptr, "parent ", 7)) {
252 struct commit *new_parent;
254 if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
255 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
259 new_parent = lookup_commit(parent);
261 pptr = &commit_list_insert(new_parent, pptr)->next;
267 struct commit *new_parent;
268 for (i = 0; i < graft->nr_parent; i++) {
269 new_parent = lookup_commit(graft->parent[i]);
272 pptr = &commit_list_insert(new_parent, pptr)->next;
276 item->date = parse_commit_date(bufptr);
278 if (track_object_refs) {
280 struct commit_list *p;
281 struct object_refs *refs = alloc_object_refs(n_refs);
283 refs->ref[i++] = &item->tree->object;
284 for (p = item->parents; p; p = p->next)
285 refs->ref[i++] = &p->item->object;
286 set_object_refs(&item->object, refs);
292 int parse_commit(struct commit *item)
299 if (item->object.parsed)
301 buffer = read_sha1_file(item->object.sha1, type, &size);
303 return error("Could not read %s",
304 sha1_to_hex(item->object.sha1));
305 if (strcmp(type, commit_type)) {
307 return error("Object %s not a commit",
308 sha1_to_hex(item->object.sha1));
310 ret = parse_commit_buffer(item, buffer, size);
311 if (save_commit_buffer && !ret) {
312 item->buffer = buffer;
319 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
321 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
322 new_list->item = item;
323 new_list->next = *list_p;
328 void free_commit_list(struct commit_list *list)
331 struct commit_list *temp = list;
337 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
339 struct commit_list **pp = list;
340 struct commit_list *p;
341 while ((p = *pp) != NULL) {
342 if (p->item->date < item->date) {
347 return commit_list_insert(item, pp);
351 void sort_by_date(struct commit_list **list)
353 struct commit_list *ret = NULL;
355 insert_by_date((*list)->item, &ret);
356 *list = (*list)->next;
361 struct commit *pop_most_recent_commit(struct commit_list **list,
364 struct commit *ret = (*list)->item;
365 struct commit_list *parents = ret->parents;
366 struct commit_list *old = *list;
368 *list = (*list)->next;
372 struct commit *commit = parents->item;
373 parse_commit(commit);
374 if (!(commit->object.flags & mark)) {
375 commit->object.flags |= mark;
376 insert_by_date(commit, list);
378 parents = parents->next;
383 void clear_commit_marks(struct commit *commit, unsigned int mark)
385 struct commit_list *parents;
387 parents = commit->parents;
388 commit->object.flags &= ~mark;
390 struct commit *parent = parents->item;
391 if (parent && parent->object.parsed &&
392 (parent->object.flags & mark))
393 clear_commit_marks(parent, mark);
394 parents = parents->next;
399 * Generic support for pretty-printing the header
401 static int get_one_line(const char *msg, unsigned long len)
416 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
422 const char *filler = " ";
424 if (fmt == CMIT_FMT_ONELINE)
426 date = strchr(line, '>');
429 namelen = ++date - line;
430 time = strtoul(date, &date, 10);
431 tz = strtol(date, NULL, 10);
433 if (fmt == CMIT_FMT_EMAIL) {
437 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
438 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
439 filler, namelen, line);
441 case CMIT_FMT_MEDIUM:
442 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
445 ret += sprintf(buf + ret, "Date: %s\n",
446 show_rfc2822_date(time, tz));
448 case CMIT_FMT_FULLER:
449 ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz));
458 static int is_empty_line(const char *line, int *len_p)
461 while (len && isspace(line[len-1]))
467 static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
469 struct commit_list *parent = commit->parents;
472 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
473 !parent || !parent->next)
476 offset = sprintf(buf, "Merge:");
479 struct commit *p = parent->item;
480 const char *hex = abbrev
481 ? find_unique_abbrev(p->object.sha1, abbrev)
482 : sha1_to_hex(p->object.sha1);
483 char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
484 parent = parent->next;
486 offset += sprintf(buf + offset, " %s%s", hex, dots);
488 buf[offset++] = '\n';
492 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)
494 int hdr = 1, body = 0;
495 unsigned long offset = 0;
497 int parents_shown = 0;
498 const char *msg = commit->buffer;
500 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
504 const char *line = msg;
505 int linelen = get_one_line(msg, len);
511 * We want some slop for indentation and a possible
512 * final "...". Thus the "+ 20".
514 if (offset + linelen + 20 > space) {
515 memcpy(buf + offset, " ...\n", 8);
525 if ((fmt != CMIT_FMT_ONELINE) && !subject)
526 buf[offset++] = '\n';
529 if (fmt == CMIT_FMT_RAW) {
530 memcpy(buf + offset, line, linelen);
534 if (!memcmp(line, "parent ", 7)) {
536 die("bad parent line in commit");
540 if (!parents_shown) {
541 offset += add_merge_info(fmt, buf + offset,
547 * MEDIUM == DEFAULT shows only author with dates.
548 * FULL shows both authors but not dates.
549 * FULLER shows both authors and dates.
551 if (!memcmp(line, "author ", 7))
552 offset += add_user_info("Author", fmt,
555 if (!memcmp(line, "committer ", 10) &&
556 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
557 offset += add_user_info("Commit", fmt,
563 if (is_empty_line(line, &linelen)) {
568 if (fmt == CMIT_FMT_SHORT)
575 int slen = strlen(subject);
576 memcpy(buf + offset, subject, slen);
579 memset(buf + offset, ' ', indent);
580 memcpy(buf + offset + indent, line, linelen);
581 offset += linelen + indent;
582 buf[offset++] = '\n';
583 if (fmt == CMIT_FMT_ONELINE)
587 while (offset && isspace(buf[offset-1]))
589 /* Make sure there is an EOLN for the non-oneline case */
590 if (fmt != CMIT_FMT_ONELINE)
591 buf[offset++] = '\n';
596 struct commit *pop_commit(struct commit_list **stack)
598 struct commit_list *top = *stack;
599 struct commit *item = top ? top->item : NULL;
608 int count_parents(struct commit * commit)
611 struct commit_list * parents = commit->parents;
612 for (count=0;parents; parents=parents->next,count++)
617 void topo_sort_default_setter(struct commit *c, void *data)
619 c->object.util = data;
622 void *topo_sort_default_getter(struct commit *c)
624 return c->object.util;
628 * Performs an in-place topological sort on the list supplied.
630 void sort_in_topological_order(struct commit_list ** list, int lifo)
632 sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
633 topo_sort_default_getter);
636 void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
637 topo_sort_set_fn_t setter,
638 topo_sort_get_fn_t getter)
640 struct commit_list * next = *list;
641 struct commit_list * work = NULL, **insert;
642 struct commit_list ** pptr = list;
643 struct sort_node * nodes;
644 struct sort_node * next_nodes;
647 /* determine the size of the list */
655 /* allocate an array to help sort the list */
656 nodes = xcalloc(count, sizeof(*nodes));
657 /* link the list to the array */
661 next_nodes->list_item = next;
662 setter(next->item, next_nodes);
666 /* update the indegree */
669 struct commit_list * parents = next->item->parents;
671 struct commit * parent=parents->item;
672 struct sort_node * pn = (struct sort_node *) getter(parent);
676 parents=parents->next;
683 * tips are nodes not reachable from any other node in the list
685 * the tips serve as a starting set for the work queue.
690 struct sort_node * node = (struct sort_node *) getter(next->item);
692 if (node->indegree == 0) {
693 insert = &commit_list_insert(next->item, insert)->next;
698 /* process the list in topological order */
702 struct commit * work_item = pop_commit(&work);
703 struct sort_node * work_node = (struct sort_node *) getter(work_item);
704 struct commit_list * parents = work_item->parents;
707 struct commit * parent=parents->item;
708 struct sort_node * pn = (struct sort_node *) getter(parent);
712 * parents are only enqueued for emission
713 * when all their children have been emitted thereby
714 * guaranteeing topological order.
719 insert_by_date(parent, &work);
721 commit_list_insert(parent, &work);
724 parents=parents->next;
727 * work_item is a commit all of whose children
728 * have already been emitted. we can emit it now.
730 *pptr = work_node->list_item;
731 pptr = &(*pptr)->next;
733 setter(work_item, NULL);