Merge branch 'fc/completion-updates' into maint
[git/debian.git] / builtin / bundle.c
blob053a51bea1b2ef5c6448deaa70698b7b0d164f44
1 #include "builtin.h"
2 #include "strvec.h"
3 #include "parse-options.h"
4 #include "cache.h"
5 #include "bundle.h"
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".
14 static const char * const builtin_bundle_usage[] = {
15 N_("git bundle create [<options>] <file> <git-rev-list args>"),
16 N_("git bundle verify [<options>] <file>"),
17 N_("git bundle list-heads <file> [<refname>...]"),
18 N_("git bundle unbundle <file> [<refname>...]"),
19 NULL
22 static const char * const builtin_bundle_create_usage[] = {
23 N_("git bundle create [<options>] <file> <git-rev-list args>"),
24 NULL
27 static const char * const builtin_bundle_verify_usage[] = {
28 N_("git bundle verify [<options>] <file>"),
29 NULL
32 static const char * const builtin_bundle_list_heads_usage[] = {
33 N_("git bundle list-heads <file> [<refname>...]"),
34 NULL
37 static const char * const builtin_bundle_unbundle_usage[] = {
38 N_("git bundle unbundle <file> [<refname>...]"),
39 NULL
42 static int verbose;
44 static int parse_options_cmd_bundle(int argc,
45 const char **argv,
46 const char* prefix,
47 const char * const usagestr[],
48 const struct option options[],
49 char **bundle_file) {
50 int newargc;
51 newargc = parse_options(argc, argv, NULL, options, usagestr,
52 PARSE_OPT_STOP_AT_NON_OPTION);
53 if (argc < 1)
54 usage_with_options(usagestr, options);
55 *bundle_file = prefix_filename(prefix, argv[0]);
56 return newargc;
59 static int cmd_bundle_create(int argc, const char **argv, const char *prefix) {
60 int all_progress_implied = 0;
61 int progress = isatty(STDERR_FILENO);
62 struct strvec pack_opts;
63 int version = -1;
64 int ret;
65 struct option options[] = {
66 OPT_SET_INT('q', "quiet", &progress,
67 N_("do not show progress meter"), 0),
68 OPT_SET_INT(0, "progress", &progress,
69 N_("show progress meter"), 1),
70 OPT_SET_INT(0, "all-progress", &progress,
71 N_("show progress meter during object writing phase"), 2),
72 OPT_BOOL(0, "all-progress-implied",
73 &all_progress_implied,
74 N_("similar to --all-progress when progress meter is shown")),
75 OPT_INTEGER(0, "version", &version,
76 N_("specify bundle format version")),
77 OPT_END()
79 char *bundle_file;
81 argc = parse_options_cmd_bundle(argc, argv, prefix,
82 builtin_bundle_create_usage, options, &bundle_file);
83 /* bundle internals use argv[1] as further parameters */
85 strvec_init(&pack_opts);
86 if (progress == 0)
87 strvec_push(&pack_opts, "--quiet");
88 else if (progress == 1)
89 strvec_push(&pack_opts, "--progress");
90 else if (progress == 2)
91 strvec_push(&pack_opts, "--all-progress");
92 if (progress && all_progress_implied)
93 strvec_push(&pack_opts, "--all-progress-implied");
95 if (!startup_info->have_repository)
96 die(_("Need a repository to create a bundle."));
97 ret = !!create_bundle(the_repository, bundle_file, argc, argv, &pack_opts, version);
98 free(bundle_file);
99 return ret;
102 static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
103 struct bundle_header header = BUNDLE_HEADER_INIT;
104 int bundle_fd = -1;
105 int quiet = 0;
106 int ret;
107 struct option options[] = {
108 OPT_BOOL('q', "quiet", &quiet,
109 N_("do not show bundle details")),
110 OPT_END()
112 char *bundle_file;
114 argc = parse_options_cmd_bundle(argc, argv, prefix,
115 builtin_bundle_verify_usage, options, &bundle_file);
116 /* bundle internals use argv[1] as further parameters */
118 if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
119 ret = 1;
120 goto cleanup;
122 close(bundle_fd);
123 if (verify_bundle(the_repository, &header, !quiet)) {
124 ret = 1;
125 goto cleanup;
128 fprintf(stderr, _("%s is okay\n"), bundle_file);
129 ret = 0;
130 cleanup:
131 free(bundle_file);
132 bundle_header_release(&header);
133 return ret;
136 static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix) {
137 struct bundle_header header = BUNDLE_HEADER_INIT;
138 int bundle_fd = -1;
139 int ret;
140 struct option options[] = {
141 OPT_END()
143 char *bundle_file;
145 argc = parse_options_cmd_bundle(argc, argv, prefix,
146 builtin_bundle_list_heads_usage, options, &bundle_file);
147 /* bundle internals use argv[1] as further parameters */
149 if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
150 ret = 1;
151 goto cleanup;
153 close(bundle_fd);
154 ret = !!list_bundle_refs(&header, argc, argv);
155 cleanup:
156 free(bundle_file);
157 bundle_header_release(&header);
158 return ret;
161 static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix) {
162 struct bundle_header header = BUNDLE_HEADER_INIT;
163 int bundle_fd = -1;
164 int ret;
165 struct option options[] = {
166 OPT_END()
168 char *bundle_file;
170 argc = parse_options_cmd_bundle(argc, argv, prefix,
171 builtin_bundle_unbundle_usage, options, &bundle_file);
172 /* bundle internals use argv[1] as further parameters */
174 if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
175 ret = 1;
176 goto cleanup;
178 if (!startup_info->have_repository)
179 die(_("Need a repository to unbundle."));
180 ret = !!unbundle(the_repository, &header, bundle_fd, 0) ||
181 list_bundle_refs(&header, argc, argv);
182 bundle_header_release(&header);
183 cleanup:
184 free(bundle_file);
185 return ret;
188 int cmd_bundle(int argc, const char **argv, const char *prefix)
190 struct option options[] = {
191 OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
192 OPT_END()
194 int result;
196 argc = parse_options(argc, argv, prefix, options, builtin_bundle_usage,
197 PARSE_OPT_STOP_AT_NON_OPTION);
199 packet_trace_identity("bundle");
201 if (argc < 2)
202 usage_with_options(builtin_bundle_usage, options);
204 else if (!strcmp(argv[0], "create"))
205 result = cmd_bundle_create(argc, argv, prefix);
206 else if (!strcmp(argv[0], "verify"))
207 result = cmd_bundle_verify(argc, argv, prefix);
208 else if (!strcmp(argv[0], "list-heads"))
209 result = cmd_bundle_list_heads(argc, argv, prefix);
210 else if (!strcmp(argv[0], "unbundle"))
211 result = cmd_bundle_unbundle(argc, argv, prefix);
212 else {
213 error(_("Unknown subcommand: %s"), argv[0]);
214 usage_with_options(builtin_bundle_usage, options);
216 return result ? 1 : 0;