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 if (obj->type == tag_type)
56 obj = ((struct tag *)obj)->tagged;
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 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
95 void *bufptr = buffer;
96 unsigned char parent[20];
97 struct commit_list **pptr;
99 if (item->object.parsed)
101 item->object.parsed = 1;
102 get_sha1_hex(bufptr + 5, parent);
103 item->tree = lookup_tree(parent);
105 add_ref(&item->object, &item->tree->object);
106 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
107 pptr = &item->parents;
108 while (!memcmp(bufptr, "parent ", 7) &&
109 !get_sha1_hex(bufptr + 7, parent)) {
110 struct commit *new_parent = lookup_commit(parent);
112 pptr = &commit_list_insert(new_parent, pptr)->next;
113 add_ref(&item->object, &new_parent->object);
117 item->date = parse_commit_date(bufptr);
121 int parse_commit(struct commit *item)
128 if (item->object.parsed)
130 buffer = read_sha1_file(item->object.sha1, type, &size);
132 return error("Could not read %s",
133 sha1_to_hex(item->object.sha1));
134 if (strcmp(type, commit_type)) {
136 return error("Object %s not a commit",
137 sha1_to_hex(item->object.sha1));
139 ret = parse_commit_buffer(item, buffer, size);
141 item->buffer = buffer;
148 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
150 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
151 new_list->item = item;
152 new_list->next = *list_p;
157 void free_commit_list(struct commit_list *list)
160 struct commit_list *temp = list;
166 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
168 struct commit_list **pp = list;
169 struct commit_list *p;
170 while ((p = *pp) != NULL) {
171 if (p->item->date < item->date) {
176 return commit_list_insert(item, pp);
180 void sort_by_date(struct commit_list **list)
182 struct commit_list *ret = NULL;
184 insert_by_date((*list)->item, &ret);
185 *list = (*list)->next;
190 struct commit *pop_most_recent_commit(struct commit_list **list,
193 struct commit *ret = (*list)->item;
194 struct commit_list *parents = ret->parents;
195 struct commit_list *old = *list;
197 *list = (*list)->next;
201 struct commit *commit = parents->item;
202 parse_commit(commit);
203 if (!(commit->object.flags & mark)) {
204 commit->object.flags |= mark;
205 insert_by_date(commit, list);
207 parents = parents->next;
213 * Generic support for pretty-printing the header
215 static int get_one_line(const char *msg, unsigned long len)
230 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
233 unsigned int namelen;
237 date = strchr(line, '>');
240 namelen = ++date - line;
241 time = strtoul(date, &date, 10);
242 tz = strtol(date, NULL, 10);
244 ret = sprintf(buf, "%s: %.*s\n", what, namelen, line);
245 if (fmt == CMIT_FMT_MEDIUM)
246 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
250 static int is_empty_line(const char *line, int len)
252 while (len && isspace(line[len-1]))
257 static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents)
264 /* Go back to the previous line: 40 characters of previous parent, and one '\n' */
265 offset = sprintf(buf, "Merge: %.40s\n", line-41);
268 /* Replace the previous '\n' with a space */
270 offset += sprintf(buf + offset, "%.40s\n", line+7);
275 unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space)
277 int hdr = 1, body = 0;
278 unsigned long offset = 0;
282 const char *line = msg;
283 int linelen = get_one_line(msg, len);
289 * We want some slop for indentation and a possible
290 * final "...". Thus the "+ 20".
292 if (offset + linelen + 20 > space) {
293 memcpy(buf + offset, " ...\n", 8);
303 buf[offset++] = '\n';
306 if (fmt == CMIT_FMT_RAW) {
307 memcpy(buf + offset, line, linelen);
311 if (!memcmp(line, "parent ", 7)) {
313 die("bad parent line in commit");
314 offset += add_parent_info(fmt, buf + offset, line, ++parents);
316 if (!memcmp(line, "author ", 7))
317 offset += add_user_info("Author", fmt, buf + offset, line + 7);
318 if (fmt == CMIT_FMT_FULL) {
319 if (!memcmp(line, "committer ", 10))
320 offset += add_user_info("Commit", fmt, buf + offset, line + 10);
325 if (is_empty_line(line, linelen)) {
328 if (fmt == CMIT_FMT_SHORT)
333 memset(buf + offset, ' ', 4);
334 memcpy(buf + offset + 4, line, linelen);
335 offset += linelen + 4;
337 /* Make sure there is an EOLN */
338 if (buf[offset - 1] != '\n')
339 buf[offset++] = '\n';
344 struct commit *pop_commit(struct commit_list **stack)
346 struct commit_list *top = *stack;
347 struct commit *item = top ? top->item : NULL;
356 int count_parents(struct commit * commit)
359 struct commit_list * parents = commit->parents;
360 for (count=0;parents; parents=parents->next,count++)
366 * Performs an in-place topological sort on the list supplied.
368 void sort_in_topological_order(struct commit_list ** list)
370 struct commit_list * next = *list;
371 struct commit_list * work = NULL;
372 struct commit_list ** pptr = list;
373 struct sort_node * nodes;
374 struct sort_node * next_nodes;
377 /* determine the size of the list */
382 /* allocate an array to help sort the list */
383 nodes = xcalloc(count, sizeof(*nodes));
384 /* link the list to the array */
388 next_nodes->list_item = next;
389 next->item->object.util = next_nodes;
393 /* update the indegree */
396 struct commit_list * parents = next->item->parents;
398 struct commit * parent=parents->item;
399 struct sort_node * pn = (struct sort_node *)parent->object.util;
403 parents=parents->next;
410 * tips are nodes not reachable from any other node in the list
412 * the tips serve as a starting set for the work queue.
416 struct sort_node * node = (struct sort_node *)next->item->object.util;
418 if (node->indegree == 0) {
419 commit_list_insert(next->item, &work);
423 /* process the list in topological order */
425 struct commit * work_item = pop_commit(&work);
426 struct sort_node * work_node = (struct sort_node *)work_item->object.util;
427 struct commit_list * parents = work_item->parents;
430 struct commit * parent=parents->item;
431 struct sort_node * pn = (struct sort_node *)parent->object.util;
435 * parents are only enqueued for emission
436 * when all their children have been emitted thereby
437 * guaranteeing topological order.
441 commit_list_insert(parent, &work);
443 parents=parents->next;
446 * work_item is a commit all of whose children
447 * have already been emitted. we can emit it now.
449 *pptr = work_node->list_item;
450 pptr = &(*pptr)->next;
452 work_item->object.util = NULL;