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];
67 if (item
->object
.parsed
)
69 item
->object
.parsed
= 1;
70 get_sha1_hex(bufptr
+ 5, parent
);
71 item
->tree
= lookup_tree(parent
);
73 add_ref(&item
->object
, &item
->tree
->object
);
74 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
75 while (!memcmp(bufptr
, "parent ", 7) &&
76 !get_sha1_hex(bufptr
+ 7, parent
)) {
77 struct commit
*new_parent
= lookup_commit(parent
);
79 commit_list_insert(new_parent
, &item
->parents
);
80 add_ref(&item
->object
, &new_parent
->object
);
84 item
->date
= parse_commit_date(bufptr
);
88 int parse_commit(struct commit
*item
)
95 if (item
->object
.parsed
)
97 buffer
= read_sha1_file(item
->object
.sha1
, type
, &size
);
99 return error("Could not read %s",
100 sha1_to_hex(item
->object
.sha1
));
101 if (strcmp(type
, commit_type
)) {
103 return error("Object %s not a commit",
104 sha1_to_hex(item
->object
.sha1
));
106 ret
= parse_commit_buffer(item
, buffer
, size
);
108 item
->buffer
= buffer
;
115 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
117 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
118 new_list
->item
= item
;
119 new_list
->next
= *list_p
;
124 void free_commit_list(struct commit_list
*list
)
127 struct commit_list
*temp
= list
;
133 void insert_by_date(struct commit_list
**list
, struct commit
*item
)
135 struct commit_list
**pp
= list
;
136 struct commit_list
*p
;
137 while ((p
= *pp
) != NULL
) {
138 if (p
->item
->date
< item
->date
) {
143 commit_list_insert(item
, pp
);
147 void sort_by_date(struct commit_list
**list
)
149 struct commit_list
*ret
= NULL
;
151 insert_by_date(&ret
, (*list
)->item
);
152 *list
= (*list
)->next
;
157 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
160 struct commit
*ret
= (*list
)->item
;
161 struct commit_list
*parents
= ret
->parents
;
162 struct commit_list
*old
= *list
;
164 *list
= (*list
)->next
;
168 struct commit
*commit
= parents
->item
;
169 parse_commit(commit
);
170 if (!(commit
->object
.flags
& mark
)) {
171 commit
->object
.flags
|= mark
;
172 insert_by_date(list
, commit
);
174 parents
= parents
->next
;
180 * Generic support for pretty-printing the header
182 static int get_one_line(const char *msg
, unsigned long len
)
197 static int add_author_info(enum cmit_fmt fmt
, char *buf
, const char *line
, int len
)
200 unsigned int namelen
;
204 line
+= strlen("author ");
205 date
= strchr(line
, '>');
208 namelen
= ++date
- line
;
209 time
= strtoul(date
, &date
, 10);
210 tz
= strtol(date
, NULL
, 10);
212 ret
= sprintf(buf
, "Author: %.*s\n", namelen
, line
);
213 if (fmt
== CMIT_FMT_MEDIUM
)
214 ret
+= sprintf(buf
+ ret
, "Date: %s\n", show_date(time
, tz
));
218 static int is_empty_line(const char *line
, int len
)
220 while (len
&& isspace(line
[len
-1]))
225 static int add_parent_info(enum cmit_fmt fmt
, char *buf
, const char *line
, int parents
)
232 /* Go back to the previous line: 40 characters of previous parent, and one '\n' */
233 offset
= sprintf(buf
, "Merge: %.40s\n", line
-41);
236 /* Replace the previous '\n' with a space */
238 offset
+= sprintf(buf
+ offset
, "%.40s\n", line
+7);
243 unsigned long pretty_print_commit(enum cmit_fmt fmt
, const char *msg
, unsigned long len
, char *buf
, unsigned long space
)
245 int hdr
= 1, body
= 0;
246 unsigned long offset
= 0;
250 const char *line
= msg
;
251 int linelen
= get_one_line(msg
, len
);
257 * We want some slop for indentation and a possible
258 * final "...". Thus the "+ 20".
260 if (offset
+ linelen
+ 20 > space
) {
261 memcpy(buf
+ offset
, " ...\n", 8);
271 buf
[offset
++] = '\n';
274 if (fmt
== CMIT_FMT_RAW
) {
275 memcpy(buf
+ offset
, line
, linelen
);
279 if (!memcmp(line
, "parent ", 7)) {
281 die("bad parent line in commit");
282 offset
+= add_parent_info(fmt
, buf
+ offset
, line
, ++parents
);
284 if (!memcmp(line
, "author ", 7))
285 offset
+= add_author_info(fmt
, buf
+ offset
, line
, linelen
);
289 if (is_empty_line(line
, linelen
)) {
292 if (fmt
== CMIT_FMT_SHORT
)
297 memset(buf
+ offset
, ' ', 4);
298 memcpy(buf
+ offset
+ 4, line
, linelen
);
299 offset
+= linelen
+ 4;
301 /* Make sure there is an EOLN */
302 if (buf
[offset
- 1] != '\n')
303 buf
[offset
++] = '\n';
308 struct commit
*pop_commit(struct commit_list
**stack
)
310 struct commit_list
*top
= *stack
;
311 struct commit
*item
= top
? top
->item
: NULL
;
320 int count_parents(struct commit
* commit
)
323 struct commit_list
* parents
= commit
->parents
;
324 for (count
=0;parents
; parents
=parents
->next
,count
++)