]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/bundle.c
Merge branch 'jk/bundle-use-dash-for-stdfiles'
[thirdparty/git.git] / builtin / bundle.c
1 #include "builtin.h"
2 #include "strvec.h"
3 #include "parse-options.h"
4 #include "cache.h"
5 #include "bundle.h"
6
7 /*
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".
12 */
13
14 #define BUILTIN_BUNDLE_CREATE_USAGE \
15 N_("git bundle create [-q | --quiet | --progress]\n" \
16 " [--version=<version>] <file> <git-rev-list-args>")
17 #define BUILTIN_BUNDLE_VERIFY_USAGE \
18 N_("git bundle verify [-q | --quiet] <file>")
19 #define BUILTIN_BUNDLE_LIST_HEADS_USAGE \
20 N_("git bundle list-heads <file> [<refname>...]")
21 #define BUILTIN_BUNDLE_UNBUNDLE_USAGE \
22 N_("git bundle unbundle [--progress] <file> [<refname>...]")
23
24 static char const * const builtin_bundle_usage[] = {
25 BUILTIN_BUNDLE_CREATE_USAGE,
26 BUILTIN_BUNDLE_VERIFY_USAGE,
27 BUILTIN_BUNDLE_LIST_HEADS_USAGE,
28 BUILTIN_BUNDLE_UNBUNDLE_USAGE,
29 NULL,
30 };
31
32 static const char * const builtin_bundle_create_usage[] = {
33 BUILTIN_BUNDLE_CREATE_USAGE,
34 NULL
35 };
36
37 static const char * const builtin_bundle_verify_usage[] = {
38 BUILTIN_BUNDLE_VERIFY_USAGE,
39 NULL
40 };
41
42 static const char * const builtin_bundle_list_heads_usage[] = {
43 BUILTIN_BUNDLE_LIST_HEADS_USAGE,
44 NULL
45 };
46
47 static const char * const builtin_bundle_unbundle_usage[] = {
48 BUILTIN_BUNDLE_UNBUNDLE_USAGE,
49 NULL
50 };
51
52 static int parse_options_cmd_bundle(int argc,
53 const char **argv,
54 const char* prefix,
55 const char * const usagestr[],
56 const struct option options[],
57 char **bundle_file) {
58 argc = parse_options(argc, argv, NULL, options, usagestr,
59 PARSE_OPT_STOP_AT_NON_OPTION);
60 if (!argc)
61 usage_msg_opt(_("need a <file> argument"), usagestr, options);
62 *bundle_file = prefix_filename_except_for_dash(prefix, argv[0]);
63 return argc;
64 }
65
66 static int cmd_bundle_create(int argc, const char **argv, const char *prefix) {
67 int all_progress_implied = 1;
68 int progress = isatty(STDERR_FILENO);
69 struct strvec pack_opts;
70 int version = -1;
71 int ret;
72 struct option options[] = {
73 OPT_SET_INT('q', "quiet", &progress,
74 N_("do not show progress meter"), 0),
75 OPT_SET_INT(0, "progress", &progress,
76 N_("show progress meter"), 1),
77 OPT_SET_INT_F(0, "all-progress", &progress,
78 N_("historical; same as --progress"), 2,
79 PARSE_OPT_HIDDEN),
80 OPT_HIDDEN_BOOL(0, "all-progress-implied",
81 &all_progress_implied,
82 N_("historical; does nothing")),
83 OPT_INTEGER(0, "version", &version,
84 N_("specify bundle format version")),
85 OPT_END()
86 };
87 char *bundle_file;
88
89 argc = parse_options_cmd_bundle(argc, argv, prefix,
90 builtin_bundle_create_usage, options, &bundle_file);
91 /* bundle internals use argv[1] as further parameters */
92
93 strvec_init(&pack_opts);
94 if (progress == 0)
95 strvec_push(&pack_opts, "--quiet");
96 else if (progress == 1)
97 strvec_push(&pack_opts, "--progress");
98 else if (progress == 2)
99 strvec_push(&pack_opts, "--all-progress");
100 if (progress && all_progress_implied)
101 strvec_push(&pack_opts, "--all-progress-implied");
102
103 if (!startup_info->have_repository)
104 die(_("Need a repository to create a bundle."));
105 ret = !!create_bundle(the_repository, bundle_file, argc, argv, &pack_opts, version);
106 strvec_clear(&pack_opts);
107 free(bundle_file);
108 return ret;
109 }
110
111 /*
112 * Similar to read_bundle_header(), but handle "-" as stdin.
113 */
114 static int open_bundle(const char *path, struct bundle_header *header,
115 const char **name)
116 {
117 if (!strcmp(path, "-")) {
118 if (name)
119 *name = "<stdin>";
120 return read_bundle_header_fd(0, header, "<stdin>");
121 }
122
123 if (name)
124 *name = path;
125 return read_bundle_header(path, header);
126 }
127
128 static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
129 struct bundle_header header = BUNDLE_HEADER_INIT;
130 int bundle_fd = -1;
131 int quiet = 0;
132 int ret;
133 struct option options[] = {
134 OPT_BOOL('q', "quiet", &quiet,
135 N_("do not show bundle details")),
136 OPT_END()
137 };
138 char *bundle_file;
139 const char *name;
140
141 argc = parse_options_cmd_bundle(argc, argv, prefix,
142 builtin_bundle_verify_usage, options, &bundle_file);
143 /* bundle internals use argv[1] as further parameters */
144
145 if ((bundle_fd = open_bundle(bundle_file, &header, &name)) < 0) {
146 ret = 1;
147 goto cleanup;
148 }
149 close(bundle_fd);
150 if (verify_bundle(the_repository, &header,
151 quiet ? VERIFY_BUNDLE_QUIET : VERIFY_BUNDLE_VERBOSE)) {
152 ret = 1;
153 goto cleanup;
154 }
155
156 fprintf(stderr, _("%s is okay\n"), name);
157 ret = 0;
158 cleanup:
159 free(bundle_file);
160 bundle_header_release(&header);
161 return ret;
162 }
163
164 static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix) {
165 struct bundle_header header = BUNDLE_HEADER_INIT;
166 int bundle_fd = -1;
167 int ret;
168 struct option options[] = {
169 OPT_END()
170 };
171 char *bundle_file;
172
173 argc = parse_options_cmd_bundle(argc, argv, prefix,
174 builtin_bundle_list_heads_usage, options, &bundle_file);
175 /* bundle internals use argv[1] as further parameters */
176
177 if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) {
178 ret = 1;
179 goto cleanup;
180 }
181 close(bundle_fd);
182 ret = !!list_bundle_refs(&header, argc, argv);
183 cleanup:
184 free(bundle_file);
185 bundle_header_release(&header);
186 return ret;
187 }
188
189 static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix) {
190 struct bundle_header header = BUNDLE_HEADER_INIT;
191 int bundle_fd = -1;
192 int ret;
193 int progress = isatty(2);
194
195 struct option options[] = {
196 OPT_BOOL(0, "progress", &progress,
197 N_("show progress meter")),
198 OPT_END()
199 };
200 char *bundle_file;
201 struct strvec extra_index_pack_args = STRVEC_INIT;
202
203 argc = parse_options_cmd_bundle(argc, argv, prefix,
204 builtin_bundle_unbundle_usage, options, &bundle_file);
205 /* bundle internals use argv[1] as further parameters */
206
207 if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) {
208 ret = 1;
209 goto cleanup;
210 }
211 if (!startup_info->have_repository)
212 die(_("Need a repository to unbundle."));
213 if (progress)
214 strvec_pushl(&extra_index_pack_args, "-v", "--progress-title",
215 _("Unbundling objects"), NULL);
216 ret = !!unbundle(the_repository, &header, bundle_fd,
217 &extra_index_pack_args, 0) ||
218 list_bundle_refs(&header, argc, argv);
219 bundle_header_release(&header);
220 cleanup:
221 free(bundle_file);
222 return ret;
223 }
224
225 int cmd_bundle(int argc, const char **argv, const char *prefix)
226 {
227 parse_opt_subcommand_fn *fn = NULL;
228 struct option options[] = {
229 OPT_SUBCOMMAND("create", &fn, cmd_bundle_create),
230 OPT_SUBCOMMAND("verify", &fn, cmd_bundle_verify),
231 OPT_SUBCOMMAND("list-heads", &fn, cmd_bundle_list_heads),
232 OPT_SUBCOMMAND("unbundle", &fn, cmd_bundle_unbundle),
233 OPT_END()
234 };
235
236 argc = parse_options(argc, argv, prefix, options, builtin_bundle_usage,
237 0);
238
239 packet_trace_identity("bundle");
240
241 return !!fn(argc, argv, prefix);
242 }