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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Jul 29, 2014 at 13:56 UTC
b9c7d6e4330e3dcdb3b3e5f013e5667e47555c95
4 files changed
+11
-3
combine-diff.c
+2
-1
@@ -1397,7 +1397,8 @@ void diff_tree_combined(const unsigned char *sha1,
1397
show_log(rev);
1398
1399
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))
1402
printf("%s%c", diff_line_prefix(opt),
1403
opt->line_termination);
1404
}
commit.h
+1
@@ -159,6 +159,7 @@ extern void get_commit_format(const char *arg, struct rev_info *);
159
extern const char *format_subject(struct strbuf *sb, const char *msg,
160
const char *line_separator);
161
extern void userformat_find_requirements(const char *fmt, struct userformat_want *w);
162
+extern int commit_format_is_empty(enum cmit_fmt);
163
extern void format_commit_message(const struct commit *commit,
164
const char *format, struct strbuf *sb,
165
const struct pretty_print_context *context);
log-tree.c
+3
-2
@@ -649,7 +649,7 @@ void show_log(struct rev_info *opt)
649
graph_show_commit_msg(opt->graph, &msgbuf);
650
else
651
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)) {
653
if (!opt->missing_newline)
654
graph_show_padding(opt->graph);
655
putchar(opt->diffopt.line_termination);
@@ -676,7 +676,8 @@ int log_tree_diff_flush(struct rev_info *opt)
676
show_log(opt);
677
if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
678
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)) {
681
/*
682
* When showing a verbose header (i.e. log message),
683
* and not in --pretty=oneline format, we would want
pretty.c
+5
@@ -24,6 +24,11 @@ static size_t commit_formats_len;
24
static size_t commit_formats_alloc;
25
static struct cmt_fmt_map *find_commit_format(const char *sought);
26
27
+int commit_format_is_empty(enum cmit_fmt fmt)
28
+{
29
+ return fmt == CMIT_FMT_USERFORMAT && !*user_format;
30
+}
31
+
32
static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
33
{
34
free(user_format);