{"thread":{"id":"66888","subject":"[PATCH] archive: reject remote --add-file in callback","startedAt":"2026-11-19T19:23:11Z","lastAt":"2026-11-19T19:23:11Z","messageCount":1,"participants":["Dominik Maier"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"558881","messageId":"20261119192311.2685319-1-dmnk@google.com","threadId":"66888","inReplyTo":null,"subject":"[PATCH] archive: reject remote --add-file in callback","fromName":"Dominik Maier","fromEmail":"dmnk@google.com","sentAt":"2026-11-19T19:23:11Z","receivedAt":"2026-11-19T19:23:11Z","isPatch":true,"sender":{"key":"dmnk@google.com","avatar":null},"body":"parse_archive_args() only checks \"is_remote && args->extra_files.nr\"\nafter parse_options() returns. When git-upload-archive parses client\narguments, add_file_cb() runs first and calls stat() on the requested\npath on the server filesystem, reporting \"File not found\" or \"Not a\nregular file\" before the \"--remote\" incompatibility check is reached\n(and succeeding altogether if \"--no-add-file\" follows).\n\nPass is_remote into add_file_cb() and reject \"--add-file\" and\n\"--add-virtual-file\" immediately before touching the filesystem.\n\nSigned-off-by: Dominik Maier <dmnk@google.com>\n---\n archive.c           | 31 ++++++++++++++++++++++---------\n t/t5000-tar-tree.sh |  9 +++++++++\n 2 files changed, 31 insertions(+), 9 deletions(-)\n\ndiff --git a/archive.c b/archive.c\nindex 59790be986..b983209ce4 100644\n--- a/archive.c\n+++ b/archive.c\n@@ -559,15 +559,27 @@ static void extra_file_info_clear(void *util, const char *str UNUSED)\n \tfree(info);\n }\n \n+struct add_file_data {\n+\tstruct archiver_args *args;\n+\tconst char **basep;\n+\tint is_remote;\n+};\n+\n static int add_file_cb(const struct option *opt, const char *arg, int unset)\n {\n-\tstruct archiver_args *args = opt->value;\n-\tconst char **basep = (const char **)opt->defval;\n-\tconst char *base = *basep;\n+\tstruct add_file_data *data = opt->value;\n+\tstruct archiver_args *args = data->args;\n+\tconst char *base = *data->basep;\n \tchar *path;\n \tstruct string_list_item *item;\n \tstruct extra_file_info *info;\n \n+\tif (data->is_remote)\n+\t\tdie(_(\"options '%s' and '%s' cannot be used together\"),\n+\t\t    !strcmp(opt->long_name, \"add-virtual-file\") ?\n+\t\t    \"--add-virtual-file\" : \"--add-file\",\n+\t\t    \"--remote\");\n+\n \tif (unset) {\n \t\tstring_list_clear_func(&args->extra_files,\n \t\t\t\t       extra_file_info_clear);\n@@ -645,6 +657,11 @@ static int parse_archive_args(int argc, const char **argv,\n \tint i;\n \tint list = 0;\n \tint worktree_attributes = 0;\n+\tstruct add_file_data add_file_data = {\n+\t\t.args = args,\n+\t\t.basep = &base,\n+\t\t.is_remote = is_remote,\n+\t};\n \tstruct option opts[] = {\n \t\tOPT_GROUP(\"\"),\n \t\tOPT_STRING(0, \"format\", &format, N_(\"fmt\"), N_(\"archive format\")),\n@@ -653,20 +670,18 @@ static int parse_archive_args(int argc, const char **argv,\n \t\t{\n \t\t\t.type = OPTION_CALLBACK,\n \t\t\t.long_name = \"add-file\",\n-\t\t\t.value = args,\n+\t\t\t.value = &add_file_data,\n \t\t\t.argh = N_(\"file\"),\n \t\t\t.help = N_(\"add untracked file to archive\"),\n \t\t\t.callback = add_file_cb,\n-\t\t\t.defval = (intptr_t) &base,\n \t\t},\n \t\t{\n \t\t\t.type = OPTION_CALLBACK,\n \t\t\t.long_name = \"add-virtual-file\",\n-\t\t\t.value = args,\n+\t\t\t.value = &add_file_data,\n \t\t\t.argh = N_(\"path:content\"),\n \t\t\t.help = N_(\"add untracked file to archive\"),\n \t\t\t.callback = add_file_cb,\n-\t\t\t.defval = (intptr_t) &base,\n \t\t},\n \t\tOPT_STRING('o', \"output\", &output, N_(\"file\"),\n \t\t\tN_(\"write the archive to this file\")),\n@@ -702,8 +717,6 @@ static int parse_archive_args(int argc, const char **argv,\n \t\tdie(_(\"the option '%s' requires '%s'\"), \"--exec\", \"--remote\");\n \tif (output)\n \t\tdie(_(\"Unexpected option --output\"));\n-\tif (is_remote && args->extra_files.nr)\n-\t\tdie(_(\"options '%s' and '%s' cannot be used together\"), \"--add-file\", \"--remote\");\n \n \tif (!base)\n \t\tbase = \"\";\ndiff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh\nindex 3ad600c793..930a9fcb29 100755\n--- a/t/t5000-tar-tree.sh\n+++ b/t/t5000-tar-tree.sh\n@@ -237,6 +237,15 @@ check_tar with_untracked2\n check_added with_untracked2 untracked one/untracked\n check_added with_untracked2 untracked two/untracked\n \n+test_expect_success 'git archive --remote rejects --add-file before stat' '\n+\ttest_must_fail git archive --remote=. --add-file=no-such-file HEAD 2>err &&\n+\ttest_grep \"cannot be used together\" err &&\n+\ttest_grep ! \"File not found\" err &&\n+\techo untracked >untracked &&\n+\ttest_must_fail git archive --remote=. --add-file=untracked --no-add-file HEAD 2>err &&\n+\ttest_grep \"cannot be used together\" err\n+'\n+\n test_expect_success 'git archive on large files' '\n \ttest_config core.bigfilethreshold 1 &&\n \tgit archive HEAD >b3.tar &&\n-- \n2.56.1.750.gf70dc682cf-goog\n\n\n"}]}