5 int save_commit_buffer
= 1;
10 * the number of children of the associated commit
11 * that also occur in the list being sorted.
13 unsigned int indegree
;
16 * reference to original list item that we will re-use
19 struct commit_list
* list_item
;
23 const char *commit_type
= "commit";
25 enum cmit_fmt
get_commit_format(const char *arg
)
28 return CMIT_FMT_DEFAULT
;
29 if (!strcmp(arg
, "=raw"))
31 if (!strcmp(arg
, "=medium"))
32 return CMIT_FMT_MEDIUM
;
33 if (!strcmp(arg
, "=short"))
34 return CMIT_FMT_SHORT
;
35 if (!strcmp(arg
, "=full"))
37 if (!strcmp(arg
, "=oneline"))
38 return CMIT_FMT_ONELINE
;
39 die("invalid --pretty format");
42 static struct commit
*check_commit(struct object
*obj
,
43 const unsigned char *sha1
,
46 if (obj
->type
!= commit_type
) {
48 error("Object %s is a %s, not a commit",
49 sha1_to_hex(sha1
), obj
->type
);
52 return (struct commit
*) obj
;
55 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
58 struct object
*obj
= deref_tag(parse_object(sha1
));
62 return check_commit(obj
, sha1
, quiet
);
65 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
67 return lookup_commit_reference_gently(sha1
, 0);
70 struct commit
*lookup_commit(const unsigned char *sha1
)
72 struct object
*obj
= lookup_object(sha1
);
74 struct commit
*ret
= xmalloc(sizeof(struct commit
));
75 memset(ret
, 0, sizeof(struct commit
));
76 created_object(sha1
, &ret
->object
);
77 ret
->object
.type
= commit_type
;
81 obj
->type
= commit_type
;
82 return check_commit(obj
, sha1
, 0);
85 static unsigned long parse_commit_date(const char *buf
)
89 if (memcmp(buf
, "author", 6))
91 while (*buf
++ != '\n')
93 if (memcmp(buf
, "committer", 9))
97 date
= strtoul(buf
, NULL
, 10);
98 if (date
== ULONG_MAX
)
103 static struct commit_graft
{
104 unsigned char sha1
[20];
106 unsigned char parent
[0][20]; /* more */
108 static int commit_graft_alloc
, commit_graft_nr
;
110 static int commit_graft_pos(const unsigned char *sha1
)
114 hi
= commit_graft_nr
;
116 int mi
= (lo
+ hi
) / 2;
117 struct commit_graft
*graft
= commit_graft
[mi
];
118 int cmp
= memcmp(sha1
, graft
->sha1
, 20);
129 static void prepare_commit_graft(void)
131 char *graft_file
= get_graft_file();
132 FILE *fp
= fopen(graft_file
, "r");
135 commit_graft
= (struct commit_graft
**) "hack";
138 while (fgets(buf
, sizeof(buf
), fp
)) {
139 /* The format is just "Commit Parent1 Parent2 ...\n" */
140 int len
= strlen(buf
);
142 struct commit_graft
*graft
= NULL
;
144 if (buf
[len
-1] == '\n')
148 if ((len
+ 1) % 41) {
150 error("bad graft data: %s", buf
);
154 i
= (len
+ 1) / 41 - 1;
155 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
156 graft
->nr_parent
= i
;
157 if (get_sha1_hex(buf
, graft
->sha1
))
159 for (i
= 40; i
< len
; i
+= 41) {
162 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
165 i
= commit_graft_pos(graft
->sha1
);
167 error("duplicate graft data: %s", buf
);
172 if (commit_graft_alloc
<= ++commit_graft_nr
) {
173 commit_graft_alloc
= alloc_nr(commit_graft_alloc
);
174 commit_graft
= xrealloc(commit_graft
,
175 sizeof(*commit_graft
) *
178 if (i
< commit_graft_nr
)
179 memmove(commit_graft
+ i
+ 1,
181 (commit_graft_nr
- i
- 1) *
182 sizeof(*commit_graft
));
183 commit_graft
[i
] = graft
;
188 static struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
192 prepare_commit_graft();
193 pos
= commit_graft_pos(sha1
);
196 return commit_graft
[pos
];
199 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
201 char *bufptr
= buffer
;
202 unsigned char parent
[20];
203 struct commit_list
**pptr
;
204 struct commit_graft
*graft
;
206 if (item
->object
.parsed
)
208 item
->object
.parsed
= 1;
209 if (memcmp(bufptr
, "tree ", 5))
210 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
211 if (get_sha1_hex(bufptr
+ 5, parent
) < 0)
212 return error("bad tree pointer in commit %s\n", sha1_to_hex(item
->object
.sha1
));
213 item
->tree
= lookup_tree(parent
);
215 add_ref(&item
->object
, &item
->tree
->object
);
216 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
217 pptr
= &item
->parents
;
219 graft
= lookup_commit_graft(item
->object
.sha1
);
220 while (!memcmp(bufptr
, "parent ", 7)) {
221 struct commit
*new_parent
;
223 if (get_sha1_hex(bufptr
+ 7, parent
) || bufptr
[47] != '\n')
224 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
228 new_parent
= lookup_commit(parent
);
230 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
231 add_ref(&item
->object
, &new_parent
->object
);
236 struct commit
*new_parent
;
237 for (i
= 0; i
< graft
->nr_parent
; i
++) {
238 new_parent
= lookup_commit(graft
->parent
[i
]);
241 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
242 add_ref(&item
->object
, &new_parent
->object
);
245 item
->date
= parse_commit_date(bufptr
);
249 int parse_commit(struct commit
*item
)
256 if (item
->object
.parsed
)
258 buffer
= read_sha1_file(item
->object
.sha1
, type
, &size
);
260 return error("Could not read %s",
261 sha1_to_hex(item
->object
.sha1
));
262 if (strcmp(type
, commit_type
)) {
264 return error("Object %s not a commit",
265 sha1_to_hex(item
->object
.sha1
));
267 ret
= parse_commit_buffer(item
, buffer
, size
);
268 if (save_commit_buffer
&& !ret
) {
269 item
->buffer
= buffer
;
276 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
278 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
279 new_list
->item
= item
;
280 new_list
->next
= *list_p
;
285 void free_commit_list(struct commit_list
*list
)
288 struct commit_list
*temp
= list
;
294 struct commit_list
* insert_by_date(struct commit
*item
, struct commit_list
**list
)
296 struct commit_list
**pp
= list
;
297 struct commit_list
*p
;
298 while ((p
= *pp
) != NULL
) {
299 if (p
->item
->date
< item
->date
) {
304 return commit_list_insert(item
, pp
);
308 void sort_by_date(struct commit_list
**list
)
310 struct commit_list
*ret
= NULL
;
312 insert_by_date((*list
)->item
, &ret
);
313 *list
= (*list
)->next
;
318 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
321 struct commit
*ret
= (*list
)->item
;
322 struct commit_list
*parents
= ret
->parents
;
323 struct commit_list
*old
= *list
;
325 *list
= (*list
)->next
;
329 struct commit
*commit
= parents
->item
;
330 parse_commit(commit
);
331 if (!(commit
->object
.flags
& mark
)) {
332 commit
->object
.flags
|= mark
;
333 insert_by_date(commit
, list
);
335 parents
= parents
->next
;
341 * Generic support for pretty-printing the header
343 static int get_one_line(const char *msg
, unsigned long len
)
358 static int add_user_info(const char *what
, enum cmit_fmt fmt
, char *buf
, const char *line
)
365 if (fmt
== CMIT_FMT_ONELINE
)
367 date
= strchr(line
, '>');
370 namelen
= ++date
- line
;
371 time
= strtoul(date
, &date
, 10);
372 tz
= strtol(date
, NULL
, 10);
374 ret
= sprintf(buf
, "%s: %.*s\n", what
, namelen
, line
);
375 if (fmt
== CMIT_FMT_MEDIUM
)
376 ret
+= sprintf(buf
+ ret
, "Date: %s\n", show_date(time
, tz
));
380 static int is_empty_line(const char *line
, int len
)
382 while (len
&& isspace(line
[len
-1]))
387 static int add_parent_info(enum cmit_fmt fmt
, char *buf
, const char *line
, int parents
)
391 if (fmt
== CMIT_FMT_ONELINE
)
397 /* Go back to the previous line: 40 characters of previous parent, and one '\n' */
398 offset
= sprintf(buf
, "Merge: %.40s\n", line
-41);
401 /* Replace the previous '\n' with a space */
403 offset
+= sprintf(buf
+ offset
, "%.40s\n", line
+7);
408 unsigned long pretty_print_commit(enum cmit_fmt fmt
, const char *msg
, unsigned long len
, char *buf
, unsigned long space
)
410 int hdr
= 1, body
= 0;
411 unsigned long offset
= 0;
413 int indent
= (fmt
== CMIT_FMT_ONELINE
) ? 0 : 4;
416 const char *line
= msg
;
417 int linelen
= get_one_line(msg
, len
);
423 * We want some slop for indentation and a possible
424 * final "...". Thus the "+ 20".
426 if (offset
+ linelen
+ 20 > space
) {
427 memcpy(buf
+ offset
, " ...\n", 8);
437 if (fmt
!= CMIT_FMT_ONELINE
)
438 buf
[offset
++] = '\n';
441 if (fmt
== CMIT_FMT_RAW
) {
442 memcpy(buf
+ offset
, line
, linelen
);
446 if (!memcmp(line
, "parent ", 7)) {
448 die("bad parent line in commit");
449 offset
+= add_parent_info(fmt
, buf
+ offset
, line
, ++parents
);
451 if (!memcmp(line
, "author ", 7))
452 offset
+= add_user_info("Author", fmt
, buf
+ offset
, line
+ 7);
453 if (fmt
== CMIT_FMT_FULL
) {
454 if (!memcmp(line
, "committer ", 10))
455 offset
+= add_user_info("Commit", fmt
, buf
+ offset
, line
+ 10);
460 if (is_empty_line(line
, linelen
)) {
463 if (fmt
== CMIT_FMT_SHORT
)
469 memset(buf
+ offset
, ' ', indent
);
470 memcpy(buf
+ offset
+ indent
, line
, linelen
);
471 offset
+= linelen
+ indent
;
472 if (fmt
== CMIT_FMT_ONELINE
)
475 if (fmt
== CMIT_FMT_ONELINE
) {
476 /* We do not want the terminating newline */
477 if (buf
[offset
- 1] == '\n')
481 /* Make sure there is an EOLN */
482 if (buf
[offset
- 1] != '\n')
483 buf
[offset
++] = '\n';
489 struct commit
*pop_commit(struct commit_list
**stack
)
491 struct commit_list
*top
= *stack
;
492 struct commit
*item
= top
? top
->item
: NULL
;
501 int count_parents(struct commit
* commit
)
504 struct commit_list
* parents
= commit
->parents
;
505 for (count
=0;parents
; parents
=parents
->next
,count
++)
511 * Performs an in-place topological sort on the list supplied.
513 void sort_in_topological_order(struct commit_list
** list
)
515 struct commit_list
* next
= *list
;
516 struct commit_list
* work
= NULL
;
517 struct commit_list
** pptr
= list
;
518 struct sort_node
* nodes
;
519 struct sort_node
* next_nodes
;
522 /* determine the size of the list */
527 /* allocate an array to help sort the list */
528 nodes
= xcalloc(count
, sizeof(*nodes
));
529 /* link the list to the array */
533 next_nodes
->list_item
= next
;
534 next
->item
->object
.util
= next_nodes
;
538 /* update the indegree */
541 struct commit_list
* parents
= next
->item
->parents
;
543 struct commit
* parent
=parents
->item
;
544 struct sort_node
* pn
= (struct sort_node
*)parent
->object
.util
;
548 parents
=parents
->next
;
555 * tips are nodes not reachable from any other node in the list
557 * the tips serve as a starting set for the work queue.
561 struct sort_node
* node
= (struct sort_node
*)next
->item
->object
.util
;
563 if (node
->indegree
== 0) {
564 commit_list_insert(next
->item
, &work
);
568 /* process the list in topological order */
570 struct commit
* work_item
= pop_commit(&work
);
571 struct sort_node
* work_node
= (struct sort_node
*)work_item
->object
.util
;
572 struct commit_list
* parents
= work_item
->parents
;
575 struct commit
* parent
=parents
->item
;
576 struct sort_node
* pn
= (struct sort_node
*)parent
->object
.util
;
580 * parents are only enqueued for emission
581 * when all their children have been emitted thereby
582 * guaranteeing topological order.
586 commit_list_insert(parent
, &work
);
588 parents
=parents
->next
;
591 * work_item is a commit all of whose children
592 * have already been emitted. we can emit it now.
594 *pptr
= work_node
->list_item
;
595 pptr
= &(*pptr
)->next
;
597 work_item
->object
.util
= NULL
;