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 { "full", 5, CMIT_FMT_FULL },
34 { "fuller", 5, CMIT_FMT_FULLER },
35 { "oneline", 1, CMIT_FMT_ONELINE },
38 enum cmit_fmt get_commit_format(const char *arg)
43 return CMIT_FMT_DEFAULT;
46 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
47 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len))
51 die("invalid --pretty format: %s", arg);
54 static struct commit *check_commit(struct object *obj,
55 const unsigned char *sha1,
58 if (obj->type != commit_type) {
60 error("Object %s is a %s, not a commit",
61 sha1_to_hex(sha1), obj->type);
64 return (struct commit *) obj;
67 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
70 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
74 return check_commit(obj, sha1, quiet);
77 struct commit *lookup_commit_reference(const unsigned char *sha1)
79 return lookup_commit_reference_gently(sha1, 0);
82 struct commit *lookup_commit(const unsigned char *sha1)
84 struct object *obj = lookup_object(sha1);
86 struct commit *ret = xcalloc(1, sizeof(struct commit));
87 created_object(sha1, &ret->object);
88 ret->object.type = commit_type;
92 obj->type = commit_type;
93 return check_commit(obj, sha1, 0);
96 static unsigned long parse_commit_date(const char *buf)
100 if (memcmp(buf, "author", 6))
102 while (*buf++ != '\n')
104 if (memcmp(buf, "committer", 9))
106 while (*buf++ != '>')
108 date = strtoul(buf, NULL, 10);
109 if (date == ULONG_MAX)
114 static struct commit_graft **commit_graft;
115 static int commit_graft_alloc, commit_graft_nr;
117 static int commit_graft_pos(const unsigned char *sha1)
121 hi = commit_graft_nr;
123 int mi = (lo + hi) / 2;
124 struct commit_graft *graft = commit_graft[mi];
125 int cmp = memcmp(sha1, graft->sha1, 20);
136 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
138 int pos = commit_graft_pos(graft->sha1);
144 free(commit_graft[pos]);
145 commit_graft[pos] = graft;
150 if (commit_graft_alloc <= ++commit_graft_nr) {
151 commit_graft_alloc = alloc_nr(commit_graft_alloc);
152 commit_graft = xrealloc(commit_graft,
153 sizeof(*commit_graft) *
156 if (pos < commit_graft_nr)
157 memmove(commit_graft + pos + 1,
159 (commit_graft_nr - pos - 1) *
160 sizeof(*commit_graft));
161 commit_graft[pos] = graft;
165 struct commit_graft *read_graft_line(char *buf, int len)
167 /* The format is just "Commit Parent1 Parent2 ...\n" */
169 struct commit_graft *graft = NULL;
171 if (buf[len-1] == '\n')
173 if (buf[0] == '#' || buf[0] == '\0')
175 if ((len + 1) % 41) {
177 error("bad graft data: %s", buf);
181 i = (len + 1) / 41 - 1;
182 graft = xmalloc(sizeof(*graft) + 20 * i);
183 graft->nr_parent = i;
184 if (get_sha1_hex(buf, graft->sha1))
186 for (i = 40; i < len; i += 41) {
189 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
195 int read_graft_file(const char *graft_file)
197 FILE *fp = fopen(graft_file, "r");
201 while (fgets(buf, sizeof(buf), fp)) {
202 /* The format is just "Commit Parent1 Parent2 ...\n" */
203 int len = strlen(buf);
204 struct commit_graft *graft = read_graft_line(buf, len);
207 if (register_commit_graft(graft, 1))
208 error("duplicate graft data: %s", buf);
214 static void prepare_commit_graft(void)
216 static int commit_graft_prepared;
219 if (commit_graft_prepared)
221 graft_file = get_graft_file();
222 read_graft_file(graft_file);
223 commit_graft_prepared = 1;
226 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
229 prepare_commit_graft();
230 pos = commit_graft_pos(sha1);
233 return commit_graft[pos];
236 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
238 char *bufptr = buffer;
239 unsigned char parent[20];
240 struct commit_list **pptr;
241 struct commit_graft *graft;
244 if (item->object.parsed)
246 item->object.parsed = 1;
247 if (memcmp(bufptr, "tree ", 5))
248 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
249 if (get_sha1_hex(bufptr + 5, parent) < 0)
250 return error("bad tree pointer in commit %s",
251 sha1_to_hex(item->object.sha1));
252 item->tree = lookup_tree(parent);
255 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
256 pptr = &item->parents;
258 graft = lookup_commit_graft(item->object.sha1);
259 while (!memcmp(bufptr, "parent ", 7)) {
260 struct commit *new_parent;
262 if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
263 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
267 new_parent = lookup_commit(parent);
269 pptr = &commit_list_insert(new_parent, pptr)->next;
275 struct commit *new_parent;
276 for (i = 0; i < graft->nr_parent; i++) {
277 new_parent = lookup_commit(graft->parent[i]);
280 pptr = &commit_list_insert(new_parent, pptr)->next;
284 item->date = parse_commit_date(bufptr);
286 if (track_object_refs) {
288 struct commit_list *p;
289 struct object_refs *refs = alloc_object_refs(n_refs);
291 refs->ref[i++] = &item->tree->object;
292 for (p = item->parents; p; p = p->next)
293 refs->ref[i++] = &p->item->object;
294 set_object_refs(&item->object, refs);
300 int parse_commit(struct commit *item)
307 if (item->object.parsed)
309 buffer = read_sha1_file(item->object.sha1, type, &size);
311 return error("Could not read %s",
312 sha1_to_hex(item->object.sha1));
313 if (strcmp(type, commit_type)) {
315 return error("Object %s not a commit",
316 sha1_to_hex(item->object.sha1));
318 ret = parse_commit_buffer(item, buffer, size);
319 if (save_commit_buffer && !ret) {
320 item->buffer = buffer;
327 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
329 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
330 new_list->item = item;
331 new_list->next = *list_p;
336 void free_commit_list(struct commit_list *list)
339 struct commit_list *temp = list;
345 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
347 struct commit_list **pp = list;
348 struct commit_list *p;
349 while ((p = *pp) != NULL) {
350 if (p->item->date < item->date) {
355 return commit_list_insert(item, pp);
359 void sort_by_date(struct commit_list **list)
361 struct commit_list *ret = NULL;
363 insert_by_date((*list)->item, &ret);
364 *list = (*list)->next;
369 struct commit *pop_most_recent_commit(struct commit_list **list,
372 struct commit *ret = (*list)->item;
373 struct commit_list *parents = ret->parents;
374 struct commit_list *old = *list;
376 *list = (*list)->next;
380 struct commit *commit = parents->item;
381 parse_commit(commit);
382 if (!(commit->object.flags & mark)) {
383 commit->object.flags |= mark;
384 insert_by_date(commit, list);
386 parents = parents->next;
391 void clear_commit_marks(struct commit *commit, unsigned int mark)
393 struct commit_list *parents;
395 parents = commit->parents;
396 commit->object.flags &= ~mark;
398 struct commit *parent = parents->item;
399 if (parent && parent->object.parsed &&
400 (parent->object.flags & mark))
401 clear_commit_marks(parent, mark);
402 parents = parents->next;
407 * Generic support for pretty-printing the header
409 static int get_one_line(const char *msg, unsigned long len)
424 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
430 const char *filler = " ";
432 if (fmt == CMIT_FMT_ONELINE)
434 date = strchr(line, '>');
437 namelen = ++date - line;
438 time = strtoul(date, &date, 10);
439 tz = strtol(date, NULL, 10);
441 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
442 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
443 filler, namelen, line);
445 case CMIT_FMT_MEDIUM:
446 ret += sprintf(buf + ret, "Date: %s\n", show_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)
460 while (len && isspace(line[len-1]))
465 static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
467 struct commit_list *parent = commit->parents;
470 if ((fmt == CMIT_FMT_ONELINE) || !parent || !parent->next)
473 offset = sprintf(buf, "Merge:");
476 struct commit *p = parent->item;
477 const char *hex = abbrev
478 ? find_unique_abbrev(p->object.sha1, abbrev)
479 : sha1_to_hex(p->object.sha1);
480 char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
481 parent = parent->next;
483 offset += sprintf(buf + offset, " %s%s", hex, dots);
485 buf[offset++] = '\n';
489 unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, unsigned long len, char *buf, unsigned long space, int abbrev)
491 int hdr = 1, body = 0;
492 unsigned long offset = 0;
493 int indent = (fmt == CMIT_FMT_ONELINE) ? 0 : 4;
494 int parents_shown = 0;
495 const char *msg = commit->buffer;
498 const char *line = msg;
499 int linelen = get_one_line(msg, len);
505 * We want some slop for indentation and a possible
506 * final "...". Thus the "+ 20".
508 if (offset + linelen + 20 > space) {
509 memcpy(buf + offset, " ...\n", 8);
519 if (fmt != CMIT_FMT_ONELINE)
520 buf[offset++] = '\n';
523 if (fmt == CMIT_FMT_RAW) {
524 memcpy(buf + offset, line, linelen);
528 if (!memcmp(line, "parent ", 7)) {
530 die("bad parent line in commit");
534 if (!parents_shown) {
535 offset += add_merge_info(fmt, buf + offset,
541 * MEDIUM == DEFAULT shows only author with dates.
542 * FULL shows both authors but not dates.
543 * FULLER shows both authors and dates.
545 if (!memcmp(line, "author ", 7))
546 offset += add_user_info("Author", fmt,
549 if (!memcmp(line, "committer ", 10) &&
550 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
551 offset += add_user_info("Commit", fmt,
557 if (is_empty_line(line, linelen)) {
560 if (fmt == CMIT_FMT_SHORT)
566 memset(buf + offset, ' ', indent);
567 memcpy(buf + offset + indent, line, linelen);
568 offset += linelen + indent;
569 if (fmt == CMIT_FMT_ONELINE)
572 while (offset && isspace(buf[offset-1]))
574 /* Make sure there is an EOLN for the non-oneline case */
575 if (fmt != CMIT_FMT_ONELINE)
576 buf[offset++] = '\n';
581 struct commit *pop_commit(struct commit_list **stack)
583 struct commit_list *top = *stack;
584 struct commit *item = top ? top->item : NULL;
593 int count_parents(struct commit * commit)
596 struct commit_list * parents = commit->parents;
597 for (count=0;parents; parents=parents->next,count++)
602 void topo_sort_default_setter(struct commit *c, void *data)
604 c->object.util = data;
607 void *topo_sort_default_getter(struct commit *c)
609 return c->object.util;
613 * Performs an in-place topological sort on the list supplied.
615 void sort_in_topological_order(struct commit_list ** list, int lifo)
617 sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
618 topo_sort_default_getter);
621 void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
622 topo_sort_set_fn_t setter,
623 topo_sort_get_fn_t getter)
625 struct commit_list * next = *list;
626 struct commit_list * work = NULL, **insert;
627 struct commit_list ** pptr = list;
628 struct sort_node * nodes;
629 struct sort_node * next_nodes;
632 /* determine the size of the list */
640 /* allocate an array to help sort the list */
641 nodes = xcalloc(count, sizeof(*nodes));
642 /* link the list to the array */
646 next_nodes->list_item = next;
647 setter(next->item, next_nodes);
651 /* update the indegree */
654 struct commit_list * parents = next->item->parents;
656 struct commit * parent=parents->item;
657 struct sort_node * pn = (struct sort_node *) getter(parent);
661 parents=parents->next;
668 * tips are nodes not reachable from any other node in the list
670 * the tips serve as a starting set for the work queue.
675 struct sort_node * node = (struct sort_node *) getter(next->item);
677 if (node->indegree == 0) {
678 insert = &commit_list_insert(next->item, insert)->next;
683 /* process the list in topological order */
687 struct commit * work_item = pop_commit(&work);
688 struct sort_node * work_node = (struct sort_node *) getter(work_item);
689 struct commit_list * parents = work_item->parents;
692 struct commit * parent=parents->item;
693 struct sort_node * pn = (struct sort_node *) getter(parent);
697 * parents are only enqueued for emission
698 * when all their children have been emitted thereby
699 * guaranteeing topological order.
704 insert_by_date(parent, &work);
706 commit_list_insert(parent, &work);
709 parents=parents->next;
712 * work_item is a commit all of whose children
713 * have already been emitted. we can emit it now.
715 *pptr = work_node->list_item;
716 pptr = &(*pptr)->next;
718 setter(work_item, NULL);