6 #include "parse-options.h"
11 * Basic handler for bundle files to connect repositories via sneakernet.
12 * Invocation must include action.
13 * This function can create a bundle or provide information on an existing
14 * bundle supporting "fetch", "pull", and "ls-remote".
17 #define BUILTIN_BUNDLE_CREATE_USAGE \
18 N_("git bundle create [-q | --quiet | --progress]\n" \
19 " [--version=<version>] <file> <git-rev-list-args>")
20 #define BUILTIN_BUNDLE_VERIFY_USAGE \
21 N_("git bundle verify [-q | --quiet] <file>")
22 #define BUILTIN_BUNDLE_LIST_HEADS_USAGE \
23 N_("git bundle list-heads <file> [<refname>...]")
24 #define BUILTIN_BUNDLE_UNBUNDLE_USAGE \
25 N_("git bundle unbundle [--progress] <file> [<refname>...]")
27 static char const * const builtin_bundle_usage
[] = {
28 BUILTIN_BUNDLE_CREATE_USAGE
,
29 BUILTIN_BUNDLE_VERIFY_USAGE
,
30 BUILTIN_BUNDLE_LIST_HEADS_USAGE
,
31 BUILTIN_BUNDLE_UNBUNDLE_USAGE
,
35 static const char * const builtin_bundle_create_usage
[] = {
36 BUILTIN_BUNDLE_CREATE_USAGE
,
40 static const char * const builtin_bundle_verify_usage
[] = {
41 BUILTIN_BUNDLE_VERIFY_USAGE
,
45 static const char * const builtin_bundle_list_heads_usage
[] = {
46 BUILTIN_BUNDLE_LIST_HEADS_USAGE
,
50 static const char * const builtin_bundle_unbundle_usage
[] = {
51 BUILTIN_BUNDLE_UNBUNDLE_USAGE
,
55 static int parse_options_cmd_bundle(int argc
,
58 const char * const usagestr
[],
59 const struct option options
[],
61 argc
= parse_options(argc
, argv
, NULL
, options
, usagestr
,
62 PARSE_OPT_STOP_AT_NON_OPTION
);
64 usage_msg_opt(_("need a <file> argument"), usagestr
, options
);
65 *bundle_file
= prefix_filename_except_for_dash(prefix
, argv
[0]);
69 static int cmd_bundle_create(int argc
, const char **argv
, const char *prefix
) {
70 int all_progress_implied
= 1;
71 int progress
= isatty(STDERR_FILENO
);
72 struct strvec pack_opts
;
75 struct option options
[] = {
76 OPT_SET_INT('q', "quiet", &progress
,
77 N_("do not show progress meter"), 0),
78 OPT_SET_INT(0, "progress", &progress
,
79 N_("show progress meter"), 1),
80 OPT_SET_INT_F(0, "all-progress", &progress
,
81 N_("historical; same as --progress"), 2,
83 OPT_HIDDEN_BOOL(0, "all-progress-implied",
84 &all_progress_implied
,
85 N_("historical; does nothing")),
86 OPT_INTEGER(0, "version", &version
,
87 N_("specify bundle format version")),
92 argc
= parse_options_cmd_bundle(argc
, argv
, prefix
,
93 builtin_bundle_create_usage
, options
, &bundle_file
);
94 /* bundle internals use argv[1] as further parameters */
96 strvec_init(&pack_opts
);
98 strvec_push(&pack_opts
, "--quiet");
99 else if (progress
== 1)
100 strvec_push(&pack_opts
, "--progress");
101 else if (progress
== 2)
102 strvec_push(&pack_opts
, "--all-progress");
103 if (progress
&& all_progress_implied
)
104 strvec_push(&pack_opts
, "--all-progress-implied");
106 if (!startup_info
->have_repository
)
107 die(_("Need a repository to create a bundle."));
108 ret
= !!create_bundle(the_repository
, bundle_file
, argc
, argv
, &pack_opts
, version
);
109 strvec_clear(&pack_opts
);
115 * Similar to read_bundle_header(), but handle "-" as stdin.
117 static int open_bundle(const char *path
, struct bundle_header
*header
,
120 if (!strcmp(path
, "-")) {
123 return read_bundle_header_fd(0, header
, "<stdin>");
128 return read_bundle_header(path
, header
);
131 static int cmd_bundle_verify(int argc
, const char **argv
, const char *prefix
) {
132 struct bundle_header header
= BUNDLE_HEADER_INIT
;
136 struct option options
[] = {
137 OPT_BOOL('q', "quiet", &quiet
,
138 N_("do not show bundle details")),
144 argc
= parse_options_cmd_bundle(argc
, argv
, prefix
,
145 builtin_bundle_verify_usage
, options
, &bundle_file
);
146 /* bundle internals use argv[1] as further parameters */
148 if ((bundle_fd
= open_bundle(bundle_file
, &header
, &name
)) < 0) {
153 if (verify_bundle(the_repository
, &header
,
154 quiet
? VERIFY_BUNDLE_QUIET
: VERIFY_BUNDLE_VERBOSE
)) {
159 fprintf(stderr
, _("%s is okay\n"), name
);
163 bundle_header_release(&header
);
167 static int cmd_bundle_list_heads(int argc
, const char **argv
, const char *prefix
) {
168 struct bundle_header header
= BUNDLE_HEADER_INIT
;
171 struct option options
[] = {
176 argc
= parse_options_cmd_bundle(argc
, argv
, prefix
,
177 builtin_bundle_list_heads_usage
, options
, &bundle_file
);
178 /* bundle internals use argv[1] as further parameters */
180 if ((bundle_fd
= open_bundle(bundle_file
, &header
, NULL
)) < 0) {
185 ret
= !!list_bundle_refs(&header
, argc
, argv
);
188 bundle_header_release(&header
);
192 static int cmd_bundle_unbundle(int argc
, const char **argv
, const char *prefix
) {
193 struct bundle_header header
= BUNDLE_HEADER_INIT
;
196 int progress
= isatty(2);
198 struct option options
[] = {
199 OPT_BOOL(0, "progress", &progress
,
200 N_("show progress meter")),
204 struct strvec extra_index_pack_args
= STRVEC_INIT
;
206 argc
= parse_options_cmd_bundle(argc
, argv
, prefix
,
207 builtin_bundle_unbundle_usage
, options
, &bundle_file
);
208 /* bundle internals use argv[1] as further parameters */
210 if ((bundle_fd
= open_bundle(bundle_file
, &header
, NULL
)) < 0) {
214 if (!startup_info
->have_repository
)
215 die(_("Need a repository to unbundle."));
217 strvec_pushl(&extra_index_pack_args
, "-v", "--progress-title",
218 _("Unbundling objects"), NULL
);
219 ret
= !!unbundle(the_repository
, &header
, bundle_fd
,
220 &extra_index_pack_args
, 0) ||
221 list_bundle_refs(&header
, argc
, argv
);
222 bundle_header_release(&header
);
228 int cmd_bundle(int argc
, const char **argv
, const char *prefix
)
230 parse_opt_subcommand_fn
*fn
= NULL
;
231 struct option options
[] = {
232 OPT_SUBCOMMAND("create", &fn
, cmd_bundle_create
),
233 OPT_SUBCOMMAND("verify", &fn
, cmd_bundle_verify
),
234 OPT_SUBCOMMAND("list-heads", &fn
, cmd_bundle_list_heads
),
235 OPT_SUBCOMMAND("unbundle", &fn
, cmd_bundle_unbundle
),
239 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_bundle_usage
,
242 packet_trace_identity("bundle");
244 return !!fn(argc
, argv
, prefix
);