From: Dominik Maier Date: Thu, 19 Nov 2026 19:23:11 GMT Subject: [PATCH] archive: reject remote --add-file in callback Message-ID: <20261119192311.2685319-1-dmnk@google.com> 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). Pass is_remote into add_file_cb() and reject "--add-file" and "--add-virtual-file" immediately before touching the filesystem. Signed-off-by: Dominik Maier --- 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"); + 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"); 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 && -- 2.56.1.750.gf70dc682cf-goog