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 char *bufptr
= buffer
;
97 unsigned char parent
[20];
98 struct commit_list
**pptr
;
100 if (item
->object
.parsed
)
102 item
->object
.parsed
= 1;
103 if (memcmp(bufptr
, "tree ", 5))
104 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
105 if (get_sha1_hex(bufptr
+ 5, parent
) < 0)
106 return error("bad tree pointer in commit %s\n", sha1_to_hex(item
->object
.sha1
));
107 item
->tree
= lookup_tree(parent
);
109 add_ref(&item
->object
, &item
->tree
->object
);
110 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
111 pptr
= &item
->parents
;
112 while (!memcmp(bufptr
, "parent ", 7)) {
113 struct commit
*new_parent
;
115 if (get_sha1_hex(bufptr
+ 7, parent
) || bufptr
[47] != '\n')
116 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
117 new_parent
= lookup_commit(parent
);
119 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
120 add_ref(&item
->object
, &new_parent
->object
);
124 item
->date
= parse_commit_date(bufptr
);
128 int parse_commit(struct commit
*item
)
135 if (item
->object
.parsed
)
137 buffer
= read_sha1_file(item
->object
.sha1
, type
, &size
);
139 return error("Could not read %s",
140 sha1_to_hex(item
->object
.sha1
));
141 if (strcmp(type
, commit_type
)) {
143 return error("Object %s not a commit",
144 sha1_to_hex(item
->object
.sha1
));
146 ret
= parse_commit_buffer(item
, buffer
, size
);
148 item
->buffer
= buffer
;
155 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
157 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
158 new_list
->item
= item
;
159 new_list
->next
= *list_p
;
164 void free_commit_list(struct commit_list
*list
)
167 struct commit_list
*temp
= list
;
173 struct commit_list
* insert_by_date(struct commit
*item
, struct commit_list
**list
)
175 struct commit_list
**pp
= list
;
176 struct commit_list
*p
;
177 while ((p
= *pp
) != NULL
) {
178 if (p
->item
->date
< item
->date
) {
183 return commit_list_insert(item
, pp
);
187 void sort_by_date(struct commit_list
**list
)
189 struct commit_list
*ret
= NULL
;
191 insert_by_date((*list
)->item
, &ret
);
192 *list
= (*list
)->next
;
197 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
200 struct commit
*ret
= (*list
)->item
;
201 struct commit_list
*parents
= ret
->parents
;
202 struct commit_list
*old
= *list
;
204 *list
= (*list
)->next
;
208 struct commit
*commit
= parents
->item
;
209 parse_commit(commit
);
210 if (!(commit
->object
.flags
& mark
)) {
211 commit
->object
.flags
|= mark
;
212 insert_by_date(commit
, list
);
214 parents
= parents
->next
;
220 * Generic support for pretty-printing the header
222 static int get_one_line(const char *msg
, unsigned long len
)
237 static int add_user_info(const char *what
, enum cmit_fmt fmt
, char *buf
, const char *line
)
240 unsigned int namelen
;
244 date
= strchr(line
, '>');
247 namelen
= ++date
- line
;
248 time
= strtoul(date
, &date
, 10);
249 tz
= strtol(date
, NULL
, 10);
251 ret
= sprintf(buf
, "%s: %.*s\n", what
, namelen
, line
);
252 if (fmt
== CMIT_FMT_MEDIUM
)
253 ret
+= sprintf(buf
+ ret
, "Date: %s\n", show_date(time
, tz
));
257 static int is_empty_line(const char *line
, int len
)
259 while (len
&& isspace(line
[len
-1]))
264 static int add_parent_info(enum cmit_fmt fmt
, char *buf
, const char *line
, int parents
)
271 /* Go back to the previous line: 40 characters of previous parent, and one '\n' */
272 offset
= sprintf(buf
, "Merge: %.40s\n", line
-41);
275 /* Replace the previous '\n' with a space */
277 offset
+= sprintf(buf
+ offset
, "%.40s\n", line
+7);
282 unsigned long pretty_print_commit(enum cmit_fmt fmt
, const char *msg
, unsigned long len
, char *buf
, unsigned long space
)
284 int hdr
= 1, body
= 0;
285 unsigned long offset
= 0;
289 const char *line
= msg
;
290 int linelen
= get_one_line(msg
, len
);
296 * We want some slop for indentation and a possible
297 * final "...". Thus the "+ 20".
299 if (offset
+ linelen
+ 20 > space
) {
300 memcpy(buf
+ offset
, " ...\n", 8);
310 buf
[offset
++] = '\n';
313 if (fmt
== CMIT_FMT_RAW
) {
314 memcpy(buf
+ offset
, line
, linelen
);
318 if (!memcmp(line
, "parent ", 7)) {
320 die("bad parent line in commit");
321 offset
+= add_parent_info(fmt
, buf
+ offset
, line
, ++parents
);
323 if (!memcmp(line
, "author ", 7))
324 offset
+= add_user_info("Author", fmt
, buf
+ offset
, line
+ 7);
325 if (fmt
== CMIT_FMT_FULL
) {
326 if (!memcmp(line
, "committer ", 10))
327 offset
+= add_user_info("Commit", fmt
, buf
+ offset
, line
+ 10);
332 if (is_empty_line(line
, linelen
)) {
335 if (fmt
== CMIT_FMT_SHORT
)
340 memset(buf
+ offset
, ' ', 4);
341 memcpy(buf
+ offset
+ 4, line
, linelen
);
342 offset
+= linelen
+ 4;
344 /* Make sure there is an EOLN */
345 if (buf
[offset
- 1] != '\n')
346 buf
[offset
++] = '\n';
351 struct commit
*pop_commit(struct commit_list
**stack
)
353 struct commit_list
*top
= *stack
;
354 struct commit
*item
= top
? top
->item
: NULL
;
363 int count_parents(struct commit
* commit
)
366 struct commit_list
* parents
= commit
->parents
;
367 for (count
=0;parents
; parents
=parents
->next
,count
++)
373 * Performs an in-place topological sort on the list supplied.
375 void sort_in_topological_order(struct commit_list
** list
)
377 struct commit_list
* next
= *list
;
378 struct commit_list
* work
= NULL
;
379 struct commit_list
** pptr
= list
;
380 struct sort_node
* nodes
;
381 struct sort_node
* next_nodes
;
384 /* determine the size of the list */
389 /* allocate an array to help sort the list */
390 nodes
= xcalloc(count
, sizeof(*nodes
));
391 /* link the list to the array */
395 next_nodes
->list_item
= next
;
396 next
->item
->object
.util
= next_nodes
;
400 /* update the indegree */
403 struct commit_list
* parents
= next
->item
->parents
;
405 struct commit
* parent
=parents
->item
;
406 struct sort_node
* pn
= (struct sort_node
*)parent
->object
.util
;
410 parents
=parents
->next
;
417 * tips are nodes not reachable from any other node in the list
419 * the tips serve as a starting set for the work queue.
423 struct sort_node
* node
= (struct sort_node
*)next
->item
->object
.util
;
425 if (node
->indegree
== 0) {
426 commit_list_insert(next
->item
, &work
);
430 /* process the list in topological order */
432 struct commit
* work_item
= pop_commit(&work
);
433 struct sort_node
* work_node
= (struct sort_node
*)work_item
->object
.util
;
434 struct commit_list
* parents
= work_item
->parents
;
437 struct commit
* parent
=parents
->item
;
438 struct sort_node
* pn
= (struct sort_node
*)parent
->object
.util
;
442 * parents are only enqueued for emission
443 * when all their children have been emitted thereby
444 * guaranteeing topological order.
448 commit_list_insert(parent
, &work
);
450 parents
=parents
->next
;
453 * work_item is a commit all of whose children
454 * have already been emitted. we can emit it now.
456 *pptr
= work_node
->list_item
;
457 pptr
= &(*pptr
)->next
;
459 work_item
->object
.util
= NULL
;