]>
Commit | Line | Data |
---|---|---|
30415d50 | 1 | #include "cache.h" |
697cc8ef | 2 | #include "lockfile.h" |
30415d50 | 3 | #include "bundle.h" |
41771fa4 | 4 | #include "hex.h" |
cbd53a21 | 5 | #include "object-store.h" |
109cd76d | 6 | #include "repository.h" |
30415d50 JS |
7 | #include "object.h" |
8 | #include "commit.h" | |
9 | #include "diff.h" | |
10 | #include "revision.h" | |
11 | #include "list-objects.h" | |
12 | #include "run-command.h" | |
fa303836 | 13 | #include "refs.h" |
dbbcd44f | 14 | #include "strvec.h" |
105c6f14 | 15 | #include "list-objects-filter-options.h" |
d9fd674c | 16 | #include "connected.h" |
c5aecfc8 | 17 | |
18 | static const char v2_bundle_signature[] = "# v2 git bundle\n"; | |
19 | static const char v3_bundle_signature[] = "# v3 git bundle\n"; | |
20 | static struct { | |
21 | int version; | |
22 | const char *signature; | |
23 | } bundle_sigs[] = { | |
24 | { 2, v2_bundle_signature }, | |
25 | { 3, v3_bundle_signature }, | |
26 | }; | |
30415d50 | 27 | |
10b635b7 | 28 | void bundle_header_init(struct bundle_header *header) |
30415d50 | 29 | { |
10b635b7 ÆAB |
30 | struct bundle_header blank = BUNDLE_HEADER_INIT; |
31 | memcpy(header, &blank, sizeof(*header)); | |
32 | } | |
33 | ||
34 | void bundle_header_release(struct bundle_header *header) | |
35 | { | |
36 | string_list_clear(&header->prerequisites, 1); | |
37 | string_list_clear(&header->references, 1); | |
105c6f14 | 38 | list_objects_filter_release(&header->filter); |
30415d50 JS |
39 | } |
40 | ||
c5aecfc8 | 41 | static int parse_capability(struct bundle_header *header, const char *capability) |
6161ce7b | 42 | { |
c5aecfc8 | 43 | const char *arg; |
44 | if (skip_prefix(capability, "object-format=", &arg)) { | |
45 | int algo = hash_algo_by_name(arg); | |
46 | if (algo == GIT_HASH_UNKNOWN) | |
47 | return error(_("unrecognized bundle hash algorithm: %s"), arg); | |
48 | header->hash_algo = &hash_algos[algo]; | |
49 | return 0; | |
50 | } | |
105c6f14 DS |
51 | if (skip_prefix(capability, "filter=", &arg)) { |
52 | parse_list_objects_filter(&header->filter, arg); | |
53 | return 0; | |
54 | } | |
c5aecfc8 | 55 | return error(_("unknown capability '%s'"), capability); |
56 | } | |
6161ce7b | 57 | |
c5aecfc8 | 58 | static int parse_bundle_signature(struct bundle_header *header, const char *line) |
59 | { | |
60 | int i; | |
61 | ||
62 | for (i = 0; i < ARRAY_SIZE(bundle_sigs); i++) { | |
63 | if (!strcmp(line, bundle_sigs[i].signature)) { | |
64 | header->version = bundle_sigs[i].version; | |
65 | return 0; | |
66 | } | |
67 | } | |
68 | return -1; | |
6161ce7b | 69 | } |
70 | ||
89c6e450 ÆAB |
71 | int read_bundle_header_fd(int fd, struct bundle_header *header, |
72 | const char *report_path) | |
f3fa1838 | 73 | { |
e9ee84cf | 74 | struct strbuf buf = STRBUF_INIT; |
e9ee84cf | 75 | int status = 0; |
30415d50 | 76 | |
e9ee84cf | 77 | /* The bundle header begins with the signature */ |
5e8617f5 | 78 | if (strbuf_getwholeline_fd(&buf, fd, '\n') || |
c5aecfc8 | 79 | parse_bundle_signature(header, buf.buf)) { |
2727b71f | 80 | if (report_path) |
c5aecfc8 | 81 | error(_("'%s' does not look like a v2 or v3 bundle file"), |
2727b71f | 82 | report_path); |
e9ee84cf JH |
83 | status = -1; |
84 | goto abort; | |
30415d50 | 85 | } |
e9ee84cf | 86 | |
c5aecfc8 | 87 | header->hash_algo = the_hash_algo; |
88 | ||
e9ee84cf | 89 | /* The bundle header ends with an empty line */ |
5e8617f5 | 90 | while (!strbuf_getwholeline_fd(&buf, fd, '\n') && |
e9ee84cf | 91 | buf.len && buf.buf[0] != '\n') { |
b8607f35 | 92 | struct object_id oid; |
e9ee84cf | 93 | int is_prereq = 0; |
b8607f35 | 94 | const char *p; |
e9ee84cf | 95 | |
e9ee84cf | 96 | strbuf_rtrim(&buf); |
30415d50 | 97 | |
c5aecfc8 | 98 | if (header->version == 3 && *buf.buf == '@') { |
99 | if (parse_capability(header, buf.buf + 1)) { | |
6161ce7b | 100 | status = -1; |
101 | break; | |
102 | } | |
c5aecfc8 | 103 | continue; |
104 | } | |
105 | ||
106 | if (*buf.buf == '-') { | |
107 | is_prereq = 1; | |
108 | strbuf_remove(&buf, 0, 1); | |
6161ce7b | 109 | } |
110 | ||
e9ee84cf JH |
111 | /* |
112 | * Tip lines have object name, SP, and refname. | |
113 | * Prerequisites have object name that is optionally | |
114 | * followed by SP and subject line. | |
115 | */ | |
6161ce7b | 116 | if (parse_oid_hex_algop(buf.buf, &oid, &p, header->hash_algo) || |
b8607f35 | 117 | (*p && !isspace(*p)) || |
118 | (!is_prereq && !*p)) { | |
2727b71f | 119 | if (report_path) |
8a1e7eac | 120 | error(_("unrecognized header: %s%s (%d)"), |
2727b71f | 121 | (is_prereq ? "-" : ""), buf.buf, (int)buf.len); |
e9ee84cf JH |
122 | status = -1; |
123 | break; | |
124 | } else { | |
10b635b7 | 125 | struct object_id *dup = oiddup(&oid); |
e9ee84cf | 126 | if (is_prereq) |
10b635b7 | 127 | string_list_append(&header->prerequisites, "")->util = dup; |
e9ee84cf | 128 | else |
10b635b7 | 129 | string_list_append(&header->references, p + 1)->util = dup; |
30415d50 | 130 | } |
30415d50 | 131 | } |
e9ee84cf JH |
132 | |
133 | abort: | |
134 | if (status) { | |
135 | close(fd); | |
136 | fd = -1; | |
30415d50 | 137 | } |
e9ee84cf | 138 | strbuf_release(&buf); |
30415d50 JS |
139 | return fd; |
140 | } | |
141 | ||
2727b71f JH |
142 | int read_bundle_header(const char *path, struct bundle_header *header) |
143 | { | |
144 | int fd = open(path, O_RDONLY); | |
145 | ||
30415d50 | 146 | if (fd < 0) |
8a1e7eac | 147 | return error(_("could not open '%s'"), path); |
89c6e450 | 148 | return read_bundle_header_fd(fd, header, path); |
2727b71f JH |
149 | } |
150 | ||
151 | int is_bundle(const char *path, int quiet) | |
152 | { | |
10b635b7 | 153 | struct bundle_header header = BUNDLE_HEADER_INIT; |
2727b71f JH |
154 | int fd = open(path, O_RDONLY); |
155 | ||
156 | if (fd < 0) | |
157 | return 0; | |
89c6e450 | 158 | fd = read_bundle_header_fd(fd, &header, quiet ? NULL : path); |
2727b71f JH |
159 | if (fd >= 0) |
160 | close(fd); | |
10b635b7 | 161 | bundle_header_release(&header); |
2727b71f | 162 | return (fd >= 0); |
30415d50 JS |
163 | } |
164 | ||
10b635b7 | 165 | static int list_refs(struct string_list *r, int argc, const char **argv) |
30415d50 JS |
166 | { |
167 | int i; | |
168 | ||
169 | for (i = 0; i < r->nr; i++) { | |
15e7c7dc ÆAB |
170 | struct object_id *oid; |
171 | const char *name; | |
172 | ||
30415d50 JS |
173 | if (argc > 1) { |
174 | int j; | |
175 | for (j = 1; j < argc; j++) | |
10b635b7 | 176 | if (!strcmp(r->items[i].string, argv[j])) |
30415d50 JS |
177 | break; |
178 | if (j == argc) | |
179 | continue; | |
180 | } | |
15e7c7dc | 181 | |
10b635b7 ÆAB |
182 | oid = r->items[i].util; |
183 | name = r->items[i].string; | |
15e7c7dc | 184 | printf("%s %s\n", oid_to_hex(oid), name); |
30415d50 JS |
185 | } |
186 | return 0; | |
187 | } | |
188 | ||
208acbfb | 189 | /* Remember to update object flag allocation in object.h */ |
30415d50 JS |
190 | #define PREREQ_MARK (1u<<16) |
191 | ||
d9fd674c DS |
192 | struct string_list_iterator { |
193 | struct string_list *list; | |
194 | size_t cur; | |
195 | }; | |
196 | ||
197 | static const struct object_id *iterate_ref_map(void *cb_data) | |
198 | { | |
199 | struct string_list_iterator *iter = cb_data; | |
200 | ||
201 | if (iter->cur >= iter->list->nr) | |
202 | return NULL; | |
203 | ||
204 | return iter->list->items[iter->cur++].util; | |
205 | } | |
206 | ||
74ae4b63 NTND |
207 | int verify_bundle(struct repository *r, |
208 | struct bundle_header *header, | |
89bd7fed | 209 | enum verify_bundle_flags flags) |
30415d50 JS |
210 | { |
211 | /* | |
212 | * Do fast check, then if any prereqs are missing then go line by line | |
213 | * to be verbose about the errors | |
214 | */ | |
10b635b7 | 215 | struct string_list *p = &header->prerequisites; |
d9fd674c | 216 | int i, ret = 0; |
8a1e7eac | 217 | const char *message = _("Repository lacks these prerequisite commits:"); |
d9fd674c DS |
218 | struct string_list_iterator iter = { |
219 | .list = p, | |
220 | }; | |
221 | struct check_connected_options opts = { | |
222 | .quiet = 1, | |
223 | }; | |
30415d50 | 224 | |
c96060b0 DS |
225 | if (!r || !r->objects || !r->objects->odb) |
226 | return error(_("need a repository to verify a bundle")); | |
3bbbe467 | 227 | |
30415d50 | 228 | for (i = 0; i < p->nr; i++) { |
10b635b7 ÆAB |
229 | struct string_list_item *e = p->items + i; |
230 | const char *name = e->string; | |
231 | struct object_id *oid = e->util; | |
15e7c7dc | 232 | struct object *o = parse_object(r, oid); |
d9fd674c | 233 | if (o) |
30415d50 | 234 | continue; |
70334fc3 DS |
235 | ret++; |
236 | if (flags & VERIFY_BUNDLE_QUIET) | |
237 | continue; | |
238 | if (ret == 1) | |
9db56f71 | 239 | error("%s", message); |
15e7c7dc | 240 | error("%s %s", oid_to_hex(oid), name); |
30415d50 | 241 | } |
d9fd674c | 242 | if (ret) |
f196c1e9 | 243 | goto cleanup; |
105c6f14 | 244 | |
d9fd674c DS |
245 | if ((ret = check_connected(iterate_ref_map, &iter, &opts))) |
246 | error(_("some prerequisite commits exist in the object store, " | |
247 | "but are not connected to the repository's history")); | |
30415d50 | 248 | |
d9fd674c | 249 | /* TODO: preserve this verbose language. */ |
89bd7fed | 250 | if (flags & VERIFY_BUNDLE_VERBOSE) { |
10b635b7 | 251 | struct string_list *r; |
30415d50 JS |
252 | |
253 | r = &header->references; | |
a02ffe0e | 254 | printf_ln(Q_("The bundle contains this ref:", |
99d60545 | 255 | "The bundle contains these %"PRIuMAX" refs:", |
8a1e7eac | 256 | r->nr), |
99d60545 | 257 | (uintmax_t)r->nr); |
30415d50 | 258 | list_refs(r, 0, NULL); |
105c6f14 | 259 | |
71ba6b10 | 260 | r = &header->prerequisites; |
8c3710fd JH |
261 | if (!r->nr) { |
262 | printf_ln(_("The bundle records a complete history.")); | |
263 | } else { | |
a02ffe0e | 264 | printf_ln(Q_("The bundle requires this ref:", |
99d60545 | 265 | "The bundle requires these %"PRIuMAX" refs:", |
8c3710fd | 266 | r->nr), |
99d60545 | 267 | (uintmax_t)r->nr); |
8c3710fd JH |
268 | list_refs(r, 0, NULL); |
269 | } | |
017303eb | 270 | |
8ba221e2 DS |
271 | printf_ln("The bundle uses this hash algorithm: %s", |
272 | header->hash_algo->name); | |
017303eb DS |
273 | if (header->filter.choice) |
274 | printf_ln("The bundle uses this filter: %s", | |
275 | list_objects_filter_spec(&header->filter)); | |
30415d50 | 276 | } |
f196c1e9 | 277 | cleanup: |
30415d50 JS |
278 | return ret; |
279 | } | |
280 | ||
281 | int list_bundle_refs(struct bundle_header *header, int argc, const char **argv) | |
282 | { | |
283 | return list_refs(&header->references, argc, argv); | |
284 | } | |
285 | ||
c9a42c4a JS |
286 | static int is_tag_in_date_range(struct object *tag, struct rev_info *revs) |
287 | { | |
288 | unsigned long size; | |
289 | enum object_type type; | |
64045940 | 290 | char *buf = NULL, *line, *lineend; |
dddbad72 | 291 | timestamp_t date; |
64045940 | 292 | int result = 1; |
c9a42c4a JS |
293 | |
294 | if (revs->max_age == -1 && revs->min_age == -1) | |
64045940 | 295 | goto out; |
c9a42c4a | 296 | |
b4f5aca4 | 297 | buf = read_object_file(&tag->oid, &type, &size); |
c9a42c4a | 298 | if (!buf) |
64045940 | 299 | goto out; |
c9a42c4a JS |
300 | line = memmem(buf, size, "\ntagger ", 8); |
301 | if (!line++) | |
64045940 | 302 | goto out; |
2c8544ab LF |
303 | lineend = memchr(line, '\n', buf + size - line); |
304 | line = memchr(line, '>', lineend ? lineend - line : buf + size - line); | |
c9a42c4a | 305 | if (!line++) |
64045940 | 306 | goto out; |
1aeb7e75 | 307 | date = parse_timestamp(line, NULL, 10); |
64045940 | 308 | result = (revs->max_age == -1 || revs->max_age < date) && |
c9a42c4a | 309 | (revs->min_age == -1 || revs->min_age > date); |
64045940 RS |
310 | out: |
311 | free(buf); | |
312 | return result; | |
c9a42c4a JS |
313 | } |
314 | ||
e54c347c | 315 | |
2c8ee1f5 | 316 | /* Write the pack data to bundle_fd */ |
ef8d7ac4 | 317 | static int write_pack_data(int bundle_fd, struct rev_info *revs, struct strvec *pack_options) |
5e626b91 JH |
318 | { |
319 | struct child_process pack_objects = CHILD_PROCESS_INIT; | |
320 | int i; | |
321 | ||
ef8d7ac4 | 322 | strvec_pushl(&pack_objects.args, |
f6d8942b JK |
323 | "pack-objects", |
324 | "--stdout", "--thin", "--delta-base-offset", | |
325 | NULL); | |
d70a9eb6 | 326 | strvec_pushv(&pack_objects.args, pack_options->v); |
f18b512b DS |
327 | if (revs->filter.choice) |
328 | strvec_pushf(&pack_objects.args, "--filter=%s", | |
329 | list_objects_filter_spec(&revs->filter)); | |
5e626b91 JH |
330 | pack_objects.in = -1; |
331 | pack_objects.out = bundle_fd; | |
332 | pack_objects.git_cmd = 1; | |
2c8ee1f5 JK |
333 | |
334 | /* | |
335 | * start_command() will close our descriptor if it's >1. Duplicate it | |
336 | * to avoid surprising the caller. | |
337 | */ | |
338 | if (pack_objects.out > 1) { | |
339 | pack_objects.out = dup(pack_objects.out); | |
340 | if (pack_objects.out < 0) { | |
341 | error_errno(_("unable to dup bundle descriptor")); | |
342 | child_process_clear(&pack_objects); | |
343 | return -1; | |
344 | } | |
345 | } | |
346 | ||
5e626b91 JH |
347 | if (start_command(&pack_objects)) |
348 | return error(_("Could not spawn pack-objects")); | |
349 | ||
5e626b91 JH |
350 | for (i = 0; i < revs->pending.nr; i++) { |
351 | struct object *object = revs->pending.objects[i].item; | |
352 | if (object->flags & UNINTERESTING) | |
353 | write_or_die(pack_objects.in, "^", 1); | |
703d2d41 | 354 | write_or_die(pack_objects.in, oid_to_hex(&object->oid), the_hash_algo->hexsz); |
5e626b91 JH |
355 | write_or_die(pack_objects.in, "\n", 1); |
356 | } | |
357 | close(pack_objects.in); | |
358 | if (finish_command(&pack_objects)) | |
359 | return error(_("pack-objects died")); | |
360 | return 0; | |
361 | } | |
362 | ||
d9362ef9 JK |
363 | /* |
364 | * Write out bundle refs based on the tips already | |
365 | * parsed into revs.pending. As a side effect, may | |
366 | * manipulate revs.pending to include additional | |
367 | * necessary objects (like tags). | |
368 | * | |
369 | * Returns the number of refs written, or negative | |
370 | * on error. | |
371 | */ | |
372 | static int write_bundle_refs(int bundle_fd, struct rev_info *revs) | |
e8eb2512 | 373 | { |
d9362ef9 JK |
374 | int i; |
375 | int ref_count = 0; | |
b2a6d1c6 | 376 | |
d9362ef9 JK |
377 | for (i = 0; i < revs->pending.nr; i++) { |
378 | struct object_array_entry *e = revs->pending.objects + i; | |
f2fd0760 | 379 | struct object_id oid; |
30415d50 | 380 | char *ref; |
fa303836 JH |
381 | const char *display_ref; |
382 | int flag; | |
30415d50 JS |
383 | |
384 | if (e->item->flags & UNINTERESTING) | |
385 | continue; | |
f24c30e0 | 386 | if (dwim_ref(e->name, strlen(e->name), &oid, &ref, 0) != 1) |
c8a571d8 | 387 | goto skip_write_ref; |
34c290a6 | 388 | if (read_ref_full(e->name, RESOLVE_REF_READING, &oid, &flag)) |
fa303836 JH |
389 | flag = 0; |
390 | display_ref = (flag & REF_ISSYMREF) ? e->name : ref; | |
391 | ||
c9a42c4a | 392 | if (e->item->type == OBJ_TAG && |
d9362ef9 | 393 | !is_tag_in_date_range(e->item, revs)) { |
c9a42c4a | 394 | e->item->flags |= UNINTERESTING; |
c8a571d8 | 395 | goto skip_write_ref; |
c9a42c4a JS |
396 | } |
397 | ||
30415d50 JS |
398 | /* |
399 | * Make sure the refs we wrote out is correct; --max-count and | |
400 | * other limiting options could have prevented all the tips | |
401 | * from getting output. | |
402 | * | |
403 | * Non commit objects such as tags and blobs do not have | |
404 | * this issue as they are not affected by those extra | |
405 | * constraints. | |
406 | */ | |
407 | if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) { | |
8a1e7eac | 408 | warning(_("ref '%s' is excluded by the rev-list options"), |
30415d50 | 409 | e->name); |
c8a571d8 | 410 | goto skip_write_ref; |
30415d50 JS |
411 | } |
412 | /* | |
413 | * If you run "git bundle create bndl v1.0..v2.0", the | |
414 | * name of the positive ref is "v2.0" but that is the | |
415 | * commit that is referenced by the tag, and not the tag | |
416 | * itself. | |
417 | */ | |
9001dc2a | 418 | if (!oideq(&oid, &e->item->oid)) { |
30415d50 JS |
419 | /* |
420 | * Is this the positive end of a range expressed | |
421 | * in terms of a tag (e.g. v2.0 from the range | |
422 | * "v1.0..v2.0")? | |
423 | */ | |
74ae4b63 | 424 | struct commit *one = lookup_commit_reference(revs->repo, &oid); |
30415d50 JS |
425 | struct object *obj; |
426 | ||
427 | if (e->item == &(one->object)) { | |
428 | /* | |
429 | * Need to include e->name as an | |
430 | * independent ref to the pack-objects | |
431 | * input, so that the tag is included | |
432 | * in the output; otherwise we would | |
433 | * end up triggering "empty bundle" | |
434 | * error. | |
435 | */ | |
c251c83d | 436 | obj = parse_object_or_die(&oid, e->name); |
30415d50 | 437 | obj->flags |= SHOWN; |
d9362ef9 | 438 | add_pending_object(revs, obj, e->name); |
30415d50 | 439 | } |
c8a571d8 | 440 | goto skip_write_ref; |
30415d50 JS |
441 | } |
442 | ||
443 | ref_count++; | |
703d2d41 | 444 | write_or_die(bundle_fd, oid_to_hex(&e->item->oid), the_hash_algo->hexsz); |
30415d50 | 445 | write_or_die(bundle_fd, " ", 1); |
fa303836 | 446 | write_or_die(bundle_fd, display_ref, strlen(display_ref)); |
30415d50 | 447 | write_or_die(bundle_fd, "\n", 1); |
c8a571d8 | 448 | skip_write_ref: |
30415d50 JS |
449 | free(ref); |
450 | } | |
30415d50 JS |
451 | |
452 | /* end header */ | |
453 | write_or_die(bundle_fd, "\n", 1); | |
d9362ef9 JK |
454 | return ref_count; |
455 | } | |
456 | ||
5bb0fd2c JX |
457 | struct bundle_prerequisites_info { |
458 | struct object_array *pending; | |
459 | int fd; | |
460 | }; | |
461 | ||
462 | static void write_bundle_prerequisites(struct commit *commit, void *data) | |
463 | { | |
464 | struct bundle_prerequisites_info *bpi = data; | |
465 | struct object *object; | |
466 | struct pretty_print_context ctx = { 0 }; | |
467 | struct strbuf buf = STRBUF_INIT; | |
468 | ||
469 | if (!(commit->object.flags & BOUNDARY)) | |
470 | return; | |
471 | strbuf_addf(&buf, "-%s ", oid_to_hex(&commit->object.oid)); | |
472 | write_or_die(bpi->fd, buf.buf, buf.len); | |
473 | ||
474 | ctx.fmt = CMIT_FMT_ONELINE; | |
475 | ctx.output_encoding = get_log_output_encoding(); | |
476 | strbuf_reset(&buf); | |
477 | pretty_print_commit(&ctx, commit, &buf); | |
478 | strbuf_trim(&buf); | |
479 | ||
480 | object = (struct object *)commit; | |
481 | object->flags |= UNINTERESTING; | |
482 | add_object_array_with_path(object, buf.buf, bpi->pending, S_IFINVALID, | |
483 | NULL); | |
484 | strbuf_addch(&buf, '\n'); | |
485 | write_or_die(bpi->fd, buf.buf, buf.len); | |
486 | strbuf_release(&buf); | |
487 | } | |
488 | ||
fcb133e9 | 489 | int create_bundle(struct repository *r, const char *path, |
e0ad9574 | 490 | int argc, const char **argv, struct strvec *pack_options, int version) |
d9362ef9 | 491 | { |
b2275868 | 492 | struct lock_file lock = LOCK_INIT; |
d9362ef9 JK |
493 | int bundle_fd = -1; |
494 | int bundle_to_stdout; | |
495 | int ref_count = 0; | |
5bb0fd2c | 496 | struct rev_info revs, revs_copy; |
f18b512b | 497 | int min_version = 2; |
5bb0fd2c JX |
498 | struct bundle_prerequisites_info bpi; |
499 | int i; | |
d9362ef9 | 500 | |
f18b512b DS |
501 | /* init revs to list objects for pack-objects later */ |
502 | save_commit_buffer = 0; | |
503 | repo_init_revisions(r, &revs, NULL); | |
504 | ||
505 | /* | |
506 | * Pre-initialize the '--objects' flag so we can parse a | |
507 | * --filter option successfully. | |
508 | */ | |
509 | revs.tree_objects = revs.blob_objects = 1; | |
510 | ||
511 | argc = setup_revisions(argc, argv, &revs, NULL); | |
512 | ||
513 | /* | |
514 | * Reasons to require version 3: | |
515 | * | |
516 | * 1. @object-format is required because our hash algorithm is not | |
517 | * SHA1. | |
518 | * 2. @filter is required because we parsed an object filter. | |
519 | */ | |
520 | if (the_hash_algo != &hash_algos[GIT_HASH_SHA1] || revs.filter.choice) | |
521 | min_version = 3; | |
522 | ||
523 | if (argc > 1) { | |
524 | error(_("unrecognized argument: %s"), argv[1]); | |
525 | goto err; | |
526 | } | |
527 | ||
d9362ef9 JK |
528 | bundle_to_stdout = !strcmp(path, "-"); |
529 | if (bundle_to_stdout) | |
530 | bundle_fd = 1; | |
2c8ee1f5 | 531 | else |
d9362ef9 JK |
532 | bundle_fd = hold_lock_file_for_update(&lock, path, |
533 | LOCK_DIE_ON_ERROR); | |
534 | ||
c5aecfc8 | 535 | if (version == -1) |
536 | version = min_version; | |
537 | ||
538 | if (version < 2 || version > 3) { | |
539 | die(_("unsupported bundle version %d"), version); | |
540 | } else if (version < min_version) { | |
541 | die(_("cannot write bundle version %d with algorithm %s"), version, the_hash_algo->name); | |
542 | } else if (version == 2) { | |
543 | write_or_die(bundle_fd, v2_bundle_signature, strlen(v2_bundle_signature)); | |
544 | } else { | |
545 | const char *capability = "@object-format="; | |
546 | write_or_die(bundle_fd, v3_bundle_signature, strlen(v3_bundle_signature)); | |
547 | write_or_die(bundle_fd, capability, strlen(capability)); | |
548 | write_or_die(bundle_fd, the_hash_algo->name, strlen(the_hash_algo->name)); | |
549 | write_or_die(bundle_fd, "\n", 1); | |
d9362ef9 | 550 | |
f18b512b DS |
551 | if (revs.filter.choice) { |
552 | const char *value = expand_list_objects_filter_spec(&revs.filter); | |
553 | capability = "@filter="; | |
554 | write_or_die(bundle_fd, capability, strlen(capability)); | |
555 | write_or_die(bundle_fd, value, strlen(value)); | |
556 | write_or_die(bundle_fd, "\n", 1); | |
557 | } | |
f5ff5fb5 | 558 | } |
d9362ef9 | 559 | |
5bb0fd2c JX |
560 | /* save revs.pending in revs_copy for later use */ |
561 | memcpy(&revs_copy, &revs, sizeof(revs)); | |
562 | revs_copy.pending.nr = 0; | |
563 | revs_copy.pending.alloc = 0; | |
564 | revs_copy.pending.objects = NULL; | |
565 | for (i = 0; i < revs.pending.nr; i++) { | |
566 | struct object_array_entry *e = revs.pending.objects + i; | |
567 | if (e) | |
568 | add_object_array_with_path(e->item, e->name, | |
569 | &revs_copy.pending, | |
570 | e->mode, e->path); | |
571 | } | |
d9362ef9 | 572 | |
5bb0fd2c JX |
573 | /* write prerequisites */ |
574 | revs.boundary = 1; | |
575 | if (prepare_revision_walk(&revs)) | |
576 | die("revision walk setup failed"); | |
577 | bpi.fd = bundle_fd; | |
578 | bpi.pending = &revs_copy.pending; | |
4f33a634 | 579 | |
f18b512b DS |
580 | /* |
581 | * Remove any object walking here. We only care about commits and | |
582 | * tags here. The revs_copy has the right instances of these values. | |
583 | */ | |
4f33a634 | 584 | revs.blob_objects = revs.tree_objects = 0; |
5bb0fd2c JX |
585 | traverse_commit_list(&revs, write_bundle_prerequisites, NULL, &bpi); |
586 | object_array_remove_duplicates(&revs_copy.pending); | |
587 | ||
588 | /* write bundle refs */ | |
589 | ref_count = write_bundle_refs(bundle_fd, &revs_copy); | |
d9362ef9 JK |
590 | if (!ref_count) |
591 | die(_("Refusing to create empty bundle.")); | |
592 | else if (ref_count < 0) | |
f5ff5fb5 | 593 | goto err; |
30415d50 JS |
594 | |
595 | /* write pack */ | |
5bb0fd2c | 596 | if (write_pack_data(bundle_fd, &revs_copy, pack_options)) |
f5ff5fb5 | 597 | goto err; |
4ed7cd3a | 598 | |
0f5cdf65 CH |
599 | if (!bundle_to_stdout) { |
600 | if (commit_lock_file(&lock)) | |
8a1e7eac | 601 | die_errno(_("cannot create '%s'"), path); |
0f5cdf65 | 602 | } |
c20181e3 | 603 | return 0; |
f5ff5fb5 | 604 | err: |
2c8ee1f5 | 605 | rollback_lock_file(&lock); |
f5ff5fb5 | 606 | return -1; |
30415d50 JS |
607 | } |
608 | ||
74ae4b63 | 609 | int unbundle(struct repository *r, struct bundle_header *header, |
89bd7fed DS |
610 | int bundle_fd, struct strvec *extra_index_pack_args, |
611 | enum verify_bundle_flags flags) | |
30415d50 | 612 | { |
d3180279 | 613 | struct child_process ip = CHILD_PROCESS_INIT; |
53537c6c ÆAB |
614 | |
615 | if (verify_bundle(r, header, flags)) | |
616 | return -1; | |
617 | ||
7366096d | 618 | strvec_pushl(&ip.args, "index-pack", "--fix-thin", "--stdin", NULL); |
30415d50 | 619 | |
4f39eb03 DS |
620 | /* If there is a filter, then we need to create the promisor pack. */ |
621 | if (header->filter.choice) | |
622 | strvec_push(&ip.args, "--promisor=from-bundle"); | |
623 | ||
7366096d ÆAB |
624 | if (extra_index_pack_args) { |
625 | strvec_pushv(&ip.args, extra_index_pack_args->v); | |
626 | strvec_clear(extra_index_pack_args); | |
627 | } | |
be042aff | 628 | |
30415d50 JS |
629 | ip.in = bundle_fd; |
630 | ip.no_stdout = 1; | |
631 | ip.git_cmd = 1; | |
632 | if (run_command(&ip)) | |
8a1e7eac | 633 | return error(_("index-pack died")); |
30415d50 JS |
634 | return 0; |
635 | } |