6 int save_commit_buffer = 1;
11 * the number of children of the associated commit
12 * that also occur in the list being sorted.
14 unsigned int indegree;
17 * reference to original list item that we will re-use
20 struct commit_list * list_item;
24 const char *commit_type = "commit";
26 enum cmit_fmt get_commit_format(const char *arg)
29 return CMIT_FMT_DEFAULT;
30 if (!strcmp(arg, "=raw"))
32 if (!strcmp(arg, "=medium"))
33 return CMIT_FMT_MEDIUM;
34 if (!strcmp(arg, "=short"))
35 return CMIT_FMT_SHORT;
36 if (!strcmp(arg, "=full"))
38 if (!strcmp(arg, "=oneline"))
39 return CMIT_FMT_ONELINE;
40 die("invalid --pretty format");
43 static struct commit *check_commit(struct object *obj,
44 const unsigned char *sha1,
47 if (obj->type != commit_type) {
49 error("Object %s is a %s, not a commit",
50 sha1_to_hex(sha1), obj->type);
53 return (struct commit *) obj;
56 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
59 struct object *obj = deref_tag(parse_object(sha1));
63 return check_commit(obj, sha1, quiet);
66 struct commit *lookup_commit_reference(const unsigned char *sha1)
68 return lookup_commit_reference_gently(sha1, 0);
71 struct commit *lookup_commit(const unsigned char *sha1)
73 struct object *obj = lookup_object(sha1);
75 struct commit *ret = xmalloc(sizeof(struct commit));
76 memset(ret, 0, sizeof(struct commit));
77 created_object(sha1, &ret->object);
78 ret->object.type = commit_type;
82 obj->type = commit_type;
83 return check_commit(obj, sha1, 0);
86 static unsigned long parse_commit_date(const char *buf)
90 if (memcmp(buf, "author", 6))
92 while (*buf++ != '\n')
94 if (memcmp(buf, "committer", 9))
98 date = strtoul(buf, NULL, 10);
99 if (date == ULONG_MAX)
104 static struct commit_graft {
105 unsigned char sha1[20];
107 unsigned char parent[0][20]; /* more */
109 static int commit_graft_alloc, commit_graft_nr;
111 static int commit_graft_pos(const unsigned char *sha1)
115 hi = commit_graft_nr;
117 int mi = (lo + hi) / 2;
118 struct commit_graft *graft = commit_graft[mi];
119 int cmp = memcmp(sha1, graft->sha1, 20);
130 static void prepare_commit_graft(void)
132 char *graft_file = get_graft_file();
133 FILE *fp = fopen(graft_file, "r");
136 commit_graft = (struct commit_graft **) "hack";
139 while (fgets(buf, sizeof(buf), fp)) {
140 /* The format is just "Commit Parent1 Parent2 ...\n" */
141 int len = strlen(buf);
143 struct commit_graft *graft = NULL;
145 if (buf[len-1] == '\n')
149 if ((len + 1) % 41) {
151 error("bad graft data: %s", buf);
155 i = (len + 1) / 41 - 1;
156 graft = xmalloc(sizeof(*graft) + 20 * i);
157 graft->nr_parent = i;
158 if (get_sha1_hex(buf, graft->sha1))
160 for (i = 40; i < len; i += 41) {
163 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
166 i = commit_graft_pos(graft->sha1);
168 error("duplicate graft data: %s", buf);
173 if (commit_graft_alloc <= ++commit_graft_nr) {
174 commit_graft_alloc = alloc_nr(commit_graft_alloc);
175 commit_graft = xrealloc(commit_graft,
176 sizeof(*commit_graft) *
179 if (i < commit_graft_nr)
180 memmove(commit_graft + i + 1,
182 (commit_graft_nr - i - 1) *
183 sizeof(*commit_graft));
184 commit_graft[i] = graft;
189 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
193 prepare_commit_graft();
194 pos = commit_graft_pos(sha1);
197 return commit_graft[pos];
200 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
202 char *bufptr = buffer;
203 unsigned char parent[20];
204 struct commit_list **pptr;
205 struct commit_graft *graft;
207 if (item->object.parsed)
209 item->object.parsed = 1;
210 if (memcmp(bufptr, "tree ", 5))
211 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
212 if (get_sha1_hex(bufptr + 5, parent) < 0)
213 return error("bad tree pointer in commit %s\n", sha1_to_hex(item->object.sha1));
214 item->tree = lookup_tree(parent);
216 add_ref(&item->object, &item->tree->object);
217 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
218 pptr = &item->parents;
220 graft = lookup_commit_graft(item->object.sha1);
221 while (!memcmp(bufptr, "parent ", 7)) {
222 struct commit *new_parent;
224 if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
225 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
229 new_parent = lookup_commit(parent);
231 pptr = &commit_list_insert(new_parent, pptr)->next;
232 add_ref(&item->object, &new_parent->object);
237 struct commit *new_parent;
238 for (i = 0; i < graft->nr_parent; i++) {
239 new_parent = lookup_commit(graft->parent[i]);
242 pptr = &commit_list_insert(new_parent, pptr)->next;
243 add_ref(&item->object, &new_parent->object);
246 item->date = parse_commit_date(bufptr);
250 int parse_commit(struct commit *item)
257 if (item->object.parsed)
259 buffer = read_sha1_file(item->object.sha1, type, &size);
261 return error("Could not read %s",
262 sha1_to_hex(item->object.sha1));
263 if (strcmp(type, commit_type)) {
265 return error("Object %s not a commit",
266 sha1_to_hex(item->object.sha1));
268 ret = parse_commit_buffer(item, buffer, size);
269 if (save_commit_buffer && !ret) {
270 item->buffer = buffer;
277 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
279 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
280 new_list->item = item;
281 new_list->next = *list_p;
286 void free_commit_list(struct commit_list *list)
289 struct commit_list *temp = list;
295 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
297 struct commit_list **pp = list;
298 struct commit_list *p;
299 while ((p = *pp) != NULL) {
300 if (p->item->date < item->date) {
305 return commit_list_insert(item, pp);
309 void sort_by_date(struct commit_list **list)
311 struct commit_list *ret = NULL;
313 insert_by_date((*list)->item, &ret);
314 *list = (*list)->next;
319 struct commit *pop_most_recent_commit(struct commit_list **list,
322 struct commit *ret = (*list)->item;
323 struct commit_list *parents = ret->parents;
324 struct commit_list *old = *list;
326 *list = (*list)->next;
330 struct commit *commit = parents->item;
331 parse_commit(commit);
332 if (!(commit->object.flags & mark)) {
333 commit->object.flags |= mark;
334 insert_by_date(commit, list);
336 parents = parents->next;
342 * Generic support for pretty-printing the header
344 static int get_one_line(const char *msg, unsigned long len)
359 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
366 if (fmt == CMIT_FMT_ONELINE)
368 date = strchr(line, '>');
371 namelen = ++date - line;
372 time = strtoul(date, &date, 10);
373 tz = strtol(date, NULL, 10);
375 ret = sprintf(buf, "%s: %.*s\n", what, namelen, line);
376 if (fmt == CMIT_FMT_MEDIUM)
377 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
381 static int is_empty_line(const char *line, int len)
383 while (len && isspace(line[len-1]))
388 static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents)
392 if (fmt == CMIT_FMT_ONELINE)
398 /* Go back to the previous line: 40 characters of previous parent, and one '\n' */
399 offset = sprintf(buf, "Merge: %.40s\n", line-41);
402 /* Replace the previous '\n' with a space */
404 offset += sprintf(buf + offset, "%.40s\n", line+7);
409 unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space)
411 int hdr = 1, body = 0;
412 unsigned long offset = 0;
414 int indent = (fmt == CMIT_FMT_ONELINE) ? 0 : 4;
417 const char *line = msg;
418 int linelen = get_one_line(msg, len);
424 * We want some slop for indentation and a possible
425 * final "...". Thus the "+ 20".
427 if (offset + linelen + 20 > space) {
428 memcpy(buf + offset, " ...\n", 8);
438 if (fmt != CMIT_FMT_ONELINE)
439 buf[offset++] = '\n';
442 if (fmt == CMIT_FMT_RAW) {
443 memcpy(buf + offset, line, linelen);
447 if (!memcmp(line, "parent ", 7)) {
449 die("bad parent line in commit");
450 offset += add_parent_info(fmt, buf + offset, line, ++parents);
452 if (!memcmp(line, "author ", 7))
453 offset += add_user_info("Author", fmt, buf + offset, line + 7);
454 if (fmt == CMIT_FMT_FULL) {
455 if (!memcmp(line, "committer ", 10))
456 offset += add_user_info("Commit", fmt, buf + offset, line + 10);
461 if (is_empty_line(line, linelen)) {
464 if (fmt == CMIT_FMT_SHORT)
470 memset(buf + offset, ' ', indent);
471 memcpy(buf + offset + indent, line, linelen);
472 offset += linelen + indent;
473 if (fmt == CMIT_FMT_ONELINE)
476 if (fmt == CMIT_FMT_ONELINE) {
477 /* We do not want the terminating newline */
478 if (buf[offset - 1] == '\n')
482 /* Make sure there is an EOLN */
483 if (buf[offset - 1] != '\n')
484 buf[offset++] = '\n';
490 struct commit *pop_commit(struct commit_list **stack)
492 struct commit_list *top = *stack;
493 struct commit *item = top ? top->item : NULL;
502 int count_parents(struct commit * commit)
505 struct commit_list * parents = commit->parents;
506 for (count=0;parents; parents=parents->next,count++)
512 * Performs an in-place topological sort on the list supplied.
514 void sort_in_topological_order(struct commit_list ** list)
516 struct commit_list * next = *list;
517 struct commit_list * work = NULL;
518 struct commit_list ** pptr = list;
519 struct sort_node * nodes;
520 struct sort_node * next_nodes;
523 /* determine the size of the list */
528 /* allocate an array to help sort the list */
529 nodes = xcalloc(count, sizeof(*nodes));
530 /* link the list to the array */
534 next_nodes->list_item = next;
535 next->item->object.util = next_nodes;
539 /* update the indegree */
542 struct commit_list * parents = next->item->parents;
544 struct commit * parent=parents->item;
545 struct sort_node * pn = (struct sort_node *)parent->object.util;
549 parents=parents->next;
556 * tips are nodes not reachable from any other node in the list
558 * the tips serve as a starting set for the work queue.
562 struct sort_node * node = (struct sort_node *)next->item->object.util;
564 if (node->indegree == 0) {
565 commit_list_insert(next->item, &work);
569 /* process the list in topological order */
571 struct commit * work_item = pop_commit(&work);
572 struct sort_node * work_node = (struct sort_node *)work_item->object.util;
573 struct commit_list * parents = work_item->parents;
576 struct commit * parent=parents->item;
577 struct sort_node * pn = (struct sort_node *)parent->object.util;
581 * parents are only enqueued for emission
582 * when all their children have been emitted thereby
583 * guaranteeing topological order.
587 commit_list_insert(parent, &work);
589 parents=parents->next;
592 * work_item is a commit all of whose children
593 * have already been emitted. we can emit it now.
595 *pptr = work_node->list_item;
596 pptr = &(*pptr)->next;
598 work_item->object.util = NULL;