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 = xmalloc(sizeof(struct commit));
77 memset(ret, 0, sizeof(struct commit));
78 created_object(sha1, &ret->object);
79 ret->object.type = commit_type;
83 obj->type = commit_type;
84 return check_commit(obj, sha1, 0);
87 static unsigned long parse_commit_date(const char *buf)
91 if (memcmp(buf, "author", 6))
93 while (*buf++ != '\n')
95 if (memcmp(buf, "committer", 9))
99 date = strtoul(buf, NULL, 10);
100 if (date == ULONG_MAX)
105 static struct commit_graft {
106 unsigned char sha1[20];
108 unsigned char parent[0][20]; /* more */
110 static int commit_graft_alloc, commit_graft_nr;
112 static int commit_graft_pos(const unsigned char *sha1)
116 hi = commit_graft_nr;
118 int mi = (lo + hi) / 2;
119 struct commit_graft *graft = commit_graft[mi];
120 int cmp = memcmp(sha1, graft->sha1, 20);
131 static void prepare_commit_graft(void)
133 char *graft_file = get_graft_file();
134 FILE *fp = fopen(graft_file, "r");
137 commit_graft = (struct commit_graft **) "hack";
140 while (fgets(buf, sizeof(buf), fp)) {
141 /* The format is just "Commit Parent1 Parent2 ...\n" */
142 int len = strlen(buf);
144 struct commit_graft *graft = NULL;
146 if (buf[len-1] == '\n')
150 if ((len + 1) % 41) {
152 error("bad graft data: %s", buf);
156 i = (len + 1) / 41 - 1;
157 graft = xmalloc(sizeof(*graft) + 20 * i);
158 graft->nr_parent = i;
159 if (get_sha1_hex(buf, graft->sha1))
161 for (i = 40; i < len; i += 41) {
164 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
167 i = commit_graft_pos(graft->sha1);
169 error("duplicate graft data: %s", buf);
174 if (commit_graft_alloc <= ++commit_graft_nr) {
175 commit_graft_alloc = alloc_nr(commit_graft_alloc);
176 commit_graft = xrealloc(commit_graft,
177 sizeof(*commit_graft) *
180 if (i < commit_graft_nr)
181 memmove(commit_graft + i + 1,
183 (commit_graft_nr - i - 1) *
184 sizeof(*commit_graft));
185 commit_graft[i] = graft;
190 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
194 prepare_commit_graft();
195 pos = commit_graft_pos(sha1);
198 return commit_graft[pos];
201 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
203 char *bufptr = buffer;
204 unsigned char parent[20];
205 struct commit_list **pptr;
206 struct commit_graft *graft;
208 if (item->object.parsed)
210 item->object.parsed = 1;
211 if (memcmp(bufptr, "tree ", 5))
212 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
213 if (get_sha1_hex(bufptr + 5, parent) < 0)
214 return error("bad tree pointer in commit %s\n", sha1_to_hex(item->object.sha1));
215 item->tree = lookup_tree(parent);
217 add_ref(&item->object, &item->tree->object);
218 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
219 pptr = &item->parents;
221 graft = lookup_commit_graft(item->object.sha1);
222 while (!memcmp(bufptr, "parent ", 7)) {
223 struct commit *new_parent;
225 if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
226 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
230 new_parent = lookup_commit(parent);
232 pptr = &commit_list_insert(new_parent, pptr)->next;
233 add_ref(&item->object, &new_parent->object);
238 struct commit *new_parent;
239 for (i = 0; i < graft->nr_parent; i++) {
240 new_parent = lookup_commit(graft->parent[i]);
243 pptr = &commit_list_insert(new_parent, pptr)->next;
244 add_ref(&item->object, &new_parent->object);
247 item->date = parse_commit_date(bufptr);
251 int parse_commit(struct commit *item)
258 if (item->object.parsed)
260 buffer = read_sha1_file(item->object.sha1, type, &size);
262 return error("Could not read %s",
263 sha1_to_hex(item->object.sha1));
264 if (strcmp(type, commit_type)) {
266 return error("Object %s not a commit",
267 sha1_to_hex(item->object.sha1));
269 ret = parse_commit_buffer(item, buffer, size);
270 if (save_commit_buffer && !ret) {
271 item->buffer = buffer;
278 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
280 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
281 new_list->item = item;
282 new_list->next = *list_p;
287 void free_commit_list(struct commit_list *list)
290 struct commit_list *temp = list;
296 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
298 struct commit_list **pp = list;
299 struct commit_list *p;
300 while ((p = *pp) != NULL) {
301 if (p->item->date < item->date) {
306 return commit_list_insert(item, pp);
310 void sort_by_date(struct commit_list **list)
312 struct commit_list *ret = NULL;
314 insert_by_date((*list)->item, &ret);
315 *list = (*list)->next;
320 struct commit *pop_most_recent_commit(struct commit_list **list,
323 struct commit *ret = (*list)->item;
324 struct commit_list *parents = ret->parents;
325 struct commit_list *old = *list;
327 *list = (*list)->next;
331 struct commit *commit = parents->item;
332 parse_commit(commit);
333 if (!(commit->object.flags & mark)) {
334 commit->object.flags |= mark;
335 insert_by_date(commit, list);
337 parents = parents->next;
343 * Generic support for pretty-printing the header
345 static int get_one_line(const char *msg, unsigned long len)
360 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
366 const char *filler = " ";
368 if (fmt == CMIT_FMT_ONELINE)
370 date = strchr(line, '>');
373 namelen = ++date - line;
374 time = strtoul(date, &date, 10);
375 tz = strtol(date, NULL, 10);
377 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
378 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
379 filler, namelen, line);
381 case CMIT_FMT_MEDIUM:
382 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
384 case CMIT_FMT_FULLER:
385 ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz));
394 static int is_empty_line(const char *line, int len)
396 while (len && isspace(line[len-1]))
401 static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents)
405 if (fmt == CMIT_FMT_ONELINE)
411 /* Go back to the previous line: 40 characters of previous parent, and one '\n' */
412 offset = sprintf(buf, "Merge: %.40s\n", line-41);
415 /* Replace the previous '\n' with a space */
417 offset += sprintf(buf + offset, "%.40s\n", line+7);
422 unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space)
424 int hdr = 1, body = 0;
425 unsigned long offset = 0;
427 int indent = (fmt == CMIT_FMT_ONELINE) ? 0 : 4;
430 const char *line = msg;
431 int linelen = get_one_line(msg, len);
437 * We want some slop for indentation and a possible
438 * final "...". Thus the "+ 20".
440 if (offset + linelen + 20 > space) {
441 memcpy(buf + offset, " ...\n", 8);
451 if (fmt != CMIT_FMT_ONELINE)
452 buf[offset++] = '\n';
455 if (fmt == CMIT_FMT_RAW) {
456 memcpy(buf + offset, line, linelen);
460 if (!memcmp(line, "parent ", 7)) {
462 die("bad parent line in commit");
463 offset += add_parent_info(fmt, buf + offset, line, ++parents);
467 * MEDIUM == DEFAULT shows only author with dates.
468 * FULL shows both authors but not dates.
469 * FULLER shows both authors and dates.
471 if (!memcmp(line, "author ", 7))
472 offset += add_user_info("Author", fmt,
475 if (!memcmp(line, "committer ", 10) &&
476 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
477 offset += add_user_info("Commit", fmt,
483 if (is_empty_line(line, linelen)) {
486 if (fmt == CMIT_FMT_SHORT)
492 memset(buf + offset, ' ', indent);
493 memcpy(buf + offset + indent, line, linelen);
494 offset += linelen + indent;
495 if (fmt == CMIT_FMT_ONELINE)
498 if (fmt == CMIT_FMT_ONELINE) {
499 /* We do not want the terminating newline */
500 if (buf[offset - 1] == '\n')
504 /* Make sure there is an EOLN */
505 if (buf[offset - 1] != '\n')
506 buf[offset++] = '\n';
512 struct commit *pop_commit(struct commit_list **stack)
514 struct commit_list *top = *stack;
515 struct commit *item = top ? top->item : NULL;
524 int count_parents(struct commit * commit)
527 struct commit_list * parents = commit->parents;
528 for (count=0;parents; parents=parents->next,count++)
534 * Performs an in-place topological sort on the list supplied.
536 void sort_in_topological_order(struct commit_list ** list)
538 struct commit_list * next = *list;
539 struct commit_list * work = NULL;
540 struct commit_list ** pptr = list;
541 struct sort_node * nodes;
542 struct sort_node * next_nodes;
545 /* determine the size of the list */
550 /* allocate an array to help sort the list */
551 nodes = xcalloc(count, sizeof(*nodes));
552 /* link the list to the array */
556 next_nodes->list_item = next;
557 next->item->object.util = next_nodes;
561 /* update the indegree */
564 struct commit_list * parents = next->item->parents;
566 struct commit * parent=parents->item;
567 struct sort_node * pn = (struct sort_node *)parent->object.util;
571 parents=parents->next;
578 * tips are nodes not reachable from any other node in the list
580 * the tips serve as a starting set for the work queue.
584 struct sort_node * node = (struct sort_node *)next->item->object.util;
586 if (node->indegree == 0) {
587 commit_list_insert(next->item, &work);
591 /* process the list in topological order */
593 struct commit * work_item = pop_commit(&work);
594 struct sort_node * work_node = (struct sort_node *)work_item->object.util;
595 struct commit_list * parents = work_item->parents;
598 struct commit * parent=parents->item;
599 struct sort_node * pn = (struct sort_node *)parent->object.util;
603 * parents are only enqueued for emission
604 * when all their children have been emitted thereby
605 * guaranteeing topological order.
609 commit_list_insert(parent, &work);
611 parents=parents->next;
614 * work_item is a commit all of whose children
615 * have already been emitted. we can emit it now.
617 *pptr = work_node->list_item;
618 pptr = &(*pptr)->next;
620 work_item->object.util = NULL;