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