git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Fix archive format with -- on the command line

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 10, 2009, 23:27 UTC
Message-ID
<7vhbry8l54.fsf_-_@alter.siamese.dyndns.org>
In-Reply-To
<7vws0u8n99.fsf@alter.siamese.dyndns.org>

Giving --format from the command line, or using output file extention to DWIM the output format, with a pathspec that is disambiguated with an explicit double-dash on the command line, e.g.

    git archive -o file --format=zip HEAD -- path
    git archive -o file.zip HEAD -- path
didn't work correctly.

This was because the code reordered (when one was given) or added (when the former was inferred) a --format argument at the end, effectively making it to "archive HEAD -- path --format=zip", i.e. an extra pathspec that is unlikely to match anything.

The command line argument list should always be "options, revs and then paths", and we should set a good example by inserting the --format at the beginning instead.

Reported-by: Ilari Liusvaara <ilari.liusvaara@elisanet.fi>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * So here is one with a proper commit log message. 
 builtin-archive.c |   16 ++++++++++------
 1 files changed, 10 insertions(+), 6 deletions(-)
diff --git a/builtin-archive.c b/builtin-archive.c
index 12351e9..8ef5ab3 100644
--- a/builtin-archive.c
+++ b/builtin-archive.c
@@ -106,13 +106,17 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
 	if (format) {
 		sprintf(fmt_opt, "--format=%s", format);
 		/*
-		 * This is safe because either --format and/or --output must
-		 * have been given on the original command line if we get to
-		 * this point, and parse_options() must have eaten at least
-		 * one argument, i.e. we have enough room to append to argv[].
+		 * We have enough room in argv[] to muck it in place,
+		 * because either --format and/or --output must have
+		 * been given on the original command line if we get
+		 * to this point, and parse_options() must have eaten
+		 * it, i.e. we can add back one element to the array.
+		 * But argv[] may contain "--"; we should make it the
+		 * first option.
 		 */
-		argv[argc++] = fmt_opt;
-		argv[argc] = NULL;
+		memmove(argv + 2, argv + 1, sizeof(*argv) * argc);
+		argv[1] = fmt_opt;
+		argv[++argc] = NULL;
 	}
 
 	if (remote)
-- 
1.6.6.rc2
Previous: Junio C HamanoNext: Junio C Hamano
Message 5 of 11 in “[BUG] git archive formats and dashdash”
  1. Ilari LiusvaaraDec 10, 2009
  2. Junio C HamanoDec 10, 2009
  3. Miklos VajnaDec 10, 2009
  4. Junio C HamanoDec 10, 2009
  5. Fix archive format with -- on the command lineJunio C Hamano, Dec 10, 2009
  6. Junio C HamanoDec 10, 2009
  7. René ScharfeDec 12, 2009
  8. Nanako ShiraishiDec 30, 2009
  9. Junio C HamanoDec 30, 2009
  10. Ilari LiusvaaraDec 10, 2009
  11. builtin-archive: insert --format before double dash if necessary.Miklos Vajna, Dec 10, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.