3 #include "parse-options.h"
8 * Basic handler for bundle files to connect repositories via sneakernet.
9 * Invocation must include action.
10 * This function can create a bundle or provide information on an existing
11 * bundle supporting "fetch", "pull", and "ls-remote".
14 static const char * const builtin_bundle_usage[] = {
15 N_("git bundle create [<options>] <file> <git-rev-list args>"),
16 N_("git bundle verify [<options>] <file>"),
17 N_("git bundle list-heads <file> [<refname>...]"),
18 N_("git bundle unbundle <file> [<refname>...]"),
22 static const char * const builtin_bundle_create_usage[] = {
23 N_("git bundle create [<options>] <file> <git-rev-list args>"),
27 static const char * const builtin_bundle_verify_usage[] = {
28 N_("git bundle verify [<options>] <file>"),
32 static const char * const builtin_bundle_list_heads_usage[] = {
33 N_("git bundle list-heads <file> [<refname>...]"),
37 static const char * const builtin_bundle_unbundle_usage[] = {
38 N_("git bundle unbundle <file> [<refname>...]"),
44 static int parse_options_cmd_bundle(int argc,
47 const char * const usagestr[],
48 const struct option options[],
49 const char **bundle_file) {
51 newargc = parse_options(argc, argv, NULL, options, usagestr,
52 PARSE_OPT_STOP_AT_NON_OPTION);
54 usage_with_options(usagestr, options);
55 *bundle_file = prefix_filename(prefix, argv[0]);
59 static int cmd_bundle_create(int argc, const char **argv, const char *prefix) {
60 int all_progress_implied = 0;
61 int progress = isatty(STDERR_FILENO);
62 struct strvec pack_opts;
64 struct option options[] = {
65 OPT_SET_INT('q', "quiet", &progress,
66 N_("do not show progress meter"), 0),
67 OPT_SET_INT(0, "progress", &progress,
68 N_("show progress meter"), 1),
69 OPT_SET_INT(0, "all-progress", &progress,
70 N_("show progress meter during object writing phase"), 2),
71 OPT_BOOL(0, "all-progress-implied",
72 &all_progress_implied,
73 N_("similar to --all-progress when progress meter is shown")),
76 const char* bundle_file;
78 argc = parse_options_cmd_bundle(argc, argv, prefix,
79 builtin_bundle_create_usage, options, &bundle_file);
80 /* bundle internals use argv[1] as further parameters */
82 strvec_init(&pack_opts);
84 strvec_push(&pack_opts, "--quiet");
85 else if (progress == 1)
86 strvec_push(&pack_opts, "--progress");
87 else if (progress == 2)
88 strvec_push(&pack_opts, "--all-progress");
89 if (progress && all_progress_implied)
90 strvec_push(&pack_opts, "--all-progress-implied");
92 if (!startup_info->have_repository)
93 die(_("Need a repository to create a bundle."));
94 return !!create_bundle(the_repository, bundle_file, argc, argv, &pack_opts);
97 static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
98 struct bundle_header header;
102 struct option options[] = {
103 OPT_BOOL('q', "quiet", &quiet,
104 N_("do not show bundle details")),
107 const char* bundle_file;
109 argc = parse_options_cmd_bundle(argc, argv, prefix,
110 builtin_bundle_verify_usage, options, &bundle_file);
111 /* bundle internals use argv[1] as further parameters */
113 memset(&header, 0, sizeof(header));
114 if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0)
117 if (verify_bundle(the_repository, &header, !quiet))
119 fprintf(stderr, _("%s is okay\n"), bundle_file);
123 static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix) {
124 struct bundle_header header;
127 struct option options[] = {
130 const char* bundle_file;
132 argc = parse_options_cmd_bundle(argc, argv, prefix,
133 builtin_bundle_list_heads_usage, options, &bundle_file);
134 /* bundle internals use argv[1] as further parameters */
136 memset(&header, 0, sizeof(header));
137 if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0)
140 return !!list_bundle_refs(&header, argc, argv);
143 static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix) {
144 struct bundle_header header;
147 struct option options[] = {
150 const char* bundle_file;
152 argc = parse_options_cmd_bundle(argc, argv, prefix,
153 builtin_bundle_unbundle_usage, options, &bundle_file);
154 /* bundle internals use argv[1] as further parameters */
156 memset(&header, 0, sizeof(header));
157 if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0)
159 if (!startup_info->have_repository)
160 die(_("Need a repository to unbundle."));
161 return !!unbundle(the_repository, &header, bundle_fd, 0) ||
162 list_bundle_refs(&header, argc, argv);
165 int cmd_bundle(int argc, const char **argv, const char *prefix)
167 struct option options[] = {
168 OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
173 argc = parse_options(argc, argv, prefix, options, builtin_bundle_usage,
174 PARSE_OPT_STOP_AT_NON_OPTION);
176 packet_trace_identity("bundle");
179 usage_with_options(builtin_bundle_usage, options);
181 else if (!strcmp(argv[0], "create"))
182 result = cmd_bundle_create(argc, argv, prefix);
183 else if (!strcmp(argv[0], "verify"))
184 result = cmd_bundle_verify(argc, argv, prefix);
185 else if (!strcmp(argv[0], "list-heads"))
186 result = cmd_bundle_list_heads(argc, argv, prefix);
187 else if (!strcmp(argv[0], "unbundle"))
188 result = cmd_bundle_unbundle(argc, argv, prefix);
190 error(_("Unknown subcommand: %s"), argv[0]);
191 usage_with_options(builtin_bundle_usage, options);
193 return result ? 1 : 0;