# [PATCH 0/1] Fix format detection when archiving remotely

20 messages from 2018-10-19 to 2018-10-26. Participants: steadmon@google.com, Jeff King, Junio C Hamano, Josh Steadmon.
Thread: https://gitlist.dev/t/49621

## steadmon@google.com, 2018-10-19 23:19

Subject: [PATCH 0/1] Fix format detection when archiving remotely
Message-ID: <cover.1539990488.git.steadmon@google.com>
URL: https://gitlist.dev/e/cover.1539990488.git.steadmon%40google.com

```
Currently, git-archive does not properly determine the desired archive
format when both --output and --remote are provided, because
run_remote_archiver() does not initialize the archivers prior to calling
archive_format_from_filename(). This results in the remote archiver
always returning a TAR file, regardless of the requested format.

This patch initializes the TAR and ZIP archivers before calling
archive_format_from_filename(), which fixes format detection.

Steps to reproduce:

∫ git version
git version 2.19.1.568.g152ad8e336-goog
∫ cd ~/src/git
∫ git archive --output ~/good.zip HEAD
∫ file ~/good.zip
/home/steadmon/good.zip: Zip archive data, at least v1.0 to extract
∫ git archive --output ~/bad.zip --remote=. HEAD
∫ file ~/bad.zip
/home/steadmon/bad.zip: POSIX tar archive

(apply patch and build)

∫ ./bin-wrappers/git archive --output ~/fixed.zip --remote=. HEAD
∫ file ~/fixed.zip
/home/steadmon/fixed.zip: Zip archive data, at least v1.0 to extract


Josh Steadmon (1):
  archive: init archivers before determining format

 builtin/archive.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

-- 
2.19.1.568.g152ad8e336-goog


```

## steadmon@google.com, 2018-10-19 23:19

Subject: [PATCH 1/1] archive: init archivers before determining format
Message-ID: <b1f8e288dde9a9dd46386524189da66c7ad2c333.1539990488.git.steadmon@google.com>
URL: https://gitlist.dev/e/b1f8e288dde9a9dd46386524189da66c7ad2c333.1539990488.git.steadmon%40google.com
In-Reply-To: <cover.1539990488.git.steadmon@google.com>

```
When passing both --remote and --output to git-archive, initialize the
archivers before attempting to determine the format from the output
filename. Without initialization, the format cannot be determined.

Signed-off-by: Josh Steadmon <steadmon@google.com>
---
 builtin/archive.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/builtin/archive.c b/builtin/archive.c
index e74f675390..dd3283a247 100644
--- a/builtin/archive.c
+++ b/builtin/archive.c
@@ -45,7 +45,10 @@ static int run_remote_archiver(int argc, const char **argv,
 	 * it.
 	 */
 	if (name_hint) {
-		const char *format = archive_format_from_filename(name_hint);
+		const char *format;
+		init_tar_archiver();
+		init_zip_archiver();
+		format = archive_format_from_filename(name_hint);
 		if (format)
 			packet_write_fmt(fd[1], "argument --format=%s\n", format);
 	}
-- 
2.19.1.568.g152ad8e336-goog


```

## Jeff King, 2018-10-19 23:41

Subject: Re: [PATCH 0/1] Fix format detection when archiving remotely
Message-ID: <20181019234126.GA7326@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20181019234126.GA7326%40sigill.intra.peff.net
In-Reply-To: <cover.1539990488.git.steadmon@google.com>

```
On Fri, Oct 19, 2018 at 04:19:27PM -0700, steadmon@google.com wrote:

> Currently, git-archive does not properly determine the desired archive
> format when both --output and --remote are provided, because
> run_remote_archiver() does not initialize the archivers prior to calling
> archive_format_from_filename(). This results in the remote archiver
> always returning a TAR file, regardless of the requested format.
> 
> This patch initializes the TAR and ZIP archivers before calling
> archive_format_from_filename(), which fixes format detection.

It seems like some of this content could be in the commit message of the
actual patch.

> Steps to reproduce:
> 
> ∫ git version
> git version 2.19.1.568.g152ad8e336-goog
> ∫ cd ~/src/git
> ∫ git archive --output ~/good.zip HEAD
> ∫ file ~/good.zip
> /home/steadmon/good.zip: Zip archive data, at least v1.0 to extract
> ∫ git archive --output ~/bad.zip --remote=. HEAD
> ∫ file ~/bad.zip
> /home/steadmon/bad.zip: POSIX tar archive

And this could be in a test script in the actual patch. :)

-Peff

```

## Jeff King, 2018-10-19 23:59

Subject: Re: [PATCH 1/1] archive: init archivers before determining format
Message-ID: <20181019235945.GB7326@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20181019235945.GB7326%40sigill.intra.peff.net
In-Reply-To: <b1f8e288dde9a9dd46386524189da66c7ad2c333.1539990488.git.steadmon@google.com>

```
On Fri, Oct 19, 2018 at 04:19:28PM -0700, steadmon@google.com wrote:

> diff --git a/builtin/archive.c b/builtin/archive.c
> index e74f675390..dd3283a247 100644
> --- a/builtin/archive.c
> +++ b/builtin/archive.c
> @@ -45,7 +45,10 @@ static int run_remote_archiver(int argc, const char **argv,
>  	 * it.
>  	 */
>  	if (name_hint) {
> -		const char *format = archive_format_from_filename(name_hint);
> +		const char *format;
> +		init_tar_archiver();
> +		init_zip_archiver();
> +		format = archive_format_from_filename(name_hint);
>  		if (format)
>  			packet_write_fmt(fd[1], "argument --format=%s\n", format);

Hrm. This code was added back in 56baa61d01 (archive: move file
extension format-guessing lower, 2011-06-21), and your example
invocation worked back then!

Unfortunately it was broken by the very next patch in the series,
08716b3c11 (archive: refactor file extension format-guessing,
2011-06-21). I guess that's what I get for not adding regression tests.

It's probably worth mentioning those points in the commit message.

Does this work with configured archiver extensions, too? I think so,
because we load them via init_tar_archiver().

Can we avoid repeating the list of archivers here? This needs to stay in
sync with the list in write_archive(). I know there are only two, but
can we factor out an init_archivers() call or something?

We also should probably just call it unconditionally when we start the
archiver command (I don't think there are any other bugs like this
lurking, but it doesn't cost very much to initialize these; it makes
sense to just do it early).

Other than those minor points (and the lack of test), your fix looks
good to me.

-Peff

```

## Junio C Hamano, 2018-10-22 03:24

Subject: Re: [PATCH 1/1] archive: init archivers before determining format
Message-ID: <xmqq4ldey876.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqq4ldey876.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <20181019235945.GB7326@sigill.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> On Fri, Oct 19, 2018 at 04:19:28PM -0700, steadmon@google.com wrote:
>
>> diff --git a/builtin/archive.c b/builtin/archive.c
>> index e74f675390..dd3283a247 100644
>> --- a/builtin/archive.c
>> +++ b/builtin/archive.c
>> @@ -45,7 +45,10 @@ static int run_remote_archiver(int argc, const char **argv,
>>  	 * it.
>>  	 */
>>  	if (name_hint) {
>> -		const char *format = archive_format_from_filename(name_hint);
>> +		const char *format;
>> +		init_tar_archiver();
>> +		init_zip_archiver();
>> +		format = archive_format_from_filename(name_hint);
>>  		if (format)
>>  			packet_write_fmt(fd[1], "argument --format=%s\n", format);
>
> Hrm. This code was added back in 56baa61d01 (archive: move file
> extension format-guessing lower, 2011-06-21), and your example
> invocation worked back then!
>
> Unfortunately it was broken by the very next patch in the series,
> 08716b3c11 (archive: refactor file extension format-guessing,
> 2011-06-21). I guess that's what I get for not adding regression tests.
>
> It's probably worth mentioning those points in the commit message.
>
> Does this work with configured archiver extensions, too? I think so,
> because we load them via init_tar_archiver().
>
> Can we avoid repeating the list of archivers here? This needs to stay in
> sync with the list in write_archive(). I know there are only two, but
> can we factor out an init_archivers() call or something?
>
> We also should probably just call it unconditionally when we start the
> archiver command (I don't think there are any other bugs like this
> lurking, but it doesn't cost very much to initialize these; it makes
> sense to just do it early).
>
> Other than those minor points (and the lack of test), your fix looks
> good to me.

Thanks for a patch and an excellent review.  Looking forward to the
finalized version.

Thanks, both.

```

## Josh Steadmon, 2018-10-22 21:46

Subject: Re: [PATCH 0/1] Fix format detection when archiving remotely
Message-ID: <20181022214617.GB233961@google.com>
URL: https://gitlist.dev/e/20181022214617.GB233961%40google.com
In-Reply-To: <20181019234126.GA7326@sigill.intra.peff.net>

```
On 2018.10.19 19:41, Jeff King wrote:
> On Fri, Oct 19, 2018 at 04:19:27PM -0700, steadmon@google.com wrote:
> 
> > Currently, git-archive does not properly determine the desired archive
> > format when both --output and --remote are provided, because
> > run_remote_archiver() does not initialize the archivers prior to calling
> > archive_format_from_filename(). This results in the remote archiver
> > always returning a TAR file, regardless of the requested format.
> > 
> > This patch initializes the TAR and ZIP archivers before calling
> > archive_format_from_filename(), which fixes format detection.
> 
> It seems like some of this content could be in the commit message of the
> actual patch.

Ack. I'll be sending v2 shortly, please let me know if I've missed
anything that should be included.

> > Steps to reproduce:
> > 
> > ∫ git version
> > git version 2.19.1.568.g152ad8e336-goog
> > ∫ cd ~/src/git
> > ∫ git archive --output ~/good.zip HEAD
> > ∫ file ~/good.zip
> > /home/steadmon/good.zip: Zip archive data, at least v1.0 to extract
> > ∫ git archive --output ~/bad.zip --remote=. HEAD
> > ∫ file ~/bad.zip
> > /home/steadmon/bad.zip: POSIX tar archive
> 
> And this could be in a test script in the actual patch. :)

Done.

```

## Josh Steadmon, 2018-10-22 21:47

Subject: Re: [PATCH 1/1] archive: init archivers before determining format
Message-ID: <20181022214756.GC233961@google.com>
URL: https://gitlist.dev/e/20181022214756.GC233961%40google.com
In-Reply-To: <20181019235945.GB7326@sigill.intra.peff.net>

```
On 2018.10.19 19:59, Jeff King wrote:
> On Fri, Oct 19, 2018 at 04:19:28PM -0700, steadmon@google.com wrote:
> 
> > diff --git a/builtin/archive.c b/builtin/archive.c
> > index e74f675390..dd3283a247 100644
> > --- a/builtin/archive.c
> > +++ b/builtin/archive.c
> > @@ -45,7 +45,10 @@ static int run_remote_archiver(int argc, const char **argv,
> >  	 * it.
> >  	 */
> >  	if (name_hint) {
> > -		const char *format = archive_format_from_filename(name_hint);
> > +		const char *format;
> > +		init_tar_archiver();
> > +		init_zip_archiver();
> > +		format = archive_format_from_filename(name_hint);
> >  		if (format)
> >  			packet_write_fmt(fd[1], "argument --format=%s\n", format);
> 
> Hrm. This code was added back in 56baa61d01 (archive: move file
> extension format-guessing lower, 2011-06-21), and your example
> invocation worked back then!
> 
> Unfortunately it was broken by the very next patch in the series,
> 08716b3c11 (archive: refactor file extension format-guessing,
> 2011-06-21). I guess that's what I get for not adding regression tests.
> 
> It's probably worth mentioning those points in the commit message.

Done.

> Does this work with configured archiver extensions, too? I think so,
> because we load them via init_tar_archiver().

If you mean things like .tgz and .tar.gz, then yes, they are affected by
the bug as well, and this patch fixes them. The test included in v2 uses
a .tgz file.

> Can we avoid repeating the list of archivers here? This needs to stay in
> sync with the list in write_archive(). I know there are only two, but
> can we factor out an init_archivers() call or something?

Done.

> We also should probably just call it unconditionally when we start the
> archiver command (I don't think there are any other bugs like this
> lurking, but it doesn't cost very much to initialize these; it makes
> sense to just do it early).

Done.

> Other than those minor points (and the lack of test), your fix looks
> good to me.

Thanks for the review!

```

## steadmon@google.com, 2018-10-22 21:48

Subject: [PATCH v2] archive: initialize archivers earlier
Message-ID: <bc6f20274dfe11f1451745e0accb065544cc59ca.1540244445.git.steadmon@google.com>
URL: https://gitlist.dev/e/bc6f20274dfe11f1451745e0accb065544cc59ca.1540244445.git.steadmon%40google.com
In-Reply-To: <cover.1539990488.git.steadmon@google.com>

```
Initialize archivers as soon as possible when running git-archive and
git-upload-archive. Various non-obvious behavior depends on having the
archivers initialized, such as determining the desired archival format
from the provided filename.

Since 08716b3c11 ("archive: refactor file extension format-guessing",
2011-06-21), archive_format_from_filename() has used the registered
archivers to match filenames (provided via --output) to archival
formats. However, when git-archive is executed with --remote, format
detection happens before the archivers have been registered. This causes
archives from remotes to always be generated as TAR files, regardless of
the actual filename (unless an explicit --format is provided).

This patch fixes that behavior; archival format is determined properly
from the output filename, even when --remote is used.

Signed-off-by: Josh Steadmon <steadmon@google.com>
Helped-by: Jeff King <peff@peff.net>
---
 archive.c                | 9 ++++++---
 archive.h                | 1 +
 builtin/archive.c        | 2 ++
 builtin/upload-archive.c | 1 +
 t/t5000-tar-tree.sh      | 6 ++++++
 5 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/archive.c b/archive.c
index c1870105eb..ce0f8a0362 100644
--- a/archive.c
+++ b/archive.c
@@ -29,6 +29,12 @@ void register_archiver(struct archiver *ar)
 	archivers[nr_archivers++] = ar;
 }
 
+void init_archivers(void)
+{
+	init_tar_archiver();
+	init_zip_archiver();
+}
+
 static void format_subst(const struct commit *commit,
                          const char *src, size_t len,
                          struct strbuf *buf)
@@ -531,9 +537,6 @@ int write_archive(int argc, const char **argv, const char *prefix,
 	git_config_get_bool("uploadarchive.allowunreachable", &remote_allow_unreachable);
 	git_config(git_default_config, NULL);
 
-	init_tar_archiver();
-	init_zip_archiver();
-
 	args.repo = repo;
 	argc = parse_archive_args(argc, argv, &ar, &args, name_hint, remote);
 	if (!startup_info->have_repository) {
diff --git a/archive.h b/archive.h
index d4f97a00f5..21ac010699 100644
--- a/archive.h
+++ b/archive.h
@@ -43,6 +43,7 @@ extern void register_archiver(struct archiver *);
 
 extern void init_tar_archiver(void);
 extern void init_zip_archiver(void);
+extern void init_archivers(void);
 
 typedef int (*write_archive_entry_fn_t)(struct archiver_args *args,
 					const struct object_id *oid,
diff --git a/builtin/archive.c b/builtin/archive.c
index e74f675390..d2455237ce 100644
--- a/builtin/archive.c
+++ b/builtin/archive.c
@@ -97,6 +97,8 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, local_opts, NULL,
 			     PARSE_OPT_KEEP_ALL);
 
+	init_archivers();
+
 	if (output)
 		create_output_file(output);
 
diff --git a/builtin/upload-archive.c b/builtin/upload-archive.c
index 25d9116356..3f35ebcfe8 100644
--- a/builtin/upload-archive.c
+++ b/builtin/upload-archive.c
@@ -43,6 +43,7 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
 	}
 
 	/* parse all options sent by the client */
+	init_archivers();
 	return write_archive(sent_argv.argc, sent_argv.argv, prefix,
 			     the_repository, NULL, 1);
 }
diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
index 2a97b27b0a..3e95fdf660 100755
--- a/t/t5000-tar-tree.sh
+++ b/t/t5000-tar-tree.sh
@@ -206,6 +206,12 @@ test_expect_success 'git archive with --output, override inferred format' '
 	test_cmp_bin b.tar d4.zip
 '
 
+test_expect_success GZIP 'git archive with --output and --remote uses expected format' '
+	git archive --output=d5.tgz --remote=. HEAD &&
+	gzip -d -c < d5.tgz > d5.tar &&
+	test_cmp_bin b.tar d5.tar
+'
+
 test_expect_success 'git archive --list outside of a git repo' '
 	nongit git archive --list
 '
-- 
2.19.1.568.g152ad8e336-goog


```

## Jeff King, 2018-10-22 22:30

Subject: Re: [PATCH 1/1] archive: init archivers before determining format
Message-ID: <20181022223041.GA19019@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20181022223041.GA19019%40sigill.intra.peff.net
In-Reply-To: <20181022214756.GC233961@google.com>

```
On Mon, Oct 22, 2018 at 02:47:56PM -0700, Josh Steadmon wrote:

> > Does this work with configured archiver extensions, too? I think so,
> > because we load them via init_tar_archiver().
> 
> If you mean things like .tgz and .tar.gz, then yes, they are affected by
> the bug as well, and this patch fixes them. The test included in v2 uses
> a .tgz file.

Yes, but also ones that are provided by the user. E.g., does:

  git config tar.foo.command "foo"
  git archive -o out.tar.foo HEAD

work? (I think the answer is yes, because we read git_tar_config in the
same place).

-Peff

```

## Jeff King, 2018-10-22 22:35

Subject: Re: [PATCH v2] archive: initialize archivers earlier
Message-ID: <20181022223536.GB19019@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20181022223536.GB19019%40sigill.intra.peff.net
In-Reply-To: <bc6f20274dfe11f1451745e0accb065544cc59ca.1540244445.git.steadmon@google.com>

```
On Mon, Oct 22, 2018 at 02:48:11PM -0700, steadmon@google.com wrote:

> Initialize archivers as soon as possible when running git-archive and
> git-upload-archive. Various non-obvious behavior depends on having the
> archivers initialized, such as determining the desired archival format
> from the provided filename.
> 
> Since 08716b3c11 ("archive: refactor file extension format-guessing",
> 2011-06-21), archive_format_from_filename() has used the registered
> archivers to match filenames (provided via --output) to archival
> formats. However, when git-archive is executed with --remote, format
> detection happens before the archivers have been registered. This causes
> archives from remotes to always be generated as TAR files, regardless of
> the actual filename (unless an explicit --format is provided).
> 
> This patch fixes that behavior; archival format is determined properly
> from the output filename, even when --remote is used.
> 
> Signed-off-by: Josh Steadmon <steadmon@google.com>
> Helped-by: Jeff King <peff@peff.net>

Thanks, this looks good overall.

A few minor comments (that I'm not even sure are worth re-rolling for):

> diff --git a/builtin/upload-archive.c b/builtin/upload-archive.c
> index 25d9116356..3f35ebcfe8 100644
> --- a/builtin/upload-archive.c
> +++ b/builtin/upload-archive.c
> @@ -43,6 +43,7 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
>  	}
>  
>  	/* parse all options sent by the client */
> +	init_archivers();
>  	return write_archive(sent_argv.argc, sent_argv.argv, prefix,
>  			     the_repository, NULL, 1);
>  }

This seems to separate the comment from what it describes. Any reason
not to just init_archivers() closer to the top of the function here
(probably after the enter_repo() call)?

> diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
> index 2a97b27b0a..3e95fdf660 100755
> --- a/t/t5000-tar-tree.sh
> +++ b/t/t5000-tar-tree.sh
> @@ -206,6 +206,12 @@ test_expect_success 'git archive with --output, override inferred format' '
>  	test_cmp_bin b.tar d4.zip
>  '
>  
> +test_expect_success GZIP 'git archive with --output and --remote uses expected format' '
> +	git archive --output=d5.tgz --remote=. HEAD &&
> +	gzip -d -c < d5.tgz > d5.tar &&
> +	test_cmp_bin b.tar d5.tar
> +'

This nicely tests the more-interesting tgz case. But unfortunately it
won't run on machines without the GZIP prerequisite. I'd think that
would really be _most_ machines, but is it worth having a separate zip
test to cover machines without gzip? I guess that just creates the
opposite problem: not everybody has ZIP.

-Peff

```

## Josh Steadmon, 2018-10-22 23:51

Subject: Re: [PATCH v2] archive: initialize archivers earlier
Message-ID: <20181022235127.GE233961@google.com>
URL: https://gitlist.dev/e/20181022235127.GE233961%40google.com
In-Reply-To: <20181022223536.GB19019@sigill.intra.peff.net>

```
On 2018.10.22 18:35, Jeff King wrote:
> On Mon, Oct 22, 2018 at 02:48:11PM -0700, steadmon@google.com wrote:
> 
> > Initialize archivers as soon as possible when running git-archive and
> > git-upload-archive. Various non-obvious behavior depends on having the
> > archivers initialized, such as determining the desired archival format
> > from the provided filename.
> > 
> > Since 08716b3c11 ("archive: refactor file extension format-guessing",
> > 2011-06-21), archive_format_from_filename() has used the registered
> > archivers to match filenames (provided via --output) to archival
> > formats. However, when git-archive is executed with --remote, format
> > detection happens before the archivers have been registered. This causes
> > archives from remotes to always be generated as TAR files, regardless of
> > the actual filename (unless an explicit --format is provided).
> > 
> > This patch fixes that behavior; archival format is determined properly
> > from the output filename, even when --remote is used.
> > 
> > Signed-off-by: Josh Steadmon <steadmon@google.com>
> > Helped-by: Jeff King <peff@peff.net>
> 
> Thanks, this looks good overall.
> 
> A few minor comments (that I'm not even sure are worth re-rolling for):
> 
> > diff --git a/builtin/upload-archive.c b/builtin/upload-archive.c
> > index 25d9116356..3f35ebcfe8 100644
> > --- a/builtin/upload-archive.c
> > +++ b/builtin/upload-archive.c
> > @@ -43,6 +43,7 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
> >  	}
> >  
> >  	/* parse all options sent by the client */
> > +	init_archivers();
> >  	return write_archive(sent_argv.argc, sent_argv.argv, prefix,
> >  			     the_repository, NULL, 1);
> >  }
> 
> This seems to separate the comment from what it describes. Any reason
> not to just init_archivers() closer to the top of the function here
> (probably after the enter_repo() call)?

Ack, fixed.


> > diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
> > index 2a97b27b0a..3e95fdf660 100755
> > --- a/t/t5000-tar-tree.sh
> > +++ b/t/t5000-tar-tree.sh
> > @@ -206,6 +206,12 @@ test_expect_success 'git archive with --output, override inferred format' '
> >  	test_cmp_bin b.tar d4.zip
> >  '
> >  
> > +test_expect_success GZIP 'git archive with --output and --remote uses expected format' '
> > +	git archive --output=d5.tgz --remote=. HEAD &&
> > +	gzip -d -c < d5.tgz > d5.tar &&
> > +	test_cmp_bin b.tar d5.tar
> > +'
> 
> This nicely tests the more-interesting tgz case. But unfortunately it
> won't run on machines without the GZIP prerequisite. I'd think that
> would really be _most_ machines, but is it worth having a separate zip
> test to cover machines without gzip? I guess that just creates the
> opposite problem: not everybody has ZIP.

Added a test to compare the file lists from the .zip file to the
reference .tar file. I'm not sure if this is the best way to do things,
but it at least verifies that a .zip is produced. However, it's brittle
if the output of "zip -sf" changes. Let me know if you have a better
idea.

```

## steadmon@google.com, 2018-10-22 23:54

Subject: [PATCH v3] archive: initialize archivers earlier
Message-ID: <39a4e7bf8f3ebc5803393f357d1ee7dc9806252f.1540251936.git.steadmon@google.com>
URL: https://gitlist.dev/e/39a4e7bf8f3ebc5803393f357d1ee7dc9806252f.1540251936.git.steadmon%40google.com
In-Reply-To: <bc6f20274dfe11f1451745e0accb065544cc59ca.1540244445.git.steadmon@google.com>

```
Initialize archivers as soon as possible when running git-archive.
Various non-obvious behavior depends on having the archivers
initialized, such as determining the desired archival format from the
provided filename.

Since 08716b3c11 ("archive: refactor file extension format-guessing",
2011-06-21), archive_format_from_filename() has used the registered
archivers to match filenames (provided via --output) to archival
formats. However, when git-archive is executed with --remote, format
detection happens before the archivers have been registered. This causes
archives from remotes to always be generated as TAR files, regardless of
the actual filename (unless an explicit --format is provided).

This patch fixes that behavior; archival format is determined properly
from the output filename, even when --remote is used.

Signed-off-by: Josh Steadmon <steadmon@google.com>
Helped-by: Jeff King <peff@peff.net>
---
Range-diff against v2:
1:  bc6f20274d ! 1:  39a4e7bf8f archive: initialize archivers earlier
    @@ -78,26 +78,43 @@
      --- a/builtin/upload-archive.c
      +++ b/builtin/upload-archive.c
     @@
    - 	}
    + 	if (!enter_repo(argv[1], 0))
    + 		die("'%s' does not appear to be a git repository", argv[1]);
      
    - 	/* parse all options sent by the client */
     +	init_archivers();
    - 	return write_archive(sent_argv.argc, sent_argv.argv, prefix,
    - 			     the_repository, NULL, 1);
    - }
    ++
    + 	/* put received options in sent_argv[] */
    + 	argv_array_push(&sent_argv, "git-upload-archive");
    + 	for (;;) {
     
      diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
      --- a/t/t5000-tar-tree.sh
      +++ b/t/t5000-tar-tree.sh
     @@
    + 
    + test_lazy_prereq GZIP 'gzip --version'
    + 
    ++test_lazy_prereq ZIP 'zip --version'
    ++
    + get_pax_header() {
    + 	file=$1
    + 	header=$2=
    +@@
      	test_cmp_bin b.tar d4.zip
      '
      
    -+test_expect_success GZIP 'git archive with --output and --remote uses expected format' '
    ++test_expect_success GZIP 'git archive with --output and --remote creates .tgz' '
     +	git archive --output=d5.tgz --remote=. HEAD &&
     +	gzip -d -c < d5.tgz > d5.tar &&
     +	test_cmp_bin b.tar d5.tar
     +'
    ++
    ++test_expect_success ZIP 'git archive with --output and --remote creates .zip' '
    ++	git archive --output=d6.zip --remote=. HEAD &&
    ++	zip -sf d6.zip | sed "/^[^ ]/d" | sed "s/^  //" | sort > zip_manifest &&
    ++	"$TAR" tf b.tar | sort > tar_manifest &&
    ++	test_cmp zip_manifest tar_manifest
    ++'
     +
      test_expect_success 'git archive --list outside of a git repo' '
      	nongit git archive --list

 archive.c                |  9 ++++++---
 archive.h                |  1 +
 builtin/archive.c        |  2 ++
 builtin/upload-archive.c |  2 ++
 t/t5000-tar-tree.sh      | 15 +++++++++++++++
 5 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/archive.c b/archive.c
index c1870105eb..ce0f8a0362 100644
--- a/archive.c
+++ b/archive.c
@@ -29,6 +29,12 @@ void register_archiver(struct archiver *ar)
 	archivers[nr_archivers++] = ar;
 }
 
+void init_archivers(void)
+{
+	init_tar_archiver();
+	init_zip_archiver();
+}
+
 static void format_subst(const struct commit *commit,
                          const char *src, size_t len,
                          struct strbuf *buf)
@@ -531,9 +537,6 @@ int write_archive(int argc, const char **argv, const char *prefix,
 	git_config_get_bool("uploadarchive.allowunreachable", &remote_allow_unreachable);
 	git_config(git_default_config, NULL);
 
-	init_tar_archiver();
-	init_zip_archiver();
-
 	args.repo = repo;
 	argc = parse_archive_args(argc, argv, &ar, &args, name_hint, remote);
 	if (!startup_info->have_repository) {
diff --git a/archive.h b/archive.h
index d4f97a00f5..21ac010699 100644
--- a/archive.h
+++ b/archive.h
@@ -43,6 +43,7 @@ extern void register_archiver(struct archiver *);
 
 extern void init_tar_archiver(void);
 extern void init_zip_archiver(void);
+extern void init_archivers(void);
 
 typedef int (*write_archive_entry_fn_t)(struct archiver_args *args,
 					const struct object_id *oid,
diff --git a/builtin/archive.c b/builtin/archive.c
index e74f675390..d2455237ce 100644
--- a/builtin/archive.c
+++ b/builtin/archive.c
@@ -97,6 +97,8 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, local_opts, NULL,
 			     PARSE_OPT_KEEP_ALL);
 
+	init_archivers();
+
 	if (output)
 		create_output_file(output);
 
diff --git a/builtin/upload-archive.c b/builtin/upload-archive.c
index 25d9116356..018879737a 100644
--- a/builtin/upload-archive.c
+++ b/builtin/upload-archive.c
@@ -28,6 +28,8 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
 	if (!enter_repo(argv[1], 0))
 		die("'%s' does not appear to be a git repository", argv[1]);
 
+	init_archivers();
+
 	/* put received options in sent_argv[] */
 	argv_array_push(&sent_argv, "git-upload-archive");
 	for (;;) {
diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
index 2a97b27b0a..cfd5ca492f 100755
--- a/t/t5000-tar-tree.sh
+++ b/t/t5000-tar-tree.sh
@@ -39,6 +39,8 @@ test_lazy_prereq TAR_NEEDS_PAX_FALLBACK '
 
 test_lazy_prereq GZIP 'gzip --version'
 
+test_lazy_prereq ZIP 'zip --version'
+
 get_pax_header() {
 	file=$1
 	header=$2=
@@ -206,6 +208,19 @@ test_expect_success 'git archive with --output, override inferred format' '
 	test_cmp_bin b.tar d4.zip
 '
 
+test_expect_success GZIP 'git archive with --output and --remote creates .tgz' '
+	git archive --output=d5.tgz --remote=. HEAD &&
+	gzip -d -c < d5.tgz > d5.tar &&
+	test_cmp_bin b.tar d5.tar
+'
+
+test_expect_success ZIP 'git archive with --output and --remote creates .zip' '
+	git archive --output=d6.zip --remote=. HEAD &&
+	zip -sf d6.zip | sed "/^[^ ]/d" | sed "s/^  //" | sort > zip_manifest &&
+	"$TAR" tf b.tar | sort > tar_manifest &&
+	test_cmp zip_manifest tar_manifest
+'
+
 test_expect_success 'git archive --list outside of a git repo' '
 	nongit git archive --list
 '
-- 
2.19.1.568.g152ad8e336-goog


```

## Jeff King, 2018-10-23 00:06

Subject: Re: [PATCH v2] archive: initialize archivers earlier
Message-ID: <20181023000616.GA27393@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20181023000616.GA27393%40sigill.intra.peff.net
In-Reply-To: <20181022235127.GE233961@google.com>

```
On Mon, Oct 22, 2018 at 04:51:27PM -0700, Josh Steadmon wrote:

> > > +test_expect_success GZIP 'git archive with --output and --remote uses expected format' '
> > > +	git archive --output=d5.tgz --remote=. HEAD &&
> > > +	gzip -d -c < d5.tgz > d5.tar &&
> > > +	test_cmp_bin b.tar d5.tar
> > > +'
> > 
> > This nicely tests the more-interesting tgz case. But unfortunately it
> > won't run on machines without the GZIP prerequisite. I'd think that
> > would really be _most_ machines, but is it worth having a separate zip
> > test to cover machines without gzip? I guess that just creates the
> > opposite problem: not everybody has ZIP.
> 
> Added a test to compare the file lists from the .zip file to the
> reference .tar file. I'm not sure if this is the best way to do things,
> but it at least verifies that a .zip is produced. However, it's brittle
> if the output of "zip -sf" changes. Let me know if you have a better
> idea.

I wonder if we could do something more black-box. What we really care
about here is not the exact output, but rather that "-o foo.zip"
produces the same output as "--format zip". Could we do that without
even relying on ZIP?

I think it should follow even for tgz, because we use "-n" for a
repeatable output. But there we are relying on an external gzip just to
_create_ the file, so we'd still need the GZIP prereq.

Hmm. Looks like we already have a similar test in t5003. So maybe just:

diff --git a/t/t5003-archive-zip.sh b/t/t5003-archive-zip.sh
index 55c7870997..cf19f56924 100755
--- a/t/t5003-archive-zip.sh
+++ b/t/t5003-archive-zip.sh
@@ -158,11 +158,16 @@ test_expect_success 'git archive --format=zip with --output' \
     'git archive --format=zip --output=d2.zip HEAD &&
     test_cmp_bin d.zip d2.zip'
 
-test_expect_success 'git archive with --output, inferring format' '
+test_expect_success 'git archive with --output, inferring format (local)' '
 	git archive --output=d3.zip HEAD &&
 	test_cmp_bin d.zip d3.zip
 '
 
+test_expect_success 'git archive with --output, ferring format (remote)' '
+	git archive --remote=. --output=d4.zip HEAD &&
+	test_cmp_bin d.zip d4.zip
+'
+
 test_expect_success \
     'git archive --format=zip with prefix' \
     'git archive --format=zip --prefix=prefix/ HEAD >e.zip'

which I think exposes the bug and can run everywhere?

-Peff

```

## steadmon@google.com, 2018-10-23 00:20

Subject: [PATCH v4] archive: initialize archivers earlier
Message-ID: <1d7b07092872e4735dbdedfbfdd86a1c9e71290a.1540253913.git.steadmon@google.com>
URL: https://gitlist.dev/e/1d7b07092872e4735dbdedfbfdd86a1c9e71290a.1540253913.git.steadmon%40google.com
In-Reply-To: <39a4e7bf8f3ebc5803393f357d1ee7dc9806252f.1540251936.git.steadmon@google.com>

```
Initialize archivers as soon as possible when running git-archive.
Various non-obvious behavior depends on having the archivers
initialized, such as determining the desired archival format from the
provided filename.

Since 08716b3c11 ("archive: refactor file extension format-guessing",
2011-06-21), archive_format_from_filename() has used the registered
archivers to match filenames (provided via --output) to archival
formats. However, when git-archive is executed with --remote, format
detection happens before the archivers have been registered. This causes
archives from remotes to always be generated as TAR files, regardless of
the actual filename (unless an explicit --format is provided).

This patch fixes that behavior; archival format is determined properly
from the output filename, even when --remote is used.

Signed-off-by: Josh Steadmon <steadmon@google.com>
Helped-by: Jeff King <peff@peff.net>
---
Range-diff against v3:
1:  39a4e7bf8f ! 1:  1d7b070928 archive: initialize archivers earlier
    @@ -91,15 +91,6 @@
      --- a/t/t5000-tar-tree.sh
      +++ b/t/t5000-tar-tree.sh
     @@
    - 
    - test_lazy_prereq GZIP 'gzip --version'
    - 
    -+test_lazy_prereq ZIP 'zip --version'
    -+
    - get_pax_header() {
    - 	file=$1
    - 	header=$2=
    -@@
      	test_cmp_bin b.tar d4.zip
      '
      
    @@ -108,14 +99,29 @@
     +	gzip -d -c < d5.tgz > d5.tar &&
     +	test_cmp_bin b.tar d5.tar
     +'
    -+
    -+test_expect_success ZIP 'git archive with --output and --remote creates .zip' '
    -+	git archive --output=d6.zip --remote=. HEAD &&
    -+	zip -sf d6.zip | sed "/^[^ ]/d" | sed "s/^  //" | sort > zip_manifest &&
    -+	"$TAR" tf b.tar | sort > tar_manifest &&
    -+	test_cmp zip_manifest tar_manifest
    -+'
     +
      test_expect_success 'git archive --list outside of a git repo' '
      	nongit git archive --list
      '
    +
    + diff --git a/t/t5003-archive-zip.sh b/t/t5003-archive-zip.sh
    + --- a/t/t5003-archive-zip.sh
    + +++ b/t/t5003-archive-zip.sh
    +@@
    +     'git archive --format=zip --output=d2.zip HEAD &&
    +     test_cmp_bin d.zip d2.zip'
    + 
    +-test_expect_success 'git archive with --output, inferring format' '
    ++test_expect_success 'git archive with --output, inferring format (local)' '
    + 	git archive --output=d3.zip HEAD &&
    + 	test_cmp_bin d.zip d3.zip
    + '
    + 
    ++test_expect_success 'git archive with --output, inferring format (remote)' '
    ++	git archive --remote=. --output=d4.zip HEAD &&
    ++	test_cmp_bin d.zip d4.zip
    ++'
    ++
    + test_expect_success \
    +     'git archive --format=zip with prefix' \
    +     'git archive --format=zip --prefix=prefix/ HEAD >e.zip'

 archive.c                | 9 ++++++---
 archive.h                | 1 +
 builtin/archive.c        | 2 ++
 builtin/upload-archive.c | 2 ++
 t/t5000-tar-tree.sh      | 6 ++++++
 t/t5003-archive-zip.sh   | 7 ++++++-
 6 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/archive.c b/archive.c
index c1870105eb..ce0f8a0362 100644
--- a/archive.c
+++ b/archive.c
@@ -29,6 +29,12 @@ void register_archiver(struct archiver *ar)
 	archivers[nr_archivers++] = ar;
 }
 
+void init_archivers(void)
+{
+	init_tar_archiver();
+	init_zip_archiver();
+}
+
 static void format_subst(const struct commit *commit,
                          const char *src, size_t len,
                          struct strbuf *buf)
@@ -531,9 +537,6 @@ int write_archive(int argc, const char **argv, const char *prefix,
 	git_config_get_bool("uploadarchive.allowunreachable", &remote_allow_unreachable);
 	git_config(git_default_config, NULL);
 
-	init_tar_archiver();
-	init_zip_archiver();
-
 	args.repo = repo;
 	argc = parse_archive_args(argc, argv, &ar, &args, name_hint, remote);
 	if (!startup_info->have_repository) {
diff --git a/archive.h b/archive.h
index d4f97a00f5..21ac010699 100644
--- a/archive.h
+++ b/archive.h
@@ -43,6 +43,7 @@ extern void register_archiver(struct archiver *);
 
 extern void init_tar_archiver(void);
 extern void init_zip_archiver(void);
+extern void init_archivers(void);
 
 typedef int (*write_archive_entry_fn_t)(struct archiver_args *args,
 					const struct object_id *oid,
diff --git a/builtin/archive.c b/builtin/archive.c
index e74f675390..d2455237ce 100644
--- a/builtin/archive.c
+++ b/builtin/archive.c
@@ -97,6 +97,8 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, local_opts, NULL,
 			     PARSE_OPT_KEEP_ALL);
 
+	init_archivers();
+
 	if (output)
 		create_output_file(output);
 
diff --git a/builtin/upload-archive.c b/builtin/upload-archive.c
index 25d9116356..018879737a 100644
--- a/builtin/upload-archive.c
+++ b/builtin/upload-archive.c
@@ -28,6 +28,8 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
 	if (!enter_repo(argv[1], 0))
 		die("'%s' does not appear to be a git repository", argv[1]);
 
+	init_archivers();
+
 	/* put received options in sent_argv[] */
 	argv_array_push(&sent_argv, "git-upload-archive");
 	for (;;) {
diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
index 2a97b27b0a..e2ee920b06 100755
--- a/t/t5000-tar-tree.sh
+++ b/t/t5000-tar-tree.sh
@@ -206,6 +206,12 @@ test_expect_success 'git archive with --output, override inferred format' '
 	test_cmp_bin b.tar d4.zip
 '
 
+test_expect_success GZIP 'git archive with --output and --remote creates .tgz' '
+	git archive --output=d5.tgz --remote=. HEAD &&
+	gzip -d -c < d5.tgz > d5.tar &&
+	test_cmp_bin b.tar d5.tar
+'
+
 test_expect_success 'git archive --list outside of a git repo' '
 	nongit git archive --list
 '
diff --git a/t/t5003-archive-zip.sh b/t/t5003-archive-zip.sh
index 55c7870997..106eddbd85 100755
--- a/t/t5003-archive-zip.sh
+++ b/t/t5003-archive-zip.sh
@@ -158,11 +158,16 @@ test_expect_success 'git archive --format=zip with --output' \
     'git archive --format=zip --output=d2.zip HEAD &&
     test_cmp_bin d.zip d2.zip'
 
-test_expect_success 'git archive with --output, inferring format' '
+test_expect_success 'git archive with --output, inferring format (local)' '
 	git archive --output=d3.zip HEAD &&
 	test_cmp_bin d.zip d3.zip
 '
 
+test_expect_success 'git archive with --output, inferring format (remote)' '
+	git archive --remote=. --output=d4.zip HEAD &&
+	test_cmp_bin d.zip d4.zip
+'
+
 test_expect_success \
     'git archive --format=zip with prefix' \
     'git archive --format=zip --prefix=prefix/ HEAD >e.zip'
-- 
2.19.1.568.g152ad8e336-goog


```

## Josh Steadmon, 2018-10-23 00:23

Subject: Re: [PATCH v2] archive: initialize archivers earlier
Message-ID: <20181023002313.GF233961@google.com>
URL: https://gitlist.dev/e/20181023002313.GF233961%40google.com
In-Reply-To: <20181023000616.GA27393@sigill.intra.peff.net>

```
On 2018.10.22 20:06, Jeff King wrote:
> On Mon, Oct 22, 2018 at 04:51:27PM -0700, Josh Steadmon wrote:
> 
> > > > +test_expect_success GZIP 'git archive with --output and --remote uses expected format' '
> > > > +	git archive --output=d5.tgz --remote=. HEAD &&
> > > > +	gzip -d -c < d5.tgz > d5.tar &&
> > > > +	test_cmp_bin b.tar d5.tar
> > > > +'
> > > 
> > > This nicely tests the more-interesting tgz case. But unfortunately it
> > > won't run on machines without the GZIP prerequisite. I'd think that
> > > would really be _most_ machines, but is it worth having a separate zip
> > > test to cover machines without gzip? I guess that just creates the
> > > opposite problem: not everybody has ZIP.
> > 
> > Added a test to compare the file lists from the .zip file to the
> > reference .tar file. I'm not sure if this is the best way to do things,
> > but it at least verifies that a .zip is produced. However, it's brittle
> > if the output of "zip -sf" changes. Let me know if you have a better
> > idea.
> 
> I wonder if we could do something more black-box. What we really care
> about here is not the exact output, but rather that "-o foo.zip"
> produces the same output as "--format zip". Could we do that without
> even relying on ZIP?
> 
> I think it should follow even for tgz, because we use "-n" for a
> repeatable output. But there we are relying on an external gzip just to
> _create_ the file, so we'd still need the GZIP prereq.
> 
> Hmm. Looks like we already have a similar test in t5003. So maybe just:
> 
> diff --git a/t/t5003-archive-zip.sh b/t/t5003-archive-zip.sh
> index 55c7870997..cf19f56924 100755
> --- a/t/t5003-archive-zip.sh
> +++ b/t/t5003-archive-zip.sh
> @@ -158,11 +158,16 @@ test_expect_success 'git archive --format=zip with --output' \
>      'git archive --format=zip --output=d2.zip HEAD &&
>      test_cmp_bin d.zip d2.zip'
>  
> -test_expect_success 'git archive with --output, inferring format' '
> +test_expect_success 'git archive with --output, inferring format (local)' '
>  	git archive --output=d3.zip HEAD &&
>  	test_cmp_bin d.zip d3.zip
>  '
>  
> +test_expect_success 'git archive with --output, ferring format (remote)' '
> +	git archive --remote=. --output=d4.zip HEAD &&
> +	test_cmp_bin d.zip d4.zip
> +'
> +
>  test_expect_success \
>      'git archive --format=zip with prefix' \
>      'git archive --format=zip --prefix=prefix/ HEAD >e.zip'
> 
> which I think exposes the bug and can run everywhere?

Makes sense, thanks!

```

## Junio C Hamano, 2018-10-23 04:09

Subject: Re: [PATCH v3] archive: initialize archivers earlier
Message-ID: <xmqq4lddtibc.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqq4lddtibc.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <39a4e7bf8f3ebc5803393f357d1ee7dc9806252f.1540251936.git.steadmon@google.com>

```
steadmon@google.com writes:

> diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
> index 2a97b27b0a..cfd5ca492f 100755
> --- a/t/t5000-tar-tree.sh
> +++ b/t/t5000-tar-tree.sh
> @@ -39,6 +39,8 @@ test_lazy_prereq TAR_NEEDS_PAX_FALLBACK '
>  
>  test_lazy_prereq GZIP 'gzip --version'
>  
> +test_lazy_prereq ZIP 'zip --version'
> +

There are a handful of zip implementations; Info-ZIP found on many
Linux distros does support 'zip --version', but we may want to make
sure this test covers different implementations of zip sufficiently.

Queuing this patch (or an update of it) on 'pu' and hoping those
with zip from different origins to try it would not help very much,
either, as zip implementations that do not react to "zip --version"
would silently turn the prereq off without breaking anything.

In any case, please refrain from adding any ZIP prerequiste to t5000
which is about tar; t5003-archive-zip may be a much better fit.  It
has an already working machinery that validates the generated zip
archive under UNZIP prerequisite, so we may not even have to invent
our own ZIP prereq if we did so.

> @@ -206,6 +208,19 @@ test_expect_success 'git archive with --output, override inferred format' '
>  	test_cmp_bin b.tar d4.zip
>  '
>  
> +test_expect_success GZIP 'git archive with --output and --remote creates .tgz' '
> +	git archive --output=d5.tgz --remote=. HEAD &&
> +	gzip -d -c < d5.tgz > d5.tar &&
> +	test_cmp_bin b.tar d5.tar
> +'

We try to write redirections without SP between redirection operator
and target filename, i.e. "gzip -d -c <d5.tgz >d5.tar".

Thanks.

```

## Josh Steadmon, 2018-10-25 20:29

Subject: Re: [PATCH v3] archive: initialize archivers earlier
Message-ID: <20181025202915.GH233961@google.com>
URL: https://gitlist.dev/e/20181025202915.GH233961%40google.com
In-Reply-To: <xmqq4lddtibc.fsf@gitster-ct.c.googlers.com>

```
On 2018.10.23 13:09, Junio C Hamano wrote:
> steadmon@google.com writes:
> 
> > diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
> > index 2a97b27b0a..cfd5ca492f 100755
> > --- a/t/t5000-tar-tree.sh
> > +++ b/t/t5000-tar-tree.sh
> > @@ -39,6 +39,8 @@ test_lazy_prereq TAR_NEEDS_PAX_FALLBACK '
> >  
> >  test_lazy_prereq GZIP 'gzip --version'
> >  
> > +test_lazy_prereq ZIP 'zip --version'
> > +
> 
> There are a handful of zip implementations; Info-ZIP found on many
> Linux distros does support 'zip --version', but we may want to make
> sure this test covers different implementations of zip sufficiently.
> 
> Queuing this patch (or an update of it) on 'pu' and hoping those
> with zip from different origins to try it would not help very much,
> either, as zip implementations that do not react to "zip --version"
> would silently turn the prereq off without breaking anything.
> 
> In any case, please refrain from adding any ZIP prerequiste to t5000
> which is about tar; t5003-archive-zip may be a much better fit.  It
> has an already working machinery that validates the generated zip
> archive under UNZIP prerequisite, so we may not even have to invent
> our own ZIP prereq if we did so.

Ack. This has been removed in v4. V4 also has a test case in t5003 based
on Jeff's advice.

> > @@ -206,6 +208,19 @@ test_expect_success 'git archive with --output, override inferred format' '
> >  	test_cmp_bin b.tar d4.zip
> >  '
> >  
> > +test_expect_success GZIP 'git archive with --output and --remote creates .tgz' '
> > +	git archive --output=d5.tgz --remote=. HEAD &&
> > +	gzip -d -c < d5.tgz > d5.tar &&
> > +	test_cmp_bin b.tar d5.tar
> > +'
> 
> We try to write redirections without SP between redirection operator
> and target filename, i.e. "gzip -d -c <d5.tgz >d5.tar".

Fixed in v5.

```

## steadmon@google.com, 2018-10-25 20:32

Subject: [PATCH v5] archive: initialize archivers earlier
Message-ID: <c85673cee76c1c4a7e2b2a489d6139f5c89ba88c.1540499413.git.steadmon@google.com>
URL: https://gitlist.dev/e/c85673cee76c1c4a7e2b2a489d6139f5c89ba88c.1540499413.git.steadmon%40google.com
In-Reply-To: <39a4e7bf8f3ebc5803393f357d1ee7dc9806252f.1540251936.git.steadmon@google.com>

```
Initialize archivers as soon as possible when running git-archive.
Various non-obvious behavior depends on having the archivers
initialized, such as determining the desired archival format from the
provided filename.

Since 08716b3c11 ("archive: refactor file extension format-guessing",
2011-06-21), archive_format_from_filename() has used the registered
archivers to match filenames (provided via --output) to archival
formats. However, when git-archive is executed with --remote, format
detection happens before the archivers have been registered. This causes
archives from remotes to always be generated as TAR files, regardless of
the actual filename (unless an explicit --format is provided).

This patch fixes that behavior; archival format is determined properly
from the output filename, even when --remote is used.

Signed-off-by: Josh Steadmon <steadmon@google.com>
Helped-by: Jeff King <peff@peff.net>
---
Range-diff against v4:
1:  1d7b070928 ! 1:  c85673cee7 archive: initialize archivers earlier
    @@ -96,7 +96,7 @@
      
     +test_expect_success GZIP 'git archive with --output and --remote creates .tgz' '
     +	git archive --output=d5.tgz --remote=. HEAD &&
    -+	gzip -d -c < d5.tgz > d5.tar &&
    ++	gzip -d -c <d5.tgz >d5.tar &&
     +	test_cmp_bin b.tar d5.tar
     +'
     +

 archive.c                | 9 ++++++---
 archive.h                | 1 +
 builtin/archive.c        | 2 ++
 builtin/upload-archive.c | 2 ++
 t/t5000-tar-tree.sh      | 6 ++++++
 t/t5003-archive-zip.sh   | 7 ++++++-
 6 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/archive.c b/archive.c
index c1870105eb..ce0f8a0362 100644
--- a/archive.c
+++ b/archive.c
@@ -29,6 +29,12 @@ void register_archiver(struct archiver *ar)
 	archivers[nr_archivers++] = ar;
 }
 
+void init_archivers(void)
+{
+	init_tar_archiver();
+	init_zip_archiver();
+}
+
 static void format_subst(const struct commit *commit,
                          const char *src, size_t len,
                          struct strbuf *buf)
@@ -531,9 +537,6 @@ int write_archive(int argc, const char **argv, const char *prefix,
 	git_config_get_bool("uploadarchive.allowunreachable", &remote_allow_unreachable);
 	git_config(git_default_config, NULL);
 
-	init_tar_archiver();
-	init_zip_archiver();
-
 	args.repo = repo;
 	argc = parse_archive_args(argc, argv, &ar, &args, name_hint, remote);
 	if (!startup_info->have_repository) {
diff --git a/archive.h b/archive.h
index d4f97a00f5..21ac010699 100644
--- a/archive.h
+++ b/archive.h
@@ -43,6 +43,7 @@ extern void register_archiver(struct archiver *);
 
 extern void init_tar_archiver(void);
 extern void init_zip_archiver(void);
+extern void init_archivers(void);
 
 typedef int (*write_archive_entry_fn_t)(struct archiver_args *args,
 					const struct object_id *oid,
diff --git a/builtin/archive.c b/builtin/archive.c
index e74f675390..d2455237ce 100644
--- a/builtin/archive.c
+++ b/builtin/archive.c
@@ -97,6 +97,8 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, local_opts, NULL,
 			     PARSE_OPT_KEEP_ALL);
 
+	init_archivers();
+
 	if (output)
 		create_output_file(output);
 
diff --git a/builtin/upload-archive.c b/builtin/upload-archive.c
index 25d9116356..018879737a 100644
--- a/builtin/upload-archive.c
+++ b/builtin/upload-archive.c
@@ -28,6 +28,8 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
 	if (!enter_repo(argv[1], 0))
 		die("'%s' does not appear to be a git repository", argv[1]);
 
+	init_archivers();
+
 	/* put received options in sent_argv[] */
 	argv_array_push(&sent_argv, "git-upload-archive");
 	for (;;) {
diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
index 2a97b27b0a..602bfd9574 100755
--- a/t/t5000-tar-tree.sh
+++ b/t/t5000-tar-tree.sh
@@ -206,6 +206,12 @@ test_expect_success 'git archive with --output, override inferred format' '
 	test_cmp_bin b.tar d4.zip
 '
 
+test_expect_success GZIP 'git archive with --output and --remote creates .tgz' '
+	git archive --output=d5.tgz --remote=. HEAD &&
+	gzip -d -c <d5.tgz >d5.tar &&
+	test_cmp_bin b.tar d5.tar
+'
+
 test_expect_success 'git archive --list outside of a git repo' '
 	nongit git archive --list
 '
diff --git a/t/t5003-archive-zip.sh b/t/t5003-archive-zip.sh
index 55c7870997..106eddbd85 100755
--- a/t/t5003-archive-zip.sh
+++ b/t/t5003-archive-zip.sh
@@ -158,11 +158,16 @@ test_expect_success 'git archive --format=zip with --output' \
     'git archive --format=zip --output=d2.zip HEAD &&
     test_cmp_bin d.zip d2.zip'
 
-test_expect_success 'git archive with --output, inferring format' '
+test_expect_success 'git archive with --output, inferring format (local)' '
 	git archive --output=d3.zip HEAD &&
 	test_cmp_bin d.zip d3.zip
 '
 
+test_expect_success 'git archive with --output, inferring format (remote)' '
+	git archive --remote=. --output=d4.zip HEAD &&
+	test_cmp_bin d.zip d4.zip
+'
+
 test_expect_success \
     'git archive --format=zip with prefix' \
     'git archive --format=zip --prefix=prefix/ HEAD >e.zip'
-- 
2.19.1.568.g152ad8e336-goog


```

## Jeff King, 2018-10-25 21:12

Subject: Re: [PATCH v5] archive: initialize archivers earlier
Message-ID: <20181025211203.GA23050@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20181025211203.GA23050%40sigill.intra.peff.net
In-Reply-To: <c85673cee76c1c4a7e2b2a489d6139f5c89ba88c.1540499413.git.steadmon@google.com>

```
On Thu, Oct 25, 2018 at 01:32:14PM -0700, steadmon@google.com wrote:

> Initialize archivers as soon as possible when running git-archive.
> Various non-obvious behavior depends on having the archivers
> initialized, such as determining the desired archival format from the
> provided filename.
> 
> Since 08716b3c11 ("archive: refactor file extension format-guessing",
> 2011-06-21), archive_format_from_filename() has used the registered
> archivers to match filenames (provided via --output) to archival
> formats. However, when git-archive is executed with --remote, format
> detection happens before the archivers have been registered. This causes
> archives from remotes to always be generated as TAR files, regardless of
> the actual filename (unless an explicit --format is provided).
> 
> This patch fixes that behavior; archival format is determined properly
> from the output filename, even when --remote is used.

Thanks, this version looks great to me!

-Peff

```

## Junio C Hamano, 2018-10-26 01:14

Subject: Re: [PATCH v5] archive: initialize archivers earlier
Message-ID: <xmqq5zxpfr06.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqq5zxpfr06.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <20181025211203.GA23050@sigill.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> On Thu, Oct 25, 2018 at 01:32:14PM -0700, steadmon@google.com wrote:
>
>> Initialize archivers as soon as possible when running git-archive.
>> Various non-obvious behavior depends on having the archivers
>> initialized, such as determining the desired archival format from the
>> provided filename.
>> 
>> Since 08716b3c11 ("archive: refactor file extension format-guessing",
>> 2011-06-21), archive_format_from_filename() has used the registered
>> archivers to match filenames (provided via --output) to archival
>> formats. However, when git-archive is executed with --remote, format
>> detection happens before the archivers have been registered. This causes
>> archives from remotes to always be generated as TAR files, regardless of
>> the actual filename (unless an explicit --format is provided).
>> 
>> This patch fixes that behavior; archival format is determined properly
>> from the output filename, even when --remote is used.
>
> Thanks, this version looks great to me!

To me too.  Thanks, both.

```
