bundle: framework for options before bundle file
Make it possible for any of the git-bundle subcommands to include options: - before the sub-command - after the sub-command, before the bundle filename There is an immediate gain in support for help with all of the sub-commands, where 'git bundle list-heads -h' previously returned an error. Downside here is an increase in code duplication that cannot be trivially avoided short of shared global static options. Signed-off-by: Robin H. Johnson <robbat2@gentoo.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
d9f6f3b619
commit
73c3253d75
194
builtin/bundle.c
194
builtin/bundle.c
@ -1,4 +1,5 @@
|
|||||||
#include "builtin.h"
|
#include "builtin.h"
|
||||||
|
#include "parse-options.h"
|
||||||
#include "cache.h"
|
#include "cache.h"
|
||||||
#include "bundle.h"
|
#include "bundle.h"
|
||||||
|
|
||||||
@ -9,59 +10,158 @@
|
|||||||
* bundle supporting "fetch", "pull", and "ls-remote".
|
* bundle supporting "fetch", "pull", and "ls-remote".
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static const char builtin_bundle_usage[] =
|
static const char * const builtin_bundle_usage[] = {
|
||||||
"git bundle create <file> <git-rev-list args>\n"
|
N_("git bundle create <file> <git-rev-list args>"),
|
||||||
" or: git bundle verify <file>\n"
|
N_("git bundle verify <file>"),
|
||||||
" or: git bundle list-heads <file> [<refname>...]\n"
|
N_("git bundle list-heads <file> [<refname>...]"),
|
||||||
" or: git bundle unbundle <file> [<refname>...]";
|
N_("git bundle unbundle <file> [<refname>...]"),
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
static const char * const builtin_bundle_create_usage[] = {
|
||||||
|
N_("git bundle create <file> <git-rev-list args>"),
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
static const char * const builtin_bundle_verify_usage[] = {
|
||||||
|
N_("git bundle verify <file>"),
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
static const char * const builtin_bundle_list_heads_usage[] = {
|
||||||
|
N_("git bundle list-heads <file> [<refname>...]"),
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
static const char * const builtin_bundle_unbundle_usage[] = {
|
||||||
|
N_("git bundle unbundle <file> [<refname>...]"),
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
static int verbose;
|
||||||
|
|
||||||
|
static int parse_options_cmd_bundle(int argc,
|
||||||
|
const char **argv,
|
||||||
|
const char* prefix,
|
||||||
|
const char * const usagestr[],
|
||||||
|
const struct option options[],
|
||||||
|
const char **bundle_file) {
|
||||||
|
int newargc;
|
||||||
|
newargc = parse_options(argc, argv, NULL, options, usagestr,
|
||||||
|
PARSE_OPT_STOP_AT_NON_OPTION);
|
||||||
|
if (argc < 1)
|
||||||
|
usage_with_options(usagestr, options);
|
||||||
|
*bundle_file = prefix_filename(prefix, argv[0]);
|
||||||
|
return newargc;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cmd_bundle_create(int argc, const char **argv, const char *prefix) {
|
||||||
|
struct option options[] = {
|
||||||
|
OPT_END()
|
||||||
|
};
|
||||||
|
const char* bundle_file;
|
||||||
|
|
||||||
|
argc = parse_options_cmd_bundle(argc, argv, prefix,
|
||||||
|
builtin_bundle_create_usage, options, &bundle_file);
|
||||||
|
/* bundle internals use argv[1] as further parameters */
|
||||||
|
|
||||||
|
if (!startup_info->have_repository)
|
||||||
|
die(_("Need a repository to create a bundle."));
|
||||||
|
return !!create_bundle(the_repository, bundle_file, argc, argv);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
|
||||||
|
struct bundle_header header;
|
||||||
|
int bundle_fd = -1;
|
||||||
|
|
||||||
|
struct option options[] = {
|
||||||
|
OPT_END()
|
||||||
|
};
|
||||||
|
const char* bundle_file;
|
||||||
|
|
||||||
|
argc = parse_options_cmd_bundle(argc, argv, prefix,
|
||||||
|
builtin_bundle_verify_usage, options, &bundle_file);
|
||||||
|
/* bundle internals use argv[1] as further parameters */
|
||||||
|
|
||||||
|
memset(&header, 0, sizeof(header));
|
||||||
|
if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0)
|
||||||
|
return 1;
|
||||||
|
close(bundle_fd);
|
||||||
|
if (verify_bundle(the_repository, &header, 1))
|
||||||
|
return 1;
|
||||||
|
fprintf(stderr, _("%s is okay\n"), bundle_file);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix) {
|
||||||
|
struct bundle_header header;
|
||||||
|
int bundle_fd = -1;
|
||||||
|
|
||||||
|
struct option options[] = {
|
||||||
|
OPT_END()
|
||||||
|
};
|
||||||
|
const char* bundle_file;
|
||||||
|
|
||||||
|
argc = parse_options_cmd_bundle(argc, argv, prefix,
|
||||||
|
builtin_bundle_list_heads_usage, options, &bundle_file);
|
||||||
|
/* bundle internals use argv[1] as further parameters */
|
||||||
|
|
||||||
|
memset(&header, 0, sizeof(header));
|
||||||
|
if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0)
|
||||||
|
return 1;
|
||||||
|
close(bundle_fd);
|
||||||
|
return !!list_bundle_refs(&header, argc, argv);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix) {
|
||||||
|
struct bundle_header header;
|
||||||
|
int bundle_fd = -1;
|
||||||
|
|
||||||
|
struct option options[] = {
|
||||||
|
OPT_END()
|
||||||
|
};
|
||||||
|
const char* bundle_file;
|
||||||
|
|
||||||
|
argc = parse_options_cmd_bundle(argc, argv, prefix,
|
||||||
|
builtin_bundle_unbundle_usage, options, &bundle_file);
|
||||||
|
/* bundle internals use argv[1] as further parameters */
|
||||||
|
|
||||||
|
memset(&header, 0, sizeof(header));
|
||||||
|
if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0)
|
||||||
|
return 1;
|
||||||
|
if (!startup_info->have_repository)
|
||||||
|
die(_("Need a repository to unbundle."));
|
||||||
|
return !!unbundle(the_repository, &header, bundle_fd, 0) ||
|
||||||
|
list_bundle_refs(&header, argc, argv);
|
||||||
|
}
|
||||||
|
|
||||||
int cmd_bundle(int argc, const char **argv, const char *prefix)
|
int cmd_bundle(int argc, const char **argv, const char *prefix)
|
||||||
{
|
{
|
||||||
struct bundle_header header;
|
struct option options[] = {
|
||||||
const char *cmd, *bundle_file;
|
OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
|
||||||
int bundle_fd = -1;
|
OPT_END()
|
||||||
|
};
|
||||||
|
int result;
|
||||||
|
|
||||||
if (argc < 3)
|
argc = parse_options(argc, argv, prefix, options, builtin_bundle_usage,
|
||||||
usage(builtin_bundle_usage);
|
PARSE_OPT_STOP_AT_NON_OPTION);
|
||||||
|
|
||||||
cmd = argv[1];
|
packet_trace_identity("bundle");
|
||||||
bundle_file = prefix_filename(prefix, argv[2]);
|
|
||||||
argc -= 2;
|
|
||||||
argv += 2;
|
|
||||||
|
|
||||||
memset(&header, 0, sizeof(header));
|
if (argc < 2)
|
||||||
if (strcmp(cmd, "create") && (bundle_fd =
|
usage_with_options(builtin_bundle_usage, options);
|
||||||
read_bundle_header(bundle_file, &header)) < 0)
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
if (!strcmp(cmd, "verify")) {
|
else if (!strcmp(argv[0], "create"))
|
||||||
close(bundle_fd);
|
result = cmd_bundle_create(argc, argv, prefix);
|
||||||
if (argc != 1) {
|
else if (!strcmp(argv[0], "verify"))
|
||||||
usage(builtin_bundle_usage);
|
result = cmd_bundle_verify(argc, argv, prefix);
|
||||||
return 1;
|
else if (!strcmp(argv[0], "list-heads"))
|
||||||
}
|
result = cmd_bundle_list_heads(argc, argv, prefix);
|
||||||
if (verify_bundle(the_repository, &header, 1))
|
else if (!strcmp(argv[0], "unbundle"))
|
||||||
return 1;
|
result = cmd_bundle_unbundle(argc, argv, prefix);
|
||||||
fprintf(stderr, _("%s is okay\n"), bundle_file);
|
else {
|
||||||
return 0;
|
error(_("Unknown subcommand: %s"), argv[0]);
|
||||||
|
usage_with_options(builtin_bundle_usage, options);
|
||||||
}
|
}
|
||||||
if (!strcmp(cmd, "list-heads")) {
|
return result ? 1 : 0;
|
||||||
close(bundle_fd);
|
|
||||||
return !!list_bundle_refs(&header, argc, argv);
|
|
||||||
}
|
|
||||||
if (!strcmp(cmd, "create")) {
|
|
||||||
if (argc < 2) {
|
|
||||||
usage(builtin_bundle_usage);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if (!startup_info->have_repository)
|
|
||||||
die(_("Need a repository to create a bundle."));
|
|
||||||
return !!create_bundle(the_repository, bundle_file, argc, argv);
|
|
||||||
} else if (!strcmp(cmd, "unbundle")) {
|
|
||||||
if (!startup_info->have_repository)
|
|
||||||
die(_("Need a repository to unbundle."));
|
|
||||||
return !!unbundle(the_repository, &header, bundle_fd, 0) ||
|
|
||||||
list_bundle_refs(&header, argc, argv);
|
|
||||||
} else
|
|
||||||
usage(builtin_bundle_usage);
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user