threads / patch / 36688

v4format-patch --signature-file <file>

Subject: [PATCH v4] format-patch --signature-file <file>

## tl;dr

3 messages between May 18, 2014 and May 19, 2014. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Jeremiah Mahler· May 18, 2014, 20:22 UTC · lore
v4 of patch to add format-patch --signature-file <file> option.
This revision includes more suggestions from Jeff King:
  - Added a format.signaturefile config option.
  - Fixed error messages, --signature and format.signature
  - Fixed error checking, dies if strbuf_read_file fails.
  - style: "cat > expect << EOF" to "cat >expect <<-\EOF"
  - Eliminated use of fgrep, which is not used anywhere else
	in the project.  Possibility of portability problems.
    Replaced with sed which is commonly used.
  - Eliminated use of diff in favor of test_cmp.
  - Removed redundant test, "--foo=bar" and "--foo bar"
  - Changed to use test_must_fail instead of "!"
Jeremiah Mahler (1):
  format-patch --signature-file <file>
 Documentation/config.txt           |  4 ++++
 Documentation/git-format-patch.txt |  4 ++++
 builtin/log.c                      | 16 ++++++++++++++++
 t/t4014-format-patch.sh            | 33 +++++++++++++++++++++++++++++++++
 4 files changed, 57 insertions(+)
-- 
Jeremiah Mahler
jmmahler@gmail.com
http://github.com/jmahler
Jeremiah Mahler· May 18, 2014, 20:22 UTC · re: Jeremiah Mahler · lore

Added option that allows a signature file to be used with format-patch so that signatures with newlines and other special characters can be easily included.

  $ git format-patch --signature-file ~/.signature -1

The config variable format.signaturefile is also provided so that it can be added by default.

  $ git config format.signaturefile ~/.signature
  $ git format-patch -1
Signed-off-by: Jeremiah Mahler <jmmahler@gmail.com>
---
 Documentation/config.txt           |  4 ++++
 Documentation/git-format-patch.txt |  4 ++++
 builtin/log.c                      | 16 ++++++++++++++++
 t/t4014-format-patch.sh            | 33 +++++++++++++++++++++++++++++++++
 4 files changed, 57 insertions(+)
Show changes to 4 files +57 −0

Documentation/config.txt, Documentation/git-format-patch.txt, builtin/log.c, t/t4014-format-patch.sh

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 1932e9b..140ed77 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -1114,6 +1114,10 @@ format.signature::
 	Set this variable to the empty string ("") to suppress
 	signature generation.
 
+format.signaturefile::
+	Works just like format.signature except the contents of the
+	file specified by this variable will be used as the signature.
+
 format.suffix::
 	The default for format-patch is to output files with the suffix
 	`.patch`. Use this variable to change that suffix (make sure to
diff --git a/Documentation/git-format-patch.txt b/Documentation/git-format-patch.txt
index 5c0a4ab..c0fd470 100644
--- a/Documentation/git-format-patch.txt
+++ b/Documentation/git-format-patch.txt
@@ -14,6 +14,7 @@ SYNOPSIS
 		   [(--attach|--inline)[=<boundary>] | --no-attach]
 		   [-s | --signoff]
 		   [--signature=<signature> | --no-signature]
+		   [--signature-file=<file>]
 		   [-n | --numbered | -N | --no-numbered]
 		   [--start-number <n>] [--numbered-files]
 		   [--in-reply-to=Message-Id] [--suffix=.<sfx>]
@@ -233,6 +234,9 @@ configuration options in linkgit:git-notes[1] to use this workflow).
 	signature option is omitted the signature defaults to the Git version
 	number.
 
+--signature-file=<file>::
+	Works just like --signature except the signature is read from a file.
+
 --suffix=.<sfx>::
 	Instead of using `.patch` as the suffix for generated
 	filenames, use specified suffix.  A common alternative is
diff --git a/builtin/log.c b/builtin/log.c
index 39e8836..0a8f417 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -673,6 +673,7 @@ static void add_header(const char *value)
 static int thread;
 static int do_signoff;
 static const char *signature = git_version_string;
+static const char *signature_file;
 static int config_cover_letter;
 
 enum {
@@ -742,6 +743,8 @@ static int git_format_config(const char *var, const char *value, void *cb)
 	}
 	if (!strcmp(var, "format.signature"))
 		return git_config_string(&signature, var, value);
+	if (!strcmp(var, "format.signaturefile"))
+		return git_config_string(&signature_file, var, value);
 	if (!strcmp(var, "format.coverletter")) {
 		if (value && !strcasecmp(value, "auto")) {
 			config_cover_letter = COVER_AUTO;
@@ -1230,6 +1233,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 			    PARSE_OPT_OPTARG, thread_callback },
 		OPT_STRING(0, "signature", &signature, N_("signature"),
 			    N_("add a signature")),
+		OPT_FILENAME(0, "signature-file", &signature_file,
+				N_("add a signature from a file")),
 		OPT__QUIET(&quiet, N_("don't print the patch filenames")),
 		OPT_END()
 	};
@@ -1447,6 +1452,17 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 			cover_letter = (config_cover_letter == COVER_ON);
 	}
 
+	if (signature_file) {
+		if (signature && signature != git_version_string)
+			die(_("cannot specify both signature and signature-file"));
+
+		struct strbuf buf = STRBUF_INIT;
+
+		if (strbuf_read_file(&buf, signature_file, 128) < 0)
+			die_errno(_("unable to read signature file '%s'"), signature_file);
+		signature = strbuf_detach(&buf, NULL);
+	}
+
 	if (in_reply_to || thread || cover_letter)
 		rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
 	if (in_reply_to) {
diff --git a/t/t4014-format-patch.sh b/t/t4014-format-patch.sh
index 9c80633..c6d44b8 100755
--- a/t/t4014-format-patch.sh
+++ b/t/t4014-format-patch.sh
@@ -762,6 +762,39 @@ test_expect_success 'format-patch --signature="" suppresses signatures' '
 	! grep "^-- \$" output
 '
 
+cat >expect <<-\EOF
+
+Test User <test.email@kernel.org>
+http://git.kernel.org/cgit/git/git.git
+
+git.kernel.org/?p=git/git.git;a=summary
+
+EOF
+
+test_expect_success 'format-patch --signature-file=file' '
+	git format-patch --stdout --signature-file=expect -1 >output &&
+	check_patch output &&
+	sed -n "/^-- $/,\$p" <output | head --lines=-2 | tail --lines=+2 >output2 &&
+	test_cmp expect output2
+'
+
+test_expect_success 'format-patch --signature and --signature-file die' '
+	test_must_fail git format-patch --stdout --signature="foo" --signature-file=expect -1 >output
+'
+
+test_expect_success 'format-patch --no-signature and --signature-file OK' '
+	git format-patch --stdout --no-signature --signature-file=expect -1
+'
+
+test_expect_success 'format-patch with format.signaturefile config' '
+	git config format.signaturefile expect &&
+	git format-patch --stdout -1 >output &&
+	check_patch output &&
+	sed -n "/^-- $/,\$p" <output | head --lines=-2 | tail --lines=+2 >output2 &&
+	test_cmp expect output2 &&
+	git config --unset-all format.signaturefile
+'
+
 test_expect_success TTY 'format-patch --stdout paginates' '
 	rm -f pager_used &&
 	test_terminal env GIT_PAGER="wc >pager_used" git format-patch --stdout --all &&
-- 
Jeremiah Mahler
jmmahler@gmail.com
http://github.com/jmahler
Jeff King· May 19, 2014, 10:48 UTC · re: Jeremiah Mahler · lore

Re: [PATCH v4] format-patch --signature-file <file>

On Sun, May 18, 2014 at 01:22:02PM -0700, Jeremiah Mahler wrote:
Show 6 quoted lines
> @@ -742,6 +743,8 @@ static int git_format_config(const char *var, const char *value, void *cb)
>  	}
>  	if (!strcmp(var, "format.signature"))
>  		return git_config_string(&signature, var, value);
> +	if (!strcmp(var, "format.signaturefile"))
> +		return git_config_string(&signature_file, var, value);
Should this be git_config_pathname? That would handle tilde-expansion.
Show 6 quoted lines
> +test_expect_success 'format-patch --signature-file=file' '
> +	git format-patch --stdout --signature-file=expect -1 >output &&
> +	check_patch output &&
> +	sed -n "/^-- $/,\$p" <output | head --lines=-2 | tail --lines=+2 >output2 &&
> +	test_cmp expect output2
> +'

I don't think --lines is portable (it's not even POSIX), nor is a negative value for head (but a "+" form for tail is). You can replace the "head" call with a "$d" sed command, and tail should be fine with "-n +2". Like:

  sed -n '$d; /^-- $/,$p' | tail -n +2
You can also do it in one sed like:
  sed -n '$d; /^-- $/,${//!p}'
but that is starting to look a bit like line-noise. ;)
Show 8 quoted lines
> +test_expect_success 'format-patch with format.signaturefile config' '
> +	git config format.signaturefile expect &&
> +	git format-patch --stdout -1 >output &&
> +	check_patch output &&
> +	sed -n "/^-- $/,\$p" <output | head --lines=-2 | tail --lines=+2 >output2 &&
> +	test_cmp expect output2 &&
> +	git config --unset-all format.signaturefile
> +'

You might want to use "test_config format.signaturefile expect" here, which handles unsetting after the test. Besides being one line shorter, it also cleans up when the middle part of the test fails.

You could also use "git -c", but I think setting the actual config in a file is a more realistic test.

-Peff

← back to recent threads