Skip to content

Commit e7ec9de

Browse files
committed
Merge branch 'maint'
* maint: archive: simplify archive format guessing
2 parents 8424981 + fe12d8e commit e7ec9de

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

builtin-archive.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static const char *format_from_name(const char *filename)
7070
return NULL;
7171
ext++;
7272
if (!strcasecmp(ext, "zip"))
73-
return "zip";
73+
return "--format=zip";
7474
return NULL;
7575
}
7676

@@ -84,41 +84,39 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
8484
const char *exec = "git-upload-archive";
8585
const char *output = NULL;
8686
const char *remote = NULL;
87-
const char *format = NULL;
87+
const char *format_option = NULL;
8888
struct option local_opts[] = {
8989
OPT_STRING('o', "output", &output, "file",
9090
"write the archive to this file"),
9191
OPT_STRING(0, "remote", &remote, "repo",
9292
"retrieve the archive from remote repository <repo>"),
9393
OPT_STRING(0, "exec", &exec, "cmd",
9494
"path to the remote git-upload-archive command"),
95-
OPT_STRING(0, "format", &format, "fmt", "archive format"),
9695
OPT_END()
9796
};
98-
char fmt_opt[32];
9997

10098
argc = parse_options(argc, argv, prefix, local_opts, NULL,
10199
PARSE_OPT_KEEP_ALL);
102100

103101
if (output) {
104102
create_output_file(output);
105-
if (!format)
106-
format = format_from_name(output);
103+
format_option = format_from_name(output);
107104
}
108105

109-
if (format) {
110-
sprintf(fmt_opt, "--format=%s", format);
111-
/*
112-
* We have enough room in argv[] to muck it in place,
113-
* because either --format and/or --output must have
114-
* been given on the original command line if we get
115-
* to this point, and parse_options() must have eaten
116-
* it, i.e. we can add back one element to the array.
117-
* But argv[] may contain "--"; we should make it the
118-
* first option.
119-
*/
106+
/*
107+
* We have enough room in argv[] to muck it in place, because
108+
* --output must have been given on the original command line
109+
* if we get to this point, and parse_options() must have eaten
110+
* it, i.e. we can add back one element to the array.
111+
*
112+
* We add a fake --format option at the beginning, with the
113+
* format inferred from our output filename. This way explicit
114+
* --format options can override it, and the fake option is
115+
* inserted before any "--" that might have been given.
116+
*/
117+
if (format_option) {
120118
memmove(argv + 2, argv + 1, sizeof(*argv) * argc);
121-
argv[1] = fmt_opt;
119+
argv[1] = format_option;
122120
argv[++argc] = NULL;
123121
}
124122

t/t5000-tar-tree.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ test_expect_success 'git archive --format=zip with --output' \
189189
'git archive --format=zip --output=d2.zip HEAD &&
190190
test_cmp d.zip d2.zip'
191191

192+
test_expect_success 'git archive with --output, inferring format' '
193+
git archive --output=d3.zip HEAD &&
194+
test_cmp d.zip d3.zip
195+
'
196+
197+
test_expect_success 'git archive with --output, override inferred format' '
198+
git archive --format=tar --output=d4.zip HEAD &&
199+
test_cmp b.tar d4.zip
200+
'
201+
192202
$UNZIP -v >/dev/null 2>&1
193203
if [ $? -eq 127 ]; then
194204
say "Skipping ZIP tests, because unzip was not found"

0 commit comments

Comments
 (0)