Merge branch 'mh/mingw-case-sensitive-build'
[alt-git.git] / builtin / bundle.c
blob3f63631c0393bc665793d36ca0c6921ad6ace737
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 "pkt-line.h"
8 #include "repository.h"
9 #include "bundle.h"
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
15 * bundle supporting "fetch", "pull", and "ls-remote".
18 #define BUILTIN_BUNDLE_CREATE_USAGE \
19 N_("git bundle create [-q | --quiet | --progress]\n" \
20 " [--version=<version>] <file> <git-rev-list-args>")
21 #define BUILTIN_BUNDLE_VERIFY_USAGE \
22 N_("git bundle verify [-q | --quiet] <file>")
23 #define BUILTIN_BUNDLE_LIST_HEADS_USAGE \
24 N_("git bundle list-heads <file> [<refname>...]")
25 #define BUILTIN_BUNDLE_UNBUNDLE_USAGE \
26 N_("git bundle unbundle [--progress] <file> [<refname>...]")
28 static 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,
36 static const char * const builtin_bundle_create_usage[] = {
37 BUILTIN_BUNDLE_CREATE_USAGE,
38 NULL
41 static const char * const builtin_bundle_verify_usage[] = {
42 BUILTIN_BUNDLE_VERIFY_USAGE,
43 NULL
46 static const char * const builtin_bundle_list_heads_usage[] = {
47 BUILTIN_BUNDLE_LIST_HEADS_USAGE,
48 NULL
51 static const char * const builtin_bundle_unbundle_usage[] = {
52 BUILTIN_BUNDLE_UNBUNDLE_USAGE,
53 NULL
56 static 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[],
61 char **bundle_file) {
62 argc = parse_options(argc, argv, NULL, options, usagestr,
63 PARSE_OPT_STOP_AT_NON_OPTION);
64 if (!argc)
65 usage_msg_opt(_("need a <file> argument"), usagestr, options);
66 *bundle_file = prefix_filename_except_for_dash(prefix, argv[0]);
67 return argc;
70 static int cmd_bundle_create(int argc, const char **argv, const char *prefix) {
71 int all_progress_implied = 1;
72 int progress = isatty(STDERR_FILENO);
73 struct strvec pack_opts;
74 int version = -1;
75 int ret;
76 struct option options[] = {
77 OPT_SET_INT('q', "quiet", &progress,
78 N_("do not show progress meter"), 0),
79 OPT_SET_INT(0, "progress", &progress,
80 N_("show progress meter"), 1),
81 OPT_SET_INT_F(0, "all-progress", &progress,
82 N_("historical; same as --progress"), 2,
83 PARSE_OPT_HIDDEN),
84 OPT_HIDDEN_BOOL(0, "all-progress-implied",
85 &all_progress_implied,
86 N_("historical; does nothing")),
87 OPT_INTEGER(0, "version", &version,
88 N_("specify bundle format version")),
89 OPT_END()
91 char *bundle_file;
93 argc = parse_options_cmd_bundle(argc, argv, prefix,
94 builtin_bundle_create_usage, options, &bundle_file);
95 /* bundle internals use argv[1] as further parameters */
97 strvec_init(&pack_opts);
98 if (progress == 0)
99 strvec_push(&pack_opts, "--quiet");
100 else if (progress == 1)
101 strvec_push(&pack_opts, "--progress");
102 else if (progress == 2)
103 strvec_push(&pack_opts, "--all-progress");
104 if (progress && all_progress_implied)
105 strvec_push(&pack_opts, "--all-progress-implied");
107 if (!startup_info->have_repository)
108 die(_("Need a repository to create a bundle."));
109 ret = !!create_bundle(the_repository, bundle_file, argc, argv, &pack_opts, version);
110 strvec_clear(&pack_opts);
111 free(bundle_file);
112 return ret;
116 * Similar to read_bundle_header(), but handle "-" as stdin.
118 static int open_bundle(const char *path, struct bundle_header *header,
119 const char **name)
121 if (!strcmp(path, "-")) {
122 if (name)
123 *name = "<stdin>";
124 return read_bundle_header_fd(0, header, "<stdin>");
127 if (name)
128 *name = path;
129 return read_bundle_header(path, header);
132 static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
133 struct bundle_header header = BUNDLE_HEADER_INIT;
134 int bundle_fd = -1;
135 int quiet = 0;
136 int ret;
137 struct option options[] = {
138 OPT_BOOL('q', "quiet", &quiet,
139 N_("do not show bundle details")),
140 OPT_END()
142 char *bundle_file;
143 const char *name;
145 argc = parse_options_cmd_bundle(argc, argv, prefix,
146 builtin_bundle_verify_usage, options, &bundle_file);
147 /* bundle internals use argv[1] as further parameters */
149 if ((bundle_fd = open_bundle(bundle_file, &header, &name)) < 0) {
150 ret = 1;
151 goto cleanup;
153 close(bundle_fd);
154 if (verify_bundle(the_repository, &header,
155 quiet ? VERIFY_BUNDLE_QUIET : VERIFY_BUNDLE_VERBOSE)) {
156 ret = 1;
157 goto cleanup;
160 fprintf(stderr, _("%s is okay\n"), name);
161 ret = 0;
162 cleanup:
163 free(bundle_file);
164 bundle_header_release(&header);
165 return ret;
168 static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix) {
169 struct bundle_header header = BUNDLE_HEADER_INIT;
170 int bundle_fd = -1;
171 int ret;
172 struct option options[] = {
173 OPT_END()
175 char *bundle_file;
177 argc = parse_options_cmd_bundle(argc, argv, prefix,
178 builtin_bundle_list_heads_usage, options, &bundle_file);
179 /* bundle internals use argv[1] as further parameters */
181 if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) {
182 ret = 1;
183 goto cleanup;
185 close(bundle_fd);
186 ret = !!list_bundle_refs(&header, argc, argv);
187 cleanup:
188 free(bundle_file);
189 bundle_header_release(&header);
190 return ret;
193 static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix) {
194 struct bundle_header header = BUNDLE_HEADER_INIT;
195 int bundle_fd = -1;
196 int ret;
197 int progress = isatty(2);
199 struct option options[] = {
200 OPT_BOOL(0, "progress", &progress,
201 N_("show progress meter")),
202 OPT_END()
204 char *bundle_file;
205 struct strvec extra_index_pack_args = STRVEC_INIT;
207 argc = parse_options_cmd_bundle(argc, argv, prefix,
208 builtin_bundle_unbundle_usage, options, &bundle_file);
209 /* bundle internals use argv[1] as further parameters */
211 if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) {
212 ret = 1;
213 goto cleanup;
215 if (!startup_info->have_repository)
216 die(_("Need a repository to unbundle."));
217 if (progress)
218 strvec_pushl(&extra_index_pack_args, "-v", "--progress-title",
219 _("Unbundling objects"), NULL);
220 ret = !!unbundle(the_repository, &header, bundle_fd,
221 &extra_index_pack_args, 0) ||
222 list_bundle_refs(&header, argc, argv);
223 bundle_header_release(&header);
224 cleanup:
225 free(bundle_file);
226 return ret;
229 int cmd_bundle(int argc, const char **argv, const char *prefix)
231 parse_opt_subcommand_fn *fn = NULL;
232 struct option options[] = {
233 OPT_SUBCOMMAND("create", &fn, cmd_bundle_create),
234 OPT_SUBCOMMAND("verify", &fn, cmd_bundle_verify),
235 OPT_SUBCOMMAND("list-heads", &fn, cmd_bundle_list_heads),
236 OPT_SUBCOMMAND("unbundle", &fn, cmd_bundle_unbundle),
237 OPT_END()
240 argc = parse_options(argc, argv, prefix, options, builtin_bundle_usage,
243 packet_trace_identity("bundle");
245 return !!fn(argc, argv, prefix);