Git development
 help / color / mirror / Atom feed
From: "René Scharfe" <l.s.r@web.de>
To: Dominik Maier <dmnk@google.com>, git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Patrick Steinhardt" <ps@pks.im>, "Johannes Sixt" <j6t@kdbg.org>,
	"Jean-Noël Avila" <jn.avila@free.fr>,
	dmnk+artist@google.com
Subject: Re: [PATCH] archive: reject remote --add-file in callback
Date: Sat, 21 Nov 2026 16:15:29 +0100	[thread overview]
Message-ID: <389c4ebc-7c4e-4ded-a620-b6a63e3911a3@web.de> (raw)
In-Reply-To: <20261119192311.2685319-1-dmnk@google.com>

On 11/19/26 8:23 PM, Dominik Maier wrote:
> parse_archive_args() only checks "is_remote && args->extra_files.nr"
> after parse_options() returns. When git-upload-archive parses client
> arguments, add_file_cb() runs first and calls stat() on the requested
> path on the server filesystem, reporting "File not found" or "Not a
> regular file" before the "--remote" incompatibility check is reached
> (and succeeding altogether if "--no-add-file" follows).

Ugh, good find.

> Pass is_remote into add_file_cb() and reject "--add-file" and
> "--add-virtual-file" immediately before touching the filesystem.

The current code rejects the combination of --add-virtual-file and
--remote, but that's arguably also a bug -- there's no reason to
disallow it, since the option's value brings file name and content
to the remote end.

> Signed-off-by: Dominik Maier <dmnk@google.com>
> ---
>  archive.c           | 31 ++++++++++++++++++++++---------
>  t/t5000-tar-tree.sh |  9 +++++++++
>  2 files changed, 31 insertions(+), 9 deletions(-)
> 
> diff --git a/archive.c b/archive.c
> index 59790be986..b983209ce4 100644
> --- a/archive.c
> +++ b/archive.c
> @@ -559,15 +559,27 @@ static void extra_file_info_clear(void *util, const char *str UNUSED)
>  	free(info);
>  }
>  
> +struct add_file_data {
> +	struct archiver_args *args;
> +	const char **basep;
> +	int is_remote;
> +};
> +
>  static int add_file_cb(const struct option *opt, const char *arg, int unset)
>  {
> -	struct archiver_args *args = opt->value;
> -	const char **basep = (const char **)opt->defval;
> -	const char *base = *basep;
> +	struct add_file_data *data = opt->value;
> +	struct archiver_args *args = data->args;
> +	const char *base = *data->basep;
>  	char *path;
>  	struct string_list_item *item;
>  	struct extra_file_info *info;
>  
> +	if (data->is_remote)
> +		die(_("options '%s' and '%s' cannot be used together"),
> +		    !strcmp(opt->long_name, "add-virtual-file") ?
> +		    "--add-virtual-file" : "--add-file",
> +		    "--remote");

That also rejects --remote --add-file=foo --no-add-file, which we could
allow.

> +
>  	if (unset) {
>  		string_list_clear_func(&args->extra_files,
>  				       extra_file_info_clear);
> @@ -645,6 +657,11 @@ static int parse_archive_args(int argc, const char **argv,
>  	int i;
>  	int list = 0;
>  	int worktree_attributes = 0;
> +	struct add_file_data add_file_data = {
> +		.args = args,
> +		.basep = &base,
> +		.is_remote = is_remote,
> +	};
>  	struct option opts[] = {
>  		OPT_GROUP(""),
>  		OPT_STRING(0, "format", &format, N_("fmt"), N_("archive format")),
> @@ -653,20 +670,18 @@ static int parse_archive_args(int argc, const char **argv,
>  		{
>  			.type = OPTION_CALLBACK,
>  			.long_name = "add-file",
> -			.value = args,
> +			.value = &add_file_data,
>  			.argh = N_("file"),
>  			.help = N_("add untracked file to archive"),
>  			.callback = add_file_cb,
> -			.defval = (intptr_t) &base,
>  		},
>  		{
>  			.type = OPTION_CALLBACK,
>  			.long_name = "add-virtual-file",
> -			.value = args,
> +			.value = &add_file_data,
>  			.argh = N_("path:content"),
>  			.help = N_("add untracked file to archive"),
>  			.callback = add_file_cb,
> -			.defval = (intptr_t) &base,
>  		},
>  		OPT_STRING('o', "output", &output, N_("file"),
>  			N_("write the archive to this file")),
> @@ -702,8 +717,6 @@ static int parse_archive_args(int argc, const char **argv,
>  		die(_("the option '%s' requires '%s'"), "--exec", "--remote");
>  	if (output)
>  		die(_("Unexpected option --output"));
> -	if (is_remote && args->extra_files.nr)
> -		die(_("options '%s' and '%s' cannot be used together"), "--add-file", "--remote");

How about moving the stat(2) call here instead?  It would still happen
before we start writing archive content, but after applying any
--no-add-file.  We can refuse to do it if is_remote is set.  The files
added using --add-file have content == NULL.

>  
>  	if (!base)
>  		base = "";
> diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
> index 3ad600c793..930a9fcb29 100755
> --- a/t/t5000-tar-tree.sh
> +++ b/t/t5000-tar-tree.sh
> @@ -237,6 +237,15 @@ check_tar with_untracked2
>  check_added with_untracked2 untracked one/untracked
>  check_added with_untracked2 untracked two/untracked
>  
> +test_expect_success 'git archive --remote rejects --add-file before stat' '
> +	test_must_fail git archive --remote=. --add-file=no-such-file HEAD 2>err &&
> +	test_grep "cannot be used together" err &&
> +	test_grep ! "File not found" err &&
> +	echo untracked >untracked &&
> +	test_must_fail git archive --remote=. --add-file=untracked --no-add-file HEAD 2>err &&
> +	test_grep "cannot be used together" err
> +'
> +
>  test_expect_success 'git archive on large files' '
>  	test_config core.bigfilethreshold 1 &&
>  	git archive HEAD >b3.tar &&


      reply	other threads:[~2026-11-21 15:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 19:23 [PATCH] archive: reject remote --add-file in callback Dominik Maier
2026-11-21 15:15 ` René Scharfe [this message]

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=389c4ebc-7c4e-4ded-a620-b6a63e3911a3@web.de \
    --to=l.s.r@web.de \
    --cc=dmnk+artist@google.com \
    --cc=dmnk@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=j6t@kdbg.org \
    --cc=jn.avila@free.fr \
    --cc=ps@pks.im \
    /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