archive: allow --exec and --remote without equal sign
Convert git archive to parse_options(). The parameters --remote and --exec are still handled by their special parser. Define them anyway in order for them to show up in the usage notice. Note: in a command like "git archive --prefix --remote=a/ HEAD", the string "--remote=a/" will be interpreted as a remote option, not a prefix, because that special parser sees it first. If one needs such a strange prefix, it needs to be specified like this: "git archive --prefix=--remote=a/ HEAD" (with an equal sign). Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Rene Scharfe committed
Jul 25, 2008 at 12:41 UTC
4fac1d3a98bb86d855b706d7ce6a5069b9f687e8
1 file changed
+68
-42
archive.c
+68
-42
@@ -3,9 +3,15 @@
3
#include "tree-walk.h"
4
#include "attr.h"
5
#include "archive.h"
6
-
7
-static const char archive_usage[] = \
8
-"git archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
6
+#include "parse-options.h"
7
+
8
+static char const * const archive_usage[] = {
9
+ "git archive [options] <tree-ish> [path...]",
10
+ "git archive --list",
11
+ "git archive --remote <repo> [--exec <cmd>] [options] <tree-ish> [path...]",
12
+ "git archive --remote <repo> [--exec <cmd>] --list",
13
+ NULL
14
+};
15
16
#define USES_ZLIB_COMPRESSION 1
17
@@ -175,6 +181,9 @@ static const struct archiver *lookup_archiver(const char *name)
181
{
182
int i;
183
184
+ if (!name)
185
+ return NULL;
186
+
187
for (i = 0; i < ARRAY_SIZE(archivers); i++) {
188
if (!strcmp(name, archivers[i].name))
189
return &archivers[i];
@@ -232,51 +241,70 @@ static void parse_treeish_arg(const char **argv,
241
ar_args->time = archive_time;
242
}
243
244
+#define OPT__COMPR(s, v, h, p) \
245
+ { OPTION_SET_INT, (s), NULL, (v), NULL, (h), \
246
+ PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, (p) }
247
+#define OPT__COMPR_HIDDEN(s, v, p) \
248
+ { OPTION_SET_INT, (s), NULL, (v), NULL, "", \
249
+ PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_HIDDEN, NULL, (p) }
250
+
251
static int parse_archive_args(int argc, const char **argv,
252
const struct archiver **ar, struct archiver_args *args)
253
{
254
const char *format = "tar";
239
- const char *base = "";
255
+ const char *base = NULL;
256
+ const char *remote = NULL;
257
+ const char *exec = NULL;
258
int compression_level = -1;
259
int verbose = 0;
260
int i;
243
-
244
- for (i = 1; i < argc; i++) {
245
- const char *arg = argv[i];
246
-
247
- if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
248
- for (i = 0; i < ARRAY_SIZE(archivers); i++)
249
- printf("%s\n", archivers[i].name);
250
- exit(0);
251
- }
252
- if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
253
- verbose = 1;
254
- continue;
255
- }
256
- if (!prefixcmp(arg, "--format=")) {
257
- format = arg + 9;
258
- continue;
259
- }
260
- if (!prefixcmp(arg, "--prefix=")) {
261
- base = arg + 9;
262
- continue;
263
- }
264
- if (!strcmp(arg, "--")) {
265
- i++;
266
- break;
267
- }
268
- if (arg[0] == '-' && isdigit(arg[1]) && arg[2] == '\0') {
269
- compression_level = arg[1] - '0';
270
- continue;
271
- }
272
- if (arg[0] == '-')
273
- die("Unknown argument: %s", arg);
274
- break;
261
+ int list = 0;
262
+ struct option opts[] = {
263
+ OPT_GROUP(""),
264
+ OPT_STRING(0, "format", &format, "fmt", "archive format"),
265
+ OPT_STRING(0, "prefix", &base, "prefix",
266
+ "prepend prefix to each pathname in the archive"),
267
+ OPT__VERBOSE(&verbose),
268
+ OPT__COMPR('0', &compression_level, "store only", 0),
269
+ OPT__COMPR('1', &compression_level, "compress faster", 1),
270
+ OPT__COMPR_HIDDEN('2', &compression_level, 2),
271
+ OPT__COMPR_HIDDEN('3', &compression_level, 3),
272
+ OPT__COMPR_HIDDEN('4', &compression_level, 4),
273
+ OPT__COMPR_HIDDEN('5', &compression_level, 5),
274
+ OPT__COMPR_HIDDEN('6', &compression_level, 6),
275
+ OPT__COMPR_HIDDEN('7', &compression_level, 7),
276
+ OPT__COMPR_HIDDEN('8', &compression_level, 8),
277
+ OPT__COMPR('9', &compression_level, "compress better", 9),
278
+ OPT_GROUP(""),
279
+ OPT_BOOLEAN('l', "list", &list,
280
+ "list supported archive formats"),
281
+ OPT_GROUP(""),
282
+ OPT_STRING(0, "remote", &remote, "repo",
283
+ "retrieve the archive from remote repository <repo>"),
284
+ OPT_STRING(0, "exec", &exec, "cmd",
285
+ "path to the remote git-upload-archive command"),
286
+ OPT_END()
287
+ };
288
+
289
+ argc = parse_options(argc, argv, opts, archive_usage, 0);
290
+
291
+ if (remote)
292
+ die("Unexpected option --remote");
293
+ if (exec)
294
+ die("Option --exec can only be used together with --remote");
295
+
296
+ if (!base)
297
+ base = "";
298
+
299
+ if (list) {
300
+ for (i = 0; i < ARRAY_SIZE(archivers); i++)
301
+ printf("%s\n", archivers[i].name);
302
+ exit(0);
303
}
304
305
/* We need at least one parameter -- tree-ish */
278
- if (argc - 1 < i)
279
- usage(archive_usage);
306
+ if (argc < 1)
307
+ usage_with_options(archive_usage, opts);
308
*ar = lookup_archiver(format);
309
if (!*ar)
310
die("Unknown archive format '%s'", format);
@@ -294,7 +322,7 @@ static int parse_archive_args(int argc, const char **argv,
322
args->base = base;
323
args->baselen = strlen(base);
324
297
- return i;
325
+ return argc;
326
}
327
328
int write_archive(int argc, const char **argv, const char *prefix,
@@ -302,13 +330,11 @@ int write_archive(int argc, const char **argv, const char *prefix,
330
{
331
const struct archiver *ar = NULL;
332
struct archiver_args args;
305
- int tree_idx;
333
307
- tree_idx = parse_archive_args(argc, argv, &ar, &args);
334
+ argc = parse_archive_args(argc, argv, &ar, &args);
335
if (setup_prefix && prefix == NULL)
336
prefix = setup_git_directory();
337
311
- argv += tree_idx;
338
parse_treeish_arg(argv, &args, prefix);
339
parse_pathspec_arg(argv + 1, &args);
340