]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/bundle.c
The eighth batch
[thirdparty/git.git] / builtin / bundle.c
CommitLineData
baffc0e7 1#include "builtin.h"
0b027f6c 2#include "abspath.h"
f394e093 3#include "gettext.h"
e38da487 4#include "setup.h"
dbbcd44f 5#include "strvec.h"
73c3253d 6#include "parse-options.h"
b388633c 7#include "pkt-line.h"
d1cbe1e6 8#include "repository.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
70static int cmd_bundle_create(int argc, const char **argv, const char *prefix) {
d089a064 71 struct strvec pack_opts = STRVEC_INIT;
c5aecfc8 72 int version = -1;
db6bfb9f 73 int ret;
73c3253d 74 struct option options[] = {
d089a064
RS
75 OPT_PASSTHRU_ARGV('q', "quiet", &pack_opts, NULL,
76 N_("do not show progress meter"),
77 PARSE_OPT_NOARG),
78 OPT_PASSTHRU_ARGV(0, "progress", &pack_opts, NULL,
79 N_("show progress meter"),
80 PARSE_OPT_NOARG),
81 OPT_PASSTHRU_ARGV(0, "all-progress", &pack_opts, NULL,
82 N_("historical; same as --progress"),
83 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
84 OPT_PASSTHRU_ARGV(0, "all-progress-implied", &pack_opts, NULL,
85 N_("historical; does nothing"),
86 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
c5aecfc8 87 OPT_INTEGER(0, "version", &version,
88 N_("specify bundle format version")),
73c3253d
RJ
89 OPT_END()
90 };
db6bfb9f 91 char *bundle_file;
73c3253d 92
d089a064
RS
93 if (isatty(STDERR_FILENO))
94 strvec_push(&pack_opts, "--progress");
95 strvec_push(&pack_opts, "--all-progress-implied");
96
73c3253d
RJ
97 argc = parse_options_cmd_bundle(argc, argv, prefix,
98 builtin_bundle_create_usage, options, &bundle_file);
99 /* bundle internals use argv[1] as further parameters */
100
101 if (!startup_info->have_repository)
102 die(_("Need a repository to create a bundle."));
db6bfb9f 103 ret = !!create_bundle(the_repository, bundle_file, argc, argv, &pack_opts, version);
bf67dd8d 104 strvec_clear(&pack_opts);
db6bfb9f
ÆAB
105 free(bundle_file);
106 return ret;
73c3253d
RJ
107}
108
bf8b1e04
JK
109/*
110 * Similar to read_bundle_header(), but handle "-" as stdin.
111 */
112static int open_bundle(const char *path, struct bundle_header *header,
113 const char **name)
114{
115 if (!strcmp(path, "-")) {
116 if (name)
117 *name = "<stdin>";
118 return read_bundle_header_fd(0, header, "<stdin>");
119 }
120
121 if (name)
122 *name = path;
123 return read_bundle_header(path, header);
124}
125
73c3253d 126static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
10b635b7 127 struct bundle_header header = BUNDLE_HEADER_INIT;
2e0afafe 128 int bundle_fd = -1;
e0eba649 129 int quiet = 0;
db6bfb9f 130 int ret;
73c3253d 131 struct option options[] = {
e0eba649
RJ
132 OPT_BOOL('q', "quiet", &quiet,
133 N_("do not show bundle details")),
73c3253d
RJ
134 OPT_END()
135 };
db6bfb9f 136 char *bundle_file;
bf8b1e04 137 const char *name;
2e0afafe 138
73c3253d
RJ
139 argc = parse_options_cmd_bundle(argc, argv, prefix,
140 builtin_bundle_verify_usage, options, &bundle_file);
141 /* bundle internals use argv[1] as further parameters */
2e0afafe 142
bf8b1e04 143 if ((bundle_fd = open_bundle(bundle_file, &header, &name)) < 0) {
db6bfb9f
ÆAB
144 ret = 1;
145 goto cleanup;
146 }
73c3253d 147 close(bundle_fd);
89bd7fed 148 if (verify_bundle(the_repository, &header,
70334fc3 149 quiet ? VERIFY_BUNDLE_QUIET : VERIFY_BUNDLE_VERBOSE)) {
db6bfb9f
ÆAB
150 ret = 1;
151 goto cleanup;
152 }
153
bf8b1e04 154 fprintf(stderr, _("%s is okay\n"), name);
db6bfb9f
ÆAB
155 ret = 0;
156cleanup:
157 free(bundle_file);
10b635b7 158 bundle_header_release(&header);
db6bfb9f 159 return ret;
73c3253d 160}
2e0afafe 161
73c3253d 162static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix) {
10b635b7 163 struct bundle_header header = BUNDLE_HEADER_INIT;
73c3253d 164 int bundle_fd = -1;
db6bfb9f 165 int ret;
73c3253d
RJ
166 struct option options[] = {
167 OPT_END()
168 };
db6bfb9f 169 char *bundle_file;
73c3253d
RJ
170
171 argc = parse_options_cmd_bundle(argc, argv, prefix,
172 builtin_bundle_list_heads_usage, options, &bundle_file);
173 /* bundle internals use argv[1] as further parameters */
174
bf8b1e04 175 if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) {
db6bfb9f
ÆAB
176 ret = 1;
177 goto cleanup;
178 }
73c3253d 179 close(bundle_fd);
db6bfb9f
ÆAB
180 ret = !!list_bundle_refs(&header, argc, argv);
181cleanup:
182 free(bundle_file);
10b635b7 183 bundle_header_release(&header);
db6bfb9f 184 return ret;
73c3253d
RJ
185}
186
187static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix) {
10b635b7 188 struct bundle_header header = BUNDLE_HEADER_INIT;
73c3253d 189 int bundle_fd = -1;
db6bfb9f 190 int ret;
d941cc4c
ÆAB
191 int progress = isatty(2);
192
73c3253d 193 struct option options[] = {
d941cc4c
ÆAB
194 OPT_BOOL(0, "progress", &progress,
195 N_("show progress meter")),
73c3253d
RJ
196 OPT_END()
197 };
db6bfb9f 198 char *bundle_file;
7366096d 199 struct strvec extra_index_pack_args = STRVEC_INIT;
73c3253d
RJ
200
201 argc = parse_options_cmd_bundle(argc, argv, prefix,
202 builtin_bundle_unbundle_usage, options, &bundle_file);
203 /* bundle internals use argv[1] as further parameters */
204
bf8b1e04 205 if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) {
db6bfb9f
ÆAB
206 ret = 1;
207 goto cleanup;
208 }
73c3253d
RJ
209 if (!startup_info->have_repository)
210 die(_("Need a repository to unbundle."));
d941cc4c
ÆAB
211 if (progress)
212 strvec_pushl(&extra_index_pack_args, "-v", "--progress-title",
213 _("Unbundling objects"), NULL);
7366096d 214 ret = !!unbundle(the_repository, &header, bundle_fd,
89bd7fed 215 &extra_index_pack_args, 0) ||
73c3253d 216 list_bundle_refs(&header, argc, argv);
10b635b7 217 bundle_header_release(&header);
db6bfb9f
ÆAB
218cleanup:
219 free(bundle_file);
220 return ret;
73c3253d
RJ
221}
222
223int cmd_bundle(int argc, const char **argv, const char *prefix)
224{
aef7d75e 225 parse_opt_subcommand_fn *fn = NULL;
73c3253d 226 struct option options[] = {
aef7d75e
SG
227 OPT_SUBCOMMAND("create", &fn, cmd_bundle_create),
228 OPT_SUBCOMMAND("verify", &fn, cmd_bundle_verify),
229 OPT_SUBCOMMAND("list-heads", &fn, cmd_bundle_list_heads),
230 OPT_SUBCOMMAND("unbundle", &fn, cmd_bundle_unbundle),
73c3253d
RJ
231 OPT_END()
232 };
73c3253d
RJ
233
234 argc = parse_options(argc, argv, prefix, options, builtin_bundle_usage,
aef7d75e 235 0);
73c3253d
RJ
236
237 packet_trace_identity("bundle");
238
aef7d75e 239 return !!fn(argc, argv, prefix);
2e0afafe 240}