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