9 * the number of children of the associated commit
10 * that also occur in the list being sorted.
12 unsigned int indegree;
15 * reference to original list item that we will re-use
18 struct commit_list * list_item;
22 const char *commit_type = "commit";
24 enum cmit_fmt get_commit_format(const char *arg)
27 return CMIT_FMT_DEFAULT;
28 if (!strcmp(arg, "=raw"))
30 if (!strcmp(arg, "=medium"))
31 return CMIT_FMT_MEDIUM;
32 if (!strcmp(arg, "=short"))
33 return CMIT_FMT_SHORT;
34 if (!strcmp(arg, "=full"))
36 if (!strcmp(arg, "=oneline"))
37 return CMIT_FMT_ONELINE;
38 die("invalid --pretty format");
41 static struct commit *check_commit(struct object *obj,
42 const unsigned char *sha1,
45 if (obj->type != commit_type) {
47 error("Object %s is a %s, not a commit",
48 sha1_to_hex(sha1), obj->type);
51 return (struct commit *) obj;
54 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
57 struct object *obj = deref_tag(parse_object(sha1));
61 return check_commit(obj, sha1, quiet);
64 struct commit *lookup_commit_reference(const unsigned char *sha1)
66 return lookup_commit_reference_gently(sha1, 0);
69 struct commit *lookup_commit(const unsigned char *sha1)
71 struct object *obj = lookup_object(sha1);
73 struct commit *ret = xmalloc(sizeof(struct commit));
74 memset(ret, 0, sizeof(struct commit));
75 created_object(sha1, &ret->object);
76 ret->object.type = commit_type;
80 obj->type = commit_type;
81 return check_commit(obj, sha1, 0);
84 static unsigned long parse_commit_date(const char *buf)
88 if (memcmp(buf, "author", 6))
90 while (*buf++ != '\n')
92 if (memcmp(buf, "committer", 9))
96 date = strtoul(buf, NULL, 10);
97 if (date == ULONG_MAX)
102 static struct commit_graft {
103 unsigned char sha1[20];
105 unsigned char parent[][20]; /* more */
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 static void prepare_commit_graft(void)
130 char *graft_file = get_graft_file();
131 FILE *fp = fopen(graft_file, "r");
134 commit_graft = (struct commit_graft **) "hack";
137 while (fgets(buf, sizeof(buf), fp)) {
138 /* The format is just "Commit Parent1 Parent2 ...\n" */
139 int len = strlen(buf);
141 struct commit_graft *graft = NULL;
143 if (buf[len-1] == '\n')
147 if ((len + 1) % 41) {
149 error("bad graft data: %s", buf);
153 i = (len + 1) / 41 - 1;
154 graft = xmalloc(sizeof(*graft) + 20 * i);
155 graft->nr_parent = i;
156 if (get_sha1_hex(buf, graft->sha1))
158 for (i = 40; i < len; i += 41) {
161 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
164 i = commit_graft_pos(graft->sha1);
166 error("duplicate graft data: %s", buf);
171 if (commit_graft_alloc <= ++commit_graft_nr) {
172 commit_graft_alloc = alloc_nr(commit_graft_alloc);
173 commit_graft = xrealloc(commit_graft,
174 sizeof(*commit_graft) *
177 if (i < commit_graft_nr)
178 memmove(commit_graft + i + 1,
180 (commit_graft_nr - i - 1) *
181 sizeof(*commit_graft));
182 commit_graft[i] = graft;
187 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
191 prepare_commit_graft();
192 pos = commit_graft_pos(sha1);
195 return commit_graft[pos];
198 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
200 char *bufptr = buffer;
201 unsigned char parent[20];
202 struct commit_list **pptr;
203 struct commit_graft *graft;
205 if (item->object.parsed)
207 item->object.parsed = 1;
208 if (memcmp(bufptr, "tree ", 5))
209 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
210 if (get_sha1_hex(bufptr + 5, parent) < 0)
211 return error("bad tree pointer in commit %s\n", sha1_to_hex(item->object.sha1));
212 item->tree = lookup_tree(parent);
214 add_ref(&item->object, &item->tree->object);
215 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
216 pptr = &item->parents;
218 graft = lookup_commit_graft(item->object.sha1);
219 while (!memcmp(bufptr, "parent ", 7)) {
220 struct commit *new_parent;
222 if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
223 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
227 new_parent = lookup_commit(parent);
229 pptr = &commit_list_insert(new_parent, pptr)->next;
230 add_ref(&item->object, &new_parent->object);
235 struct commit *new_parent;
236 for (i = 0; i < graft->nr_parent; i++) {
237 new_parent = lookup_commit(graft->parent[i]);
240 pptr = &commit_list_insert(new_parent, pptr)->next;
241 add_ref(&item->object, &new_parent->object);
244 item->date = parse_commit_date(bufptr);
248 int parse_commit(struct commit *item)
255 if (item->object.parsed)
257 buffer = read_sha1_file(item->object.sha1, type, &size);
259 return error("Could not read %s",
260 sha1_to_hex(item->object.sha1));
261 if (strcmp(type, commit_type)) {
263 return error("Object %s not a commit",
264 sha1_to_hex(item->object.sha1));
266 ret = parse_commit_buffer(item, buffer, size);
268 item->buffer = buffer;
275 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
277 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
278 new_list->item = item;
279 new_list->next = *list_p;
284 void free_commit_list(struct commit_list *list)
287 struct commit_list *temp = list;
293 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
295 struct commit_list **pp = list;
296 struct commit_list *p;
297 while ((p = *pp) != NULL) {
298 if (p->item->date < item->date) {
303 return commit_list_insert(item, pp);
307 void sort_by_date(struct commit_list **list)
309 struct commit_list *ret = NULL;
311 insert_by_date((*list)->item, &ret);
312 *list = (*list)->next;
317 struct commit *pop_most_recent_commit(struct commit_list **list,
320 struct commit *ret = (*list)->item;
321 struct commit_list *parents = ret->parents;
322 struct commit_list *old = *list;
324 *list = (*list)->next;
328 struct commit *commit = parents->item;
329 parse_commit(commit);
330 if (!(commit->object.flags & mark)) {
331 commit->object.flags |= mark;
332 insert_by_date(commit, list);
334 parents = parents->next;
340 * Generic support for pretty-printing the header
342 static int get_one_line(const char *msg, unsigned long len)
357 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
360 unsigned int namelen;
364 if (fmt == CMIT_FMT_ONELINE)
366 date = strchr(line, '>');
369 namelen = ++date - line;
370 time = strtoul(date, &date, 10);
371 tz = strtol(date, NULL, 10);
373 ret = sprintf(buf, "%s: %.*s\n", what, namelen, line);
374 if (fmt == CMIT_FMT_MEDIUM)
375 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
379 static int is_empty_line(const char *line, int len)
381 while (len && isspace(line[len-1]))
386 static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents)
390 if (fmt == CMIT_FMT_ONELINE)
396 /* Go back to the previous line: 40 characters of previous parent, and one '\n' */
397 offset = sprintf(buf, "Merge: %.40s\n", line-41);
400 /* Replace the previous '\n' with a space */
402 offset += sprintf(buf + offset, "%.40s\n", line+7);
407 unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space)
409 int hdr = 1, body = 0;
410 unsigned long offset = 0;
412 int indent = (fmt == CMIT_FMT_ONELINE) ? 0 : 4;
415 const char *line = msg;
416 int linelen = get_one_line(msg, len);
422 * We want some slop for indentation and a possible
423 * final "...". Thus the "+ 20".
425 if (offset + linelen + 20 > space) {
426 memcpy(buf + offset, " ...\n", 8);
436 if (fmt != CMIT_FMT_ONELINE)
437 buf[offset++] = '\n';
440 if (fmt == CMIT_FMT_RAW) {
441 memcpy(buf + offset, line, linelen);
445 if (!memcmp(line, "parent ", 7)) {
447 die("bad parent line in commit");
448 offset += add_parent_info(fmt, buf + offset, line, ++parents);
450 if (!memcmp(line, "author ", 7))
451 offset += add_user_info("Author", fmt, buf + offset, line + 7);
452 if (fmt == CMIT_FMT_FULL) {
453 if (!memcmp(line, "committer ", 10))
454 offset += add_user_info("Commit", fmt, buf + offset, line + 10);
459 if (is_empty_line(line, linelen)) {
462 if (fmt == CMIT_FMT_SHORT)
468 memset(buf + offset, ' ', indent);
469 memcpy(buf + offset + indent, line, linelen);
470 offset += linelen + indent;
471 if (fmt == CMIT_FMT_ONELINE)
474 if (fmt == CMIT_FMT_ONELINE) {
475 /* We do not want the terminating newline */
476 if (buf[offset - 1] == '\n')
480 /* Make sure there is an EOLN */
481 if (buf[offset - 1] != '\n')
482 buf[offset++] = '\n';
488 struct commit *pop_commit(struct commit_list **stack)
490 struct commit_list *top = *stack;
491 struct commit *item = top ? top->item : NULL;
500 int count_parents(struct commit * commit)
503 struct commit_list * parents = commit->parents;
504 for (count=0;parents; parents=parents->next,count++)
510 * Performs an in-place topological sort on the list supplied.
512 void sort_in_topological_order(struct commit_list ** list)
514 struct commit_list * next = *list;
515 struct commit_list * work = NULL;
516 struct commit_list ** pptr = list;
517 struct sort_node * nodes;
518 struct sort_node * next_nodes;
521 /* determine the size of the list */
526 /* allocate an array to help sort the list */
527 nodes = xcalloc(count, sizeof(*nodes));
528 /* link the list to the array */
532 next_nodes->list_item = next;
533 next->item->object.util = next_nodes;
537 /* update the indegree */
540 struct commit_list * parents = next->item->parents;
542 struct commit * parent=parents->item;
543 struct sort_node * pn = (struct sort_node *)parent->object.util;
547 parents=parents->next;
554 * tips are nodes not reachable from any other node in the list
556 * the tips serve as a starting set for the work queue.
560 struct sort_node * node = (struct sort_node *)next->item->object.util;
562 if (node->indegree == 0) {
563 commit_list_insert(next->item, &work);
567 /* process the list in topological order */
569 struct commit * work_item = pop_commit(&work);
570 struct sort_node * work_node = (struct sort_node *)work_item->object.util;
571 struct commit_list * parents = work_item->parents;
574 struct commit * parent=parents->item;
575 struct sort_node * pn = (struct sort_node *)parent->object.util;
579 * parents are only enqueued for emission
580 * when all their children have been emitted thereby
581 * guaranteeing topological order.
585 commit_list_insert(parent, &work);
587 parents=parents->next;
590 * work_item is a commit all of whose children
591 * have already been emitted. we can emit it now.
593 *pptr = work_node->list_item;
594 pptr = &(*pptr)->next;
596 work_item->object.util = NULL;