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