@matheusbsbs / matheus-bagliano / commits / 782a0005fc
Fork 0

Fix archive format with -- on the command line

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 format 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>

Junio C Hamano committed Dec 10, 2009 at 15:27 UTC 782a0005fcb26bb7ef27f720fd139ae40a6f434b
1 file changed +10 -6
builtin-archive.c
+10 -6
@@ -106,13 +106,17 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
106 if (format) {
107 sprintf(fmt_opt, "--format=%s", format);
108 /*
109 - * This is safe because either --format and/or --output must
110 - * have been given on the original command line if we get to
111 - * this point, and parse_options() must have eaten at least
112 - * one argument, i.e. we have enough room to append to argv[].
109 + * We have enough room in argv[] to muck it in place,
110 + * because either --format and/or --output must have
111 + * been given on the original command line if we get
112 + * to this point, and parse_options() must have eaten
113 + * it, i.e. we can add back one element to the array.
114 + * But argv[] may contain "--"; we should make it the
115 + * first option.
116 */
114 - argv[argc++] = fmt_opt;
115 - argv[argc] = NULL;
117 + memmove(argv + 2, argv + 1, sizeof(*argv) * argc);
118 + argv[1] = fmt_opt;
119 + argv[++argc] = NULL;
120 }
121
122 if (remote)