Code

Fix typo in 1.6.6.2 release notes
[git.git] / builtin-archive.c
index f9a4bea41e95b9a34f190fc2b696c7394d0e0971..faf4554d5edbb21dbd043daec669bc684bf056cc 100644 (file)
@@ -60,6 +60,17 @@ static int run_remote_archiver(int argc, const char **argv,
        return !!rv;
 }
 
+static const char *format_from_name(const char *filename)
+{
+       const char *ext = strrchr(filename, '.');
+       if (!ext)
+               return NULL;
+       ext++;
+       if (!strcasecmp(ext, "zip"))
+               return "--format=zip";
+       return NULL;
+}
+
 #define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH |         \
                             PARSE_OPT_KEEP_ARGV0 |     \
                             PARSE_OPT_KEEP_UNKNOWN |   \
@@ -70,8 +81,9 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
        const char *exec = "git-upload-archive";
        const char *output = NULL;
        const char *remote = NULL;
+       const char *format_option = NULL;
        struct option local_opts[] = {
-               OPT_STRING(0, "output", &output, "file",
+               OPT_STRING('o', "output", &output, "file",
                        "write the archive to this file"),
                OPT_STRING(0, "remote", &remote, "repo",
                        "retrieve the archive from remote repository <repo>"),
@@ -83,8 +95,27 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
        argc = parse_options(argc, argv, prefix, local_opts, NULL,
                             PARSE_OPT_KEEP_ALL);
 
-       if (output)
+       if (output) {
                create_output_file(output);
+               format_option = format_from_name(output);
+       }
+
+       /*
+        * We have enough room in argv[] to muck it in place, because
+        * --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.
+        *
+        * We add a fake --format option at the beginning, with the
+        * format inferred from our output filename.  This way explicit
+        * --format options can override it, and the fake option is
+        * inserted before any "--" that might have been given.
+        */
+       if (format_option) {
+               memmove(argv + 2, argv + 1, sizeof(*argv) * argc);
+               argv[1] = format_option;
+               argv[++argc] = NULL;
+       }
 
        if (remote)
                return run_remote_archiver(argc, argv, remote, exec);