format-patch: add "--signature-file=<file>" option

Add an option to format-patch for reading a signature from a file. $ git format-patch -1 --signature-file=$HOME/.signature The config variable `format.signaturefile` can also be used to make this the default. $ git config format.signaturefile $HOME/.signature $ git format-patch -1 Signed-off-by: Jeremiah Mahler <jmmahler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeremiah Mahler committed May 23, 2014 at 21:08 UTC 7022650f61ff7d7c9297db4a04f4e93ae14e1750
4 files changed +86
Documentation/config.txt
+4
@@ -1114,6 +1114,10 @@ format.signature::
1114 Set this variable to the empty string ("") to suppress
1115 signature generation.
1116
1117 +format.signaturefile::
1118 + Works just like format.signature except the contents of the
1119 + file specified by this variable will be used as the signature.
1120 +
1121 format.suffix::
1122 The default for format-patch is to output files with the suffix
1123 `.patch`. Use this variable to change that suffix (make sure to
Documentation/git-format-patch.txt
+4
@@ -14,6 +14,7 @@ SYNOPSIS
14 [(--attach|--inline)[=<boundary>] | --no-attach]
15 [-s | --signoff]
16 [--signature=<signature> | --no-signature]
17 + [--signature-file=<file>]
18 [-n | --numbered | -N | --no-numbered]
19 [--start-number <n>] [--numbered-files]
20 [--in-reply-to=Message-Id] [--suffix=.<sfx>]
@@ -233,6 +234,9 @@ configuration options in linkgit:git-notes[1] to use this workflow).
234 signature option is omitted the signature defaults to the Git version
235 number.
236
237 +--signature-file=<file>::
238 + Works just like --signature except the signature is read from a file.
239 +
240 --suffix=.<sfx>::
241 Instead of using `.patch` as the suffix for generated
242 filenames, use specified suffix. A common alternative is
builtin/log.c
+17
@@ -673,6 +673,7 @@ static void add_header(const char *value)
673 static int thread;
674 static int do_signoff;
675 static const char *signature = git_version_string;
676 +static const char *signature_file;
677 static int config_cover_letter;
678
679 enum {
@@ -742,6 +743,8 @@ static int git_format_config(const char *var, const char *value, void *cb)
743 }
744 if (!strcmp(var, "format.signature"))
745 return git_config_string(&signature, var, value);
746 + if (!strcmp(var, "format.signaturefile"))
747 + return git_config_pathname(&signature_file, var, value);
748 if (!strcmp(var, "format.coverletter")) {
749 if (value && !strcasecmp(value, "auto")) {
750 config_cover_letter = COVER_AUTO;
@@ -1235,6 +1238,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1238 PARSE_OPT_OPTARG, thread_callback },
1239 OPT_STRING(0, "signature", &signature, N_("signature"),
1240 N_("add a signature")),
1241 + OPT_FILENAME(0, "signature-file", &signature_file,
1242 + N_("add a signature from a file")),
1243 OPT__QUIET(&quiet, N_("don't print the patch filenames")),
1244 OPT_END()
1245 };
@@ -1452,6 +1457,18 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1457 cover_letter = (config_cover_letter == COVER_ON);
1458 }
1459
1460 + if (!signature) {
1461 + ; /* --no-signature inhibits all signatures */
1462 + } else if (signature && signature != git_version_string) {
1463 + ; /* non-default signature already set */
1464 + } else if (signature_file) {
1465 + struct strbuf buf = STRBUF_INIT;
1466 +
1467 + if (strbuf_read_file(&buf, signature_file, 128) < 0)
1468 + die_errno(_("unable to read signature file '%s'"), signature_file);
1469 + signature = strbuf_detach(&buf, NULL);
1470 + }
1471 +
1472 if (in_reply_to || thread || cover_letter)
1473 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1474 if (in_reply_to) {
t/t4014-format-patch.sh
+61
@@ -762,6 +762,67 @@ test_expect_success 'format-patch --signature="" suppresses signatures' '
762 ! grep "^-- \$" output
763 '
764
765 +test_expect_success 'prepare mail-signature input' '
766 + cat >mail-signature <<-\EOF
767 +
768 + Test User <test.email@kernel.org>
769 + http://git.kernel.org/cgit/git/git.git
770 +
771 + git.kernel.org/?p=git/git.git;a=summary
772 +
773 + EOF
774 +'
775 +
776 +test_expect_success '--signature-file=file works' '
777 + git format-patch --stdout --signature-file=mail-signature -1 >output &&
778 + check_patch output &&
779 + sed -e "1,/^-- \$/d" <output >actual &&
780 + {
781 + cat mail-signature && echo
782 + } >expect &&
783 + test_cmp expect actual
784 +'
785 +
786 +test_expect_success 'format.signaturefile works' '
787 + test_config format.signaturefile mail-signature &&
788 + git format-patch --stdout -1 >output &&
789 + check_patch output &&
790 + sed -e "1,/^-- \$/d" <output >actual &&
791 + {
792 + cat mail-signature && echo
793 + } >expect &&
794 + test_cmp expect actual
795 +'
796 +
797 +test_expect_success '--no-signature suppresses format.signaturefile ' '
798 + test_config format.signaturefile mail-signature &&
799 + git format-patch --stdout --no-signature -1 >output &&
800 + check_patch output &&
801 + ! grep "^-- \$" output
802 +'
803 +
804 +test_expect_success '--signature-file overrides format.signaturefile' '
805 + cat >other-mail-signature <<-\EOF
806 + Use this other signature instead of mail-signature.
807 + EOF
808 + test_config format.signaturefile mail-signature &&
809 + git format-patch --stdout \
810 + --signature-file=other-mail-signature -1 >output &&
811 + check_patch output &&
812 + sed -e "1,/^-- \$/d" <output >actual &&
813 + {
814 + cat other-mail-signature && echo
815 + } >expect &&
816 + test_cmp expect actual
817 +'
818 +
819 +test_expect_success '--signature overrides format.signaturefile' '
820 + test_config format.signaturefile mail-signature &&
821 + git format-patch --stdout --signature="my sig" -1 >output &&
822 + check_patch output &&
823 + grep "my sig" output
824 +'
825 +
826 test_expect_success TTY 'format-patch --stdout paginates' '
827 rm -f pager_used &&
828 test_terminal env GIT_PAGER="wc >pager_used" git format-patch --stdout --all &&