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 die("invalid --pretty format");
39 static struct commit *check_commit(struct object *obj, const unsigned char *sha1)
41 if (obj->type != commit_type) {
42 error("Object %s is a %s, not a commit",
43 sha1_to_hex(sha1), obj->type);
46 return (struct commit *) obj;
49 struct commit *lookup_commit_reference(const unsigned char *sha1)
51 struct object *obj = parse_object(sha1);
55 while (obj->type == tag_type)
56 obj = parse_object(((struct tag *)obj)->tagged->sha1);
58 return check_commit(obj, sha1);
61 struct commit *lookup_commit(const unsigned char *sha1)
63 struct object *obj = lookup_object(sha1);
65 struct commit *ret = xmalloc(sizeof(struct commit));
66 memset(ret, 0, sizeof(struct commit));
67 created_object(sha1, &ret->object);
68 ret->object.type = commit_type;
72 obj->type = commit_type;
73 return check_commit(obj, sha1);
76 static unsigned long parse_commit_date(const char *buf)
80 if (memcmp(buf, "author", 6))
82 while (*buf++ != '\n')
84 if (memcmp(buf, "committer", 9))
88 date = strtoul(buf, NULL, 10);
89 if (date == ULONG_MAX)
94 static struct commit_graft {
95 unsigned char sha1[20];
97 unsigned char parent[0][20]; /* more */
99 static int commit_graft_alloc, commit_graft_nr;
101 static int commit_graft_pos(const unsigned char *sha1)
105 hi = commit_graft_nr;
107 int mi = (lo + hi) / 2;
108 struct commit_graft *graft = commit_graft[mi];
109 int cmp = memcmp(sha1, graft->sha1, 20);
120 static void prepare_commit_graft(void)
122 char *graft_file = get_graft_file();
123 FILE *fp = fopen(graft_file, "r");
126 commit_graft = (struct commit_graft **) "hack";
129 while (fgets(buf, sizeof(buf), fp)) {
130 /* The format is just "Commit Parent1 Parent2 ...\n" */
131 int len = strlen(buf);
133 struct commit_graft *graft = NULL;
135 if (buf[len-1] == '\n')
139 if ((len + 1) % 41) {
141 error("bad graft data: %s", buf);
145 i = (len + 1) / 41 - 1;
146 graft = xmalloc(sizeof(*graft) + 20 * i);
147 graft->nr_parent = i;
148 if (get_sha1_hex(buf, graft->sha1))
150 for (i = 40; i < len; i += 41) {
153 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
156 i = commit_graft_pos(graft->sha1);
158 error("duplicate graft data: %s", buf);
163 if (commit_graft_alloc <= ++commit_graft_nr) {
164 commit_graft_alloc = alloc_nr(commit_graft_alloc);
165 commit_graft = xrealloc(commit_graft,
166 sizeof(*commit_graft) *
169 if (i < commit_graft_nr)
170 memmove(commit_graft + i + 1,
172 (commit_graft_nr - i - 1) *
173 sizeof(*commit_graft));
174 commit_graft[i] = graft;
179 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
183 prepare_commit_graft();
184 pos = commit_graft_pos(sha1);
187 return commit_graft[pos];
190 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
192 char *bufptr = buffer;
193 unsigned char parent[20];
194 struct commit_list **pptr;
195 struct commit_graft *graft;
197 if (item->object.parsed)
199 item->object.parsed = 1;
200 if (memcmp(bufptr, "tree ", 5))
201 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
202 if (get_sha1_hex(bufptr + 5, parent) < 0)
203 return error("bad tree pointer in commit %s\n", sha1_to_hex(item->object.sha1));
204 item->tree = lookup_tree(parent);
206 add_ref(&item->object, &item->tree->object);
207 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
208 pptr = &item->parents;
210 graft = lookup_commit_graft(item->object.sha1);
211 while (!memcmp(bufptr, "parent ", 7)) {
212 struct commit *new_parent;
214 if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
215 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
219 new_parent = lookup_commit(parent);
221 pptr = &commit_list_insert(new_parent, pptr)->next;
222 add_ref(&item->object, &new_parent->object);
227 struct commit *new_parent;
228 for (i = 0; i < graft->nr_parent; i++) {
229 new_parent = lookup_commit(graft->parent[i]);
232 pptr = &commit_list_insert(new_parent, pptr)->next;
233 add_ref(&item->object, &new_parent->object);
236 item->date = parse_commit_date(bufptr);
240 int parse_commit(struct commit *item)
247 if (item->object.parsed)
249 buffer = read_sha1_file(item->object.sha1, type, &size);
251 return error("Could not read %s",
252 sha1_to_hex(item->object.sha1));
253 if (strcmp(type, commit_type)) {
255 return error("Object %s not a commit",
256 sha1_to_hex(item->object.sha1));
258 ret = parse_commit_buffer(item, buffer, size);
260 item->buffer = buffer;
267 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
269 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
270 new_list->item = item;
271 new_list->next = *list_p;
276 void free_commit_list(struct commit_list *list)
279 struct commit_list *temp = list;
285 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
287 struct commit_list **pp = list;
288 struct commit_list *p;
289 while ((p = *pp) != NULL) {
290 if (p->item->date < item->date) {
295 return commit_list_insert(item, pp);
299 void sort_by_date(struct commit_list **list)
301 struct commit_list *ret = NULL;
303 insert_by_date((*list)->item, &ret);
304 *list = (*list)->next;
309 struct commit *pop_most_recent_commit(struct commit_list **list,
312 struct commit *ret = (*list)->item;
313 struct commit_list *parents = ret->parents;
314 struct commit_list *old = *list;
316 *list = (*list)->next;
320 struct commit *commit = parents->item;
321 parse_commit(commit);
322 if (!(commit->object.flags & mark)) {
323 commit->object.flags |= mark;
324 insert_by_date(commit, list);
326 parents = parents->next;
332 * Generic support for pretty-printing the header
334 static int get_one_line(const char *msg, unsigned long len)
349 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
352 unsigned int namelen;
356 date = strchr(line, '>');
359 namelen = ++date - line;
360 time = strtoul(date, &date, 10);
361 tz = strtol(date, NULL, 10);
363 ret = sprintf(buf, "%s: %.*s\n", what, namelen, line);
364 if (fmt == CMIT_FMT_MEDIUM)
365 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
369 static int is_empty_line(const char *line, int len)
371 while (len && isspace(line[len-1]))
376 static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents)
383 /* Go back to the previous line: 40 characters of previous parent, and one '\n' */
384 offset = sprintf(buf, "Merge: %.40s\n", line-41);
387 /* Replace the previous '\n' with a space */
389 offset += sprintf(buf + offset, "%.40s\n", line+7);
394 unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space)
396 int hdr = 1, body = 0;
397 unsigned long offset = 0;
401 const char *line = msg;
402 int linelen = get_one_line(msg, len);
408 * We want some slop for indentation and a possible
409 * final "...". Thus the "+ 20".
411 if (offset + linelen + 20 > space) {
412 memcpy(buf + offset, " ...\n", 8);
422 buf[offset++] = '\n';
425 if (fmt == CMIT_FMT_RAW) {
426 memcpy(buf + offset, line, linelen);
430 if (!memcmp(line, "parent ", 7)) {
432 die("bad parent line in commit");
433 offset += add_parent_info(fmt, buf + offset, line, ++parents);
435 if (!memcmp(line, "author ", 7))
436 offset += add_user_info("Author", fmt, buf + offset, line + 7);
437 if (fmt == CMIT_FMT_FULL) {
438 if (!memcmp(line, "committer ", 10))
439 offset += add_user_info("Commit", fmt, buf + offset, line + 10);
444 if (is_empty_line(line, linelen)) {
447 if (fmt == CMIT_FMT_SHORT)
452 memset(buf + offset, ' ', 4);
453 memcpy(buf + offset + 4, line, linelen);
454 offset += linelen + 4;
456 /* Make sure there is an EOLN */
457 if (buf[offset - 1] != '\n')
458 buf[offset++] = '\n';
463 struct commit *pop_commit(struct commit_list **stack)
465 struct commit_list *top = *stack;
466 struct commit *item = top ? top->item : NULL;
475 int count_parents(struct commit * commit)
478 struct commit_list * parents = commit->parents;
479 for (count=0;parents; parents=parents->next,count++)
485 * Performs an in-place topological sort on the list supplied.
487 void sort_in_topological_order(struct commit_list ** list)
489 struct commit_list * next = *list;
490 struct commit_list * work = NULL;
491 struct commit_list ** pptr = list;
492 struct sort_node * nodes;
493 struct sort_node * next_nodes;
496 /* determine the size of the list */
501 /* allocate an array to help sort the list */
502 nodes = xcalloc(count, sizeof(*nodes));
503 /* link the list to the array */
507 next_nodes->list_item = next;
508 next->item->object.util = next_nodes;
512 /* update the indegree */
515 struct commit_list * parents = next->item->parents;
517 struct commit * parent=parents->item;
518 struct sort_node * pn = (struct sort_node *)parent->object.util;
522 parents=parents->next;
529 * tips are nodes not reachable from any other node in the list
531 * the tips serve as a starting set for the work queue.
535 struct sort_node * node = (struct sort_node *)next->item->object.util;
537 if (node->indegree == 0) {
538 commit_list_insert(next->item, &work);
542 /* process the list in topological order */
544 struct commit * work_item = pop_commit(&work);
545 struct sort_node * work_node = (struct sort_node *)work_item->object.util;
546 struct commit_list * parents = work_item->parents;
549 struct commit * parent=parents->item;
550 struct sort_node * pn = (struct sort_node *)parent->object.util;
554 * parents are only enqueued for emission
555 * when all their children have been emitted thereby
556 * guaranteeing topological order.
560 commit_list_insert(parent, &work);
562 parents=parents->next;
565 * work_item is a commit all of whose children
566 * have already been emitted. we can emit it now.
568 *pptr = work_node->list_item;
569 pptr = &(*pptr)->next;
571 work_item->object.util = NULL;