Skip to content

Commit b9c7d6e

Browse files
peffgitster
authored andcommitted
pretty: make empty userformats truly empty
If the user provides an empty format with "--format=", we end up putting in extra whitespace that the user cannot prevent. This comes from two places: 1. If the format is missing a terminating newline, we add one automatically. This makes sense for --format=%h, but not for a truly empty format. 2. We add an extra newline between the pretty-printed format and a diff or diffstat. If the format is empty, there's no point in doing so if there's nothing to separate. With this patch, one can get a diff with no other cruft out of "diff-tree --format= $commit". Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c75e7ad commit b9c7d6e

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

combine-diff.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,8 @@ void diff_tree_combined(const unsigned char *sha1,
13971397
show_log(rev);
13981398

13991399
if (rev->verbose_header && opt->output_format &&
1400-
opt->output_format != DIFF_FORMAT_NO_OUTPUT)
1400+
opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1401+
!commit_format_is_empty(rev->commit_format))
14011402
printf("%s%c", diff_line_prefix(opt),
14021403
opt->line_termination);
14031404
}

commit.h

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ extern void get_commit_format(const char *arg, struct rev_info *);
159159
extern const char *format_subject(struct strbuf *sb, const char *msg,
160160
const char *line_separator);
161161
extern void userformat_find_requirements(const char *fmt, struct userformat_want *w);
162+
extern int commit_format_is_empty(enum cmit_fmt);
162163
extern void format_commit_message(const struct commit *commit,
163164
const char *format, struct strbuf *sb,
164165
const struct pretty_print_context *context);

log-tree.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ void show_log(struct rev_info *opt)
649649
graph_show_commit_msg(opt->graph, &msgbuf);
650650
else
651651
fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
652-
if (opt->use_terminator) {
652+
if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
653653
if (!opt->missing_newline)
654654
graph_show_padding(opt->graph);
655655
putchar(opt->diffopt.line_termination);
@@ -676,7 +676,8 @@ int log_tree_diff_flush(struct rev_info *opt)
676676
show_log(opt);
677677
if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
678678
opt->verbose_header &&
679-
opt->commit_format != CMIT_FMT_ONELINE) {
679+
opt->commit_format != CMIT_FMT_ONELINE &&
680+
!commit_format_is_empty(opt->commit_format)) {
680681
/*
681682
* When showing a verbose header (i.e. log message),
682683
* and not in --pretty=oneline format, we would want

pretty.c

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ static size_t commit_formats_len;
2424
static size_t commit_formats_alloc;
2525
static struct cmt_fmt_map *find_commit_format(const char *sought);
2626

27+
int commit_format_is_empty(enum cmit_fmt fmt)
28+
{
29+
return fmt == CMIT_FMT_USERFORMAT && !*user_format;
30+
}
31+
2732
static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
2833
{
2934
free(user_format);

0 commit comments

Comments
 (0)