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