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
, "=fuller"))
38 return CMIT_FMT_FULLER
;
39 if (!strcmp(arg
, "=oneline"))
40 return CMIT_FMT_ONELINE
;
41 die("invalid --pretty format");
44 static struct commit
*check_commit(struct object
*obj
,
45 const unsigned char *sha1
,
48 if (obj
->type
!= commit_type
) {
50 error("Object %s is a %s, not a commit",
51 sha1_to_hex(sha1
), obj
->type
);
54 return (struct commit
*) obj
;
57 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
60 struct object
*obj
= deref_tag(parse_object(sha1
), NULL
, 0);
64 return check_commit(obj
, sha1
, quiet
);
67 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
69 return lookup_commit_reference_gently(sha1
, 0);
72 struct commit
*lookup_commit(const unsigned char *sha1
)
74 struct object
*obj
= lookup_object(sha1
);
76 struct commit
*ret
= xcalloc(1, sizeof(struct commit
));
77 created_object(sha1
, &ret
->object
);
78 ret
->object
.type
= commit_type
;
82 obj
->type
= commit_type
;
83 return check_commit(obj
, sha1
, 0);
86 static unsigned long parse_commit_date(const char *buf
)
90 if (memcmp(buf
, "author", 6))
92 while (*buf
++ != '\n')
94 if (memcmp(buf
, "committer", 9))
98 date
= strtoul(buf
, NULL
, 10);
99 if (date
== ULONG_MAX
)
104 static struct commit_graft
{
105 unsigned char sha1
[20];
107 unsigned char parent
[0][20]; /* more */
109 static int commit_graft_alloc
, commit_graft_nr
;
111 static int commit_graft_pos(const unsigned char *sha1
)
115 hi
= commit_graft_nr
;
117 int mi
= (lo
+ hi
) / 2;
118 struct commit_graft
*graft
= commit_graft
[mi
];
119 int cmp
= memcmp(sha1
, graft
->sha1
, 20);
130 static void prepare_commit_graft(void)
132 char *graft_file
= get_graft_file();
133 FILE *fp
= fopen(graft_file
, "r");
136 commit_graft
= (struct commit_graft
**) "hack";
139 while (fgets(buf
, sizeof(buf
), fp
)) {
140 /* The format is just "Commit Parent1 Parent2 ...\n" */
141 int len
= strlen(buf
);
143 struct commit_graft
*graft
= NULL
;
145 if (buf
[len
-1] == '\n')
149 if ((len
+ 1) % 41) {
151 error("bad graft data: %s", buf
);
155 i
= (len
+ 1) / 41 - 1;
156 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
157 graft
->nr_parent
= i
;
158 if (get_sha1_hex(buf
, graft
->sha1
))
160 for (i
= 40; i
< len
; i
+= 41) {
163 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
166 i
= commit_graft_pos(graft
->sha1
);
168 error("duplicate graft data: %s", buf
);
173 if (commit_graft_alloc
<= ++commit_graft_nr
) {
174 commit_graft_alloc
= alloc_nr(commit_graft_alloc
);
175 commit_graft
= xrealloc(commit_graft
,
176 sizeof(*commit_graft
) *
179 if (i
< commit_graft_nr
)
180 memmove(commit_graft
+ i
+ 1,
182 (commit_graft_nr
- i
- 1) *
183 sizeof(*commit_graft
));
184 commit_graft
[i
] = graft
;
189 static struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
193 prepare_commit_graft();
194 pos
= commit_graft_pos(sha1
);
197 return commit_graft
[pos
];
200 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
202 char *bufptr
= buffer
;
203 unsigned char parent
[20];
204 struct commit_list
**pptr
;
205 struct commit_graft
*graft
;
208 if (item
->object
.parsed
)
210 item
->object
.parsed
= 1;
211 if (memcmp(bufptr
, "tree ", 5))
212 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
213 if (get_sha1_hex(bufptr
+ 5, parent
) < 0)
214 return error("bad tree pointer in commit %s",
215 sha1_to_hex(item
->object
.sha1
));
216 item
->tree
= lookup_tree(parent
);
219 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
220 pptr
= &item
->parents
;
222 graft
= lookup_commit_graft(item
->object
.sha1
);
223 while (!memcmp(bufptr
, "parent ", 7)) {
224 struct commit
*new_parent
;
226 if (get_sha1_hex(bufptr
+ 7, parent
) || bufptr
[47] != '\n')
227 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
231 new_parent
= lookup_commit(parent
);
233 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
239 struct commit
*new_parent
;
240 for (i
= 0; i
< graft
->nr_parent
; i
++) {
241 new_parent
= lookup_commit(graft
->parent
[i
]);
244 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
248 item
->date
= parse_commit_date(bufptr
);
250 if (track_object_refs
) {
252 struct commit_list
*p
;
253 struct object_refs
*refs
= alloc_object_refs(n_refs
);
255 refs
->ref
[i
++] = &item
->tree
->object
;
256 for (p
= item
->parents
; p
; p
= p
->next
)
257 refs
->ref
[i
++] = &p
->item
->object
;
258 set_object_refs(&item
->object
, refs
);
264 int parse_commit(struct commit
*item
)
271 if (item
->object
.parsed
)
273 buffer
= read_sha1_file(item
->object
.sha1
, type
, &size
);
275 return error("Could not read %s",
276 sha1_to_hex(item
->object
.sha1
));
277 if (strcmp(type
, commit_type
)) {
279 return error("Object %s not a commit",
280 sha1_to_hex(item
->object
.sha1
));
282 ret
= parse_commit_buffer(item
, buffer
, size
);
283 if (save_commit_buffer
&& !ret
) {
284 item
->buffer
= buffer
;
291 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
293 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
294 new_list
->item
= item
;
295 new_list
->next
= *list_p
;
300 void free_commit_list(struct commit_list
*list
)
303 struct commit_list
*temp
= list
;
309 struct commit_list
* insert_by_date(struct commit
*item
, struct commit_list
**list
)
311 struct commit_list
**pp
= list
;
312 struct commit_list
*p
;
313 while ((p
= *pp
) != NULL
) {
314 if (p
->item
->date
< item
->date
) {
319 return commit_list_insert(item
, pp
);
323 void sort_by_date(struct commit_list
**list
)
325 struct commit_list
*ret
= NULL
;
327 insert_by_date((*list
)->item
, &ret
);
328 *list
= (*list
)->next
;
333 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
336 struct commit
*ret
= (*list
)->item
;
337 struct commit_list
*parents
= ret
->parents
;
338 struct commit_list
*old
= *list
;
340 *list
= (*list
)->next
;
344 struct commit
*commit
= parents
->item
;
345 parse_commit(commit
);
346 if (!(commit
->object
.flags
& mark
)) {
347 commit
->object
.flags
|= mark
;
348 insert_by_date(commit
, list
);
350 parents
= parents
->next
;
355 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
357 struct commit_list
*parents
;
359 parents
= commit
->parents
;
360 commit
->object
.flags
&= ~mark
;
362 struct commit
*parent
= parents
->item
;
363 if (parent
&& parent
->object
.parsed
&&
364 (parent
->object
.flags
& mark
))
365 clear_commit_marks(parent
, mark
);
366 parents
= parents
->next
;
371 * Generic support for pretty-printing the header
373 static int get_one_line(const char *msg
, unsigned long len
)
388 static int add_user_info(const char *what
, enum cmit_fmt fmt
, char *buf
, const char *line
)
394 const char *filler
= " ";
396 if (fmt
== CMIT_FMT_ONELINE
)
398 date
= strchr(line
, '>');
401 namelen
= ++date
- line
;
402 time
= strtoul(date
, &date
, 10);
403 tz
= strtol(date
, NULL
, 10);
405 ret
= sprintf(buf
, "%s: %.*s%.*s\n", what
,
406 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
407 filler
, namelen
, line
);
409 case CMIT_FMT_MEDIUM
:
410 ret
+= sprintf(buf
+ ret
, "Date: %s\n", show_date(time
, tz
));
412 case CMIT_FMT_FULLER
:
413 ret
+= sprintf(buf
+ ret
, "%sDate: %s\n", what
, show_date(time
, tz
));
422 static int is_empty_line(const char *line
, int len
)
424 while (len
&& isspace(line
[len
-1]))
429 static int add_merge_info(enum cmit_fmt fmt
, char *buf
, const struct commit
*commit
, int abbrev
)
431 struct commit_list
*parent
= commit
->parents
;
434 if ((fmt
== CMIT_FMT_ONELINE
) || !parent
|| !parent
->next
)
437 offset
= sprintf(buf
, "Merge:");
440 struct commit
*p
= parent
->item
;
441 const char *hex
= abbrev
442 ? find_unique_abbrev(p
->object
.sha1
, abbrev
)
443 : sha1_to_hex(p
->object
.sha1
);
444 char *dots
= (abbrev
&& strlen(hex
) != 40) ? "..." : "";
445 parent
= parent
->next
;
447 offset
+= sprintf(buf
+ offset
, " %s%s", hex
, dots
);
449 buf
[offset
++] = '\n';
453 unsigned long pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
, unsigned long len
, char *buf
, unsigned long space
, int abbrev
)
455 int hdr
= 1, body
= 0;
456 unsigned long offset
= 0;
457 int indent
= (fmt
== CMIT_FMT_ONELINE
) ? 0 : 4;
458 int parents_shown
= 0;
459 const char *msg
= commit
->buffer
;
462 const char *line
= msg
;
463 int linelen
= get_one_line(msg
, len
);
469 * We want some slop for indentation and a possible
470 * final "...". Thus the "+ 20".
472 if (offset
+ linelen
+ 20 > space
) {
473 memcpy(buf
+ offset
, " ...\n", 8);
483 if (fmt
!= CMIT_FMT_ONELINE
)
484 buf
[offset
++] = '\n';
487 if (fmt
== CMIT_FMT_RAW
) {
488 memcpy(buf
+ offset
, line
, linelen
);
492 if (!memcmp(line
, "parent ", 7)) {
494 die("bad parent line in commit");
498 if (!parents_shown
) {
499 offset
+= add_merge_info(fmt
, buf
+ offset
,
505 * MEDIUM == DEFAULT shows only author with dates.
506 * FULL shows both authors but not dates.
507 * FULLER shows both authors and dates.
509 if (!memcmp(line
, "author ", 7))
510 offset
+= add_user_info("Author", fmt
,
513 if (!memcmp(line
, "committer ", 10) &&
514 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
))
515 offset
+= add_user_info("Commit", fmt
,
521 if (is_empty_line(line
, linelen
)) {
524 if (fmt
== CMIT_FMT_SHORT
)
530 memset(buf
+ offset
, ' ', indent
);
531 memcpy(buf
+ offset
+ indent
, line
, linelen
);
532 offset
+= linelen
+ indent
;
533 if (fmt
== CMIT_FMT_ONELINE
)
536 if (fmt
== CMIT_FMT_ONELINE
) {
537 /* We do not want the terminating newline */
538 if (buf
[offset
- 1] == '\n')
542 /* Make sure there is an EOLN */
543 if (buf
[offset
- 1] != '\n')
544 buf
[offset
++] = '\n';
550 struct commit
*pop_commit(struct commit_list
**stack
)
552 struct commit_list
*top
= *stack
;
553 struct commit
*item
= top
? top
->item
: NULL
;
562 int count_parents(struct commit
* commit
)
565 struct commit_list
* parents
= commit
->parents
;
566 for (count
=0;parents
; parents
=parents
->next
,count
++)
571 void topo_sort_default_setter(struct commit
*c
, void *data
)
573 c
->object
.util
= data
;
576 void *topo_sort_default_getter(struct commit
*c
)
578 return c
->object
.util
;
582 * Performs an in-place topological sort on the list supplied.
584 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
586 sort_in_topological_order_fn(list
, lifo
, topo_sort_default_setter
,
587 topo_sort_default_getter
);
590 void sort_in_topological_order_fn(struct commit_list
** list
, int lifo
,
591 topo_sort_set_fn_t setter
,
592 topo_sort_get_fn_t getter
)
594 struct commit_list
* next
= *list
;
595 struct commit_list
* work
= NULL
, **insert
;
596 struct commit_list
** pptr
= list
;
597 struct sort_node
* nodes
;
598 struct sort_node
* next_nodes
;
601 /* determine the size of the list */
609 /* allocate an array to help sort the list */
610 nodes
= xcalloc(count
, sizeof(*nodes
));
611 /* link the list to the array */
615 next_nodes
->list_item
= next
;
616 setter(next
->item
, next_nodes
);
620 /* update the indegree */
623 struct commit_list
* parents
= next
->item
->parents
;
625 struct commit
* parent
=parents
->item
;
626 struct sort_node
* pn
= (struct sort_node
*) getter(parent
);
630 parents
=parents
->next
;
637 * tips are nodes not reachable from any other node in the list
639 * the tips serve as a starting set for the work queue.
644 struct sort_node
* node
= (struct sort_node
*) getter(next
->item
);
646 if (node
->indegree
== 0) {
647 insert
= &commit_list_insert(next
->item
, insert
)->next
;
652 /* process the list in topological order */
656 struct commit
* work_item
= pop_commit(&work
);
657 struct sort_node
* work_node
= (struct sort_node
*) getter(work_item
);
658 struct commit_list
* parents
= work_item
->parents
;
661 struct commit
* parent
=parents
->item
;
662 struct sort_node
* pn
= (struct sort_node
*) getter(parent
);
666 * parents are only enqueued for emission
667 * when all their children have been emitted thereby
668 * guaranteeing topological order.
673 insert_by_date(parent
, &work
);
675 commit_list_insert(parent
, &work
);
678 parents
=parents
->next
;
681 * work_item is a commit all of whose children
682 * have already been emitted. we can emit it now.
684 *pptr
= work_node
->list_item
;
685 pptr
= &(*pptr
)->next
;
687 setter(work_item
, NULL
);