]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/bundle.c
The sixth batch
[thirdparty/git.git] / builtin / bundle.c
CommitLineData
03eae9af 1#define USE_THE_REPOSITORY_VARIABLE
baffc0e7 2#include "builtin.h"
0b027f6c 3#include "abspath.h"
f394e093 4#include "gettext.h"
e38da487 5#include "setup.h"
dbbcd44f 6#include "strvec.h"
73c3253d 7#include "parse-options.h"
b388633c 8#include "pkt-line.h"
30415d50 9#include "bundle.h"
2e0afafe
JS
10
11/*
12 * Basic handler for bundle files to connect repositories via sneakernet.
13 * Invocation must include action.
14 * This function can create a bundle or provide information on an existing
34baebce 15 * bundle supporting "fetch", "pull", and "ls-remote".
2e0afafe
JS
16 */
17
f587d164 18#define BUILTIN_BUNDLE_CREATE_USAGE \
8b95521e 19 N_("git bundle create [-q | --quiet | --progress]\n" \
a5748670 20 " [--version=<version>] <file> <git-rev-list-args>")
f587d164 21#define BUILTIN_BUNDLE_VERIFY_USAGE \
a5748670 22 N_("git bundle verify [-q | --quiet] <file>")
f587d164
ÆAB
23#define BUILTIN_BUNDLE_LIST_HEADS_USAGE \
24 N_("git bundle list-heads <file> [<refname>...]")
25#define BUILTIN_BUNDLE_UNBUNDLE_USAGE \
a5748670 26 N_("git bundle unbundle [--progress] <file> [<refname>...]")
f587d164
ÆAB
27
28static char const * const builtin_bundle_usage[] = {
29 BUILTIN_BUNDLE_CREATE_USAGE,
30 BUILTIN_BUNDLE_VERIFY_USAGE,
31 BUILTIN_BUNDLE_LIST_HEADS_USAGE,
32 BUILTIN_BUNDLE_UNBUNDLE_USAGE,
33 NULL,
73c3253d 34};
2e0afafe 35
73c3253d 36static const char * const builtin_bundle_create_usage[] = {
f587d164 37 BUILTIN_BUNDLE_CREATE_USAGE,
968a04e4 38 NULL
73c3253d
RJ
39};
40
41static const char * const builtin_bundle_verify_usage[] = {
f587d164 42 BUILTIN_BUNDLE_VERIFY_USAGE,
968a04e4 43 NULL
73c3253d
RJ
44};
45
46static const char * const builtin_bundle_list_heads_usage[] = {
f587d164 47 BUILTIN_BUNDLE_LIST_HEADS_USAGE,
968a04e4 48 NULL
73c3253d
RJ
49};
50
51static const char * const builtin_bundle_unbundle_usage[] = {
f587d164 52 BUILTIN_BUNDLE_UNBUNDLE_USAGE,
968a04e4 53 NULL
73c3253d
RJ
54};
55
73c3253d
RJ
56static int parse_options_cmd_bundle(int argc,
57 const char **argv,
58 const char* prefix,
59 const char * const usagestr[],
60 const struct option options[],
db6bfb9f 61 char **bundle_file) {
e778ecbc 62 argc = parse_options(argc, argv, NULL, options, usagestr,
73c3253d 63 PARSE_OPT_STOP_AT_NON_OPTION);
e778ecbc 64 if (!argc)
6d5e9e53 65 usage_msg_opt(_("need a <file> argument"), usagestr, options);
a8bfa99d 66 *bundle_file = prefix_filename_except_for_dash(prefix, argv[0]);
e778ecbc 67 return argc;
73c3253d
RJ
68}
69
6f33d8e2
KN
70static int cmd_bundle_create(int argc, const char **argv, const char *prefix,
71 struct repository *repo UNUSED) {
d089a064 72 struct strvec pack_opts = STRVEC_INIT;
c5aecfc8 73 int version = -1;
db6bfb9f 74 int ret;
73c3253d 75 struct option options[] = {
d089a064
RS
76 OPT_PASSTHRU_ARGV('q', "quiet", &pack_opts, NULL,
77 N_("do not show progress meter"),
78 PARSE_OPT_NOARG),
79 OPT_PASSTHRU_ARGV(0, "progress", &pack_opts, NULL,
80 N_("show progress meter"),
81 PARSE_OPT_NOARG),
82 OPT_PASSTHRU_ARGV(0, "all-progress", &pack_opts, NULL,
83 N_("historical; same as --progress"),
84 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
85 OPT_PASSTHRU_ARGV(0, "all-progress-implied", &pack_opts, NULL,
86 N_("historical; does nothing"),
87 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
c5aecfc8 88 OPT_INTEGER(0, "version", &version,
89 N_("specify bundle format version")),
73c3253d
RJ
90 OPT_END()
91 };
db6bfb9f 92 char *bundle_file;
73c3253d 93
d089a064
RS
94 if (isatty(STDERR_FILENO))
95 strvec_push(&pack_opts, "--progress");
96 strvec_push(&pack_opts, "--all-progress-implied");
97
73c3253d
RJ
98 argc = parse_options_cmd_bundle(argc, argv, prefix,
99 builtin_bundle_create_usage, options, &bundle_file);
100 /* bundle internals use argv[1] as further parameters */
101
102 if (!startup_info->have_repository)
103 die(_("Need a repository to create a bundle."));
db6bfb9f 104 ret = !!create_bundle(the_repository, bundle_file, argc, argv, &pack_opts, version);
bf67dd8d 105 strvec_clear(&pack_opts);
db6bfb9f
ÆAB
106 free(bundle_file);
107 return ret;
73c3253d
RJ
108}
109
bf8b1e04
JK
110/*
111 * Similar to read_bundle_header(), but handle "-" as stdin.
112 */
113static int open_bundle(const char *path, struct bundle_header *header,
114 const char **name)
115{
116 if (!strcmp(path, "-")) {
117 if (name)
118 *name = "<stdin>";
119 return read_bundle_header_fd(0, header, "<stdin>");
120 }
121
122 if (name)
123 *name = path;
124 return read_bundle_header(path, header);
125}
126
6f33d8e2
KN
127static int cmd_bundle_verify(int argc, const char **argv, const char *prefix,
128 struct repository *repo UNUSED) {
10b635b7 129 struct bundle_header header = BUNDLE_HEADER_INIT;
2e0afafe 130 int bundle_fd = -1;
e0eba649 131 int quiet = 0;
db6bfb9f 132 int ret;
73c3253d 133 struct option options[] = {
e0eba649
RJ
134 OPT_BOOL('q', "quiet", &quiet,
135 N_("do not show bundle details")),
73c3253d
RJ
136 OPT_END()
137 };
db6bfb9f 138 char *bundle_file;
bf8b1e04 139 const char *name;
2e0afafe 140
73c3253d
RJ
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 */
2e0afafe 144
332b56b7
PS
145 if (!startup_info->have_repository) {
146 ret = error(_("need a repository to verify a bundle"));
147 goto cleanup;
148 }
149
bf8b1e04 150 if ((bundle_fd = open_bundle(bundle_file, &header, &name)) < 0) {
db6bfb9f
ÆAB
151 ret = 1;
152 goto cleanup;
153 }
73c3253d 154 close(bundle_fd);
89bd7fed 155 if (verify_bundle(the_repository, &header,
70334fc3 156 quiet ? VERIFY_BUNDLE_QUIET : VERIFY_BUNDLE_VERBOSE)) {
db6bfb9f
ÆAB
157 ret = 1;
158 goto cleanup;
159 }
160
bf8b1e04 161 fprintf(stderr, _("%s is okay\n"), name);
db6bfb9f
ÆAB
162 ret = 0;
163cleanup:
164 free(bundle_file);
10b635b7 165 bundle_header_release(&header);
db6bfb9f 166 return ret;
73c3253d 167}
2e0afafe 168
6f33d8e2
KN
169static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix,
170 struct repository *repo UNUSED) {
10b635b7 171 struct bundle_header header = BUNDLE_HEADER_INIT;
73c3253d 172 int bundle_fd = -1;
db6bfb9f 173 int ret;
73c3253d
RJ
174 struct option options[] = {
175 OPT_END()
176 };
db6bfb9f 177 char *bundle_file;
73c3253d
RJ
178
179 argc = parse_options_cmd_bundle(argc, argv, prefix,
180 builtin_bundle_list_heads_usage, options, &bundle_file);
181 /* bundle internals use argv[1] as further parameters */
182
bf8b1e04 183 if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) {
db6bfb9f
ÆAB
184 ret = 1;
185 goto cleanup;
186 }
73c3253d 187 close(bundle_fd);
db6bfb9f
ÆAB
188 ret = !!list_bundle_refs(&header, argc, argv);
189cleanup:
190 free(bundle_file);
10b635b7 191 bundle_header_release(&header);
db6bfb9f 192 return ret;
73c3253d
RJ
193}
194
6f33d8e2
KN
195static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix,
196 struct repository *repo UNUSED) {
10b635b7 197 struct bundle_header header = BUNDLE_HEADER_INIT;
73c3253d 198 int bundle_fd = -1;
db6bfb9f 199 int ret;
d941cc4c
ÆAB
200 int progress = isatty(2);
201
73c3253d 202 struct option options[] = {
d941cc4c
ÆAB
203 OPT_BOOL(0, "progress", &progress,
204 N_("show progress meter")),
73c3253d
RJ
205 OPT_END()
206 };
db6bfb9f 207 char *bundle_file;
7366096d 208 struct strvec extra_index_pack_args = STRVEC_INIT;
73c3253d
RJ
209
210 argc = parse_options_cmd_bundle(argc, argv, prefix,
211 builtin_bundle_unbundle_usage, options, &bundle_file);
212 /* bundle internals use argv[1] as further parameters */
213
7298bcc5
PS
214 if (!startup_info->have_repository)
215 die(_("Need a repository to unbundle."));
216
bf8b1e04 217 if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) {
db6bfb9f
ÆAB
218 ret = 1;
219 goto cleanup;
220 }
d941cc4c
ÆAB
221 if (progress)
222 strvec_pushl(&extra_index_pack_args, "-v", "--progress-title",
223 _("Unbundling objects"), NULL);
7366096d 224 ret = !!unbundle(the_repository, &header, bundle_fd,
87c01003 225 &extra_index_pack_args, NULL) ||
73c3253d 226 list_bundle_refs(&header, argc, argv);
10b635b7 227 bundle_header_release(&header);
7720460c 228
db6bfb9f 229cleanup:
7720460c 230 strvec_clear(&extra_index_pack_args);
db6bfb9f
ÆAB
231 free(bundle_file);
232 return ret;
73c3253d
RJ
233}
234
9b1cb507
JC
235int cmd_bundle(int argc,
236 const char **argv,
237 const char *prefix,
6f33d8e2 238 struct repository *repo)
73c3253d 239{
aef7d75e 240 parse_opt_subcommand_fn *fn = NULL;
73c3253d 241 struct option options[] = {
aef7d75e
SG
242 OPT_SUBCOMMAND("create", &fn, cmd_bundle_create),
243 OPT_SUBCOMMAND("verify", &fn, cmd_bundle_verify),
244 OPT_SUBCOMMAND("list-heads", &fn, cmd_bundle_list_heads),
245 OPT_SUBCOMMAND("unbundle", &fn, cmd_bundle_unbundle),
73c3253d
RJ
246 OPT_END()
247 };
73c3253d
RJ
248
249 argc = parse_options(argc, argv, prefix, options, builtin_bundle_usage,
aef7d75e 250 0);
73c3253d
RJ
251
252 packet_trace_identity("bundle");
253
6f33d8e2 254 return !!fn(argc, argv, prefix, repo);
2e0afafe 255}