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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 30, 2011 at 10:19 UTC e7af8e49cd54f1784fa2a0e382f22ca2f98cf4d8
2 files changed +20 -1
log-tree.c
+2 -1
@@ -294,8 +294,9 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
294 if (opt->total > 0) {
295 static char buffer[64];
296 snprintf(buffer, sizeof(buffer),
297 - "Subject: [%s %0*d/%d] ",
297 + "Subject: [%s%s%0*d/%d] ",
298 opt->subject_prefix,
299 + *opt->subject_prefix ? " " : "",
300 digits_in_number(opt->total),
301 opt->nr, opt->total);
302 subject = buffer;
t/t4014-format-patch.sh
+18
@@ -851,4 +851,22 @@ test_expect_success 'subject lines do not have 822 atom-quoting' '
851 test_cmp expect actual
852 '
853
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 +
872 test_done