6 const char *commit_type = "commit";
8 static struct commit *check_commit(struct object *obj, const unsigned char *sha1)
10 if (obj->type != commit_type) {
11 error("Object %s is a %s, not a commit",
12 sha1_to_hex(sha1), obj->type);
15 return (struct commit *) obj;
18 struct commit *lookup_commit_reference(const unsigned char *sha1)
20 struct object *obj = parse_object(sha1);
24 if (obj->type == tag_type)
25 obj = ((struct tag *)obj)->tagged;
26 return check_commit(obj, sha1);
29 struct commit *lookup_commit(const unsigned char *sha1)
31 struct object *obj = lookup_object(sha1);
33 struct commit *ret = xmalloc(sizeof(struct commit));
34 memset(ret, 0, sizeof(struct commit));
35 created_object(sha1, &ret->object);
36 ret->object.type = commit_type;
40 obj->type = commit_type;
41 return check_commit(obj, sha1);
44 static unsigned long parse_commit_date(const char *buf)
48 if (memcmp(buf, "author", 6))
50 while (*buf++ != '\n')
52 if (memcmp(buf, "committer", 9))
56 date = strtoul(buf, NULL, 10);
57 if (date == ULONG_MAX)
62 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
64 void *bufptr = buffer;
65 unsigned char parent[20];
66 struct commit_list **pptr;
68 if (item->object.parsed)
70 item->object.parsed = 1;
71 get_sha1_hex(bufptr + 5, parent);
72 item->tree = lookup_tree(parent);
74 add_ref(&item->object, &item->tree->object);
75 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
76 pptr = &item->parents;
77 while (!memcmp(bufptr, "parent ", 7) &&
78 !get_sha1_hex(bufptr + 7, parent)) {
79 struct commit *new_parent = lookup_commit(parent);
81 pptr = &commit_list_insert(new_parent, pptr)->next;
82 add_ref(&item->object, &new_parent->object);
86 item->date = parse_commit_date(bufptr);
90 int parse_commit(struct commit *item)
97 if (item->object.parsed)
99 buffer = read_sha1_file(item->object.sha1, type, &size);
101 return error("Could not read %s",
102 sha1_to_hex(item->object.sha1));
103 if (strcmp(type, commit_type)) {
105 return error("Object %s not a commit",
106 sha1_to_hex(item->object.sha1));
108 ret = parse_commit_buffer(item, buffer, size);
110 item->buffer = buffer;
117 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
119 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
120 new_list->item = item;
121 new_list->next = *list_p;
126 void free_commit_list(struct commit_list *list)
129 struct commit_list *temp = list;
135 void insert_by_date(struct commit_list **list, struct commit *item)
137 struct commit_list **pp = list;
138 struct commit_list *p;
139 while ((p = *pp) != NULL) {
140 if (p->item->date < item->date) {
145 commit_list_insert(item, pp);
149 void sort_by_date(struct commit_list **list)
151 struct commit_list *ret = NULL;
153 insert_by_date(&ret, (*list)->item);
154 *list = (*list)->next;
159 struct commit *pop_most_recent_commit(struct commit_list **list,
162 struct commit *ret = (*list)->item;
163 struct commit_list *parents = ret->parents;
164 struct commit_list *old = *list;
166 *list = (*list)->next;
170 struct commit *commit = parents->item;
171 parse_commit(commit);
172 if (!(commit->object.flags & mark)) {
173 commit->object.flags |= mark;
174 insert_by_date(list, commit);
176 parents = parents->next;
182 * Generic support for pretty-printing the header
184 static int get_one_line(const char *msg, unsigned long len)
199 static int add_author_info(enum cmit_fmt fmt, char *buf, const char *line, int len)
202 unsigned int namelen;
206 line += strlen("author ");
207 date = strchr(line, '>');
210 namelen = ++date - line;
211 time = strtoul(date, &date, 10);
212 tz = strtol(date, NULL, 10);
214 ret = sprintf(buf, "Author: %.*s\n", namelen, line);
215 if (fmt == CMIT_FMT_MEDIUM)
216 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
220 static int is_empty_line(const char *line, int len)
222 while (len && isspace(line[len-1]))
227 static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents)
234 /* Go back to the previous line: 40 characters of previous parent, and one '\n' */
235 offset = sprintf(buf, "Merge: %.40s\n", line-41);
238 /* Replace the previous '\n' with a space */
240 offset += sprintf(buf + offset, "%.40s\n", line+7);
245 unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space)
247 int hdr = 1, body = 0;
248 unsigned long offset = 0;
252 const char *line = msg;
253 int linelen = get_one_line(msg, len);
259 * We want some slop for indentation and a possible
260 * final "...". Thus the "+ 20".
262 if (offset + linelen + 20 > space) {
263 memcpy(buf + offset, " ...\n", 8);
273 buf[offset++] = '\n';
276 if (fmt == CMIT_FMT_RAW) {
277 memcpy(buf + offset, line, linelen);
281 if (!memcmp(line, "parent ", 7)) {
283 die("bad parent line in commit");
284 offset += add_parent_info(fmt, buf + offset, line, ++parents);
286 if (!memcmp(line, "author ", 7))
287 offset += add_author_info(fmt, buf + offset, line, linelen);
291 if (is_empty_line(line, linelen)) {
294 if (fmt == CMIT_FMT_SHORT)
299 memset(buf + offset, ' ', 4);
300 memcpy(buf + offset + 4, line, linelen);
301 offset += linelen + 4;
303 /* Make sure there is an EOLN */
304 if (buf[offset - 1] != '\n')
305 buf[offset++] = '\n';
310 struct commit *pop_commit(struct commit_list **stack)
312 struct commit_list *top = *stack;
313 struct commit *item = top ? top->item : NULL;
322 int count_parents(struct commit * commit)
325 struct commit_list * parents = commit->parents;
326 for (count=0;parents; parents=parents->next,count++)