format-patch: output header for empty commits

When formatting an empty commit, it is surprising that a totally empty file is generated. Set the flag to always print the header, matching the behaviour of git-log. Signed-off-by: John Keeping <john@keeping.me.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

John Keeping committed Mar 3, 2023 at 16:03 UTC 94c4289435605a9c39951ccebaf82f131e07c255
2 files changed +11
builtin/log.c
+1
@@ -2097,6 +2097,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
2097
2098 /* Always generate a patch */
2099 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
2100 + rev.always_show_header = 1;
2101
2102 rev.zero_commit = zero_commit;
2103 rev.patch_name_max = fmt_patch_name_max;
t/t4014-format-patch.sh
+10
@@ -59,6 +59,10 @@ test_expect_success setup '
59 test_tick &&
60 git commit -m "patchid 3" &&
61
62 + git checkout -b empty main &&
63 + test_tick &&
64 + git commit --allow-empty -m "empty commit" &&
65 +
66 git checkout main
67 '
68
@@ -128,6 +132,12 @@ test_expect_success 'replay did not screw up the log message' '
132 grep "^Side .* with .* backslash-n" actual
133 '
134
135 +test_expect_success 'format-patch empty commit' '
136 + git format-patch --stdout main..empty >empty &&
137 + grep "^From " empty >from &&
138 + test_line_count = 1 from
139 +'
140 +
141 test_expect_success 'extra headers' '
142 git config format.headers "To: R E Cipient <rcipient@example.com>
143 " &&