@@ -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
0 commit comments