Skip to content

Commit 782a000

Browse files
committed
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1456b04 commit 782a000

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

builtin-archive.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,17 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
106106
if (format) {
107107
sprintf(fmt_opt, "--format=%s", format);
108108
/*
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.
113116
*/
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;
116120
}
117121

118122
if (remote)

0 commit comments

Comments
 (0)