]>
Commit | Line | Data |
---|---|---|
baffc0e7 | 1 | #include "builtin.h" |
dbbcd44f | 2 | #include "strvec.h" |
73c3253d | 3 | #include "parse-options.h" |
2e0afafe | 4 | #include "cache.h" |
30415d50 | 5 | #include "bundle.h" |
2e0afafe JS |
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 | |
34baebce | 11 | * bundle supporting "fetch", "pull", and "ls-remote". |
2e0afafe JS |
12 | */ |
13 | ||
73c3253d | 14 | static const char * const builtin_bundle_usage[] = { |
79862b6b | 15 | N_("git bundle create [<options>] <file> <git-rev-list args>"), |
e0eba649 | 16 | N_("git bundle verify [<options>] <file>"), |
73c3253d RJ |
17 | N_("git bundle list-heads <file> [<refname>...]"), |
18 | N_("git bundle unbundle <file> [<refname>...]"), | |
19 | NULL | |
20 | }; | |
2e0afafe | 21 | |
73c3253d | 22 | static const char * const builtin_bundle_create_usage[] = { |
79862b6b | 23 | N_("git bundle create [<options>] <file> <git-rev-list args>"), |
73c3253d RJ |
24 | NULL |
25 | }; | |
26 | ||
27 | static const char * const builtin_bundle_verify_usage[] = { | |
e0eba649 | 28 | N_("git bundle verify [<options>] <file>"), |
73c3253d RJ |
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 | ||
73c3253d RJ |
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[], | |
db6bfb9f | 47 | char **bundle_file) { |
73c3253d RJ |
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) { | |
79862b6b RJ |
58 | int all_progress_implied = 0; |
59 | int progress = isatty(STDERR_FILENO); | |
22f9b7f3 | 60 | struct strvec pack_opts; |
c5aecfc8 | 61 | int version = -1; |
db6bfb9f | 62 | int ret; |
73c3253d | 63 | struct option options[] = { |
79862b6b RJ |
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")), | |
c5aecfc8 | 73 | OPT_INTEGER(0, "version", &version, |
74 | N_("specify bundle format version")), | |
73c3253d RJ |
75 | OPT_END() |
76 | }; | |
db6bfb9f | 77 | char *bundle_file; |
73c3253d RJ |
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 | ||
22f9b7f3 | 83 | strvec_init(&pack_opts); |
79862b6b | 84 | if (progress == 0) |
22f9b7f3 | 85 | strvec_push(&pack_opts, "--quiet"); |
79862b6b | 86 | else if (progress == 1) |
22f9b7f3 | 87 | strvec_push(&pack_opts, "--progress"); |
79862b6b | 88 | else if (progress == 2) |
22f9b7f3 | 89 | strvec_push(&pack_opts, "--all-progress"); |
79862b6b | 90 | if (progress && all_progress_implied) |
22f9b7f3 | 91 | strvec_push(&pack_opts, "--all-progress-implied"); |
79862b6b | 92 | |
73c3253d RJ |
93 | if (!startup_info->have_repository) |
94 | die(_("Need a repository to create a bundle.")); | |
db6bfb9f | 95 | ret = !!create_bundle(the_repository, bundle_file, argc, argv, &pack_opts, version); |
bf67dd8d | 96 | strvec_clear(&pack_opts); |
db6bfb9f ÆAB |
97 | free(bundle_file); |
98 | return ret; | |
73c3253d RJ |
99 | } |
100 | ||
101 | static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) { | |
10b635b7 | 102 | struct bundle_header header = BUNDLE_HEADER_INIT; |
2e0afafe | 103 | int bundle_fd = -1; |
e0eba649 | 104 | int quiet = 0; |
db6bfb9f | 105 | int ret; |
73c3253d | 106 | struct option options[] = { |
e0eba649 RJ |
107 | OPT_BOOL('q', "quiet", &quiet, |
108 | N_("do not show bundle details")), | |
73c3253d RJ |
109 | OPT_END() |
110 | }; | |
db6bfb9f | 111 | char *bundle_file; |
2e0afafe | 112 | |
73c3253d RJ |
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 */ | |
2e0afafe | 116 | |
db6bfb9f ÆAB |
117 | if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) { |
118 | ret = 1; | |
119 | goto cleanup; | |
120 | } | |
73c3253d | 121 | close(bundle_fd); |
89bd7fed DS |
122 | if (verify_bundle(the_repository, &header, |
123 | quiet ? 0 : VERIFY_BUNDLE_VERBOSE)) { | |
db6bfb9f ÆAB |
124 | ret = 1; |
125 | goto cleanup; | |
126 | } | |
127 | ||
73c3253d | 128 | fprintf(stderr, _("%s is okay\n"), bundle_file); |
db6bfb9f ÆAB |
129 | ret = 0; |
130 | cleanup: | |
131 | free(bundle_file); | |
10b635b7 | 132 | bundle_header_release(&header); |
db6bfb9f | 133 | return ret; |
73c3253d | 134 | } |
2e0afafe | 135 | |
73c3253d | 136 | static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix) { |
10b635b7 | 137 | struct bundle_header header = BUNDLE_HEADER_INIT; |
73c3253d | 138 | int bundle_fd = -1; |
db6bfb9f | 139 | int ret; |
73c3253d RJ |
140 | struct option options[] = { |
141 | OPT_END() | |
142 | }; | |
db6bfb9f | 143 | char *bundle_file; |
73c3253d RJ |
144 | |
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 */ | |
148 | ||
db6bfb9f ÆAB |
149 | if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) { |
150 | ret = 1; | |
151 | goto cleanup; | |
152 | } | |
73c3253d | 153 | close(bundle_fd); |
db6bfb9f ÆAB |
154 | ret = !!list_bundle_refs(&header, argc, argv); |
155 | cleanup: | |
156 | free(bundle_file); | |
10b635b7 | 157 | bundle_header_release(&header); |
db6bfb9f | 158 | return ret; |
73c3253d RJ |
159 | } |
160 | ||
161 | static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix) { | |
10b635b7 | 162 | struct bundle_header header = BUNDLE_HEADER_INIT; |
73c3253d | 163 | int bundle_fd = -1; |
db6bfb9f | 164 | int ret; |
d941cc4c ÆAB |
165 | int progress = isatty(2); |
166 | ||
73c3253d | 167 | struct option options[] = { |
d941cc4c ÆAB |
168 | OPT_BOOL(0, "progress", &progress, |
169 | N_("show progress meter")), | |
73c3253d RJ |
170 | OPT_END() |
171 | }; | |
db6bfb9f | 172 | char *bundle_file; |
7366096d | 173 | struct strvec extra_index_pack_args = STRVEC_INIT; |
73c3253d RJ |
174 | |
175 | argc = parse_options_cmd_bundle(argc, argv, prefix, | |
176 | builtin_bundle_unbundle_usage, options, &bundle_file); | |
177 | /* bundle internals use argv[1] as further parameters */ | |
178 | ||
db6bfb9f ÆAB |
179 | if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) { |
180 | ret = 1; | |
181 | goto cleanup; | |
182 | } | |
73c3253d RJ |
183 | if (!startup_info->have_repository) |
184 | die(_("Need a repository to unbundle.")); | |
d941cc4c ÆAB |
185 | if (progress) |
186 | strvec_pushl(&extra_index_pack_args, "-v", "--progress-title", | |
187 | _("Unbundling objects"), NULL); | |
7366096d | 188 | ret = !!unbundle(the_repository, &header, bundle_fd, |
89bd7fed | 189 | &extra_index_pack_args, 0) || |
73c3253d | 190 | list_bundle_refs(&header, argc, argv); |
10b635b7 | 191 | bundle_header_release(&header); |
db6bfb9f ÆAB |
192 | cleanup: |
193 | free(bundle_file); | |
194 | return ret; | |
73c3253d RJ |
195 | } |
196 | ||
197 | int cmd_bundle(int argc, const char **argv, const char *prefix) | |
198 | { | |
199 | struct option options[] = { | |
73c3253d RJ |
200 | OPT_END() |
201 | }; | |
202 | int result; | |
203 | ||
204 | argc = parse_options(argc, argv, prefix, options, builtin_bundle_usage, | |
205 | PARSE_OPT_STOP_AT_NON_OPTION); | |
206 | ||
207 | packet_trace_identity("bundle"); | |
208 | ||
209 | if (argc < 2) | |
210 | usage_with_options(builtin_bundle_usage, options); | |
211 | ||
212 | else if (!strcmp(argv[0], "create")) | |
213 | result = cmd_bundle_create(argc, argv, prefix); | |
214 | else if (!strcmp(argv[0], "verify")) | |
215 | result = cmd_bundle_verify(argc, argv, prefix); | |
216 | else if (!strcmp(argv[0], "list-heads")) | |
217 | result = cmd_bundle_list_heads(argc, argv, prefix); | |
218 | else if (!strcmp(argv[0], "unbundle")) | |
219 | result = cmd_bundle_unbundle(argc, argv, prefix); | |
220 | else { | |
221 | error(_("Unknown subcommand: %s"), argv[0]); | |
222 | usage_with_options(builtin_bundle_usage, options); | |
2e0afafe | 223 | } |
73c3253d | 224 | return result ? 1 : 0; |
2e0afafe | 225 | } |