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";
30 { "raw", 1, CMIT_FMT_RAW
},
31 { "medium", 1, CMIT_FMT_MEDIUM
},
32 { "short", 1, CMIT_FMT_SHORT
},
33 { "email", 1, CMIT_FMT_EMAIL
},
34 { "full", 5, CMIT_FMT_FULL
},
35 { "fuller", 5, CMIT_FMT_FULLER
},
36 { "oneline", 1, CMIT_FMT_ONELINE
},
39 enum cmit_fmt
get_commit_format(const char *arg
)
44 return CMIT_FMT_DEFAULT
;
47 for (i
= 0; i
< ARRAY_SIZE(cmt_fmts
); i
++) {
48 if (!strncmp(arg
, cmt_fmts
[i
].n
, cmt_fmts
[i
].cmp_len
))
52 die("invalid --pretty format: %s", arg
);
55 static struct commit
*check_commit(struct object
*obj
,
56 const unsigned char *sha1
,
59 if (obj
->type
!= commit_type
) {
61 error("Object %s is a %s, not a commit",
62 sha1_to_hex(sha1
), obj
->type
);
65 return (struct commit
*) obj
;
68 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
71 struct object
*obj
= deref_tag(parse_object(sha1
), NULL
, 0);
75 return check_commit(obj
, sha1
, quiet
);
78 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
80 return lookup_commit_reference_gently(sha1
, 0);
83 struct commit
*lookup_commit(const unsigned char *sha1
)
85 struct object
*obj
= lookup_object(sha1
);
87 struct commit
*ret
= xcalloc(1, sizeof(struct commit
));
88 created_object(sha1
, &ret
->object
);
89 ret
->object
.type
= commit_type
;
93 obj
->type
= commit_type
;
94 return check_commit(obj
, sha1
, 0);
97 static unsigned long parse_commit_date(const char *buf
)
101 if (memcmp(buf
, "author", 6))
103 while (*buf
++ != '\n')
105 if (memcmp(buf
, "committer", 9))
107 while (*buf
++ != '>')
109 date
= strtoul(buf
, NULL
, 10);
110 if (date
== ULONG_MAX
)
115 static struct commit_graft
**commit_graft
;
116 static int commit_graft_alloc
, commit_graft_nr
;
118 static int commit_graft_pos(const unsigned char *sha1
)
122 hi
= commit_graft_nr
;
124 int mi
= (lo
+ hi
) / 2;
125 struct commit_graft
*graft
= commit_graft
[mi
];
126 int cmp
= memcmp(sha1
, graft
->sha1
, 20);
137 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
139 int pos
= commit_graft_pos(graft
->sha1
);
145 free(commit_graft
[pos
]);
146 commit_graft
[pos
] = graft
;
151 if (commit_graft_alloc
<= ++commit_graft_nr
) {
152 commit_graft_alloc
= alloc_nr(commit_graft_alloc
);
153 commit_graft
= xrealloc(commit_graft
,
154 sizeof(*commit_graft
) *
157 if (pos
< commit_graft_nr
)
158 memmove(commit_graft
+ pos
+ 1,
160 (commit_graft_nr
- pos
- 1) *
161 sizeof(*commit_graft
));
162 commit_graft
[pos
] = graft
;
166 struct commit_graft
*read_graft_line(char *buf
, int len
)
168 /* The format is just "Commit Parent1 Parent2 ...\n" */
170 struct commit_graft
*graft
= NULL
;
172 if (buf
[len
-1] == '\n')
174 if (buf
[0] == '#' || buf
[0] == '\0')
176 if ((len
+ 1) % 41) {
178 error("bad graft data: %s", buf
);
182 i
= (len
+ 1) / 41 - 1;
183 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
184 graft
->nr_parent
= i
;
185 if (get_sha1_hex(buf
, graft
->sha1
))
187 for (i
= 40; i
< len
; i
+= 41) {
190 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
196 int read_graft_file(const char *graft_file
)
198 FILE *fp
= fopen(graft_file
, "r");
202 while (fgets(buf
, sizeof(buf
), fp
)) {
203 /* The format is just "Commit Parent1 Parent2 ...\n" */
204 int len
= strlen(buf
);
205 struct commit_graft
*graft
= read_graft_line(buf
, len
);
208 if (register_commit_graft(graft
, 1))
209 error("duplicate graft data: %s", buf
);
215 static void prepare_commit_graft(void)
217 static int commit_graft_prepared
;
220 if (commit_graft_prepared
)
222 graft_file
= get_graft_file();
223 read_graft_file(graft_file
);
224 commit_graft_prepared
= 1;
227 static struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
230 prepare_commit_graft();
231 pos
= commit_graft_pos(sha1
);
234 return commit_graft
[pos
];
237 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
239 char *bufptr
= buffer
;
240 unsigned char parent
[20];
241 struct commit_list
**pptr
;
242 struct commit_graft
*graft
;
245 if (item
->object
.parsed
)
247 item
->object
.parsed
= 1;
248 if (memcmp(bufptr
, "tree ", 5))
249 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
250 if (get_sha1_hex(bufptr
+ 5, parent
) < 0)
251 return error("bad tree pointer in commit %s",
252 sha1_to_hex(item
->object
.sha1
));
253 item
->tree
= lookup_tree(parent
);
256 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
257 pptr
= &item
->parents
;
259 graft
= lookup_commit_graft(item
->object
.sha1
);
260 while (!memcmp(bufptr
, "parent ", 7)) {
261 struct commit
*new_parent
;
263 if (get_sha1_hex(bufptr
+ 7, parent
) || bufptr
[47] != '\n')
264 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
268 new_parent
= lookup_commit(parent
);
270 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
276 struct commit
*new_parent
;
277 for (i
= 0; i
< graft
->nr_parent
; i
++) {
278 new_parent
= lookup_commit(graft
->parent
[i
]);
281 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
285 item
->date
= parse_commit_date(bufptr
);
287 if (track_object_refs
) {
289 struct commit_list
*p
;
290 struct object_refs
*refs
= alloc_object_refs(n_refs
);
292 refs
->ref
[i
++] = &item
->tree
->object
;
293 for (p
= item
->parents
; p
; p
= p
->next
)
294 refs
->ref
[i
++] = &p
->item
->object
;
295 set_object_refs(&item
->object
, refs
);
301 int parse_commit(struct commit
*item
)
308 if (item
->object
.parsed
)
310 buffer
= read_sha1_file(item
->object
.sha1
, type
, &size
);
312 return error("Could not read %s",
313 sha1_to_hex(item
->object
.sha1
));
314 if (strcmp(type
, commit_type
)) {
316 return error("Object %s not a commit",
317 sha1_to_hex(item
->object
.sha1
));
319 ret
= parse_commit_buffer(item
, buffer
, size
);
320 if (save_commit_buffer
&& !ret
) {
321 item
->buffer
= buffer
;
328 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
330 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
331 new_list
->item
= item
;
332 new_list
->next
= *list_p
;
337 void free_commit_list(struct commit_list
*list
)
340 struct commit_list
*temp
= list
;
346 struct commit_list
* insert_by_date(struct commit
*item
, struct commit_list
**list
)
348 struct commit_list
**pp
= list
;
349 struct commit_list
*p
;
350 while ((p
= *pp
) != NULL
) {
351 if (p
->item
->date
< item
->date
) {
356 return commit_list_insert(item
, pp
);
360 void sort_by_date(struct commit_list
**list
)
362 struct commit_list
*ret
= NULL
;
364 insert_by_date((*list
)->item
, &ret
);
365 *list
= (*list
)->next
;
370 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
373 struct commit
*ret
= (*list
)->item
;
374 struct commit_list
*parents
= ret
->parents
;
375 struct commit_list
*old
= *list
;
377 *list
= (*list
)->next
;
381 struct commit
*commit
= parents
->item
;
382 parse_commit(commit
);
383 if (!(commit
->object
.flags
& mark
)) {
384 commit
->object
.flags
|= mark
;
385 insert_by_date(commit
, list
);
387 parents
= parents
->next
;
392 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
394 struct commit_list
*parents
;
396 parents
= commit
->parents
;
397 commit
->object
.flags
&= ~mark
;
399 struct commit
*parent
= parents
->item
;
400 if (parent
&& parent
->object
.parsed
&&
401 (parent
->object
.flags
& mark
))
402 clear_commit_marks(parent
, mark
);
403 parents
= parents
->next
;
408 * Generic support for pretty-printing the header
410 static int get_one_line(const char *msg
, unsigned long len
)
425 static int add_user_info(const char *what
, enum cmit_fmt fmt
, char *buf
, const char *line
)
431 const char *filler
= " ";
433 if (fmt
== CMIT_FMT_ONELINE
)
435 date
= strchr(line
, '>');
438 namelen
= ++date
- line
;
439 time
= strtoul(date
, &date
, 10);
440 tz
= strtol(date
, NULL
, 10);
442 if (fmt
== CMIT_FMT_EMAIL
) {
446 ret
= sprintf(buf
, "%s: %.*s%.*s\n", what
,
447 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
448 filler
, namelen
, line
);
450 case CMIT_FMT_MEDIUM
:
451 ret
+= sprintf(buf
+ ret
, "Date: %s\n", show_date(time
, tz
));
454 ret
+= sprintf(buf
+ ret
, "Date: %s\n",
455 show_rfc2822_date(time
, tz
));
457 case CMIT_FMT_FULLER
:
458 ret
+= sprintf(buf
+ ret
, "%sDate: %s\n", what
, show_date(time
, tz
));
467 static int is_empty_line(const char *line
, int *len_p
)
470 while (len
&& isspace(line
[len
-1]))
476 static int add_merge_info(enum cmit_fmt fmt
, char *buf
, const struct commit
*commit
, int abbrev
)
478 struct commit_list
*parent
= commit
->parents
;
481 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
482 !parent
|| !parent
->next
)
485 offset
= sprintf(buf
, "Merge:");
488 struct commit
*p
= parent
->item
;
489 const char *hex
= abbrev
490 ? find_unique_abbrev(p
->object
.sha1
, abbrev
)
491 : sha1_to_hex(p
->object
.sha1
);
492 char *dots
= (abbrev
&& strlen(hex
) != 40) ? "..." : "";
493 parent
= parent
->next
;
495 offset
+= sprintf(buf
+ offset
, " %s%s", hex
, dots
);
497 buf
[offset
++] = '\n';
501 unsigned long pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
, unsigned long len
, char *buf
, unsigned long space
, int abbrev
, const char *subject
, const char *after_subject
)
503 int hdr
= 1, body
= 0;
504 unsigned long offset
= 0;
506 int parents_shown
= 0;
507 const char *msg
= commit
->buffer
;
509 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
513 const char *line
= msg
;
514 int linelen
= get_one_line(msg
, len
);
520 * We want some slop for indentation and a possible
521 * final "...". Thus the "+ 20".
523 if (offset
+ linelen
+ 20 > space
) {
524 memcpy(buf
+ offset
, " ...\n", 8);
534 if ((fmt
!= CMIT_FMT_ONELINE
) && !subject
)
535 buf
[offset
++] = '\n';
538 if (fmt
== CMIT_FMT_RAW
) {
539 memcpy(buf
+ offset
, line
, linelen
);
543 if (!memcmp(line
, "parent ", 7)) {
545 die("bad parent line in commit");
549 if (!parents_shown
) {
550 offset
+= add_merge_info(fmt
, buf
+ offset
,
556 * MEDIUM == DEFAULT shows only author with dates.
557 * FULL shows both authors but not dates.
558 * FULLER shows both authors and dates.
560 if (!memcmp(line
, "author ", 7))
561 offset
+= add_user_info("Author", fmt
,
564 if (!memcmp(line
, "committer ", 10) &&
565 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
))
566 offset
+= add_user_info("Commit", fmt
,
572 if (is_empty_line(line
, &linelen
)) {
577 if (fmt
== CMIT_FMT_SHORT
)
584 int slen
= strlen(subject
);
585 memcpy(buf
+ offset
, subject
, slen
);
588 memset(buf
+ offset
, ' ', indent
);
589 memcpy(buf
+ offset
+ indent
, line
, linelen
);
590 offset
+= linelen
+ indent
;
591 buf
[offset
++] = '\n';
592 if (fmt
== CMIT_FMT_ONELINE
)
595 int slen
= strlen(after_subject
);
596 if (slen
> space
- offset
- 1)
597 slen
= space
- offset
- 1;
598 memcpy(buf
+ offset
, after_subject
, slen
);
600 after_subject
= NULL
;
604 while (offset
&& isspace(buf
[offset
-1]))
606 /* Make sure there is an EOLN for the non-oneline case */
607 if (fmt
!= CMIT_FMT_ONELINE
)
608 buf
[offset
++] = '\n';
613 struct commit
*pop_commit(struct commit_list
**stack
)
615 struct commit_list
*top
= *stack
;
616 struct commit
*item
= top
? top
->item
: NULL
;
625 int count_parents(struct commit
* commit
)
628 struct commit_list
* parents
= commit
->parents
;
629 for (count
=0;parents
; parents
=parents
->next
,count
++)
634 void topo_sort_default_setter(struct commit
*c
, void *data
)
636 c
->object
.util
= data
;
639 void *topo_sort_default_getter(struct commit
*c
)
641 return c
->object
.util
;
645 * Performs an in-place topological sort on the list supplied.
647 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
649 sort_in_topological_order_fn(list
, lifo
, topo_sort_default_setter
,
650 topo_sort_default_getter
);
653 void sort_in_topological_order_fn(struct commit_list
** list
, int lifo
,
654 topo_sort_set_fn_t setter
,
655 topo_sort_get_fn_t getter
)
657 struct commit_list
* next
= *list
;
658 struct commit_list
* work
= NULL
, **insert
;
659 struct commit_list
** pptr
= list
;
660 struct sort_node
* nodes
;
661 struct sort_node
* next_nodes
;
664 /* determine the size of the list */
672 /* allocate an array to help sort the list */
673 nodes
= xcalloc(count
, sizeof(*nodes
));
674 /* link the list to the array */
678 next_nodes
->list_item
= next
;
679 setter(next
->item
, next_nodes
);
683 /* update the indegree */
686 struct commit_list
* parents
= next
->item
->parents
;
688 struct commit
* parent
=parents
->item
;
689 struct sort_node
* pn
= (struct sort_node
*) getter(parent
);
693 parents
=parents
->next
;
700 * tips are nodes not reachable from any other node in the list
702 * the tips serve as a starting set for the work queue.
707 struct sort_node
* node
= (struct sort_node
*) getter(next
->item
);
709 if (node
->indegree
== 0) {
710 insert
= &commit_list_insert(next
->item
, insert
)->next
;
715 /* process the list in topological order */
719 struct commit
* work_item
= pop_commit(&work
);
720 struct sort_node
* work_node
= (struct sort_node
*) getter(work_item
);
721 struct commit_list
* parents
= work_item
->parents
;
724 struct commit
* parent
=parents
->item
;
725 struct sort_node
* pn
= (struct sort_node
*) getter(parent
);
729 * parents are only enqueued for emission
730 * when all their children have been emitted thereby
731 * guaranteeing topological order.
736 insert_by_date(parent
, &work
);
738 commit_list_insert(parent
, &work
);
741 parents
=parents
->next
;
744 * work_item is a commit all of whose children
745 * have already been emitted. we can emit it now.
747 *pptr
= work_node
->list_item
;
748 pptr
= &(*pptr
)->next
;
750 setter(work_item
, NULL
);