archive: remove extra arguments parsing code

Replace the code that calls backend specific argument parsers by a
simple flag mechanism.  This reduces code size and complexity.

We can add back such a mechanism (based on incremental parse_opt(),
perhaps) when we need it.  The compression level parameter, though,
is going to be shared by future compressing backends like tgz.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe 2008-07-14 21:22:05 +02:00 committed by Junio C Hamano
parent 1d11d5bb85
commit 489e351ea0
3 changed files with 17 additions and 31 deletions

View File

@ -282,16 +282,3 @@ int write_zip_archive(struct archiver_args *args)
return err; return err;
} }
void *parse_extra_zip_args(int argc, const char **argv)
{
for (; argc > 0; argc--, argv++) {
const char *arg = argv[0];
if (arg[0] == '-' && isdigit(arg[1]) && arg[2] == '\0')
zlib_compression_level = arg[1] - '0';
else
die("Unknown argument for zip format: %s", arg);
}
return NULL;
}

View File

@ -13,19 +13,16 @@ struct archiver_args {
time_t time; time_t time;
const char **pathspec; const char **pathspec;
unsigned int verbose : 1; unsigned int verbose : 1;
void *extra;
}; };
typedef int (*write_archive_fn_t)(struct archiver_args *); typedef int (*write_archive_fn_t)(struct archiver_args *);
typedef void *(*parse_extra_args_fn_t)(int argc, const char **argv);
typedef int (*write_archive_entry_fn_t)(struct archiver_args *args, const unsigned char *sha1, const char *path, size_t pathlen, unsigned int mode, void *buffer, unsigned long size); typedef int (*write_archive_entry_fn_t)(struct archiver_args *args, const unsigned char *sha1, const char *path, size_t pathlen, unsigned int mode, void *buffer, unsigned long size);
struct archiver { struct archiver {
const char *name; const char *name;
write_archive_fn_t write_archive; write_archive_fn_t write_archive;
parse_extra_args_fn_t parse_extra; unsigned int flags;
}; };
extern int parse_archive_args(int argc, const char **argv, const struct archiver **ar, struct archiver_args *args); extern int parse_archive_args(int argc, const char **argv, const struct archiver **ar, struct archiver_args *args);
@ -41,7 +38,6 @@ extern void parse_pathspec_arg(const char **pathspec,
*/ */
extern int write_tar_archive(struct archiver_args *); extern int write_tar_archive(struct archiver_args *);
extern int write_zip_archive(struct archiver_args *); extern int write_zip_archive(struct archiver_args *);
extern void *parse_extra_zip_args(int argc, const char **argv);
extern int write_archive_entries(struct archiver_args *args, write_archive_entry_fn_t write_entry); extern int write_archive_entries(struct archiver_args *args, write_archive_entry_fn_t write_entry);

View File

@ -15,9 +15,11 @@
static const char archive_usage[] = \ static const char archive_usage[] = \
"git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]"; "git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
#define USES_ZLIB_COMPRESSION 1
const struct archiver archivers[] = { const struct archiver archivers[] = {
{ "tar", write_tar_archive, NULL }, { "tar", write_tar_archive },
{ "zip", write_zip_archive, parse_extra_zip_args }, { "zip", write_zip_archive, USES_ZLIB_COMPRESSION },
}; };
static int run_remote_archiver(const char *remote, int argc, static int run_remote_archiver(const char *remote, int argc,
@ -137,10 +139,9 @@ void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
int parse_archive_args(int argc, const char **argv, const struct archiver **ar, int parse_archive_args(int argc, const char **argv, const struct archiver **ar,
struct archiver_args *args) struct archiver_args *args)
{ {
const char *extra_argv[MAX_EXTRA_ARGS];
int extra_argc = 0;
const char *format = "tar"; const char *format = "tar";
const char *base = ""; const char *base = "";
int compression_level = -1;
int verbose = 0; int verbose = 0;
int i; int i;
@ -168,12 +169,12 @@ int parse_archive_args(int argc, const char **argv, const struct archiver **ar,
i++; i++;
break; break;
} }
if (arg[0] == '-') { if (arg[0] == '-' && isdigit(arg[1]) && arg[2] == '\0') {
if (extra_argc > MAX_EXTRA_ARGS - 1) compression_level = arg[1] - '0';
die("Too many extra options");
extra_argv[extra_argc++] = arg;
continue; continue;
} }
if (arg[0] == '-')
die("Unknown argument: %s", arg);
break; break;
} }
@ -184,11 +185,13 @@ int parse_archive_args(int argc, const char **argv, const struct archiver **ar,
if (!*ar) if (!*ar)
die("Unknown archive format '%s'", format); die("Unknown archive format '%s'", format);
if (extra_argc) { if (compression_level != -1) {
if (!(*ar)->parse_extra) if ((*ar)->flags & USES_ZLIB_COMPRESSION)
die("'%s' format does not handle %s", zlib_compression_level = compression_level;
(*ar)->name, extra_argv[0]); else {
args->extra = (*ar)->parse_extra(extra_argc, extra_argv); die("Argument not supported for format '%s': -%d",
format, compression_level);
}
} }
args->verbose = verbose; args->verbose = verbose;
args->base = base; args->base = base;