6 int save_commit_buffer
= 1;
11 * the number of children of the associated commit
12 * that also occur in the list being sorted.
14 unsigned int indegree
;
17 * reference to original list item that we will re-use
20 struct commit_list
* list_item
;
24 const char *commit_type
= "commit";
31 { "raw", 1, CMIT_FMT_RAW
},
32 { "medium", 1, CMIT_FMT_MEDIUM
},
33 { "short", 1, CMIT_FMT_SHORT
},
34 { "email", 1, CMIT_FMT_EMAIL
},
35 { "full", 5, CMIT_FMT_FULL
},
36 { "fuller", 5, CMIT_FMT_FULLER
},
37 { "oneline", 1, CMIT_FMT_ONELINE
},
40 enum cmit_fmt
get_commit_format(const char *arg
)
45 return CMIT_FMT_DEFAULT
;
48 for (i
= 0; i
< ARRAY_SIZE(cmt_fmts
); i
++) {
49 if (!strncmp(arg
, cmt_fmts
[i
].n
, cmt_fmts
[i
].cmp_len
))
53 die("invalid --pretty format: %s", arg
);
56 static struct commit
*check_commit(struct object
*obj
,
57 const unsigned char *sha1
,
60 if (obj
->type
!= OBJ_COMMIT
) {
62 error("Object %s is a %s, not a commit",
63 sha1_to_hex(sha1
), typename(obj
->type
));
66 return (struct commit
*) obj
;
69 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
72 struct object
*obj
= deref_tag(parse_object(sha1
), NULL
, 0);
76 return check_commit(obj
, sha1
, quiet
);
79 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
81 return lookup_commit_reference_gently(sha1
, 0);
84 struct commit
*lookup_commit(const unsigned char *sha1
)
86 struct object
*obj
= lookup_object(sha1
);
88 struct commit
*ret
= alloc_commit_node();
89 created_object(sha1
, &ret
->object
);
90 ret
->object
.type
= OBJ_COMMIT
;
94 obj
->type
= OBJ_COMMIT
;
95 return check_commit(obj
, sha1
, 0);
98 static unsigned long parse_commit_date(const char *buf
)
102 if (memcmp(buf
, "author", 6))
104 while (*buf
++ != '\n')
106 if (memcmp(buf
, "committer", 9))
108 while (*buf
++ != '>')
110 date
= strtoul(buf
, NULL
, 10);
111 if (date
== ULONG_MAX
)
116 static struct commit_graft
**commit_graft
;
117 static int commit_graft_alloc
, commit_graft_nr
;
119 static int commit_graft_pos(const unsigned char *sha1
)
123 hi
= commit_graft_nr
;
125 int mi
= (lo
+ hi
) / 2;
126 struct commit_graft
*graft
= commit_graft
[mi
];
127 int cmp
= hashcmp(sha1
, graft
->sha1
);
138 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
140 int pos
= commit_graft_pos(graft
->sha1
);
146 free(commit_graft
[pos
]);
147 commit_graft
[pos
] = graft
;
152 if (commit_graft_alloc
<= ++commit_graft_nr
) {
153 commit_graft_alloc
= alloc_nr(commit_graft_alloc
);
154 commit_graft
= xrealloc(commit_graft
,
155 sizeof(*commit_graft
) *
158 if (pos
< commit_graft_nr
)
159 memmove(commit_graft
+ pos
+ 1,
161 (commit_graft_nr
- pos
- 1) *
162 sizeof(*commit_graft
));
163 commit_graft
[pos
] = graft
;
167 struct commit_graft
*read_graft_line(char *buf
, int len
)
169 /* The format is just "Commit Parent1 Parent2 ...\n" */
171 struct commit_graft
*graft
= NULL
;
173 if (buf
[len
-1] == '\n')
175 if (buf
[0] == '#' || buf
[0] == '\0')
177 if ((len
+ 1) % 41) {
179 error("bad graft data: %s", buf
);
183 i
= (len
+ 1) / 41 - 1;
184 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
185 graft
->nr_parent
= i
;
186 if (get_sha1_hex(buf
, graft
->sha1
))
188 for (i
= 40; i
< len
; i
+= 41) {
191 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
197 int read_graft_file(const char *graft_file
)
199 FILE *fp
= fopen(graft_file
, "r");
203 while (fgets(buf
, sizeof(buf
), fp
)) {
204 /* The format is just "Commit Parent1 Parent2 ...\n" */
205 int len
= strlen(buf
);
206 struct commit_graft
*graft
= read_graft_line(buf
, len
);
209 if (register_commit_graft(graft
, 1))
210 error("duplicate graft data: %s", buf
);
216 static void prepare_commit_graft(void)
218 static int commit_graft_prepared
;
221 if (commit_graft_prepared
)
223 graft_file
= get_graft_file();
224 read_graft_file(graft_file
);
225 /* make sure shallows are read */
226 is_repository_shallow();
227 commit_graft_prepared
= 1;
230 static struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
233 prepare_commit_graft();
234 pos
= commit_graft_pos(sha1
);
237 return commit_graft
[pos
];
240 int write_shallow_commits(int fd
, int use_pack_protocol
)
243 for (i
= 0; i
< commit_graft_nr
; i
++)
244 if (commit_graft
[i
]->nr_parent
< 0) {
246 sha1_to_hex(commit_graft
[i
]->sha1
);
248 if (use_pack_protocol
)
249 packet_write(fd
, "shallow %s", hex
);
258 int unregister_shallow(const unsigned char *sha1
)
260 int pos
= commit_graft_pos(sha1
);
263 if (pos
+ 1 < commit_graft_nr
)
264 memcpy(commit_graft
+ pos
, commit_graft
+ pos
+ 1,
265 sizeof(struct commit_graft
*)
266 * (commit_graft_nr
- pos
- 1));
271 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
274 char *bufptr
= buffer
;
275 unsigned char parent
[20];
276 struct commit_list
**pptr
;
277 struct commit_graft
*graft
;
280 if (item
->object
.parsed
)
282 item
->object
.parsed
= 1;
284 if (tail
<= bufptr
+ 5 || memcmp(bufptr
, "tree ", 5))
285 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
286 if (tail
<= bufptr
+ 45 || get_sha1_hex(bufptr
+ 5, parent
) < 0)
287 return error("bad tree pointer in commit %s",
288 sha1_to_hex(item
->object
.sha1
));
289 item
->tree
= lookup_tree(parent
);
292 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
293 pptr
= &item
->parents
;
295 graft
= lookup_commit_graft(item
->object
.sha1
);
296 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
297 struct commit
*new_parent
;
299 if (tail
<= bufptr
+ 48 ||
300 get_sha1_hex(bufptr
+ 7, parent
) ||
302 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
306 new_parent
= lookup_commit(parent
);
308 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
314 struct commit
*new_parent
;
315 for (i
= 0; i
< graft
->nr_parent
; i
++) {
316 new_parent
= lookup_commit(graft
->parent
[i
]);
319 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
323 item
->date
= parse_commit_date(bufptr
);
325 if (track_object_refs
) {
327 struct commit_list
*p
;
328 struct object_refs
*refs
= alloc_object_refs(n_refs
);
330 refs
->ref
[i
++] = &item
->tree
->object
;
331 for (p
= item
->parents
; p
; p
= p
->next
)
332 refs
->ref
[i
++] = &p
->item
->object
;
333 set_object_refs(&item
->object
, refs
);
339 int parse_commit(struct commit
*item
)
346 if (item
->object
.parsed
)
348 buffer
= read_sha1_file(item
->object
.sha1
, type
, &size
);
350 return error("Could not read %s",
351 sha1_to_hex(item
->object
.sha1
));
352 if (strcmp(type
, commit_type
)) {
354 return error("Object %s not a commit",
355 sha1_to_hex(item
->object
.sha1
));
357 ret
= parse_commit_buffer(item
, buffer
, size
);
358 if (save_commit_buffer
&& !ret
) {
359 item
->buffer
= buffer
;
366 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
368 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
369 new_list
->item
= item
;
370 new_list
->next
= *list_p
;
375 void free_commit_list(struct commit_list
*list
)
378 struct commit_list
*temp
= list
;
384 struct commit_list
* insert_by_date(struct commit
*item
, struct commit_list
**list
)
386 struct commit_list
**pp
= list
;
387 struct commit_list
*p
;
388 while ((p
= *pp
) != NULL
) {
389 if (p
->item
->date
< item
->date
) {
394 return commit_list_insert(item
, pp
);
398 void sort_by_date(struct commit_list
**list
)
400 struct commit_list
*ret
= NULL
;
402 insert_by_date((*list
)->item
, &ret
);
403 *list
= (*list
)->next
;
408 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
411 struct commit
*ret
= (*list
)->item
;
412 struct commit_list
*parents
= ret
->parents
;
413 struct commit_list
*old
= *list
;
415 *list
= (*list
)->next
;
419 struct commit
*commit
= parents
->item
;
420 parse_commit(commit
);
421 if (!(commit
->object
.flags
& mark
)) {
422 commit
->object
.flags
|= mark
;
423 insert_by_date(commit
, list
);
425 parents
= parents
->next
;
430 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
432 struct commit_list
*parents
;
434 commit
->object
.flags
&= ~mark
;
435 parents
= commit
->parents
;
437 struct commit
*parent
= parents
->item
;
439 /* Have we already cleared this? */
440 if (mark
& parent
->object
.flags
)
441 clear_commit_marks(parent
, mark
);
442 parents
= parents
->next
;
447 * Generic support for pretty-printing the header
449 static int get_one_line(const char *msg
, unsigned long len
)
464 static int is_rfc2047_special(char ch
)
466 return ((ch
& 0x80) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
469 static int add_rfc2047(char *buf
, const char *line
, int len
)
473 static const char q_utf8
[] = "=?utf-8?q?";
475 for (i
= needquote
= 0; !needquote
&& i
< len
; i
++) {
476 unsigned ch
= line
[i
];
480 (ch
== '=' && line
[i
+1] == '?'))
484 return sprintf(buf
, "%.*s", len
, line
);
486 memcpy(bp
, q_utf8
, sizeof(q_utf8
)-1);
487 bp
+= sizeof(q_utf8
)-1;
488 for (i
= 0; i
< len
; i
++) {
489 unsigned ch
= line
[i
] & 0xFF;
490 if (is_rfc2047_special(ch
)) {
491 sprintf(bp
, "=%02X", ch
);
504 static int add_user_info(const char *what
, enum cmit_fmt fmt
, char *buf
,
505 const char *line
, int relative_date
)
511 const char *filler
= " ";
513 if (fmt
== CMIT_FMT_ONELINE
)
515 date
= strchr(line
, '>');
518 namelen
= ++date
- line
;
519 time
= strtoul(date
, &date
, 10);
520 tz
= strtol(date
, NULL
, 10);
522 if (fmt
== CMIT_FMT_EMAIL
) {
523 char *name_tail
= strchr(line
, '<');
524 int display_name_length
;
527 while (line
< name_tail
&& isspace(name_tail
[-1]))
529 display_name_length
= name_tail
- line
;
531 strcpy(buf
, "From: ");
533 ret
+= add_rfc2047(buf
+ ret
, line
, display_name_length
);
534 memcpy(buf
+ ret
, name_tail
, namelen
- display_name_length
);
535 ret
+= namelen
- display_name_length
;
539 ret
= sprintf(buf
, "%s: %.*s%.*s\n", what
,
540 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
541 filler
, namelen
, line
);
544 case CMIT_FMT_MEDIUM
:
545 ret
+= sprintf(buf
+ ret
, "Date: %s\n",
546 show_date(time
, tz
, relative_date
));
549 ret
+= sprintf(buf
+ ret
, "Date: %s\n",
550 show_rfc2822_date(time
, tz
));
552 case CMIT_FMT_FULLER
:
553 ret
+= sprintf(buf
+ ret
, "%sDate: %s\n", what
,
554 show_date(time
, tz
, relative_date
));
563 static int is_empty_line(const char *line
, int *len_p
)
566 while (len
&& isspace(line
[len
-1]))
572 static int add_merge_info(enum cmit_fmt fmt
, char *buf
, const struct commit
*commit
, int abbrev
)
574 struct commit_list
*parent
= commit
->parents
;
577 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
578 !parent
|| !parent
->next
)
581 offset
= sprintf(buf
, "Merge:");
584 struct commit
*p
= parent
->item
;
585 const char *hex
= NULL
;
588 hex
= find_unique_abbrev(p
->object
.sha1
, abbrev
);
590 hex
= sha1_to_hex(p
->object
.sha1
);
591 dots
= (abbrev
&& strlen(hex
) != 40) ? "..." : "";
592 parent
= parent
->next
;
594 offset
+= sprintf(buf
+ offset
, " %s%s", hex
, dots
);
596 buf
[offset
++] = '\n';
600 unsigned long pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
,
601 unsigned long len
, char *buf
, unsigned long space
,
602 int abbrev
, const char *subject
,
603 const char *after_subject
, int relative_date
)
605 int hdr
= 1, body
= 0;
606 unsigned long offset
= 0;
608 int parents_shown
= 0;
609 const char *msg
= commit
->buffer
;
610 int plain_non_ascii
= 0;
612 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
615 /* After-subject is used to pass in Content-Type: multipart
616 * MIME header; in that case we do not have to do the
617 * plaintext content type even if the commit message has
618 * non 7-bit ASCII character. Otherwise, check if we need
619 * to say this is not a 7-bit ASCII.
621 if (fmt
== CMIT_FMT_EMAIL
&& !after_subject
) {
624 for (in_body
= i
= 0; (ch
= msg
[i
]) && i
< len
; i
++) {
626 /* author could be non 7-bit ASCII but
627 * the log may so; skip over the
631 i
+ 1 < len
&& msg
[i
+1] == '\n')
634 else if (ch
& 0x80) {
642 const char *line
= msg
;
643 int linelen
= get_one_line(msg
, len
);
649 * We want some slop for indentation and a possible
650 * final "...". Thus the "+ 20".
652 if (offset
+ linelen
+ 20 > space
) {
653 memcpy(buf
+ offset
, " ...\n", 8);
663 if ((fmt
!= CMIT_FMT_ONELINE
) && !subject
)
664 buf
[offset
++] = '\n';
667 if (fmt
== CMIT_FMT_RAW
) {
668 memcpy(buf
+ offset
, line
, linelen
);
672 if (!memcmp(line
, "parent ", 7)) {
674 die("bad parent line in commit");
678 if (!parents_shown
) {
679 offset
+= add_merge_info(fmt
, buf
+ offset
,
685 * MEDIUM == DEFAULT shows only author with dates.
686 * FULL shows both authors but not dates.
687 * FULLER shows both authors and dates.
689 if (!memcmp(line
, "author ", 7))
690 offset
+= add_user_info("Author", fmt
,
694 if (!memcmp(line
, "committer ", 10) &&
695 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
))
696 offset
+= add_user_info("Commit", fmt
,
706 if (is_empty_line(line
, &linelen
)) {
711 if (fmt
== CMIT_FMT_SHORT
)
716 int slen
= strlen(subject
);
717 memcpy(buf
+ offset
, subject
, slen
);
719 offset
+= add_rfc2047(buf
+ offset
, line
, linelen
);
722 memset(buf
+ offset
, ' ', indent
);
723 memcpy(buf
+ offset
+ indent
, line
, linelen
);
724 offset
+= linelen
+ indent
;
726 buf
[offset
++] = '\n';
727 if (fmt
== CMIT_FMT_ONELINE
)
729 if (subject
&& plain_non_ascii
) {
730 static const char header
[] =
731 "Content-Type: text/plain; charset=UTF-8\n"
732 "Content-Transfer-Encoding: 8bit\n";
733 memcpy(buf
+ offset
, header
, sizeof(header
)-1);
734 offset
+= sizeof(header
)-1;
737 int slen
= strlen(after_subject
);
738 if (slen
> space
- offset
- 1)
739 slen
= space
- offset
- 1;
740 memcpy(buf
+ offset
, after_subject
, slen
);
742 after_subject
= NULL
;
746 while (offset
&& isspace(buf
[offset
-1]))
748 /* Make sure there is an EOLN for the non-oneline case */
749 if (fmt
!= CMIT_FMT_ONELINE
)
750 buf
[offset
++] = '\n';
752 * make sure there is another EOLN to separate the headers from whatever
753 * body the caller appends if we haven't already written a body
755 if (fmt
== CMIT_FMT_EMAIL
&& !body
)
756 buf
[offset
++] = '\n';
761 struct commit
*pop_commit(struct commit_list
**stack
)
763 struct commit_list
*top
= *stack
;
764 struct commit
*item
= top
? top
->item
: NULL
;
773 int count_parents(struct commit
* commit
)
776 struct commit_list
* parents
= commit
->parents
;
777 for (count
= 0; parents
; parents
= parents
->next
,count
++)
782 void topo_sort_default_setter(struct commit
*c
, void *data
)
787 void *topo_sort_default_getter(struct commit
*c
)
793 * Performs an in-place topological sort on the list supplied.
795 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
797 sort_in_topological_order_fn(list
, lifo
, topo_sort_default_setter
,
798 topo_sort_default_getter
);
801 void sort_in_topological_order_fn(struct commit_list
** list
, int lifo
,
802 topo_sort_set_fn_t setter
,
803 topo_sort_get_fn_t getter
)
805 struct commit_list
* next
= *list
;
806 struct commit_list
* work
= NULL
, **insert
;
807 struct commit_list
** pptr
= list
;
808 struct sort_node
* nodes
;
809 struct sort_node
* next_nodes
;
812 /* determine the size of the list */
820 /* allocate an array to help sort the list */
821 nodes
= xcalloc(count
, sizeof(*nodes
));
822 /* link the list to the array */
826 next_nodes
->list_item
= next
;
827 setter(next
->item
, next_nodes
);
831 /* update the indegree */
834 struct commit_list
* parents
= next
->item
->parents
;
836 struct commit
* parent
=parents
->item
;
837 struct sort_node
* pn
= (struct sort_node
*) getter(parent
);
841 parents
=parents
->next
;
848 * tips are nodes not reachable from any other node in the list
850 * the tips serve as a starting set for the work queue.
855 struct sort_node
* node
= (struct sort_node
*) getter(next
->item
);
857 if (node
->indegree
== 0) {
858 insert
= &commit_list_insert(next
->item
, insert
)->next
;
863 /* process the list in topological order */
867 struct commit
* work_item
= pop_commit(&work
);
868 struct sort_node
* work_node
= (struct sort_node
*) getter(work_item
);
869 struct commit_list
* parents
= work_item
->parents
;
872 struct commit
* parent
=parents
->item
;
873 struct sort_node
* pn
= (struct sort_node
*) getter(parent
);
877 * parents are only enqueued for emission
878 * when all their children have been emitted thereby
879 * guaranteeing topological order.
884 insert_by_date(parent
, &work
);
886 commit_list_insert(parent
, &work
);
889 parents
=parents
->next
;
892 * work_item is a commit all of whose children
893 * have already been emitted. we can emit it now.
895 *pptr
= work_node
->list_item
;
896 pptr
= &(*pptr
)->next
;
898 setter(work_item
, NULL
);
903 /* merge-rebase stuff */
905 /* bits #0..15 in revision.h */
906 #define PARENT1 (1u<<16)
907 #define PARENT2 (1u<<17)
908 #define STALE (1u<<18)
909 #define RESULT (1u<<19)
911 static struct commit
*interesting(struct commit_list
*list
)
914 struct commit
*commit
= list
->item
;
916 if (commit
->object
.flags
& STALE
)
923 static struct commit_list
*merge_bases(struct commit
*one
, struct commit
*two
)
925 struct commit_list
*list
= NULL
;
926 struct commit_list
*result
= NULL
;
929 /* We do not mark this even with RESULT so we do not
930 * have to clean it up.
932 return commit_list_insert(one
, &result
);
937 one
->object
.flags
|= PARENT1
;
938 two
->object
.flags
|= PARENT2
;
939 insert_by_date(one
, &list
);
940 insert_by_date(two
, &list
);
942 while (interesting(list
)) {
943 struct commit
*commit
;
944 struct commit_list
*parents
;
945 struct commit_list
*n
;
953 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
954 if (flags
== (PARENT1
| PARENT2
)) {
955 if (!(commit
->object
.flags
& RESULT
)) {
956 commit
->object
.flags
|= RESULT
;
957 insert_by_date(commit
, &result
);
959 /* Mark parents of a found merge stale */
962 parents
= commit
->parents
;
964 struct commit
*p
= parents
->item
;
965 parents
= parents
->next
;
966 if ((p
->object
.flags
& flags
) == flags
)
969 p
->object
.flags
|= flags
;
970 insert_by_date(p
, &list
);
974 /* Clean up the result to remove stale ones */
975 list
= result
; result
= NULL
;
977 struct commit_list
*n
= list
->next
;
978 if (!(list
->item
->object
.flags
& STALE
))
979 insert_by_date(list
->item
, &result
);
986 struct commit_list
*get_merge_bases(struct commit
*one
,
990 const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
991 struct commit_list
*list
;
992 struct commit
**rslt
;
993 struct commit_list
*result
;
996 result
= merge_bases(one
, two
);
999 if (!result
|| !result
->next
) {
1001 clear_commit_marks(one
, all_flags
);
1002 clear_commit_marks(two
, all_flags
);
1007 /* There are more than one */
1014 rslt
= xcalloc(cnt
, sizeof(*rslt
));
1015 for (list
= result
, i
= 0; list
; list
= list
->next
)
1016 rslt
[i
++] = list
->item
;
1017 free_commit_list(result
);
1019 clear_commit_marks(one
, all_flags
);
1020 clear_commit_marks(two
, all_flags
);
1021 for (i
= 0; i
< cnt
- 1; i
++) {
1022 for (j
= i
+1; j
< cnt
; j
++) {
1023 if (!rslt
[i
] || !rslt
[j
])
1025 result
= merge_bases(rslt
[i
], rslt
[j
]);
1026 clear_commit_marks(rslt
[i
], all_flags
);
1027 clear_commit_marks(rslt
[j
], all_flags
);
1028 for (list
= result
; list
; list
= list
->next
) {
1029 if (rslt
[i
] == list
->item
)
1031 if (rslt
[j
] == list
->item
)
1037 /* Surviving ones in rslt[] are the independent results */
1039 for (i
= 0; i
< cnt
; i
++) {
1041 insert_by_date(rslt
[i
], &result
);
1047 int in_merge_bases(struct commit
*rev1
, struct commit
*rev2
)
1049 struct commit_list
*bases
, *b
;
1052 bases
= get_merge_bases(rev1
, rev2
, 1);
1053 for (b
= bases
; b
; b
= b
->next
) {
1054 if (!hashcmp(rev1
->object
.sha1
, b
->item
->object
.sha1
)) {
1060 free_commit_list(bases
);