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 = deref_tag(parse_object(sha1));
55 return check_commit(obj, sha1);
58 struct commit *lookup_commit(const unsigned char *sha1)
60 struct object *obj = lookup_object(sha1);
62 struct commit *ret = xmalloc(sizeof(struct commit));
63 memset(ret, 0, sizeof(struct commit));
64 created_object(sha1, &ret->object);
65 ret->object.type = commit_type;
69 obj->type = commit_type;
70 return check_commit(obj, sha1);
73 static unsigned long parse_commit_date(const char *buf)
77 if (memcmp(buf, "author", 6))
79 while (*buf++ != '\n')
81 if (memcmp(buf, "committer", 9))
85 date = strtoul(buf, NULL, 10);
86 if (date == ULONG_MAX)
91 static struct commit_graft {
92 unsigned char sha1[20];
94 unsigned char parent[0][20]; /* more */
96 static int commit_graft_alloc, commit_graft_nr;
98 static int commit_graft_pos(const unsigned char *sha1)
102 hi = commit_graft_nr;
104 int mi = (lo + hi) / 2;
105 struct commit_graft *graft = commit_graft[mi];
106 int cmp = memcmp(sha1, graft->sha1, 20);
117 static void prepare_commit_graft(void)
119 char *graft_file = get_graft_file();
120 FILE *fp = fopen(graft_file, "r");
123 commit_graft = (struct commit_graft **) "hack";
126 while (fgets(buf, sizeof(buf), fp)) {
127 /* The format is just "Commit Parent1 Parent2 ...\n" */
128 int len = strlen(buf);
130 struct commit_graft *graft = NULL;
132 if (buf[len-1] == '\n')
136 if ((len + 1) % 41) {
138 error("bad graft data: %s", buf);
142 i = (len + 1) / 41 - 1;
143 graft = xmalloc(sizeof(*graft) + 20 * i);
144 graft->nr_parent = i;
145 if (get_sha1_hex(buf, graft->sha1))
147 for (i = 40; i < len; i += 41) {
150 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
153 i = commit_graft_pos(graft->sha1);
155 error("duplicate graft data: %s", buf);
160 if (commit_graft_alloc <= ++commit_graft_nr) {
161 commit_graft_alloc = alloc_nr(commit_graft_alloc);
162 commit_graft = xrealloc(commit_graft,
163 sizeof(*commit_graft) *
166 if (i < commit_graft_nr)
167 memmove(commit_graft + i + 1,
169 (commit_graft_nr - i - 1) *
170 sizeof(*commit_graft));
171 commit_graft[i] = graft;
176 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
180 prepare_commit_graft();
181 pos = commit_graft_pos(sha1);
184 return commit_graft[pos];
187 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
189 char *bufptr = buffer;
190 unsigned char parent[20];
191 struct commit_list **pptr;
192 struct commit_graft *graft;
194 if (item->object.parsed)
196 item->object.parsed = 1;
197 if (memcmp(bufptr, "tree ", 5))
198 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
199 if (get_sha1_hex(bufptr + 5, parent) < 0)
200 return error("bad tree pointer in commit %s\n", sha1_to_hex(item->object.sha1));
201 item->tree = lookup_tree(parent);
203 add_ref(&item->object, &item->tree->object);
204 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
205 pptr = &item->parents;
207 graft = lookup_commit_graft(item->object.sha1);
208 while (!memcmp(bufptr, "parent ", 7)) {
209 struct commit *new_parent;
211 if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
212 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
216 new_parent = lookup_commit(parent);
218 pptr = &commit_list_insert(new_parent, pptr)->next;
219 add_ref(&item->object, &new_parent->object);
224 struct commit *new_parent;
225 for (i = 0; i < graft->nr_parent; i++) {
226 new_parent = lookup_commit(graft->parent[i]);
229 pptr = &commit_list_insert(new_parent, pptr)->next;
230 add_ref(&item->object, &new_parent->object);
233 item->date = parse_commit_date(bufptr);
237 int parse_commit(struct commit *item)
244 if (item->object.parsed)
246 buffer = read_sha1_file(item->object.sha1, type, &size);
248 return error("Could not read %s",
249 sha1_to_hex(item->object.sha1));
250 if (strcmp(type, commit_type)) {
252 return error("Object %s not a commit",
253 sha1_to_hex(item->object.sha1));
255 ret = parse_commit_buffer(item, buffer, size);
257 item->buffer = buffer;
264 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
266 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
267 new_list->item = item;
268 new_list->next = *list_p;
273 void free_commit_list(struct commit_list *list)
276 struct commit_list *temp = list;
282 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
284 struct commit_list **pp = list;
285 struct commit_list *p;
286 while ((p = *pp) != NULL) {
287 if (p->item->date < item->date) {
292 return commit_list_insert(item, pp);
296 void sort_by_date(struct commit_list **list)
298 struct commit_list *ret = NULL;
300 insert_by_date((*list)->item, &ret);
301 *list = (*list)->next;
306 struct commit *pop_most_recent_commit(struct commit_list **list,
309 struct commit *ret = (*list)->item;
310 struct commit_list *parents = ret->parents;
311 struct commit_list *old = *list;
313 *list = (*list)->next;
317 struct commit *commit = parents->item;
318 parse_commit(commit);
319 if (!(commit->object.flags & mark)) {
320 commit->object.flags |= mark;
321 insert_by_date(commit, list);
323 parents = parents->next;
329 * Generic support for pretty-printing the header
331 static int get_one_line(const char *msg, unsigned long len)
346 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
349 unsigned int namelen;
353 date = strchr(line, '>');
356 namelen = ++date - line;
357 time = strtoul(date, &date, 10);
358 tz = strtol(date, NULL, 10);
360 ret = sprintf(buf, "%s: %.*s\n", what, namelen, line);
361 if (fmt == CMIT_FMT_MEDIUM)
362 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
366 static int is_empty_line(const char *line, int len)
368 while (len && isspace(line[len-1]))
373 static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents)
380 /* Go back to the previous line: 40 characters of previous parent, and one '\n' */
381 offset = sprintf(buf, "Merge: %.40s\n", line-41);
384 /* Replace the previous '\n' with a space */
386 offset += sprintf(buf + offset, "%.40s\n", line+7);
391 unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space)
393 int hdr = 1, body = 0;
394 unsigned long offset = 0;
398 const char *line = msg;
399 int linelen = get_one_line(msg, len);
405 * We want some slop for indentation and a possible
406 * final "...". Thus the "+ 20".
408 if (offset + linelen + 20 > space) {
409 memcpy(buf + offset, " ...\n", 8);
419 buf[offset++] = '\n';
422 if (fmt == CMIT_FMT_RAW) {
423 memcpy(buf + offset, line, linelen);
427 if (!memcmp(line, "parent ", 7)) {
429 die("bad parent line in commit");
430 offset += add_parent_info(fmt, buf + offset, line, ++parents);
432 if (!memcmp(line, "author ", 7))
433 offset += add_user_info("Author", fmt, buf + offset, line + 7);
434 if (fmt == CMIT_FMT_FULL) {
435 if (!memcmp(line, "committer ", 10))
436 offset += add_user_info("Commit", fmt, buf + offset, line + 10);
441 if (is_empty_line(line, linelen)) {
444 if (fmt == CMIT_FMT_SHORT)
449 memset(buf + offset, ' ', 4);
450 memcpy(buf + offset + 4, line, linelen);
451 offset += linelen + 4;
453 /* Make sure there is an EOLN */
454 if (buf[offset - 1] != '\n')
455 buf[offset++] = '\n';
460 struct commit *pop_commit(struct commit_list **stack)
462 struct commit_list *top = *stack;
463 struct commit *item = top ? top->item : NULL;
472 int count_parents(struct commit * commit)
475 struct commit_list * parents = commit->parents;
476 for (count=0;parents; parents=parents->next,count++)
482 * Performs an in-place topological sort on the list supplied.
484 void sort_in_topological_order(struct commit_list ** list)
486 struct commit_list * next = *list;
487 struct commit_list * work = NULL;
488 struct commit_list ** pptr = list;
489 struct sort_node * nodes;
490 struct sort_node * next_nodes;
493 /* determine the size of the list */
498 /* allocate an array to help sort the list */
499 nodes = xcalloc(count, sizeof(*nodes));
500 /* link the list to the array */
504 next_nodes->list_item = next;
505 next->item->object.util = next_nodes;
509 /* update the indegree */
512 struct commit_list * parents = next->item->parents;
514 struct commit * parent=parents->item;
515 struct sort_node * pn = (struct sort_node *)parent->object.util;
519 parents=parents->next;
526 * tips are nodes not reachable from any other node in the list
528 * the tips serve as a starting set for the work queue.
532 struct sort_node * node = (struct sort_node *)next->item->object.util;
534 if (node->indegree == 0) {
535 commit_list_insert(next->item, &work);
539 /* process the list in topological order */
541 struct commit * work_item = pop_commit(&work);
542 struct sort_node * work_node = (struct sort_node *)work_item->object.util;
543 struct commit_list * parents = work_item->parents;
546 struct commit * parent=parents->item;
547 struct sort_node * pn = (struct sort_node *)parent->object.util;
551 * parents are only enqueued for emission
552 * when all their children have been emitted thereby
553 * guaranteeing topological order.
557 commit_list_insert(parent, &work);
559 parents=parents->next;
562 * work_item is a commit all of whose children
563 * have already been emitted. we can emit it now.
565 *pptr = work_node->list_item;
566 pptr = &(*pptr)->next;
568 work_item->object.util = NULL;