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, const unsigned char *sha1)
43 if (obj->type != commit_type) {
44 error("Object %s is a %s, not a commit",
45 sha1_to_hex(sha1), obj->type);
48 return (struct commit *) obj;
51 struct commit *lookup_commit_reference(const unsigned char *sha1)
53 struct object *obj = deref_tag(parse_object(sha1));
57 return check_commit(obj, sha1);
60 struct commit *lookup_commit(const unsigned char *sha1)
62 struct object *obj = lookup_object(sha1);
64 struct commit *ret = xmalloc(sizeof(struct commit));
65 memset(ret, 0, sizeof(struct commit));
66 created_object(sha1, &ret->object);
67 ret->object.type = commit_type;
71 obj->type = commit_type;
72 return check_commit(obj, sha1);
75 static unsigned long parse_commit_date(const char *buf)
79 if (memcmp(buf, "author", 6))
81 while (*buf++ != '\n')
83 if (memcmp(buf, "committer", 9))
87 date = strtoul(buf, NULL, 10);
88 if (date == ULONG_MAX)
93 static struct commit_graft {
94 unsigned char sha1[20];
96 unsigned char parent[0][20]; /* more */
98 static int commit_graft_alloc, commit_graft_nr;
100 static int commit_graft_pos(const unsigned char *sha1)
104 hi = commit_graft_nr;
106 int mi = (lo + hi) / 2;
107 struct commit_graft *graft = commit_graft[mi];
108 int cmp = memcmp(sha1, graft->sha1, 20);
119 static void prepare_commit_graft(void)
121 char *graft_file = get_graft_file();
122 FILE *fp = fopen(graft_file, "r");
125 commit_graft = (struct commit_graft **) "hack";
128 while (fgets(buf, sizeof(buf), fp)) {
129 /* The format is just "Commit Parent1 Parent2 ...\n" */
130 int len = strlen(buf);
132 struct commit_graft *graft = NULL;
134 if (buf[len-1] == '\n')
138 if ((len + 1) % 41) {
140 error("bad graft data: %s", buf);
144 i = (len + 1) / 41 - 1;
145 graft = xmalloc(sizeof(*graft) + 20 * i);
146 graft->nr_parent = i;
147 if (get_sha1_hex(buf, graft->sha1))
149 for (i = 40; i < len; i += 41) {
152 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
155 i = commit_graft_pos(graft->sha1);
157 error("duplicate graft data: %s", buf);
162 if (commit_graft_alloc <= ++commit_graft_nr) {
163 commit_graft_alloc = alloc_nr(commit_graft_alloc);
164 commit_graft = xrealloc(commit_graft,
165 sizeof(*commit_graft) *
168 if (i < commit_graft_nr)
169 memmove(commit_graft + i + 1,
171 (commit_graft_nr - i - 1) *
172 sizeof(*commit_graft));
173 commit_graft[i] = graft;
178 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
182 prepare_commit_graft();
183 pos = commit_graft_pos(sha1);
186 return commit_graft[pos];
189 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
191 char *bufptr = buffer;
192 unsigned char parent[20];
193 struct commit_list **pptr;
194 struct commit_graft *graft;
196 if (item->object.parsed)
198 item->object.parsed = 1;
199 if (memcmp(bufptr, "tree ", 5))
200 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
201 if (get_sha1_hex(bufptr + 5, parent) < 0)
202 return error("bad tree pointer in commit %s\n", sha1_to_hex(item->object.sha1));
203 item->tree = lookup_tree(parent);
205 add_ref(&item->object, &item->tree->object);
206 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
207 pptr = &item->parents;
209 graft = lookup_commit_graft(item->object.sha1);
210 while (!memcmp(bufptr, "parent ", 7)) {
211 struct commit *new_parent;
213 if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
214 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
218 new_parent = lookup_commit(parent);
220 pptr = &commit_list_insert(new_parent, pptr)->next;
221 add_ref(&item->object, &new_parent->object);
226 struct commit *new_parent;
227 for (i = 0; i < graft->nr_parent; i++) {
228 new_parent = lookup_commit(graft->parent[i]);
231 pptr = &commit_list_insert(new_parent, pptr)->next;
232 add_ref(&item->object, &new_parent->object);
235 item->date = parse_commit_date(bufptr);
239 int parse_commit(struct commit *item)
246 if (item->object.parsed)
248 buffer = read_sha1_file(item->object.sha1, type, &size);
250 return error("Could not read %s",
251 sha1_to_hex(item->object.sha1));
252 if (strcmp(type, commit_type)) {
254 return error("Object %s not a commit",
255 sha1_to_hex(item->object.sha1));
257 ret = parse_commit_buffer(item, buffer, size);
259 item->buffer = buffer;
266 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
268 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
269 new_list->item = item;
270 new_list->next = *list_p;
275 void free_commit_list(struct commit_list *list)
278 struct commit_list *temp = list;
284 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
286 struct commit_list **pp = list;
287 struct commit_list *p;
288 while ((p = *pp) != NULL) {
289 if (p->item->date < item->date) {
294 return commit_list_insert(item, pp);
298 void sort_by_date(struct commit_list **list)
300 struct commit_list *ret = NULL;
302 insert_by_date((*list)->item, &ret);
303 *list = (*list)->next;
308 struct commit *pop_most_recent_commit(struct commit_list **list,
311 struct commit *ret = (*list)->item;
312 struct commit_list *parents = ret->parents;
313 struct commit_list *old = *list;
315 *list = (*list)->next;
319 struct commit *commit = parents->item;
320 parse_commit(commit);
321 if (!(commit->object.flags & mark)) {
322 commit->object.flags |= mark;
323 insert_by_date(commit, list);
325 parents = parents->next;
331 * Generic support for pretty-printing the header
333 static int get_one_line(const char *msg, unsigned long len)
348 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
351 unsigned int namelen;
355 if (fmt == CMIT_FMT_ONELINE)
357 date = strchr(line, '>');
360 namelen = ++date - line;
361 time = strtoul(date, &date, 10);
362 tz = strtol(date, NULL, 10);
364 ret = sprintf(buf, "%s: %.*s\n", what, namelen, line);
365 if (fmt == CMIT_FMT_MEDIUM)
366 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
370 static int is_empty_line(const char *line, int len)
372 while (len && isspace(line[len-1]))
377 static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents)
381 if (fmt == CMIT_FMT_ONELINE)
387 /* Go back to the previous line: 40 characters of previous parent, and one '\n' */
388 offset = sprintf(buf, "Merge: %.40s\n", line-41);
391 /* Replace the previous '\n' with a space */
393 offset += sprintf(buf + offset, "%.40s\n", line+7);
398 unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space)
400 int hdr = 1, body = 0;
401 unsigned long offset = 0;
403 int indent = (fmt == CMIT_FMT_ONELINE) ? 0 : 4;
406 const char *line = msg;
407 int linelen = get_one_line(msg, len);
413 * We want some slop for indentation and a possible
414 * final "...". Thus the "+ 20".
416 if (offset + linelen + 20 > space) {
417 memcpy(buf + offset, " ...\n", 8);
427 if (fmt != CMIT_FMT_ONELINE)
428 buf[offset++] = '\n';
431 if (fmt == CMIT_FMT_RAW) {
432 memcpy(buf + offset, line, linelen);
436 if (!memcmp(line, "parent ", 7)) {
438 die("bad parent line in commit");
439 offset += add_parent_info(fmt, buf + offset, line, ++parents);
441 if (!memcmp(line, "author ", 7))
442 offset += add_user_info("Author", fmt, buf + offset, line + 7);
443 if (fmt == CMIT_FMT_FULL) {
444 if (!memcmp(line, "committer ", 10))
445 offset += add_user_info("Commit", fmt, buf + offset, line + 10);
450 if (is_empty_line(line, linelen)) {
453 if (fmt == CMIT_FMT_SHORT)
459 memset(buf + offset, ' ', indent);
460 memcpy(buf + offset + indent, line, linelen);
461 offset += linelen + indent;
462 if (fmt == CMIT_FMT_ONELINE)
465 if (fmt == CMIT_FMT_ONELINE) {
466 /* We do not want the terminating newline */
467 if (buf[offset - 1] == '\n')
471 /* Make sure there is an EOLN */
472 if (buf[offset - 1] != '\n')
473 buf[offset++] = '\n';
479 struct commit *pop_commit(struct commit_list **stack)
481 struct commit_list *top = *stack;
482 struct commit *item = top ? top->item : NULL;
491 int count_parents(struct commit * commit)
494 struct commit_list * parents = commit->parents;
495 for (count=0;parents; parents=parents->next,count++)
501 * Performs an in-place topological sort on the list supplied.
503 void sort_in_topological_order(struct commit_list ** list)
505 struct commit_list * next = *list;
506 struct commit_list * work = NULL;
507 struct commit_list ** pptr = list;
508 struct sort_node * nodes;
509 struct sort_node * next_nodes;
512 /* determine the size of the list */
517 /* allocate an array to help sort the list */
518 nodes = xcalloc(count, sizeof(*nodes));
519 /* link the list to the array */
523 next_nodes->list_item = next;
524 next->item->object.util = next_nodes;
528 /* update the indegree */
531 struct commit_list * parents = next->item->parents;
533 struct commit * parent=parents->item;
534 struct sort_node * pn = (struct sort_node *)parent->object.util;
538 parents=parents->next;
545 * tips are nodes not reachable from any other node in the list
547 * the tips serve as a starting set for the work queue.
551 struct sort_node * node = (struct sort_node *)next->item->object.util;
553 if (node->indegree == 0) {
554 commit_list_insert(next->item, &work);
558 /* process the list in topological order */
560 struct commit * work_item = pop_commit(&work);
561 struct sort_node * work_node = (struct sort_node *)work_item->object.util;
562 struct commit_list * parents = work_item->parents;
565 struct commit * parent=parents->item;
566 struct sort_node * pn = (struct sort_node *)parent->object.util;
570 * parents are only enqueued for emission
571 * when all their children have been emitted thereby
572 * guaranteeing topological order.
576 commit_list_insert(parent, &work);
578 parents=parents->next;
581 * work_item is a commit all of whose children
582 * have already been emitted. we can emit it now.
584 *pptr = work_node->list_item;
585 pptr = &(*pptr)->next;
587 work_item->object.util = NULL;