Skip to content

Commit e7af8e4

Browse files
peffgitster
authored andcommitted
format-patch: make zero-length subject prefixes prettier
If you give a zero-length subject prefix to format-patch (e.g., "format-patch --subject-prefix="), we will print the ugly: Subject: [ 1/2] your subject here because we always insert a space between the prefix and numbering. Requiring the user to provide the space in their prefix would be more flexible, but would break existing usage. This patch provides a DWIM and suppresses the space for zero-length prefixes, under the assumption that nobody actually wants "[ 1/2]". Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb674d7 commit e7af8e4

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

log-tree.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,9 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
294294
if (opt->total > 0) {
295295
static char buffer[64];
296296
snprintf(buffer, sizeof(buffer),
297-
"Subject: [%s %0*d/%d] ",
297+
"Subject: [%s%s%0*d/%d] ",
298298
opt->subject_prefix,
299+
*opt->subject_prefix ? " " : "",
299300
digits_in_number(opt->total),
300301
opt->nr, opt->total);
301302
subject = buffer;

t/t4014-format-patch.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,4 +851,22 @@ test_expect_success 'subject lines do not have 822 atom-quoting' '
851851
test_cmp expect actual
852852
'
853853

854+
cat >expect <<'EOF'
855+
Subject: [PREFIX 1/1] header with . in it
856+
EOF
857+
test_expect_success 'subject prefixes have space prepended' '
858+
git format-patch -n -1 --stdout --subject-prefix=PREFIX >patch &&
859+
grep ^Subject: patch >actual &&
860+
test_cmp expect actual
861+
'
862+
863+
cat >expect <<'EOF'
864+
Subject: [1/1] header with . in it
865+
EOF
866+
test_expect_success 'empty subject prefix does not have extra space' '
867+
git format-patch -n -1 --stdout --subject-prefix= >patch &&
868+
grep ^Subject: patch >actual &&
869+
test_cmp expect actual
870+
'
871+
854872
test_done

0 commit comments

Comments
 (0)