setup.h: move declarations for setup.c functions from cache.h
[git.git] / builtin / bundle.c
blobe68fc83d94323582d3eb46a1cc96918c04f323db
1 #include "builtin.h"
2 #include "abspath.h"
3 #include "gettext.h"
4 #include "setup.h"
5 #include "strvec.h"
6 #include "parse-options.h"
7 #include "cache.h"
8 #include "bundle.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,
32 NULL,
35 static const char * const builtin_bundle_create_usage[] = {
36 BUILTIN_BUNDLE_CREATE_USAGE,
37 NULL
40 static const char * const builtin_bundle_verify_usage[] = {
41 BUILTIN_BUNDLE_VERIFY_USAGE,
42 NULL
45 static const char * const builtin_bundle_list_heads_usage[] = {
46 BUILTIN_BUNDLE_LIST_HEADS_USAGE,
47 NULL
50 static const char * const builtin_bundle_unbundle_usage[] = {
51 BUILTIN_BUNDLE_UNBUNDLE_USAGE,
52 NULL
55 static int parse_options_cmd_bundle(int argc,
56 const char **argv,
57 const char* prefix,
58 const char * const usagestr[],
59 const struct option options[],
60 char **bundle_file) {
61 argc = parse_options(argc, argv, NULL, options, usagestr,
62 PARSE_OPT_STOP_AT_NON_OPTION);
63 if (!argc)
64 usage_msg_opt(_("need a <file> argument"), usagestr, options);
65 *bundle_file = prefix_filename_except_for_dash(prefix, argv[0]);
66 return argc;
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;
73 int version = -1;
74 int ret;
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,
82 PARSE_OPT_HIDDEN),
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")),
88 OPT_END()
90 char *bundle_file;
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);
97 if (progress == 0)
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);
110 free(bundle_file);
111 return ret;
115 * Similar to read_bundle_header(), but handle "-" as stdin.
117 static int open_bundle(const char *path, struct bundle_header *header,
118 const char **name)
120 if (!strcmp(path, "-")) {
121 if (name)
122 *name = "<stdin>";
123 return read_bundle_header_fd(0, header, "<stdin>");
126 if (name)
127 *name = path;
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;
133 int bundle_fd = -1;
134 int quiet = 0;
135 int ret;
136 struct option options[] = {
137 OPT_BOOL('q', "quiet", &quiet,
138 N_("do not show bundle details")),
139 OPT_END()
141 char *bundle_file;
142 const char *name;
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) {
149 ret = 1;
150 goto cleanup;
152 close(bundle_fd);
153 if (verify_bundle(the_repository, &header,
154 quiet ? VERIFY_BUNDLE_QUIET : VERIFY_BUNDLE_VERBOSE)) {
155 ret = 1;
156 goto cleanup;
159 fprintf(stderr, _("%s is okay\n"), name);
160 ret = 0;
161 cleanup:
162 free(bundle_file);
163 bundle_header_release(&header);
164 return ret;
167 static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix) {
168 struct bundle_header header = BUNDLE_HEADER_INIT;
169 int bundle_fd = -1;
170 int ret;
171 struct option options[] = {
172 OPT_END()
174 char *bundle_file;
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) {
181 ret = 1;
182 goto cleanup;
184 close(bundle_fd);
185 ret = !!list_bundle_refs(&header, argc, argv);
186 cleanup:
187 free(bundle_file);
188 bundle_header_release(&header);
189 return ret;
192 static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix) {
193 struct bundle_header header = BUNDLE_HEADER_INIT;
194 int bundle_fd = -1;
195 int ret;
196 int progress = isatty(2);
198 struct option options[] = {
199 OPT_BOOL(0, "progress", &progress,
200 N_("show progress meter")),
201 OPT_END()
203 char *bundle_file;
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) {
211 ret = 1;
212 goto cleanup;
214 if (!startup_info->have_repository)
215 die(_("Need a repository to unbundle."));
216 if (progress)
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);
223 cleanup:
224 free(bundle_file);
225 return ret;
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),
236 OPT_END()
239 argc = parse_options(argc, argv, prefix, options, builtin_bundle_usage,
242 packet_trace_identity("bundle");
244 return !!fn(argc, argv, prefix);