6 const char *commit_type
= "commit";
8 enum cmit_fmt
get_commit_format(const char *arg
)
11 return CMIT_FMT_DEFAULT
;
12 if (!strcmp(arg
, "=raw"))
14 if (!strcmp(arg
, "=medium"))
15 return CMIT_FMT_MEDIUM
;
16 if (!strcmp(arg
, "=short"))
17 return CMIT_FMT_SHORT
;
18 if (!strcmp(arg
, "=full"))
20 die("invalid --pretty format");
23 static struct commit
*check_commit(struct object
*obj
, const unsigned char *sha1
)
25 if (obj
->type
!= commit_type
) {
26 error("Object %s is a %s, not a commit",
27 sha1_to_hex(sha1
), obj
->type
);
30 return (struct commit
*) obj
;
33 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
35 struct object
*obj
= parse_object(sha1
);
39 if (obj
->type
== tag_type
)
40 obj
= ((struct tag
*)obj
)->tagged
;
41 return check_commit(obj
, sha1
);
44 struct commit
*lookup_commit(const unsigned char *sha1
)
46 struct object
*obj
= lookup_object(sha1
);
48 struct commit
*ret
= xmalloc(sizeof(struct commit
));
49 memset(ret
, 0, sizeof(struct commit
));
50 created_object(sha1
, &ret
->object
);
51 ret
->object
.type
= commit_type
;
55 obj
->type
= commit_type
;
56 return check_commit(obj
, sha1
);
59 static unsigned long parse_commit_date(const char *buf
)
63 if (memcmp(buf
, "author", 6))
65 while (*buf
++ != '\n')
67 if (memcmp(buf
, "committer", 9))
71 date
= strtoul(buf
, NULL
, 10);
72 if (date
== ULONG_MAX
)
77 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
79 void *bufptr
= buffer
;
80 unsigned char parent
[20];
81 struct commit_list
**pptr
;
83 if (item
->object
.parsed
)
85 item
->object
.parsed
= 1;
86 get_sha1_hex(bufptr
+ 5, parent
);
87 item
->tree
= lookup_tree(parent
);
89 add_ref(&item
->object
, &item
->tree
->object
);
90 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
91 pptr
= &item
->parents
;
92 while (!memcmp(bufptr
, "parent ", 7) &&
93 !get_sha1_hex(bufptr
+ 7, parent
)) {
94 struct commit
*new_parent
= lookup_commit(parent
);
96 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
97 add_ref(&item
->object
, &new_parent
->object
);
101 item
->date
= parse_commit_date(bufptr
);
105 int parse_commit(struct commit
*item
)
112 if (item
->object
.parsed
)
114 buffer
= read_sha1_file(item
->object
.sha1
, type
, &size
);
116 return error("Could not read %s",
117 sha1_to_hex(item
->object
.sha1
));
118 if (strcmp(type
, commit_type
)) {
120 return error("Object %s not a commit",
121 sha1_to_hex(item
->object
.sha1
));
123 ret
= parse_commit_buffer(item
, buffer
, size
);
125 item
->buffer
= buffer
;
132 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
134 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
135 new_list
->item
= item
;
136 new_list
->next
= *list_p
;
141 void free_commit_list(struct commit_list
*list
)
144 struct commit_list
*temp
= list
;
150 void insert_by_date(struct commit_list
**list
, struct commit
*item
)
152 struct commit_list
**pp
= list
;
153 struct commit_list
*p
;
154 while ((p
= *pp
) != NULL
) {
155 if (p
->item
->date
< item
->date
) {
160 commit_list_insert(item
, pp
);
164 void sort_by_date(struct commit_list
**list
)
166 struct commit_list
*ret
= NULL
;
168 insert_by_date(&ret
, (*list
)->item
);
169 *list
= (*list
)->next
;
174 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
177 struct commit
*ret
= (*list
)->item
;
178 struct commit_list
*parents
= ret
->parents
;
179 struct commit_list
*old
= *list
;
181 *list
= (*list
)->next
;
185 struct commit
*commit
= parents
->item
;
186 parse_commit(commit
);
187 if (!(commit
->object
.flags
& mark
)) {
188 commit
->object
.flags
|= mark
;
189 insert_by_date(list
, commit
);
191 parents
= parents
->next
;
197 * Generic support for pretty-printing the header
199 static int get_one_line(const char *msg
, unsigned long len
)
214 static int add_user_info(const char *what
, enum cmit_fmt fmt
, char *buf
, const char *line
)
217 unsigned int namelen
;
221 date
= strchr(line
, '>');
224 namelen
= ++date
- line
;
225 time
= strtoul(date
, &date
, 10);
226 tz
= strtol(date
, NULL
, 10);
228 ret
= sprintf(buf
, "%s: %.*s\n", what
, namelen
, line
);
229 if (fmt
== CMIT_FMT_MEDIUM
)
230 ret
+= sprintf(buf
+ ret
, "Date: %s\n", show_date(time
, tz
));
234 static int is_empty_line(const char *line
, int len
)
236 while (len
&& isspace(line
[len
-1]))
241 static int add_parent_info(enum cmit_fmt fmt
, char *buf
, const char *line
, int parents
)
248 /* Go back to the previous line: 40 characters of previous parent, and one '\n' */
249 offset
= sprintf(buf
, "Merge: %.40s\n", line
-41);
252 /* Replace the previous '\n' with a space */
254 offset
+= sprintf(buf
+ offset
, "%.40s\n", line
+7);
259 unsigned long pretty_print_commit(enum cmit_fmt fmt
, const char *msg
, unsigned long len
, char *buf
, unsigned long space
)
261 int hdr
= 1, body
= 0;
262 unsigned long offset
= 0;
266 const char *line
= msg
;
267 int linelen
= get_one_line(msg
, len
);
273 * We want some slop for indentation and a possible
274 * final "...". Thus the "+ 20".
276 if (offset
+ linelen
+ 20 > space
) {
277 memcpy(buf
+ offset
, " ...\n", 8);
287 buf
[offset
++] = '\n';
290 if (fmt
== CMIT_FMT_RAW
) {
291 memcpy(buf
+ offset
, line
, linelen
);
295 if (!memcmp(line
, "parent ", 7)) {
297 die("bad parent line in commit");
298 offset
+= add_parent_info(fmt
, buf
+ offset
, line
, ++parents
);
300 if (!memcmp(line
, "author ", 7))
301 offset
+= add_user_info("Author", fmt
, buf
+ offset
, line
+ 7);
302 if (fmt
== CMIT_FMT_FULL
) {
303 if (!memcmp(line
, "committer ", 10))
304 offset
+= add_user_info("Commit", fmt
, buf
+ offset
, line
+ 10);
309 if (is_empty_line(line
, linelen
)) {
312 if (fmt
== CMIT_FMT_SHORT
)
317 memset(buf
+ offset
, ' ', 4);
318 memcpy(buf
+ offset
+ 4, line
, linelen
);
319 offset
+= linelen
+ 4;
321 /* Make sure there is an EOLN */
322 if (buf
[offset
- 1] != '\n')
323 buf
[offset
++] = '\n';
328 struct commit
*pop_commit(struct commit_list
**stack
)
330 struct commit_list
*top
= *stack
;
331 struct commit
*item
= top
? top
->item
: NULL
;
340 int count_parents(struct commit
* commit
)
343 struct commit_list
* parents
= commit
->parents
;
344 for (count
=0;parents
; parents
=parents
->next
,count
++)