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 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
96 void *bufptr = buffer;
97 unsigned char parent[20];
98 struct commit_list **pptr;
100 if (item->object.parsed)
102 item->object.parsed = 1;
103 get_sha1_hex(bufptr + 5, parent);
104 item->tree = lookup_tree(parent);
106 add_ref(&item->object, &item->tree->object);
107 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
108 pptr = &item->parents;
109 while (!memcmp(bufptr, "parent ", 7) &&
110 !get_sha1_hex(bufptr + 7, parent)) {
111 struct commit *new_parent = lookup_commit(parent);
113 pptr = &commit_list_insert(new_parent, pptr)->next;
114 add_ref(&item->object, &new_parent->object);
118 item->date = parse_commit_date(bufptr);
122 int parse_commit(struct commit *item)
129 if (item->object.parsed)
131 buffer = read_sha1_file(item->object.sha1, type, &size);
133 return error("Could not read %s",
134 sha1_to_hex(item->object.sha1));
135 if (strcmp(type, commit_type)) {
137 return error("Object %s not a commit",
138 sha1_to_hex(item->object.sha1));
140 ret = parse_commit_buffer(item, buffer, size);
142 item->buffer = buffer;
149 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
151 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
152 new_list->item = item;
153 new_list->next = *list_p;
158 void free_commit_list(struct commit_list *list)
161 struct commit_list *temp = list;
167 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
169 struct commit_list **pp = list;
170 struct commit_list *p;
171 while ((p = *pp) != NULL) {
172 if (p->item->date < item->date) {
177 return commit_list_insert(item, pp);
181 void sort_by_date(struct commit_list **list)
183 struct commit_list *ret = NULL;
185 insert_by_date((*list)->item, &ret);
186 *list = (*list)->next;
191 struct commit *pop_most_recent_commit(struct commit_list **list,
194 struct commit *ret = (*list)->item;
195 struct commit_list *parents = ret->parents;
196 struct commit_list *old = *list;
198 *list = (*list)->next;
202 struct commit *commit = parents->item;
203 parse_commit(commit);
204 if (!(commit->object.flags & mark)) {
205 commit->object.flags |= mark;
206 insert_by_date(commit, list);
208 parents = parents->next;
214 * Generic support for pretty-printing the header
216 static int get_one_line(const char *msg, unsigned long len)
231 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
234 unsigned int namelen;
238 date = strchr(line, '>');
241 namelen = ++date - line;
242 time = strtoul(date, &date, 10);
243 tz = strtol(date, NULL, 10);
245 ret = sprintf(buf, "%s: %.*s\n", what, namelen, line);
246 if (fmt == CMIT_FMT_MEDIUM)
247 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
251 static int is_empty_line(const char *line, int len)
253 while (len && isspace(line[len-1]))
258 static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents)
265 /* Go back to the previous line: 40 characters of previous parent, and one '\n' */
266 offset = sprintf(buf, "Merge: %.40s\n", line-41);
269 /* Replace the previous '\n' with a space */
271 offset += sprintf(buf + offset, "%.40s\n", line+7);
276 unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space)
278 int hdr = 1, body = 0;
279 unsigned long offset = 0;
283 const char *line = msg;
284 int linelen = get_one_line(msg, len);
290 * We want some slop for indentation and a possible
291 * final "...". Thus the "+ 20".
293 if (offset + linelen + 20 > space) {
294 memcpy(buf + offset, " ...\n", 8);
304 buf[offset++] = '\n';
307 if (fmt == CMIT_FMT_RAW) {
308 memcpy(buf + offset, line, linelen);
312 if (!memcmp(line, "parent ", 7)) {
314 die("bad parent line in commit");
315 offset += add_parent_info(fmt, buf + offset, line, ++parents);
317 if (!memcmp(line, "author ", 7))
318 offset += add_user_info("Author", fmt, buf + offset, line + 7);
319 if (fmt == CMIT_FMT_FULL) {
320 if (!memcmp(line, "committer ", 10))
321 offset += add_user_info("Commit", fmt, buf + offset, line + 10);
326 if (is_empty_line(line, linelen)) {
329 if (fmt == CMIT_FMT_SHORT)
334 memset(buf + offset, ' ', 4);
335 memcpy(buf + offset + 4, line, linelen);
336 offset += linelen + 4;
338 /* Make sure there is an EOLN */
339 if (buf[offset - 1] != '\n')
340 buf[offset++] = '\n';
345 struct commit *pop_commit(struct commit_list **stack)
347 struct commit_list *top = *stack;
348 struct commit *item = top ? top->item : NULL;
357 int count_parents(struct commit * commit)
360 struct commit_list * parents = commit->parents;
361 for (count=0;parents; parents=parents->next,count++)
367 * Performs an in-place topological sort on the list supplied.
369 void sort_in_topological_order(struct commit_list ** list)
371 struct commit_list * next = *list;
372 struct commit_list * work = NULL;
373 struct commit_list ** pptr = list;
374 struct sort_node * nodes;
375 struct sort_node * next_nodes;
378 /* determine the size of the list */
383 /* allocate an array to help sort the list */
384 nodes = xcalloc(count, sizeof(*nodes));
385 /* link the list to the array */
389 next_nodes->list_item = next;
390 next->item->object.util = next_nodes;
394 /* update the indegree */
397 struct commit_list * parents = next->item->parents;
399 struct commit * parent=parents->item;
400 struct sort_node * pn = (struct sort_node *)parent->object.util;
404 parents=parents->next;
411 * tips are nodes not reachable from any other node in the list
413 * the tips serve as a starting set for the work queue.
417 struct sort_node * node = (struct sort_node *)next->item->object.util;
419 if (node->indegree == 0) {
420 commit_list_insert(next->item, &work);
424 /* process the list in topological order */
426 struct commit * work_item = pop_commit(&work);
427 struct sort_node * work_node = (struct sort_node *)work_item->object.util;
428 struct commit_list * parents = work_item->parents;
431 struct commit * parent=parents->item;
432 struct sort_node * pn = (struct sort_node *)parent->object.util;
436 * parents are only enqueued for emission
437 * when all their children have been emitted thereby
438 * guaranteeing topological order.
442 commit_list_insert(parent, &work);
444 parents=parents->next;
447 * work_item is a commit all of whose children
448 * have already been emitted. we can emit it now.
450 *pptr = work_node->list_item;
451 pptr = &(*pptr)->next;
453 work_item->object.util = NULL;