git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Jeremiah Mahler <jmmahler@gmail.com>
Cc: Jonathan Nieder <jrnieder@gmail.com>, git@vger.kernel.org
Subject: Re: [PATCH v2] format-patch --signature-file <file>
Date: Fri, 16 May 2014 04:14:45 -0400	[thread overview]
Message-ID: <20140516081445.GA21468@sigill.intra.peff.net> (raw)
In-Reply-To: <1400203881-2794-2-git-send-email-jmmahler@gmail.com>

On Thu, May 15, 2014 at 06:31:21PM -0700, Jeremiah Mahler wrote:

> Added feature that allows a signature file to be used with format-patch.
> 
>   $ git format-patch --signature-file ~/.signature -1
> 
> Now signatures with newlines and other special characters can be
> easily included.

I think this version looks nicer than the original.

A few questions/comments:

> +static int signature_file_callback(const struct option *opt, const char *arg,
> +							int unset)
> +{
> +	const char **signature = opt->value;
> +	static char buf[1024];
> +	size_t sz;
> +	FILE *fd;
> +
> +	fd = fopen(arg, "r");
> +	if (fd) {
> +		sz = sizeof(buf);
> +		sz = fread(buf, 1, sz - 1, fd);
> +		if (sz) {
> +			buf[sz] = '\0';
> +			*signature = buf;
> +		}
> +		fclose(fd);
> +	}
> +	return 0;
> +}

We have routines for reading directly into a strbuf, which eliminates
the need for this 1024-byte limit. We even have a wrapper that can make
this much shorter:

  struct strbuf buf = STRBUF_INIT;

  strbuf_read_file(&buf, arg, 128);
  *signature = strbuf_detach(&buf, NULL);

I notice that you ignore any errors. Is that intentional (so that we
silently ignore missing --signature files)? If so, should we actually
treat it as an empty file (e.g., in my code above, we always set
*signature, even if the file was missing)?

Finally, I suspect that:

  cd path/in/repo &&
  git format-patch --signature-file=foo

will not work, as we chdir() to the toplevel before evaluating the
arguments. You can fix that either by using parse-option's OPT_FILENAME
to save the filename, followed by opening the file after all arguments
are processed; or by manually fixing up the filename.

Since parse-options already knows how to do this fixup (it does it for
OPT_FILENAME), it would be nice if it were a flag rather than a full
type, so you could specify at as an option to your callback here:

> +		{ OPTION_CALLBACK, 0, "signature-file", &signature, N_("signature-file"),
> +				N_("add a signature from contents of a file"),
> +			    PARSE_OPT_NONEG, signature_file_callback },

Noticing your OPT_NONEG, though, I wonder if you should simply use an
OPT_FILENAME. I would expect --no-signature-file to countermand any
earlier --signature-file on the command-line (or if we eventually grow a
config option, which seems sensible, it would tell git to ignore the
option). The usual ordering for that is:

  1. Read config and store format.signatureFile as a string
     "signature_file".

  2. Parse arguments. --signature-file=... sets signature_file, and
     --no-signature-file sets it to NULL.

  3. If signature_file is non-NULL, load it.

And I believe OPT_FILENAME will implement (2) for you.

One downside of doing it this way is that you need to specify what will
happen when both "--signature" (or format.signature) and
"--signature-file" are set. With your current code, I think
"--signature=foo --signature-file=bar" will take the second one. I think
it would be fine to prefer one to the other, or to just notice that both
are set and complain.

-Peff

  reply	other threads:[~2014-05-16  8:15 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-16  1:31 [PATCH v2] format-patch --signature-file <file> Jeremiah Mahler
2014-05-16  1:31 ` Jeremiah Mahler
2014-05-16  8:14   ` Jeff King [this message]
2014-05-17  7:25     ` Jeremiah Mahler
2014-05-17  7:42       ` Jeff King
2014-05-17  8:59         ` Jeremiah Mahler
2014-05-17 10:00           ` Jeff King
2014-05-17 15:39             ` Jeremiah Mahler
2014-05-19 16:54               ` Junio C Hamano
2014-05-20  5:46                 ` Jeremiah Mahler
2014-05-20  6:21                   ` Jeff King
2014-05-20 15:06                     ` Junio C Hamano
2014-05-21  0:45                       ` Jeremiah Mahler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140516081445.GA21468@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=jmmahler@gmail.com \
    --cc=jrnieder@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).