]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/bundle.c
commit -a -m: allow the top-level tree to become empty again
[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 | --all-progress] [--all-progress-implied]\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(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 = 0;
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(0, "all-progress", &progress,
78 N_("show progress meter during object writing phase"), 2),
79 OPT_BOOL(0, "all-progress-implied",
80 &all_progress_implied,
81 N_("similar to --all-progress when progress meter is shown")),
82 OPT_INTEGER(0, "version", &version,
83 N_("specify bundle format version")),
84 OPT_END()
85 };
86 char *bundle_file;
87
88 argc = parse_options_cmd_bundle(argc, argv, prefix,
89 builtin_bundle_create_usage, options, &bundle_file);
90 /* bundle internals use argv[1] as further parameters */
91
92 strvec_init(&pack_opts);
93 if (progress == 0)
94 strvec_push(&pack_opts, "--quiet");
95 else if (progress == 1)
96 strvec_push(&pack_opts, "--progress");
97 else if (progress == 2)
98 strvec_push(&pack_opts, "--all-progress");
99 if (progress && all_progress_implied)
100 strvec_push(&pack_opts, "--all-progress-implied");
101
102 if (!startup_info->have_repository)
103 die(_("Need a repository to create a bundle."));
104 ret = !!create_bundle(the_repository, bundle_file, argc, argv, &pack_opts, version);
105 strvec_clear(&pack_opts);
106 free(bundle_file);
107 return ret;
108 }
109
110 static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
111 struct bundle_header header = BUNDLE_HEADER_INIT;
112 int bundle_fd = -1;
113 int quiet = 0;
114 int ret;
115 struct option options[] = {
116 OPT_BOOL('q', "quiet", &quiet,
117 N_("do not show bundle details")),
118 OPT_END()
119 };
120 char *bundle_file;
121
122 argc = parse_options_cmd_bundle(argc, argv, prefix,
123 builtin_bundle_verify_usage, options, &bundle_file);
124 /* bundle internals use argv[1] as further parameters */
125
126 if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
127 ret = 1;
128 goto cleanup;
129 }
130 close(bundle_fd);
131 if (verify_bundle(the_repository, &header,
132 quiet ? VERIFY_BUNDLE_QUIET : VERIFY_BUNDLE_VERBOSE)) {
133 ret = 1;
134 goto cleanup;
135 }
136
137 fprintf(stderr, _("%s is okay\n"), bundle_file);
138 ret = 0;
139 cleanup:
140 free(bundle_file);
141 bundle_header_release(&header);
142 return ret;
143 }
144
145 static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix) {
146 struct bundle_header header = BUNDLE_HEADER_INIT;
147 int bundle_fd = -1;
148 int ret;
149 struct option options[] = {
150 OPT_END()
151 };
152 char *bundle_file;
153
154 argc = parse_options_cmd_bundle(argc, argv, prefix,
155 builtin_bundle_list_heads_usage, options, &bundle_file);
156 /* bundle internals use argv[1] as further parameters */
157
158 if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
159 ret = 1;
160 goto cleanup;
161 }
162 close(bundle_fd);
163 ret = !!list_bundle_refs(&header, argc, argv);
164 cleanup:
165 free(bundle_file);
166 bundle_header_release(&header);
167 return ret;
168 }
169
170 static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix) {
171 struct bundle_header header = BUNDLE_HEADER_INIT;
172 int bundle_fd = -1;
173 int ret;
174 int progress = isatty(2);
175
176 struct option options[] = {
177 OPT_BOOL(0, "progress", &progress,
178 N_("show progress meter")),
179 OPT_END()
180 };
181 char *bundle_file;
182 struct strvec extra_index_pack_args = STRVEC_INIT;
183
184 argc = parse_options_cmd_bundle(argc, argv, prefix,
185 builtin_bundle_unbundle_usage, options, &bundle_file);
186 /* bundle internals use argv[1] as further parameters */
187
188 if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
189 ret = 1;
190 goto cleanup;
191 }
192 if (!startup_info->have_repository)
193 die(_("Need a repository to unbundle."));
194 if (progress)
195 strvec_pushl(&extra_index_pack_args, "-v", "--progress-title",
196 _("Unbundling objects"), NULL);
197 ret = !!unbundle(the_repository, &header, bundle_fd,
198 &extra_index_pack_args, 0) ||
199 list_bundle_refs(&header, argc, argv);
200 bundle_header_release(&header);
201 cleanup:
202 free(bundle_file);
203 return ret;
204 }
205
206 int cmd_bundle(int argc, const char **argv, const char *prefix)
207 {
208 parse_opt_subcommand_fn *fn = NULL;
209 struct option options[] = {
210 OPT_SUBCOMMAND("create", &fn, cmd_bundle_create),
211 OPT_SUBCOMMAND("verify", &fn, cmd_bundle_verify),
212 OPT_SUBCOMMAND("list-heads", &fn, cmd_bundle_list_heads),
213 OPT_SUBCOMMAND("unbundle", &fn, cmd_bundle_unbundle),
214 OPT_END()
215 };
216
217 argc = parse_options(argc, argv, prefix, options, builtin_bundle_usage,
218 0);
219
220 packet_trace_identity("bundle");
221
222 return !!fn(argc, argv, prefix);
223 }