* [PATCH] archive: reject remote --add-file in callback
@ 2026-11-19 19:23 Dominik Maier
2026-11-21 15:15 ` René Scharfe
2026-11-24 18:05 ` [PATCH v2 0/2] archive: defer --add-file stat() and fix zip extra-file prefix handling Dominik Maier
0 siblings, 2 replies; 9+ messages in thread
From: Dominik Maier @ 2026-11-19 19:23 UTC (permalink / raw)
To: git
Cc: Junio C Hamano, Patrick Steinhardt, Johannes Sixt,
Jean-Noël Avila, dmnk+artist, Dominik Maier
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 <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");
+
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
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH] archive: reject remote --add-file in callback
2026-11-19 19:23 [PATCH] archive: reject remote --add-file in callback Dominik Maier
@ 2026-11-21 15:15 ` René Scharfe
2026-11-24 18:05 ` [PATCH v2 0/2] archive: defer --add-file stat() and fix zip extra-file prefix handling Dominik Maier
1 sibling, 0 replies; 9+ messages in thread
From: René Scharfe @ 2026-11-21 15:15 UTC (permalink / raw)
To: Dominik Maier, git
Cc: Junio C Hamano, Patrick Steinhardt, Johannes Sixt,
Jean-Noël Avila, dmnk+artist
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 &&
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v2 0/2] archive: defer --add-file stat() and fix zip extra-file prefix handling
2026-11-19 19:23 [PATCH] archive: reject remote --add-file in callback Dominik Maier
2026-11-21 15:15 ` René Scharfe
@ 2026-11-24 18:05 ` Dominik Maier
2026-11-24 18:05 ` [PATCH v2 1/2] archive-zip: fix prefix stripping for extra and virtual files Dominik Maier
2026-11-24 18:05 ` [PATCH v2 2/2] archive: defer --add-file stat() until after option parsing Dominik Maier
1 sibling, 2 replies; 9+ messages in thread
From: Dominik Maier @ 2026-11-24 18:05 UTC (permalink / raw)
To: git; +Cc: René Scharfe, Junio C Hamano, dmnk+artist
Following René's suggestion on v1, patch 2/2 moves the --add-file
stat() check into parse_archive_args() after parse_options() so
--no-add-file can cancel an earlier --add-file and --add-virtual-file
works over --remote.
Testing --remote --add-virtual-file with --format=zip turned up an
out-of-bounds pointer advance in write_zip_entry() when --prefix is
longer than an extra or virtual file's path (path + args->baselen), so
patch 1/2 fixes that first.
Dominik Maier (2):
archive-zip: fix prefix stripping for extra and virtual files
archive: defer --add-file stat() until after option parsing
archive-zip.c | 5 +++--
archive.c | 28 +++++++++++++++++++++-------
t/t5000-tar-tree.sh | 24 ++++++++++++++++++++++++
t/t5003-archive-zip.sh | 5 +++++
4 files changed, 53 insertions(+), 9 deletions(-)
Range-diff against v1:
-: ---------- > 1: 72c0d0c0a4 archive-zip: fix prefix stripping for extra and virtual files
1: c51e62800c ! 2: 543109b748 archive: reject remote --add-file in callback
@@ Metadata
Author: Dominik Maier <dmnk@google.com>
## Commit message ##
- archive: reject remote --add-file in callback
+ archive: defer --add-file stat() until after option parsing
- 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).
+ add_file_cb() calls stat() on each --add-file argument while
+ parse_options() is still running, before parse_archive_args() checks
+ is_remote. When git-upload-archive parses client arguments, it probes
+ the requested path on the server filesystem and reports "File not found"
+ or "Not a regular file" instead of rejecting --add-file with --remote.
- Pass is_remote into add_file_cb() and reject "--add-file" and
- "--add-virtual-file" immediately before touching the filesystem.
+ Move the stat() and regular-file checks into parse_archive_args() after
+ parse_options() finishes, refusing entries with a NULL content pointer
+ when is_remote is set. This rejects remote --add-file before touching
+ the filesystem, allows a later --no-add-file to cancel an earlier
+ --add-file, and lets --add-virtual-file work with --remote since its
+ content is supplied inline.
+ Suggested-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Dominik Maier <dmnk@google.com>
## archive.c ##
-@@ archive.c: static void extra_file_info_clear(void *util, const char *str UNUSED)
- free(info);
- }
+@@ archive.c: static int add_file_cb(const struct option *opt, const char *arg, int unset)
-+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);
+ if (!strcmp(opt->long_name, "add-file")) {
+ path = prefix_filename(args->prefix, arg);
+- if (stat(path, &info->stat))
+- die(_("File not found: %s"), path);
+- if (!S_ISREG(info->stat.st_mode))
+- die(_("Not a regular file: %s"), path);
+- info->content = NULL; /* read the file later */
++ memset(&info->stat, 0, sizeof(info->stat));
++ info->content = NULL; /* stat and read the file later */
+ } else if (!strcmp(opt->long_name, "add-virtual-file")) {
+ struct strbuf buf = STRBUF_INIT;
+ const char *p = arg;
@@ archive.c: static int parse_archive_args(int argc, const char **argv,
+ const char *mtime_option = NULL;
+ int compression_level = -1;
+ int verbose = 0;
++ struct string_list_item *item;
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")),
-@@ archive.c: 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")),
@@ archive.c: 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");
++ for_each_string_list_item(item, &args->extra_files) {
++ const char *path = item->string;
++ struct extra_file_info *info = item->util;
++
++ if (info->content)
++ continue;
++ if (is_remote)
++ die(_("options '%s' and '%s' cannot be used together"),
++ "--add-file", "--remote");
++ if (stat(path, &info->stat))
++ die(_("File not found: %s"), path);
++ if (!S_ISREG(info->stat.st_mode))
++ die(_("Not a regular file: %s"), path);
++ }
if (!base)
base = "";
@@ t/t5000-tar-tree.sh: 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_expect_success 'git archive --remote rejects --add-file without 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_must_fail git archive --remote=. --add-file=. HEAD 2>err &&
++ test_grep "cannot be used together" err &&
++ test_grep ! "Not a regular file" err
+'
++
++test_expect_success 'git archive allows --no-add-file after missing --add-file' '
++ git archive --add-file=no-such-file --no-add-file HEAD >canceled.tar &&
++ test_cmp_bin b.tar canceled.tar &&
++ git archive --remote=. --add-file=no-such-file --no-add-file HEAD >remote-canceled.tar &&
++ test_cmp_bin b.tar remote-canceled.tar
++'
++
++test_expect_success 'git archive --remote with --add-virtual-file' '
++ printf virtual >expected-virtual &&
++ git archive --remote=. --add-virtual-file=virtual:virtual HEAD >remote_virtual.tar
++'
++
++check_tar remote_virtual
++check_added remote_virtual expected-virtual virtual
+
test_expect_success 'git archive on large files' '
test_config core.bigfilethreshold 1 &&
--
2.56.1.831.g720f3f5937-goog
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v2 1/2] archive-zip: fix prefix stripping for extra and virtual files
2026-11-24 18:05 ` [PATCH v2 0/2] archive: defer --add-file stat() and fix zip extra-file prefix handling Dominik Maier
@ 2026-11-24 18:05 ` Dominik Maier
2026-11-25 0:09 ` Junio C Hamano
2026-11-24 18:05 ` [PATCH v2 2/2] archive: defer --add-file stat() until after option parsing Dominik Maier
1 sibling, 1 reply; 9+ messages in thread
From: Dominik Maier @ 2026-11-24 18:05 UTC (permalink / raw)
To: git; +Cc: René Scharfe, Junio C Hamano, dmnk+artist
write_zip_entry() unconditionally computes path + args->baselen before
passing path_without_prefix to entry_is_binary(). That matches tracked
tree entries, which always begin with the final --prefix stored in
args->base.
Extra files added via --add-file use the per-entry prefix recorded when
the option was parsed (info->base), and entries from --add-virtual-file
do not carry --prefix at all. When a later --prefix is longer than the
extra or virtual file's archive path, path + args->baselen steps past
the end of path (and for --add-virtual-file, looks up worktree
gitattributes along an arbitrary virtual path).
Update args->base and args->baselen to each --add-file entry's recorded
prefix before calling write_entry(), and clear args->base to NULL for
--add-virtual-file entries so entry_is_binary() falls back to content
inspection instead of path attribute lookup.
Signed-off-by: Dominik Maier <dmnk@google.com>
---
archive-zip.c | 5 +++--
archive.c | 4 ++++
t/t5003-archive-zip.sh | 5 +++++
3 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/archive-zip.c b/archive-zip.c
index 279c3a2d72..db4a96553b 100644
--- a/archive-zip.c
+++ b/archive-zip.c
@@ -271,7 +271,8 @@ static int has_only_ascii(const char *s)
static int entry_is_binary(struct index_state *istate, const char *path,
const void *buffer, size_t size)
{
- struct userdiff_driver *driver = userdiff_find_by_path(istate, path);
+ struct userdiff_driver *driver = path ?
+ userdiff_find_by_path(istate, path) : NULL;
if (!driver)
driver = userdiff_find_by_name("default");
if (driver->binary != -1)
@@ -301,7 +302,7 @@ static int write_zip_entry(struct archiver_args *args,
struct odb_stream *stream = NULL;
unsigned long flags = 0;
int is_binary = -1;
- const char *path_without_prefix = path + args->baselen;
+ const char *path_without_prefix = args->base ? path + args->baselen : NULL;
unsigned int creator_version = 0;
unsigned int version_needed = 10;
size_t zip_dir_extra_size = zip_extra_mtime.len;
diff --git a/archive.c b/archive.c
index 59790be986..2348ab12ed 100644
--- a/archive.c
+++ b/archive.c
@@ -351,6 +351,8 @@ int write_archive_entries(struct archiver_args *args,
put_be64(fake_oid.hash, i + 1);
if (!info->content) {
+ args->base = info->base ? info->base : "";
+ args->baselen = strlen(args->base);
strbuf_reset(&path_in_archive);
if (info->base)
strbuf_addstr(&path_in_archive, info->base);
@@ -365,6 +367,8 @@ int write_archive_entries(struct archiver_args *args,
canon_mode(info->stat.st_mode),
content.buf, content.len);
} else {
+ args->base = NULL;
+ args->baselen = 0;
err = write_entry(args, &fake_oid,
path, strlen(path),
canon_mode(info->stat.st_mode),
diff --git a/t/t5003-archive-zip.sh b/t/t5003-archive-zip.sh
index c8c1c5c06b..75e3b331af 100755
--- a/t/t5003-archive-zip.sh
+++ b/t/t5003-archive-zip.sh
@@ -229,6 +229,11 @@ test_expect_success UNZIP 'git archive --format=zip --add-virtual-file' '
)
'
+test_expect_success 'git archive --format=zip --prefix longer than --add-virtual-file path' '
+ git archive --format=zip --prefix=very-long-archive-prefix/ \
+ --add-virtual-file=a:hello $EMPTY_TREE >short_virtual.zip
+'
+
test_expect_success 'git archive --format=zip --add-file twice' '
echo untracked >untracked &&
git archive --format=zip --prefix=one/ --add-file=untracked \
--
2.56.1.831.g720f3f5937-goog
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v2 1/2] archive-zip: fix prefix stripping for extra and virtual files
2026-11-24 18:05 ` [PATCH v2 1/2] archive-zip: fix prefix stripping for extra and virtual files Dominik Maier
@ 2026-11-25 0:09 ` Junio C Hamano
2026-11-25 11:39 ` René Scharfe
0 siblings, 1 reply; 9+ messages in thread
From: Junio C Hamano @ 2026-11-25 0:09 UTC (permalink / raw)
To: Dominik Maier; +Cc: git, René Scharfe, dmnk+artist
Dominik Maier <dmnk@google.com> writes:
> write_zip_entry() unconditionally computes path + args->baselen before
> passing path_without_prefix to entry_is_binary(). That matches tracked
> tree entries, which always begin with the final --prefix stored in
> args->base.
>
> Extra files added via --add-file use the per-entry prefix recorded when
> the option was parsed (info->base), and entries from --add-virtual-file
> do not carry --prefix at all. When a later --prefix is longer than the
> extra or virtual file's archive path, path + args->baselen steps past
> the end of path (and for --add-virtual-file, looks up worktree
> gitattributes along an arbitrary virtual path).
Because by default parse_treeish_arg() directs attribute lookups to the
index with GIT_ATTR_INDEX, and only consults worktree attributes when
--worktree-attributes is passed, saying just "gitattributes", not
"worktree gitattributes", would be slightly more accurate, I think.
It does not affect the conclusion, though ;-)
> diff --git a/archive-zip.c b/archive-zip.c
> index 279c3a2d72..db4a96553b 100644
> --- a/archive-zip.c
> +++ b/archive-zip.c
> @@ -271,7 +271,8 @@ static int has_only_ascii(const char *s)
> static int entry_is_binary(struct index_state *istate, const char *path,
> const void *buffer, size_t size)
> {
> - struct userdiff_driver *driver = userdiff_find_by_path(istate, path);
> + struct userdiff_driver *driver = path ?
> + userdiff_find_by_path(istate, path) : NULL;
Checking path == NULL case here may make it easy for readers to see what
is going on, but because userdiff_find_by_path() already returns NULL
when path is NULL, the only effect of this change is that we now avoid
the overhead of initializing the static attr_check, when only virtual
files are checked, in userdiff_find_by_path().
As an optimizaiton, swapping the check of !path and one-time
initializing of attr_check in userdiff_find_by_path() may be more
effective.
Either way, falling back to the "default" driver (whose '.binary' is -1
unless configured) correctly drops through to buffer_is_binary().
> @@ -365,6 +367,8 @@ int write_archive_entries(struct archiver_args *args,
> canon_mode(info->stat.st_mode),
> content.buf, content.len);
> } else {
> + args->base = NULL;
> + args->baselen = 0;
> err = write_entry(args, &fake_oid,
> path, strlen(path),
> canon_mode(info->stat.st_mode),
For entries added by '--add-virtual-file', clearing '.base' to NULL
stops write_zip_entry() from doing path + args->baselen or attribute
lookup, and the next '--add-file' iteration restores '.base' and
'.baselen' if needed.
Mutating args->base and args->baselen inside this loop leaves them
pointing at the last extra file's prefix (or NULL) when
write_archive_entries() returns. None of the current callers in
archive-zip.c, archive-tar.c, or write_archive() inspect them afterward,
and extra_file_info_clear() runs only after write_archive() finishes, so
this is safe as-is, though saving and restoring them around the loop may
avoid surprising any future caller.
Overall, nicely done.
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH v2 1/2] archive-zip: fix prefix stripping for extra and virtual files
2026-11-25 0:09 ` Junio C Hamano
@ 2026-11-25 11:39 ` René Scharfe
2026-11-25 17:42 ` Junio C Hamano
0 siblings, 1 reply; 9+ messages in thread
From: René Scharfe @ 2026-11-25 11:39 UTC (permalink / raw)
To: Junio C Hamano, Dominik Maier; +Cc: git, dmnk+artist
On 11/25/26 1:09 AM, Junio C Hamano wrote:
> Dominik Maier <dmnk@google.com> writes:
>
>> write_zip_entry() unconditionally computes path + args->baselen before
>> passing path_without_prefix to entry_is_binary(). That matches tracked
>> tree entries, which always begin with the final --prefix stored in
>> args->base.
>>
>> Extra files added via --add-file use the per-entry prefix recorded when
>> the option was parsed (info->base), and entries from --add-virtual-file
>> do not carry --prefix at all. When a later --prefix is longer than the
>> extra or virtual file's archive path, path + args->baselen steps past
>> the end of path (and for --add-virtual-file, looks up worktree
>> gitattributes along an arbitrary virtual path).
Oh, good find and straightforward fix.
> Because by default parse_treeish_arg() directs attribute lookups to the
> index with GIT_ATTR_INDEX, and only consults worktree attributes when
> --worktree-attributes is passed, saying just "gitattributes", not
> "worktree gitattributes", would be slightly more accurate, I think.
>
> It does not affect the conclusion, though ;-)
>
>> diff --git a/archive-zip.c b/archive-zip.c
>> index 279c3a2d72..db4a96553b 100644
>> --- a/archive-zip.c
>> +++ b/archive-zip.c
>> @@ -271,7 +271,8 @@ static int has_only_ascii(const char *s)
>> static int entry_is_binary(struct index_state *istate, const char *path,
>> const void *buffer, size_t size)
>> {
>> - struct userdiff_driver *driver = userdiff_find_by_path(istate, path);
>> + struct userdiff_driver *driver = path ?
>> + userdiff_find_by_path(istate, path) : NULL;
>
> Checking path == NULL case here may make it easy for readers to see what
> is going on, but because userdiff_find_by_path() already returns NULL
> when path is NULL, the only effect of this change is that we now avoid
> the overhead of initializing the static attr_check, when only virtual
> files are checked, in userdiff_find_by_path().
>
> As an optimizaiton, swapping the check of !path and one-time
> initializing of attr_check in userdiff_find_by_path() may be more
> effective.
That would help workloads that only ever call userdiff_find_by_path()
without a path. "git archive --add-virtual-file=foo:bar $EMPTY_TREE"
is the only example I can come up with on the spot. It would also skip
the sanity checks in attr_check_initl() for those workloads. Not sure
if that's a good deal.
> Either way, falling back to the "default" driver (whose '.binary' is -1
> unless configured) correctly drops through to buffer_is_binary().
>
>> @@ -365,6 +367,8 @@ int write_archive_entries(struct archiver_args *args,
>> canon_mode(info->stat.st_mode),
>> content.buf, content.len);
>> } else {
>> + args->base = NULL;
>> + args->baselen = 0;
>> err = write_entry(args, &fake_oid,
>> path, strlen(path),
>> canon_mode(info->stat.st_mode),
>
> For entries added by '--add-virtual-file', clearing '.base' to NULL
> stops write_zip_entry() from doing path + args->baselen or attribute
> lookup, and the next '--add-file' iteration restores '.base' and
> '.baselen' if needed.
>
> Mutating args->base and args->baselen inside this loop leaves them
> pointing at the last extra file's prefix (or NULL) when
> write_archive_entries() returns. None of the current callers in
> archive-zip.c, archive-tar.c, or write_archive() inspect them afterward,
> and extra_file_info_clear() runs only after write_archive() finishes, so
> this is safe as-is, though saving and restoring them around the loop may
> avoid surprising any future caller.
I wonder if it's time for a struct archive_entry prepared by
write_archive_entry() to avoid per-entry changes to struct archiver_args
and pointer arithmetic in the tar and zip handlers. Out of scope for
this series, of course.
René
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH v2 1/2] archive-zip: fix prefix stripping for extra and virtual files
2026-11-25 11:39 ` René Scharfe
@ 2026-11-25 17:42 ` Junio C Hamano
0 siblings, 0 replies; 9+ messages in thread
From: Junio C Hamano @ 2026-11-25 17:42 UTC (permalink / raw)
To: René Scharfe; +Cc: Dominik Maier, git, dmnk+artist
René Scharfe <l.s.r@web.de> writes:
> On 11/25/26 1:09 AM, Junio C Hamano wrote:
>> Dominik Maier <dmnk@google.com> writes:
>>
>>> write_zip_entry() unconditionally computes path + args->baselen before
>>> passing path_without_prefix to entry_is_binary(). That matches tracked
>>> tree entries, which always begin with the final --prefix stored in
>>> args->base.
>>>
>>> Extra files added via --add-file use the per-entry prefix recorded when
>>> the option was parsed (info->base), and entries from --add-virtual-file
>>> do not carry --prefix at all. When a later --prefix is longer than the
>>> extra or virtual file's archive path, path + args->baselen steps past
>>> the end of path (and for --add-virtual-file, looks up worktree
>>> gitattributes along an arbitrary virtual path).
>
> Oh, good find and straightforward fix.
Indeed.
> That would help workloads that only ever call userdiff_find_by_path()
> without a path. "git archive --add-virtual-file=foo:bar $EMPTY_TREE"
> is the only example I can come up with on the spot. It would also skip
> the sanity checks in attr_check_initl() for those workloads. Not sure
> if that's a good deal.
I am not sure either. My point was that the above rewrite to avoid
calling userdiff_find_by_path with path==NULL does not work as a great
optimization (and if the author wanted an optimization, it should not be
done here but in userdiff_find_by_path()).
>> Mutating args->base and args->baselen inside this loop leaves them
>> pointing at the last extra file's prefix (or NULL) when
>> write_archive_entries() returns. None of the current callers in
>> archive-zip.c, archive-tar.c, or write_archive() inspect them afterward,
>> and extra_file_info_clear() runs only after write_archive() finishes, so
>> this is safe as-is, though saving and restoring them around the loop may
>> avoid surprising any future caller.
> ...
> I wonder if it's time for a struct archive_entry prepared by
> write_archive_entry() to avoid per-entry changes to struct archiver_args
> and pointer arithmetic in the tar and zip handlers. Out of scope for
> this series, of course.
Yes, but I suspect that it would be a good longer-term direction.
Thanks.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 2/2] archive: defer --add-file stat() until after option parsing
2026-11-24 18:05 ` [PATCH v2 0/2] archive: defer --add-file stat() and fix zip extra-file prefix handling Dominik Maier
2026-11-24 18:05 ` [PATCH v2 1/2] archive-zip: fix prefix stripping for extra and virtual files Dominik Maier
@ 2026-11-24 18:05 ` Dominik Maier
2026-11-25 0:24 ` Junio C Hamano
1 sibling, 1 reply; 9+ messages in thread
From: Dominik Maier @ 2026-11-24 18:05 UTC (permalink / raw)
To: git; +Cc: René Scharfe, Junio C Hamano, dmnk+artist
add_file_cb() calls stat() on each --add-file argument while
parse_options() is still running, before parse_archive_args() checks
is_remote. When git-upload-archive parses client arguments, it probes
the requested path on the server filesystem and reports "File not found"
or "Not a regular file" instead of rejecting --add-file with --remote.
Move the stat() and regular-file checks into parse_archive_args() after
parse_options() finishes, refusing entries with a NULL content pointer
when is_remote is set. This rejects remote --add-file before touching
the filesystem, allows a later --no-add-file to cancel an earlier
--add-file, and lets --add-virtual-file work with --remote since its
content is supplied inline.
Suggested-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Dominik Maier <dmnk@google.com>
---
archive.c | 24 +++++++++++++++++-------
t/t5000-tar-tree.sh | 24 ++++++++++++++++++++++++
2 files changed, 41 insertions(+), 7 deletions(-)
diff --git a/archive.c b/archive.c
index 2348ab12ed..413fc1cd11 100644
--- a/archive.c
+++ b/archive.c
@@ -586,11 +586,8 @@ static int add_file_cb(const struct option *opt, const char *arg, int unset)
if (!strcmp(opt->long_name, "add-file")) {
path = prefix_filename(args->prefix, arg);
- if (stat(path, &info->stat))
- die(_("File not found: %s"), path);
- if (!S_ISREG(info->stat.st_mode))
- die(_("Not a regular file: %s"), path);
- info->content = NULL; /* read the file later */
+ memset(&info->stat, 0, sizeof(info->stat));
+ info->content = NULL; /* stat and read the file later */
} else if (!strcmp(opt->long_name, "add-virtual-file")) {
struct strbuf buf = STRBUF_INIT;
const char *p = arg;
@@ -646,6 +643,7 @@ static int parse_archive_args(int argc, const char **argv,
const char *mtime_option = NULL;
int compression_level = -1;
int verbose = 0;
+ struct string_list_item *item;
int i;
int list = 0;
int worktree_attributes = 0;
@@ -706,8 +704,20 @@ 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");
+ for_each_string_list_item(item, &args->extra_files) {
+ const char *path = item->string;
+ struct extra_file_info *info = item->util;
+
+ if (info->content)
+ continue;
+ if (is_remote)
+ die(_("options '%s' and '%s' cannot be used together"),
+ "--add-file", "--remote");
+ if (stat(path, &info->stat))
+ die(_("File not found: %s"), path);
+ if (!S_ISREG(info->stat.st_mode))
+ die(_("Not a regular file: %s"), path);
+ }
if (!base)
base = "";
diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
index 3ad600c793..c2bead12e9 100755
--- a/t/t5000-tar-tree.sh
+++ b/t/t5000-tar-tree.sh
@@ -237,6 +237,30 @@ 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 without 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 &&
+ test_must_fail git archive --remote=. --add-file=. HEAD 2>err &&
+ test_grep "cannot be used together" err &&
+ test_grep ! "Not a regular file" err
+'
+
+test_expect_success 'git archive allows --no-add-file after missing --add-file' '
+ git archive --add-file=no-such-file --no-add-file HEAD >canceled.tar &&
+ test_cmp_bin b.tar canceled.tar &&
+ git archive --remote=. --add-file=no-such-file --no-add-file HEAD >remote-canceled.tar &&
+ test_cmp_bin b.tar remote-canceled.tar
+'
+
+test_expect_success 'git archive --remote with --add-virtual-file' '
+ printf virtual >expected-virtual &&
+ git archive --remote=. --add-virtual-file=virtual:virtual HEAD >remote_virtual.tar
+'
+
+check_tar remote_virtual
+check_added remote_virtual expected-virtual virtual
+
test_expect_success 'git archive on large files' '
test_config core.bigfilethreshold 1 &&
git archive HEAD >b3.tar &&
--
2.56.1.831.g720f3f5937-goog
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v2 2/2] archive: defer --add-file stat() until after option parsing
2026-11-24 18:05 ` [PATCH v2 2/2] archive: defer --add-file stat() until after option parsing Dominik Maier
@ 2026-11-25 0:24 ` Junio C Hamano
0 siblings, 0 replies; 9+ messages in thread
From: Junio C Hamano @ 2026-11-25 0:24 UTC (permalink / raw)
To: Dominik Maier; +Cc: git, René Scharfe, dmnk+artist
Dominik Maier <dmnk@google.com> writes:
> add_file_cb() calls stat() on each --add-file argument while
> parse_options() is still running, before parse_archive_args() checks
> is_remote. When git-upload-archive parses client arguments, it probes
> the requested path on the server filesystem and reports "File not found"
> or "Not a regular file" instead of rejecting --add-file with --remote.
>
> Move the stat() and regular-file checks into parse_archive_args() after
> parse_options() finishes, refusing entries with a NULL content pointer
> when is_remote is set. This rejects remote --add-file before touching
> the filesystem, allows a later --no-add-file to cancel an earlier
> --add-file, and lets --add-virtual-file work with --remote since its
> content is supplied inline.
>
> Suggested-by: René Scharfe <l.s.r@web.de>
> Signed-off-by: Dominik Maier <dmnk@google.com>
> ---
> archive.c | 24 +++++++++++++++++-------
> t/t5000-tar-tree.sh | 24 ++++++++++++++++++++++++
> 2 files changed, 41 insertions(+), 7 deletions(-)
This seems to follow René's suggestion cleanly and resolves all three
issues without touching the filesystem when --remote or --no-add-file is
used.
> diff --git a/archive.c b/archive.c
> index 2348ab12ed..413fc1cd11 100644
> --- a/archive.c
> +++ b/archive.c
> @@ -586,11 +586,8 @@ static int add_file_cb(const struct option *opt, const char *arg, int unset)
>
> if (!strcmp(opt->long_name, "add-file")) {
> path = prefix_filename(args->prefix, arg);
> - if (stat(path, &info->stat))
> - die(_("File not found: %s"), path);
> - if (!S_ISREG(info->stat.st_mode))
> - die(_("Not a regular file: %s"), path);
> - info->content = NULL; /* read the file later */
> + memset(&info->stat, 0, sizeof(info->stat));
> + info->content = NULL; /* stat and read the file later */
Keeping prefix_filename() in add_file_cb() preserves relative path
resolution when invoked from a subdirectory, while deferring stat(2).
> @@ -706,8 +704,20 @@ 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");
> + for_each_string_list_item(item, &args->extra_files) {
> + const char *path = item->string;
> + struct extra_file_info *info = item->util;
> +
> + if (info->content)
> + continue;
> + if (is_remote)
> + die(_("options '%s' and '%s' cannot be used together"),
> + "--add-file", "--remote");
> + if (stat(path, &info->stat))
> + die(_("File not found: %s"), path);
> + if (!S_ISREG(info->stat.st_mode))
> + die(_("Not a regular file: %s"), path);
> + }
setup_git_directory() is called only after parse_archive_args() returns
to write_archive(), so the working directory has not changed between
add_file_cb() and this loop, which means 'path' is correct. Entries
added by '--add-virtual-file' always have a non-NULL '.content' from
xstrdup(), so skipping them here lets '--remote' work without touching
the server filesystem.
Looking good.
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-11-25 17:42 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 19:23 [PATCH] archive: reject remote --add-file in callback Dominik Maier
2026-11-21 15:15 ` René Scharfe
2026-11-24 18:05 ` [PATCH v2 0/2] archive: defer --add-file stat() and fix zip extra-file prefix handling Dominik Maier
2026-11-24 18:05 ` [PATCH v2 1/2] archive-zip: fix prefix stripping for extra and virtual files Dominik Maier
2026-11-25 0:09 ` Junio C Hamano
2026-11-25 11:39 ` René Scharfe
2026-11-25 17:42 ` Junio C Hamano
2026-11-24 18:05 ` [PATCH v2 2/2] archive: defer --add-file stat() until after option parsing Dominik Maier
2026-11-25 0:24 ` Junio C Hamano
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox