]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/bundle.c
Merge branch 'ds/midx-normalize-pathname-before-comparison'
[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 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
20 };
21
22 static const char * const builtin_bundle_create_usage[] = {
23 N_("git bundle create [<options>] <file> <git-rev-list args>"),
24 NULL
25 };
26
27 static const char * const builtin_bundle_verify_usage[] = {
28 N_("git bundle verify [<options>] <file>"),
29 NULL
30 };
31
32 static const char * const builtin_bundle_list_heads_usage[] = {
33 N_("git bundle list-heads <file> [<refname>...]"),
34 NULL
35 };
36
37 static const char * const builtin_bundle_unbundle_usage[] = {
38 N_("git bundle unbundle <file> [<refname>...]"),
39 NULL
40 };
41
42 static int parse_options_cmd_bundle(int argc,
43 const char **argv,
44 const char* prefix,
45 const char * const usagestr[],
46 const struct option options[],
47 char **bundle_file) {
48 int newargc;
49 newargc = parse_options(argc, argv, NULL, options, usagestr,
50 PARSE_OPT_STOP_AT_NON_OPTION);
51 if (argc < 1)
52 usage_with_options(usagestr, options);
53 *bundle_file = prefix_filename(prefix, argv[0]);
54 return newargc;
55 }
56
57 static int cmd_bundle_create(int argc, const char **argv, const char *prefix) {
58 int all_progress_implied = 0;
59 int progress = isatty(STDERR_FILENO);
60 struct strvec pack_opts;
61 int version = -1;
62 int ret;
63 struct option options[] = {
64 OPT_SET_INT('q', "quiet", &progress,
65 N_("do not show progress meter"), 0),
66 OPT_SET_INT(0, "progress", &progress,
67 N_("show progress meter"), 1),
68 OPT_SET_INT(0, "all-progress", &progress,
69 N_("show progress meter during object writing phase"), 2),
70 OPT_BOOL(0, "all-progress-implied",
71 &all_progress_implied,
72 N_("similar to --all-progress when progress meter is shown")),
73 OPT_INTEGER(0, "version", &version,
74 N_("specify bundle format version")),
75 OPT_END()
76 };
77 char *bundle_file;
78
79 argc = parse_options_cmd_bundle(argc, argv, prefix,
80 builtin_bundle_create_usage, options, &bundle_file);
81 /* bundle internals use argv[1] as further parameters */
82
83 strvec_init(&pack_opts);
84 if (progress == 0)
85 strvec_push(&pack_opts, "--quiet");
86 else if (progress == 1)
87 strvec_push(&pack_opts, "--progress");
88 else if (progress == 2)
89 strvec_push(&pack_opts, "--all-progress");
90 if (progress && all_progress_implied)
91 strvec_push(&pack_opts, "--all-progress-implied");
92
93 if (!startup_info->have_repository)
94 die(_("Need a repository to create a bundle."));
95 ret = !!create_bundle(the_repository, bundle_file, argc, argv, &pack_opts, version);
96 strvec_clear(&pack_opts);
97 free(bundle_file);
98 return ret;
99 }
100
101 static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
102 struct bundle_header header = BUNDLE_HEADER_INIT;
103 int bundle_fd = -1;
104 int quiet = 0;
105 int ret;
106 struct option options[] = {
107 OPT_BOOL('q', "quiet", &quiet,
108 N_("do not show bundle details")),
109 OPT_END()
110 };
111 char *bundle_file;
112
113 argc = parse_options_cmd_bundle(argc, argv, prefix,
114 builtin_bundle_verify_usage, options, &bundle_file);
115 /* bundle internals use argv[1] as further parameters */
116
117 if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
118 ret = 1;
119 goto cleanup;
120 }
121 close(bundle_fd);
122 if (verify_bundle(the_repository, &header, !quiet)) {
123 ret = 1;
124 goto cleanup;
125 }
126
127 fprintf(stderr, _("%s is okay\n"), bundle_file);
128 ret = 0;
129 cleanup:
130 free(bundle_file);
131 bundle_header_release(&header);
132 return ret;
133 }
134
135 static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix) {
136 struct bundle_header header = BUNDLE_HEADER_INIT;
137 int bundle_fd = -1;
138 int ret;
139 struct option options[] = {
140 OPT_END()
141 };
142 char *bundle_file;
143
144 argc = parse_options_cmd_bundle(argc, argv, prefix,
145 builtin_bundle_list_heads_usage, options, &bundle_file);
146 /* bundle internals use argv[1] as further parameters */
147
148 if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
149 ret = 1;
150 goto cleanup;
151 }
152 close(bundle_fd);
153 ret = !!list_bundle_refs(&header, argc, argv);
154 cleanup:
155 free(bundle_file);
156 bundle_header_release(&header);
157 return ret;
158 }
159
160 static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix) {
161 struct bundle_header header = BUNDLE_HEADER_INIT;
162 int bundle_fd = -1;
163 int ret;
164 int progress = isatty(2);
165
166 struct option options[] = {
167 OPT_BOOL(0, "progress", &progress,
168 N_("show progress meter")),
169 OPT_END()
170 };
171 char *bundle_file;
172 struct strvec extra_index_pack_args = STRVEC_INIT;
173
174 argc = parse_options_cmd_bundle(argc, argv, prefix,
175 builtin_bundle_unbundle_usage, options, &bundle_file);
176 /* bundle internals use argv[1] as further parameters */
177
178 if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
179 ret = 1;
180 goto cleanup;
181 }
182 if (!startup_info->have_repository)
183 die(_("Need a repository to unbundle."));
184 if (progress)
185 strvec_pushl(&extra_index_pack_args, "-v", "--progress-title",
186 _("Unbundling objects"), NULL);
187 ret = !!unbundle(the_repository, &header, bundle_fd,
188 &extra_index_pack_args) ||
189 list_bundle_refs(&header, argc, argv);
190 bundle_header_release(&header);
191 cleanup:
192 free(bundle_file);
193 return ret;
194 }
195
196 int cmd_bundle(int argc, const char **argv, const char *prefix)
197 {
198 struct option options[] = {
199 OPT_END()
200 };
201 int result;
202
203 argc = parse_options(argc, argv, prefix, options, builtin_bundle_usage,
204 PARSE_OPT_STOP_AT_NON_OPTION);
205
206 packet_trace_identity("bundle");
207
208 if (argc < 2)
209 usage_with_options(builtin_bundle_usage, options);
210
211 else if (!strcmp(argv[0], "create"))
212 result = cmd_bundle_create(argc, argv, prefix);
213 else if (!strcmp(argv[0], "verify"))
214 result = cmd_bundle_verify(argc, argv, prefix);
215 else if (!strcmp(argv[0], "list-heads"))
216 result = cmd_bundle_list_heads(argc, argv, prefix);
217 else if (!strcmp(argv[0], "unbundle"))
218 result = cmd_bundle_unbundle(argc, argv, prefix);
219 else {
220 error(_("Unknown subcommand: %s"), argv[0]);
221 usage_with_options(builtin_bundle_usage, options);
222 }
223 return result ? 1 : 0;
224 }