6 #include "interpolate.h"
10 int save_commit_buffer
= 1;
15 * the number of children of the associated commit
16 * that also occur in the list being sorted.
18 unsigned int indegree
;
21 * reference to original list item that we will re-use
24 struct commit_list
* list_item
;
28 const char *commit_type
= "commit";
35 { "raw", 1, CMIT_FMT_RAW
},
36 { "medium", 1, CMIT_FMT_MEDIUM
},
37 { "short", 1, CMIT_FMT_SHORT
},
38 { "email", 1, CMIT_FMT_EMAIL
},
39 { "full", 5, CMIT_FMT_FULL
},
40 { "fuller", 5, CMIT_FMT_FULLER
},
41 { "oneline", 1, CMIT_FMT_ONELINE
},
42 { "format:", 7, CMIT_FMT_USERFORMAT
},
45 static char *user_format
;
47 enum cmit_fmt
get_commit_format(const char *arg
)
52 return CMIT_FMT_DEFAULT
;
55 if (!prefixcmp(arg
, "format:")) {
58 user_format
= xstrdup(arg
+ 7);
59 return CMIT_FMT_USERFORMAT
;
61 for (i
= 0; i
< ARRAY_SIZE(cmt_fmts
); i
++) {
62 if (!strncmp(arg
, cmt_fmts
[i
].n
, cmt_fmts
[i
].cmp_len
) &&
63 !strncmp(arg
, cmt_fmts
[i
].n
, strlen(arg
)))
67 die("invalid --pretty format: %s", arg
);
70 static struct commit
*check_commit(struct object
*obj
,
71 const unsigned char *sha1
,
74 if (obj
->type
!= OBJ_COMMIT
) {
76 error("Object %s is a %s, not a commit",
77 sha1_to_hex(sha1
), typename(obj
->type
));
80 return (struct commit
*) obj
;
83 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
86 struct object
*obj
= deref_tag(parse_object(sha1
), NULL
, 0);
90 return check_commit(obj
, sha1
, quiet
);
93 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
95 return lookup_commit_reference_gently(sha1
, 0);
98 struct commit
*lookup_commit(const unsigned char *sha1
)
100 struct object
*obj
= lookup_object(sha1
);
102 struct commit
*ret
= alloc_commit_node();
103 created_object(sha1
, &ret
->object
);
104 ret
->object
.type
= OBJ_COMMIT
;
108 obj
->type
= OBJ_COMMIT
;
109 return check_commit(obj
, sha1
, 0);
112 static unsigned long parse_commit_date(const char *buf
)
116 if (memcmp(buf
, "author", 6))
118 while (*buf
++ != '\n')
120 if (memcmp(buf
, "committer", 9))
122 while (*buf
++ != '>')
124 date
= strtoul(buf
, NULL
, 10);
125 if (date
== ULONG_MAX
)
130 static struct commit_graft
**commit_graft
;
131 static int commit_graft_alloc
, commit_graft_nr
;
133 static int commit_graft_pos(const unsigned char *sha1
)
137 hi
= commit_graft_nr
;
139 int mi
= (lo
+ hi
) / 2;
140 struct commit_graft
*graft
= commit_graft
[mi
];
141 int cmp
= hashcmp(sha1
, graft
->sha1
);
152 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
154 int pos
= commit_graft_pos(graft
->sha1
);
160 free(commit_graft
[pos
]);
161 commit_graft
[pos
] = graft
;
166 if (commit_graft_alloc
<= ++commit_graft_nr
) {
167 commit_graft_alloc
= alloc_nr(commit_graft_alloc
);
168 commit_graft
= xrealloc(commit_graft
,
169 sizeof(*commit_graft
) *
172 if (pos
< commit_graft_nr
)
173 memmove(commit_graft
+ pos
+ 1,
175 (commit_graft_nr
- pos
- 1) *
176 sizeof(*commit_graft
));
177 commit_graft
[pos
] = graft
;
181 struct commit_graft
*read_graft_line(char *buf
, int len
)
183 /* The format is just "Commit Parent1 Parent2 ...\n" */
185 struct commit_graft
*graft
= NULL
;
187 if (buf
[len
-1] == '\n')
189 if (buf
[0] == '#' || buf
[0] == '\0')
191 if ((len
+ 1) % 41) {
193 error("bad graft data: %s", buf
);
197 i
= (len
+ 1) / 41 - 1;
198 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
199 graft
->nr_parent
= i
;
200 if (get_sha1_hex(buf
, graft
->sha1
))
202 for (i
= 40; i
< len
; i
+= 41) {
205 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
211 int read_graft_file(const char *graft_file
)
213 FILE *fp
= fopen(graft_file
, "r");
217 while (fgets(buf
, sizeof(buf
), fp
)) {
218 /* The format is just "Commit Parent1 Parent2 ...\n" */
219 int len
= strlen(buf
);
220 struct commit_graft
*graft
= read_graft_line(buf
, len
);
223 if (register_commit_graft(graft
, 1))
224 error("duplicate graft data: %s", buf
);
230 static void prepare_commit_graft(void)
232 static int commit_graft_prepared
;
235 if (commit_graft_prepared
)
237 graft_file
= get_graft_file();
238 read_graft_file(graft_file
);
239 /* make sure shallows are read */
240 is_repository_shallow();
241 commit_graft_prepared
= 1;
244 static struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
247 prepare_commit_graft();
248 pos
= commit_graft_pos(sha1
);
251 return commit_graft
[pos
];
254 int write_shallow_commits(int fd
, int use_pack_protocol
)
257 for (i
= 0; i
< commit_graft_nr
; i
++)
258 if (commit_graft
[i
]->nr_parent
< 0) {
260 sha1_to_hex(commit_graft
[i
]->sha1
);
262 if (use_pack_protocol
)
263 packet_write(fd
, "shallow %s", hex
);
265 if (write_in_full(fd
, hex
, 40) != 40)
267 if (write_in_full(fd
, "\n", 1) != 1)
274 int unregister_shallow(const unsigned char *sha1
)
276 int pos
= commit_graft_pos(sha1
);
279 if (pos
+ 1 < commit_graft_nr
)
280 memcpy(commit_graft
+ pos
, commit_graft
+ pos
+ 1,
281 sizeof(struct commit_graft
*)
282 * (commit_graft_nr
- pos
- 1));
287 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
290 char *bufptr
= buffer
;
291 unsigned char parent
[20];
292 struct commit_list
**pptr
;
293 struct commit_graft
*graft
;
296 if (item
->object
.parsed
)
298 item
->object
.parsed
= 1;
300 if (tail
<= bufptr
+ 5 || memcmp(bufptr
, "tree ", 5))
301 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
302 if (tail
<= bufptr
+ 45 || get_sha1_hex(bufptr
+ 5, parent
) < 0)
303 return error("bad tree pointer in commit %s",
304 sha1_to_hex(item
->object
.sha1
));
305 item
->tree
= lookup_tree(parent
);
308 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
309 pptr
= &item
->parents
;
311 graft
= lookup_commit_graft(item
->object
.sha1
);
312 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
313 struct commit
*new_parent
;
315 if (tail
<= bufptr
+ 48 ||
316 get_sha1_hex(bufptr
+ 7, parent
) ||
318 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
322 new_parent
= lookup_commit(parent
);
324 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
330 struct commit
*new_parent
;
331 for (i
= 0; i
< graft
->nr_parent
; i
++) {
332 new_parent
= lookup_commit(graft
->parent
[i
]);
335 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
339 item
->date
= parse_commit_date(bufptr
);
341 if (track_object_refs
) {
343 struct commit_list
*p
;
344 struct object_refs
*refs
= alloc_object_refs(n_refs
);
346 refs
->ref
[i
++] = &item
->tree
->object
;
347 for (p
= item
->parents
; p
; p
= p
->next
)
348 refs
->ref
[i
++] = &p
->item
->object
;
349 set_object_refs(&item
->object
, refs
);
355 int parse_commit(struct commit
*item
)
357 enum object_type type
;
362 if (item
->object
.parsed
)
364 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
366 return error("Could not read %s",
367 sha1_to_hex(item
->object
.sha1
));
368 if (type
!= OBJ_COMMIT
) {
370 return error("Object %s not a commit",
371 sha1_to_hex(item
->object
.sha1
));
373 ret
= parse_commit_buffer(item
, buffer
, size
);
374 if (save_commit_buffer
&& !ret
) {
375 item
->buffer
= buffer
;
382 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
384 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
385 new_list
->item
= item
;
386 new_list
->next
= *list_p
;
391 void free_commit_list(struct commit_list
*list
)
394 struct commit_list
*temp
= list
;
400 struct commit_list
* insert_by_date(struct commit
*item
, struct commit_list
**list
)
402 struct commit_list
**pp
= list
;
403 struct commit_list
*p
;
404 while ((p
= *pp
) != NULL
) {
405 if (p
->item
->date
< item
->date
) {
410 return commit_list_insert(item
, pp
);
414 void sort_by_date(struct commit_list
**list
)
416 struct commit_list
*ret
= NULL
;
418 insert_by_date((*list
)->item
, &ret
);
419 *list
= (*list
)->next
;
424 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
427 struct commit
*ret
= (*list
)->item
;
428 struct commit_list
*parents
= ret
->parents
;
429 struct commit_list
*old
= *list
;
431 *list
= (*list
)->next
;
435 struct commit
*commit
= parents
->item
;
436 parse_commit(commit
);
437 if (!(commit
->object
.flags
& mark
)) {
438 commit
->object
.flags
|= mark
;
439 insert_by_date(commit
, list
);
441 parents
= parents
->next
;
446 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
448 struct commit_list
*parents
;
450 commit
->object
.flags
&= ~mark
;
451 parents
= commit
->parents
;
453 struct commit
*parent
= parents
->item
;
455 /* Have we already cleared this? */
456 if (mark
& parent
->object
.flags
)
457 clear_commit_marks(parent
, mark
);
458 parents
= parents
->next
;
463 * Generic support for pretty-printing the header
465 static int get_one_line(const char *msg
, unsigned long len
)
480 /* High bit set, or ISO-2022-INT */
481 static int non_ascii(int ch
)
484 return ((ch
& 0x80) || (ch
== 0x1b));
487 static int is_rfc2047_special(char ch
)
489 return (non_ascii(ch
) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
492 static int add_rfc2047(char *buf
, const char *line
, int len
,
493 const char *encoding
)
497 char q_encoding
[128];
498 const char *q_encoding_fmt
= "=?%s?q?";
500 for (i
= needquote
= 0; !needquote
&& i
< len
; i
++) {
505 (ch
== '=' && line
[i
+1] == '?'))
509 return sprintf(buf
, "%.*s", len
, line
);
511 i
= snprintf(q_encoding
, sizeof(q_encoding
), q_encoding_fmt
, encoding
);
512 if (sizeof(q_encoding
) < i
)
513 die("Insanely long encoding name %s", encoding
);
514 memcpy(bp
, q_encoding
, i
);
516 for (i
= 0; i
< len
; i
++) {
517 unsigned ch
= line
[i
] & 0xFF;
518 if (is_rfc2047_special(ch
)) {
519 sprintf(bp
, "=%02X", ch
);
532 static int add_user_info(const char *what
, enum cmit_fmt fmt
, char *buf
,
533 const char *line
, int relative_date
,
534 const char *encoding
)
540 const char *filler
= " ";
542 if (fmt
== CMIT_FMT_ONELINE
)
544 date
= strchr(line
, '>');
547 namelen
= ++date
- line
;
548 time
= strtoul(date
, &date
, 10);
549 tz
= strtol(date
, NULL
, 10);
551 if (fmt
== CMIT_FMT_EMAIL
) {
552 char *name_tail
= strchr(line
, '<');
553 int display_name_length
;
556 while (line
< name_tail
&& isspace(name_tail
[-1]))
558 display_name_length
= name_tail
- line
;
560 strcpy(buf
, "From: ");
562 ret
+= add_rfc2047(buf
+ ret
, line
, display_name_length
,
564 memcpy(buf
+ ret
, name_tail
, namelen
- display_name_length
);
565 ret
+= namelen
- display_name_length
;
569 ret
= sprintf(buf
, "%s: %.*s%.*s\n", what
,
570 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
571 filler
, namelen
, line
);
574 case CMIT_FMT_MEDIUM
:
575 ret
+= sprintf(buf
+ ret
, "Date: %s\n",
576 show_date(time
, tz
, relative_date
));
579 ret
+= sprintf(buf
+ ret
, "Date: %s\n",
580 show_rfc2822_date(time
, tz
));
582 case CMIT_FMT_FULLER
:
583 ret
+= sprintf(buf
+ ret
, "%sDate: %s\n", what
,
584 show_date(time
, tz
, relative_date
));
593 static int is_empty_line(const char *line
, int *len_p
)
596 while (len
&& isspace(line
[len
-1]))
602 static int add_merge_info(enum cmit_fmt fmt
, char *buf
, const struct commit
*commit
, int abbrev
)
604 struct commit_list
*parent
= commit
->parents
;
607 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
608 !parent
|| !parent
->next
)
611 offset
= sprintf(buf
, "Merge:");
614 struct commit
*p
= parent
->item
;
615 const char *hex
= NULL
;
618 hex
= find_unique_abbrev(p
->object
.sha1
, abbrev
);
620 hex
= sha1_to_hex(p
->object
.sha1
);
621 dots
= (abbrev
&& strlen(hex
) != 40) ? "..." : "";
622 parent
= parent
->next
;
624 offset
+= sprintf(buf
+ offset
, " %s%s", hex
, dots
);
626 buf
[offset
++] = '\n';
630 static char *get_header(const struct commit
*commit
, const char *key
)
632 int key_len
= strlen(key
);
633 const char *line
= commit
->buffer
;
636 const char *eol
= strchr(line
, '\n'), *next
;
641 eol
= line
+ strlen(line
);
645 if (!strncmp(line
, key
, key_len
) && line
[key_len
] == ' ') {
646 int len
= eol
- line
- key_len
;
647 char *ret
= xmalloc(len
);
648 memcpy(ret
, line
+ key_len
+ 1, len
- 1);
656 static char *replace_encoding_header(char *buf
, const char *encoding
)
658 char *encoding_header
= strstr(buf
, "\nencoding ");
659 char *header_end
= strstr(buf
, "\n\n");
660 char *end_of_encoding_header
;
661 int encoding_header_pos
;
662 int encoding_header_len
;
665 int buflen
= strlen(buf
) + 1;
668 header_end
= buf
+ buflen
;
669 if (!encoding_header
|| encoding_header
>= header_end
)
672 end_of_encoding_header
= strchr(encoding_header
, '\n');
673 if (!end_of_encoding_header
)
674 return buf
; /* should not happen but be defensive */
675 end_of_encoding_header
++;
677 encoding_header_len
= end_of_encoding_header
- encoding_header
;
678 encoding_header_pos
= encoding_header
- buf
;
680 if (is_encoding_utf8(encoding
)) {
681 /* we have re-coded to UTF-8; drop the header */
682 memmove(encoding_header
, end_of_encoding_header
,
683 buflen
- (encoding_header_pos
+ encoding_header_len
));
686 new_len
= strlen(encoding
);
687 need_len
= new_len
+ strlen("encoding \n");
688 if (encoding_header_len
< need_len
) {
689 buf
= xrealloc(buf
, buflen
+ (need_len
- encoding_header_len
));
690 encoding_header
= buf
+ encoding_header_pos
;
691 end_of_encoding_header
= encoding_header
+ encoding_header_len
;
693 memmove(end_of_encoding_header
+ (need_len
- encoding_header_len
),
694 end_of_encoding_header
,
695 buflen
- (encoding_header_pos
+ encoding_header_len
));
696 memcpy(encoding_header
+ 9, encoding
, strlen(encoding
));
697 encoding_header
[9 + new_len
] = '\n';
701 static char *logmsg_reencode(const struct commit
*commit
,
702 const char *output_encoding
)
704 static const char *utf8
= "utf-8";
705 const char *use_encoding
;
709 if (!*output_encoding
)
711 encoding
= get_header(commit
, "encoding");
712 use_encoding
= encoding
? encoding
: utf8
;
713 if (!strcmp(use_encoding
, output_encoding
))
714 out
= xstrdup(commit
->buffer
);
716 out
= reencode_string(commit
->buffer
,
717 output_encoding
, use_encoding
);
719 out
= replace_encoding_header(out
, output_encoding
);
725 static char *xstrndup(const char *text
, int len
)
727 char *result
= xmalloc(len
+ 1);
728 memcpy(result
, text
, len
);
733 static void fill_person(struct interp
*table
, const char *msg
, int len
)
735 int start
, end
, tz
= 0;
740 for (end
= 0; end
< len
&& msg
[end
] != '<'; end
++)
743 while (end
> 0 && isspace(msg
[end
- 1]))
745 table
[0].value
= xstrndup(msg
, end
);
751 for (end
= start
+ 1; end
< len
&& msg
[end
] != '>'; end
++)
757 table
[1].value
= xstrndup(msg
+ start
, end
- start
);
760 for (start
= end
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
764 date
= strtoul(msg
+ start
, &ep
, 10);
765 if (msg
+ start
== ep
)
768 table
[5].value
= xstrndup(msg
+ start
, ep
- (msg
+ start
));
771 for (start
= ep
- msg
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
773 if (start
+ 1 < len
) {
774 tz
= strtoul(msg
+ start
+ 1, NULL
, 10);
775 if (msg
[start
] == '-')
779 interp_set_entry(table
, 2, show_date(date
, tz
, 0));
780 interp_set_entry(table
, 3, show_rfc2822_date(date
, tz
));
781 interp_set_entry(table
, 4, show_date(date
, tz
, 1));
784 static long format_commit_message(const struct commit
*commit
,
785 const char *msg
, char *buf
, unsigned long space
)
787 struct interp table
[] = {
788 { "%H" }, /* commit hash */
789 { "%h" }, /* abbreviated commit hash */
790 { "%T" }, /* tree hash */
791 { "%t" }, /* abbreviated tree hash */
792 { "%P" }, /* parent hashes */
793 { "%p" }, /* abbreviated parent hashes */
794 { "%an" }, /* author name */
795 { "%ae" }, /* author email */
796 { "%ad" }, /* author date */
797 { "%aD" }, /* author date, RFC2822 style */
798 { "%ar" }, /* author date, relative */
799 { "%at" }, /* author date, UNIX timestamp */
800 { "%cn" }, /* committer name */
801 { "%ce" }, /* committer email */
802 { "%cd" }, /* committer date */
803 { "%cD" }, /* committer date, RFC2822 style */
804 { "%cr" }, /* committer date, relative */
805 { "%ct" }, /* committer date, UNIX timestamp */
806 { "%e" }, /* encoding */
807 { "%s" }, /* subject */
809 { "%Cred" }, /* red */
810 { "%Cgreen" }, /* green */
811 { "%Cblue" }, /* blue */
812 { "%Creset" }, /* reset color */
813 { "%n" }, /* newline */
814 { "%m" }, /* left/right/bottom */
817 IHASH
= 0, IHASH_ABBREV
,
819 IPARENTS
, IPARENTS_ABBREV
,
820 IAUTHOR_NAME
, IAUTHOR_EMAIL
,
821 IAUTHOR_DATE
, IAUTHOR_DATE_RFC2822
, IAUTHOR_DATE_RELATIVE
,
823 ICOMMITTER_NAME
, ICOMMITTER_EMAIL
,
824 ICOMMITTER_DATE
, ICOMMITTER_DATE_RFC2822
,
825 ICOMMITTER_DATE_RELATIVE
, ICOMMITTER_TIMESTAMP
,
829 IRED
, IGREEN
, IBLUE
, IRESET_COLOR
,
833 struct commit_list
*p
;
836 enum { HEADER
, SUBJECT
, BODY
} state
;
838 if (ILEFT_RIGHT
+ 1 != ARRAY_SIZE(table
))
839 die("invalid interp table!");
841 /* these are independent of the commit */
842 interp_set_entry(table
, IRED
, "\033[31m");
843 interp_set_entry(table
, IGREEN
, "\033[32m");
844 interp_set_entry(table
, IBLUE
, "\033[34m");
845 interp_set_entry(table
, IRESET_COLOR
, "\033[m");
846 interp_set_entry(table
, INEWLINE
, "\n");
848 /* these depend on the commit */
849 if (!commit
->object
.parsed
)
850 parse_object(commit
->object
.sha1
);
851 interp_set_entry(table
, IHASH
, sha1_to_hex(commit
->object
.sha1
));
852 interp_set_entry(table
, IHASH_ABBREV
,
853 find_unique_abbrev(commit
->object
.sha1
,
855 interp_set_entry(table
, ITREE
, sha1_to_hex(commit
->tree
->object
.sha1
));
856 interp_set_entry(table
, ITREE_ABBREV
,
857 find_unique_abbrev(commit
->tree
->object
.sha1
,
859 interp_set_entry(table
, ILEFT_RIGHT
,
860 (commit
->object
.flags
& BOUNDARY
)
862 : (commit
->object
.flags
& SYMMETRIC_LEFT
)
867 for (i
= 0, p
= commit
->parents
;
868 p
&& i
< sizeof(parents
) - 1;
870 i
+= snprintf(parents
+ i
, sizeof(parents
) - i
- 1, " %s",
871 sha1_to_hex(p
->item
->object
.sha1
));
872 interp_set_entry(table
, IPARENTS
, parents
+ 1);
875 for (i
= 0, p
= commit
->parents
;
876 p
&& i
< sizeof(parents
) - 1;
878 i
+= snprintf(parents
+ i
, sizeof(parents
) - i
- 1, " %s",
879 find_unique_abbrev(p
->item
->object
.sha1
,
881 interp_set_entry(table
, IPARENTS_ABBREV
, parents
+ 1);
883 for (i
= 0, state
= HEADER
; msg
[i
] && state
< BODY
; i
++) {
885 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
888 if (state
== SUBJECT
) {
889 table
[ISUBJECT
].value
= xstrndup(msg
+ i
, eol
- i
);
894 /* strip empty lines */
895 while (msg
[eol
+ 1] == '\n')
897 } else if (!prefixcmp(msg
+ i
, "author "))
898 fill_person(table
+ IAUTHOR_NAME
,
899 msg
+ i
+ 7, eol
- i
- 7);
900 else if (!prefixcmp(msg
+ i
, "committer "))
901 fill_person(table
+ ICOMMITTER_NAME
,
902 msg
+ i
+ 10, eol
- i
- 10);
903 else if (!prefixcmp(msg
+ i
, "encoding "))
904 table
[IENCODING
].value
=
905 xstrndup(msg
+ i
+ 9, eol
- i
- 9);
909 table
[IBODY
].value
= xstrdup(msg
+ i
);
910 for (i
= 0; i
< ARRAY_SIZE(table
); i
++)
912 interp_set_entry(table
, i
, "<unknown>");
914 interpolate(buf
, space
, user_format
, table
, ARRAY_SIZE(table
));
915 interp_clear_table(table
, ARRAY_SIZE(table
));
920 unsigned long pretty_print_commit(enum cmit_fmt fmt
,
921 const struct commit
*commit
,
923 char *buf
, unsigned long space
,
924 int abbrev
, const char *subject
,
925 const char *after_subject
,
928 int hdr
= 1, body
= 0, seen_title
= 0;
929 unsigned long offset
= 0;
931 int parents_shown
= 0;
932 const char *msg
= commit
->buffer
;
933 int plain_non_ascii
= 0;
935 const char *encoding
;
937 if (fmt
== CMIT_FMT_USERFORMAT
)
938 return format_commit_message(commit
, msg
, buf
, space
);
940 encoding
= (git_log_output_encoding
941 ? git_log_output_encoding
942 : git_commit_encoding
);
945 reencoded
= logmsg_reencode(commit
, encoding
);
949 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
952 /* After-subject is used to pass in Content-Type: multipart
953 * MIME header; in that case we do not have to do the
954 * plaintext content type even if the commit message has
955 * non 7-bit ASCII character. Otherwise, check if we need
956 * to say this is not a 7-bit ASCII.
958 if (fmt
== CMIT_FMT_EMAIL
&& !after_subject
) {
961 for (in_body
= i
= 0; (ch
= msg
[i
]) && i
< len
; i
++) {
963 /* author could be non 7-bit ASCII but
964 * the log may be so; skip over the
968 i
+ 1 < len
&& msg
[i
+1] == '\n')
971 else if (non_ascii(ch
)) {
979 const char *line
= msg
;
980 int linelen
= get_one_line(msg
, len
);
986 * We want some slop for indentation and a possible
987 * final "...". Thus the "+ 20".
989 if (offset
+ linelen
+ 20 > space
) {
990 memcpy(buf
+ offset
, " ...\n", 8);
1000 if ((fmt
!= CMIT_FMT_ONELINE
) && !subject
)
1001 buf
[offset
++] = '\n';
1004 if (fmt
== CMIT_FMT_RAW
) {
1005 memcpy(buf
+ offset
, line
, linelen
);
1009 if (!memcmp(line
, "parent ", 7)) {
1011 die("bad parent line in commit");
1015 if (!parents_shown
) {
1016 offset
+= add_merge_info(fmt
, buf
+ offset
,
1022 * MEDIUM == DEFAULT shows only author with dates.
1023 * FULL shows both authors but not dates.
1024 * FULLER shows both authors and dates.
1026 if (!memcmp(line
, "author ", 7))
1027 offset
+= add_user_info("Author", fmt
,
1032 if (!memcmp(line
, "committer ", 10) &&
1033 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
))
1034 offset
+= add_user_info("Commit", fmt
,
1045 if (is_empty_line(line
, &linelen
)) {
1052 if (fmt
== CMIT_FMT_SHORT
)
1058 int slen
= strlen(subject
);
1059 memcpy(buf
+ offset
, subject
, slen
);
1061 offset
+= add_rfc2047(buf
+ offset
, line
, linelen
,
1065 memset(buf
+ offset
, ' ', indent
);
1066 memcpy(buf
+ offset
+ indent
, line
, linelen
);
1067 offset
+= linelen
+ indent
;
1069 buf
[offset
++] = '\n';
1070 if (fmt
== CMIT_FMT_ONELINE
)
1072 if (subject
&& plain_non_ascii
) {
1075 const char *header_fmt
=
1076 "Content-Type: text/plain; charset=%s\n"
1077 "Content-Transfer-Encoding: 8bit\n";
1078 sz
= snprintf(header
, sizeof(header
), header_fmt
,
1080 if (sizeof(header
) < sz
)
1081 die("Encoding name %s too long", encoding
);
1082 memcpy(buf
+ offset
, header
, sz
);
1085 if (after_subject
) {
1086 int slen
= strlen(after_subject
);
1087 if (slen
> space
- offset
- 1)
1088 slen
= space
- offset
- 1;
1089 memcpy(buf
+ offset
, after_subject
, slen
);
1091 after_subject
= NULL
;
1095 while (offset
&& isspace(buf
[offset
-1]))
1097 /* Make sure there is an EOLN for the non-oneline case */
1098 if (fmt
!= CMIT_FMT_ONELINE
)
1099 buf
[offset
++] = '\n';
1101 * make sure there is another EOLN to separate the headers from whatever
1102 * body the caller appends if we haven't already written a body
1104 if (fmt
== CMIT_FMT_EMAIL
&& !body
)
1105 buf
[offset
++] = '\n';
1112 struct commit
*pop_commit(struct commit_list
**stack
)
1114 struct commit_list
*top
= *stack
;
1115 struct commit
*item
= top
? top
->item
: NULL
;
1124 int count_parents(struct commit
* commit
)
1127 struct commit_list
* parents
= commit
->parents
;
1128 for (count
= 0; parents
; parents
= parents
->next
,count
++)
1133 void topo_sort_default_setter(struct commit
*c
, void *data
)
1138 void *topo_sort_default_getter(struct commit
*c
)
1144 * Performs an in-place topological sort on the list supplied.
1146 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
1148 sort_in_topological_order_fn(list
, lifo
, topo_sort_default_setter
,
1149 topo_sort_default_getter
);
1152 void sort_in_topological_order_fn(struct commit_list
** list
, int lifo
,
1153 topo_sort_set_fn_t setter
,
1154 topo_sort_get_fn_t getter
)
1156 struct commit_list
* next
= *list
;
1157 struct commit_list
* work
= NULL
, **insert
;
1158 struct commit_list
** pptr
= list
;
1159 struct sort_node
* nodes
;
1160 struct sort_node
* next_nodes
;
1163 /* determine the size of the list */
1171 /* allocate an array to help sort the list */
1172 nodes
= xcalloc(count
, sizeof(*nodes
));
1173 /* link the list to the array */
1177 next_nodes
->list_item
= next
;
1178 setter(next
->item
, next_nodes
);
1182 /* update the indegree */
1185 struct commit_list
* parents
= next
->item
->parents
;
1187 struct commit
* parent
=parents
->item
;
1188 struct sort_node
* pn
= (struct sort_node
*) getter(parent
);
1192 parents
=parents
->next
;
1199 * tips are nodes not reachable from any other node in the list
1201 * the tips serve as a starting set for the work queue.
1206 struct sort_node
* node
= (struct sort_node
*) getter(next
->item
);
1208 if (node
->indegree
== 0) {
1209 insert
= &commit_list_insert(next
->item
, insert
)->next
;
1214 /* process the list in topological order */
1216 sort_by_date(&work
);
1218 struct commit
* work_item
= pop_commit(&work
);
1219 struct sort_node
* work_node
= (struct sort_node
*) getter(work_item
);
1220 struct commit_list
* parents
= work_item
->parents
;
1223 struct commit
* parent
=parents
->item
;
1224 struct sort_node
* pn
= (struct sort_node
*) getter(parent
);
1228 * parents are only enqueued for emission
1229 * when all their children have been emitted thereby
1230 * guaranteeing topological order.
1233 if (!pn
->indegree
) {
1235 insert_by_date(parent
, &work
);
1237 commit_list_insert(parent
, &work
);
1240 parents
=parents
->next
;
1243 * work_item is a commit all of whose children
1244 * have already been emitted. we can emit it now.
1246 *pptr
= work_node
->list_item
;
1247 pptr
= &(*pptr
)->next
;
1249 setter(work_item
, NULL
);
1254 /* merge-base stuff */
1256 /* bits #0..15 in revision.h */
1257 #define PARENT1 (1u<<16)
1258 #define PARENT2 (1u<<17)
1259 #define STALE (1u<<18)
1260 #define RESULT (1u<<19)
1262 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
1264 static struct commit
*interesting(struct commit_list
*list
)
1267 struct commit
*commit
= list
->item
;
1269 if (commit
->object
.flags
& STALE
)
1276 static struct commit_list
*merge_bases(struct commit
*one
, struct commit
*two
)
1278 struct commit_list
*list
= NULL
;
1279 struct commit_list
*result
= NULL
;
1282 /* We do not mark this even with RESULT so we do not
1283 * have to clean it up.
1285 return commit_list_insert(one
, &result
);
1290 one
->object
.flags
|= PARENT1
;
1291 two
->object
.flags
|= PARENT2
;
1292 insert_by_date(one
, &list
);
1293 insert_by_date(two
, &list
);
1295 while (interesting(list
)) {
1296 struct commit
*commit
;
1297 struct commit_list
*parents
;
1298 struct commit_list
*n
;
1301 commit
= list
->item
;
1306 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
1307 if (flags
== (PARENT1
| PARENT2
)) {
1308 if (!(commit
->object
.flags
& RESULT
)) {
1309 commit
->object
.flags
|= RESULT
;
1310 insert_by_date(commit
, &result
);
1312 /* Mark parents of a found merge stale */
1315 parents
= commit
->parents
;
1317 struct commit
*p
= parents
->item
;
1318 parents
= parents
->next
;
1319 if ((p
->object
.flags
& flags
) == flags
)
1322 p
->object
.flags
|= flags
;
1323 insert_by_date(p
, &list
);
1327 /* Clean up the result to remove stale ones */
1328 free_commit_list(list
);
1329 list
= result
; result
= NULL
;
1331 struct commit_list
*n
= list
->next
;
1332 if (!(list
->item
->object
.flags
& STALE
))
1333 insert_by_date(list
->item
, &result
);
1340 struct commit_list
*get_merge_bases(struct commit
*one
,
1344 struct commit_list
*list
;
1345 struct commit
**rslt
;
1346 struct commit_list
*result
;
1349 result
= merge_bases(one
, two
);
1352 if (!result
|| !result
->next
) {
1354 clear_commit_marks(one
, all_flags
);
1355 clear_commit_marks(two
, all_flags
);
1360 /* There are more than one */
1367 rslt
= xcalloc(cnt
, sizeof(*rslt
));
1368 for (list
= result
, i
= 0; list
; list
= list
->next
)
1369 rslt
[i
++] = list
->item
;
1370 free_commit_list(result
);
1372 clear_commit_marks(one
, all_flags
);
1373 clear_commit_marks(two
, all_flags
);
1374 for (i
= 0; i
< cnt
- 1; i
++) {
1375 for (j
= i
+1; j
< cnt
; j
++) {
1376 if (!rslt
[i
] || !rslt
[j
])
1378 result
= merge_bases(rslt
[i
], rslt
[j
]);
1379 clear_commit_marks(rslt
[i
], all_flags
);
1380 clear_commit_marks(rslt
[j
], all_flags
);
1381 for (list
= result
; list
; list
= list
->next
) {
1382 if (rslt
[i
] == list
->item
)
1384 if (rslt
[j
] == list
->item
)
1390 /* Surviving ones in rslt[] are the independent results */
1392 for (i
= 0; i
< cnt
; i
++) {
1394 insert_by_date(rslt
[i
], &result
);
1400 int in_merge_bases(struct commit
*commit
, struct commit
**reference
, int num
)
1402 struct commit_list
*bases
, *b
;
1406 bases
= get_merge_bases(commit
, *reference
, 1);
1409 for (b
= bases
; b
; b
= b
->next
) {
1410 if (!hashcmp(commit
->object
.sha1
, b
->item
->object
.sha1
)) {
1416 free_commit_list(bases
);