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, "=oneline"))
40 return CMIT_FMT_ONELINE;
41 die("invalid --pretty format");
44 static struct commit *check_commit(struct object *obj,
45 const unsigned char *sha1,
48 if (obj->type != commit_type) {
50 error("Object %s is a %s, not a commit",
51 sha1_to_hex(sha1), obj->type);
54 return (struct commit *) obj;
57 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
60 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
64 return check_commit(obj, sha1, quiet);
67 struct commit *lookup_commit_reference(const unsigned char *sha1)
69 return lookup_commit_reference_gently(sha1, 0);
72 struct commit *lookup_commit(const unsigned char *sha1)
74 struct object *obj = lookup_object(sha1);
76 struct commit *ret = xcalloc(1, sizeof(struct commit));
77 created_object(sha1, &ret->object);
78 ret->object.type = commit_type;
82 obj->type = commit_type;
83 return check_commit(obj, sha1, 0);
86 static unsigned long parse_commit_date(const char *buf)
90 if (memcmp(buf, "author", 6))
92 while (*buf++ != '\n')
94 if (memcmp(buf, "committer", 9))
98 date = strtoul(buf, NULL, 10);
99 if (date == ULONG_MAX)
104 static struct commit_graft **commit_graft;
105 static int commit_graft_alloc, commit_graft_nr;
107 static int commit_graft_pos(const unsigned char *sha1)
111 hi = commit_graft_nr;
113 int mi = (lo + hi) / 2;
114 struct commit_graft *graft = commit_graft[mi];
115 int cmp = memcmp(sha1, graft->sha1, 20);
126 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
128 int pos = commit_graft_pos(graft->sha1);
134 free(commit_graft[pos]);
135 commit_graft[pos] = graft;
140 if (commit_graft_alloc <= ++commit_graft_nr) {
141 commit_graft_alloc = alloc_nr(commit_graft_alloc);
142 commit_graft = xrealloc(commit_graft,
143 sizeof(*commit_graft) *
146 if (pos < commit_graft_nr)
147 memmove(commit_graft + pos + 1,
149 (commit_graft_nr - pos - 1) *
150 sizeof(*commit_graft));
151 commit_graft[pos] = graft;
155 struct commit_graft *read_graft_line(char *buf, int len)
157 /* The format is just "Commit Parent1 Parent2 ...\n" */
159 struct commit_graft *graft = NULL;
161 if (buf[len-1] == '\n')
165 if ((len + 1) % 41) {
167 error("bad graft data: %s", buf);
171 i = (len + 1) / 41 - 1;
172 graft = xmalloc(sizeof(*graft) + 20 * i);
173 graft->nr_parent = i;
174 if (get_sha1_hex(buf, graft->sha1))
176 for (i = 40; i < len; i += 41) {
179 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
185 int read_graft_file(const char *graft_file)
187 FILE *fp = fopen(graft_file, "r");
191 while (fgets(buf, sizeof(buf), fp)) {
192 /* The format is just "Commit Parent1 Parent2 ...\n" */
193 int len = strlen(buf);
194 struct commit_graft *graft = read_graft_line(buf, len);
197 if (register_commit_graft(graft, 1))
198 error("duplicate graft data: %s", buf);
204 static void prepare_commit_graft(void)
206 static int commit_graft_prepared;
209 if (commit_graft_prepared)
211 graft_file = get_graft_file();
212 read_graft_file(graft_file);
213 commit_graft_prepared = 1;
216 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
219 prepare_commit_graft();
220 pos = commit_graft_pos(sha1);
223 return commit_graft[pos];
226 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
228 char *bufptr = buffer;
229 unsigned char parent[20];
230 struct commit_list **pptr;
231 struct commit_graft *graft;
234 if (item->object.parsed)
236 item->object.parsed = 1;
237 if (memcmp(bufptr, "tree ", 5))
238 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
239 if (get_sha1_hex(bufptr + 5, parent) < 0)
240 return error("bad tree pointer in commit %s",
241 sha1_to_hex(item->object.sha1));
242 item->tree = lookup_tree(parent);
245 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
246 pptr = &item->parents;
248 graft = lookup_commit_graft(item->object.sha1);
249 while (!memcmp(bufptr, "parent ", 7)) {
250 struct commit *new_parent;
252 if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
253 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
257 new_parent = lookup_commit(parent);
259 pptr = &commit_list_insert(new_parent, pptr)->next;
265 struct commit *new_parent;
266 for (i = 0; i < graft->nr_parent; i++) {
267 new_parent = lookup_commit(graft->parent[i]);
270 pptr = &commit_list_insert(new_parent, pptr)->next;
274 item->date = parse_commit_date(bufptr);
276 if (track_object_refs) {
278 struct commit_list *p;
279 struct object_refs *refs = alloc_object_refs(n_refs);
281 refs->ref[i++] = &item->tree->object;
282 for (p = item->parents; p; p = p->next)
283 refs->ref[i++] = &p->item->object;
284 set_object_refs(&item->object, refs);
290 int parse_commit(struct commit *item)
297 if (item->object.parsed)
299 buffer = read_sha1_file(item->object.sha1, type, &size);
301 return error("Could not read %s",
302 sha1_to_hex(item->object.sha1));
303 if (strcmp(type, commit_type)) {
305 return error("Object %s not a commit",
306 sha1_to_hex(item->object.sha1));
308 ret = parse_commit_buffer(item, buffer, size);
309 if (save_commit_buffer && !ret) {
310 item->buffer = buffer;
317 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
319 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
320 new_list->item = item;
321 new_list->next = *list_p;
326 void free_commit_list(struct commit_list *list)
329 struct commit_list *temp = list;
335 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
337 struct commit_list **pp = list;
338 struct commit_list *p;
339 while ((p = *pp) != NULL) {
340 if (p->item->date < item->date) {
345 return commit_list_insert(item, pp);
349 void sort_by_date(struct commit_list **list)
351 struct commit_list *ret = NULL;
353 insert_by_date((*list)->item, &ret);
354 *list = (*list)->next;
359 struct commit *pop_most_recent_commit(struct commit_list **list,
362 struct commit *ret = (*list)->item;
363 struct commit_list *parents = ret->parents;
364 struct commit_list *old = *list;
366 *list = (*list)->next;
370 struct commit *commit = parents->item;
371 parse_commit(commit);
372 if (!(commit->object.flags & mark)) {
373 commit->object.flags |= mark;
374 insert_by_date(commit, list);
376 parents = parents->next;
381 void clear_commit_marks(struct commit *commit, unsigned int mark)
383 struct commit_list *parents;
385 parents = commit->parents;
386 commit->object.flags &= ~mark;
388 struct commit *parent = parents->item;
389 if (parent && parent->object.parsed &&
390 (parent->object.flags & mark))
391 clear_commit_marks(parent, mark);
392 parents = parents->next;
397 * Generic support for pretty-printing the header
399 static int get_one_line(const char *msg, unsigned long len)
414 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
420 const char *filler = " ";
422 if (fmt == CMIT_FMT_ONELINE)
424 date = strchr(line, '>');
427 namelen = ++date - line;
428 time = strtoul(date, &date, 10);
429 tz = strtol(date, NULL, 10);
431 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
432 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
433 filler, namelen, line);
435 case CMIT_FMT_MEDIUM:
436 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
438 case CMIT_FMT_FULLER:
439 ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz));
448 static int is_empty_line(const char *line, int len)
450 while (len && isspace(line[len-1]))
455 static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
457 struct commit_list *parent = commit->parents;
460 if ((fmt == CMIT_FMT_ONELINE) || !parent || !parent->next)
463 offset = sprintf(buf, "Merge:");
466 struct commit *p = parent->item;
467 const char *hex = abbrev
468 ? find_unique_abbrev(p->object.sha1, abbrev)
469 : sha1_to_hex(p->object.sha1);
470 char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
471 parent = parent->next;
473 offset += sprintf(buf + offset, " %s%s", hex, dots);
475 buf[offset++] = '\n';
479 unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, unsigned long len, char *buf, unsigned long space, int abbrev)
481 int hdr = 1, body = 0;
482 unsigned long offset = 0;
483 int indent = (fmt == CMIT_FMT_ONELINE) ? 0 : 4;
484 int parents_shown = 0;
485 const char *msg = commit->buffer;
488 const char *line = msg;
489 int linelen = get_one_line(msg, len);
495 * We want some slop for indentation and a possible
496 * final "...". Thus the "+ 20".
498 if (offset + linelen + 20 > space) {
499 memcpy(buf + offset, " ...\n", 8);
509 if (fmt != CMIT_FMT_ONELINE)
510 buf[offset++] = '\n';
513 if (fmt == CMIT_FMT_RAW) {
514 memcpy(buf + offset, line, linelen);
518 if (!memcmp(line, "parent ", 7)) {
520 die("bad parent line in commit");
524 if (!parents_shown) {
525 offset += add_merge_info(fmt, buf + offset,
531 * MEDIUM == DEFAULT shows only author with dates.
532 * FULL shows both authors but not dates.
533 * FULLER shows both authors and dates.
535 if (!memcmp(line, "author ", 7))
536 offset += add_user_info("Author", fmt,
539 if (!memcmp(line, "committer ", 10) &&
540 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
541 offset += add_user_info("Commit", fmt,
547 if (is_empty_line(line, linelen)) {
550 if (fmt == CMIT_FMT_SHORT)
556 memset(buf + offset, ' ', indent);
557 memcpy(buf + offset + indent, line, linelen);
558 offset += linelen + indent;
559 if (fmt == CMIT_FMT_ONELINE)
562 while (offset && isspace(buf[offset-1]))
564 /* Make sure there is an EOLN for the non-oneline case */
565 if (fmt != CMIT_FMT_ONELINE)
566 buf[offset++] = '\n';
571 struct commit *pop_commit(struct commit_list **stack)
573 struct commit_list *top = *stack;
574 struct commit *item = top ? top->item : NULL;
583 int count_parents(struct commit * commit)
586 struct commit_list * parents = commit->parents;
587 for (count=0;parents; parents=parents->next,count++)
592 void topo_sort_default_setter(struct commit *c, void *data)
594 c->object.util = data;
597 void *topo_sort_default_getter(struct commit *c)
599 return c->object.util;
603 * Performs an in-place topological sort on the list supplied.
605 void sort_in_topological_order(struct commit_list ** list, int lifo)
607 sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
608 topo_sort_default_getter);
611 void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
612 topo_sort_set_fn_t setter,
613 topo_sort_get_fn_t getter)
615 struct commit_list * next = *list;
616 struct commit_list * work = NULL, **insert;
617 struct commit_list ** pptr = list;
618 struct sort_node * nodes;
619 struct sort_node * next_nodes;
622 /* determine the size of the list */
630 /* allocate an array to help sort the list */
631 nodes = xcalloc(count, sizeof(*nodes));
632 /* link the list to the array */
636 next_nodes->list_item = next;
637 setter(next->item, next_nodes);
641 /* update the indegree */
644 struct commit_list * parents = next->item->parents;
646 struct commit * parent=parents->item;
647 struct sort_node * pn = (struct sort_node *) getter(parent);
651 parents=parents->next;
658 * tips are nodes not reachable from any other node in the list
660 * the tips serve as a starting set for the work queue.
665 struct sort_node * node = (struct sort_node *) getter(next->item);
667 if (node->indegree == 0) {
668 insert = &commit_list_insert(next->item, insert)->next;
673 /* process the list in topological order */
677 struct commit * work_item = pop_commit(&work);
678 struct sort_node * work_node = (struct sort_node *) getter(work_item);
679 struct commit_list * parents = work_item->parents;
682 struct commit * parent=parents->item;
683 struct sort_node * pn = (struct sort_node *) getter(parent);
687 * parents are only enqueued for emission
688 * when all their children have been emitted thereby
689 * guaranteeing topological order.
694 insert_by_date(parent, &work);
696 commit_list_insert(parent, &work);
699 parents=parents->next;
702 * work_item is a commit all of whose children
703 * have already been emitted. we can emit it now.
705 *pptr = work_node->list_item;
706 pptr = &(*pptr)->next;
708 setter(work_item, NULL);