6 #include "object-store.h"
7 #include "repository.h"
12 #include "list-objects.h"
13 #include "run-command.h"
16 #include "list-objects-filter-options.h"
17 #include "connected.h"
19 static const char v2_bundle_signature
[] = "# v2 git bundle\n";
20 static const char v3_bundle_signature
[] = "# v3 git bundle\n";
23 const char *signature
;
25 { 2, v2_bundle_signature
},
26 { 3, v3_bundle_signature
},
29 void bundle_header_init(struct bundle_header
*header
)
31 struct bundle_header blank
= BUNDLE_HEADER_INIT
;
32 memcpy(header
, &blank
, sizeof(*header
));
35 void bundle_header_release(struct bundle_header
*header
)
37 string_list_clear(&header
->prerequisites
, 1);
38 string_list_clear(&header
->references
, 1);
39 list_objects_filter_release(&header
->filter
);
42 static int parse_capability(struct bundle_header
*header
, const char *capability
)
45 if (skip_prefix(capability
, "object-format=", &arg
)) {
46 int algo
= hash_algo_by_name(arg
);
47 if (algo
== GIT_HASH_UNKNOWN
)
48 return error(_("unrecognized bundle hash algorithm: %s"), arg
);
49 header
->hash_algo
= &hash_algos
[algo
];
52 if (skip_prefix(capability
, "filter=", &arg
)) {
53 parse_list_objects_filter(&header
->filter
, arg
);
56 return error(_("unknown capability '%s'"), capability
);
59 static int parse_bundle_signature(struct bundle_header
*header
, const char *line
)
63 for (i
= 0; i
< ARRAY_SIZE(bundle_sigs
); i
++) {
64 if (!strcmp(line
, bundle_sigs
[i
].signature
)) {
65 header
->version
= bundle_sigs
[i
].version
;
72 int read_bundle_header_fd(int fd
, struct bundle_header
*header
,
73 const char *report_path
)
75 struct strbuf buf
= STRBUF_INIT
;
78 /* The bundle header begins with the signature */
79 if (strbuf_getwholeline_fd(&buf
, fd
, '\n') ||
80 parse_bundle_signature(header
, buf
.buf
)) {
82 error(_("'%s' does not look like a v2 or v3 bundle file"),
88 header
->hash_algo
= the_hash_algo
;
90 /* The bundle header ends with an empty line */
91 while (!strbuf_getwholeline_fd(&buf
, fd
, '\n') &&
92 buf
.len
&& buf
.buf
[0] != '\n') {
99 if (header
->version
== 3 && *buf
.buf
== '@') {
100 if (parse_capability(header
, buf
.buf
+ 1)) {
107 if (*buf
.buf
== '-') {
109 strbuf_remove(&buf
, 0, 1);
113 * Tip lines have object name, SP, and refname.
114 * Prerequisites have object name that is optionally
115 * followed by SP and subject line.
117 if (parse_oid_hex_algop(buf
.buf
, &oid
, &p
, header
->hash_algo
) ||
118 (*p
&& !isspace(*p
)) ||
119 (!is_prereq
&& !*p
)) {
121 error(_("unrecognized header: %s%s (%d)"),
122 (is_prereq
? "-" : ""), buf
.buf
, (int)buf
.len
);
126 struct object_id
*dup
= oiddup(&oid
);
128 string_list_append(&header
->prerequisites
, "")->util
= dup
;
130 string_list_append(&header
->references
, p
+ 1)->util
= dup
;
139 strbuf_release(&buf
);
143 int read_bundle_header(const char *path
, struct bundle_header
*header
)
145 int fd
= open(path
, O_RDONLY
);
148 return error(_("could not open '%s'"), path
);
149 return read_bundle_header_fd(fd
, header
, path
);
152 int is_bundle(const char *path
, int quiet
)
154 struct bundle_header header
= BUNDLE_HEADER_INIT
;
155 int fd
= open(path
, O_RDONLY
);
159 fd
= read_bundle_header_fd(fd
, &header
, quiet
? NULL
: path
);
162 bundle_header_release(&header
);
166 static int list_refs(struct string_list
*r
, int argc
, const char **argv
)
170 for (i
= 0; i
< r
->nr
; i
++) {
171 struct object_id
*oid
;
176 for (j
= 1; j
< argc
; j
++)
177 if (!strcmp(r
->items
[i
].string
, argv
[j
]))
183 oid
= r
->items
[i
].util
;
184 name
= r
->items
[i
].string
;
185 printf("%s %s\n", oid_to_hex(oid
), name
);
190 /* Remember to update object flag allocation in object.h */
191 #define PREREQ_MARK (1u<<16)
193 struct string_list_iterator
{
194 struct string_list
*list
;
198 static const struct object_id
*iterate_ref_map(void *cb_data
)
200 struct string_list_iterator
*iter
= cb_data
;
202 if (iter
->cur
>= iter
->list
->nr
)
205 return iter
->list
->items
[iter
->cur
++].util
;
208 int verify_bundle(struct repository
*r
,
209 struct bundle_header
*header
,
210 enum verify_bundle_flags flags
)
213 * Do fast check, then if any prereqs are missing then go line by line
214 * to be verbose about the errors
216 struct string_list
*p
= &header
->prerequisites
;
218 const char *message
= _("Repository lacks these prerequisite commits:");
219 struct string_list_iterator iter
= {
222 struct check_connected_options opts
= {
226 if (!r
|| !r
->objects
|| !r
->objects
->odb
)
227 return error(_("need a repository to verify a bundle"));
229 for (i
= 0; i
< p
->nr
; i
++) {
230 struct string_list_item
*e
= p
->items
+ i
;
231 const char *name
= e
->string
;
232 struct object_id
*oid
= e
->util
;
233 struct object
*o
= parse_object(r
, oid
);
237 if (flags
& VERIFY_BUNDLE_QUIET
)
240 error("%s", message
);
241 error("%s %s", oid_to_hex(oid
), name
);
246 if ((ret
= check_connected(iterate_ref_map
, &iter
, &opts
)))
247 error(_("some prerequisite commits exist in the object store, "
248 "but are not connected to the repository's history"));
250 /* TODO: preserve this verbose language. */
251 if (flags
& VERIFY_BUNDLE_VERBOSE
) {
252 struct string_list
*r
;
254 r
= &header
->references
;
255 printf_ln(Q_("The bundle contains this ref:",
256 "The bundle contains these %"PRIuMAX
" refs:",
259 list_refs(r
, 0, NULL
);
261 r
= &header
->prerequisites
;
263 printf_ln(_("The bundle records a complete history."));
265 printf_ln(Q_("The bundle requires this ref:",
266 "The bundle requires these %"PRIuMAX
" refs:",
269 list_refs(r
, 0, NULL
);
272 printf_ln("The bundle uses this hash algorithm: %s",
273 header
->hash_algo
->name
);
274 if (header
->filter
.choice
)
275 printf_ln("The bundle uses this filter: %s",
276 list_objects_filter_spec(&header
->filter
));
282 int list_bundle_refs(struct bundle_header
*header
, int argc
, const char **argv
)
284 return list_refs(&header
->references
, argc
, argv
);
287 static int is_tag_in_date_range(struct object
*tag
, struct rev_info
*revs
)
290 enum object_type type
;
291 char *buf
= NULL
, *line
, *lineend
;
295 if (revs
->max_age
== -1 && revs
->min_age
== -1)
298 buf
= read_object_file(&tag
->oid
, &type
, &size
);
301 line
= memmem(buf
, size
, "\ntagger ", 8);
304 lineend
= memchr(line
, '\n', buf
+ size
- line
);
305 line
= memchr(line
, '>', lineend
? lineend
- line
: buf
+ size
- line
);
308 date
= parse_timestamp(line
, NULL
, 10);
309 result
= (revs
->max_age
== -1 || revs
->max_age
< date
) &&
310 (revs
->min_age
== -1 || revs
->min_age
> date
);
317 /* Write the pack data to bundle_fd */
318 static int write_pack_data(int bundle_fd
, struct rev_info
*revs
, struct strvec
*pack_options
)
320 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
323 strvec_pushl(&pack_objects
.args
,
325 "--stdout", "--thin", "--delta-base-offset",
327 strvec_pushv(&pack_objects
.args
, pack_options
->v
);
328 if (revs
->filter
.choice
)
329 strvec_pushf(&pack_objects
.args
, "--filter=%s",
330 list_objects_filter_spec(&revs
->filter
));
331 pack_objects
.in
= -1;
332 pack_objects
.out
= bundle_fd
;
333 pack_objects
.git_cmd
= 1;
336 * start_command() will close our descriptor if it's >1. Duplicate it
337 * to avoid surprising the caller.
339 if (pack_objects
.out
> 1) {
340 pack_objects
.out
= dup(pack_objects
.out
);
341 if (pack_objects
.out
< 0) {
342 error_errno(_("unable to dup bundle descriptor"));
343 child_process_clear(&pack_objects
);
348 if (start_command(&pack_objects
))
349 return error(_("Could not spawn pack-objects"));
351 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
352 struct object
*object
= revs
->pending
.objects
[i
].item
;
353 if (object
->flags
& UNINTERESTING
)
354 write_or_die(pack_objects
.in
, "^", 1);
355 write_or_die(pack_objects
.in
, oid_to_hex(&object
->oid
), the_hash_algo
->hexsz
);
356 write_or_die(pack_objects
.in
, "\n", 1);
358 close(pack_objects
.in
);
359 if (finish_command(&pack_objects
))
360 return error(_("pack-objects died"));
365 * Write out bundle refs based on the tips already
366 * parsed into revs.pending. As a side effect, may
367 * manipulate revs.pending to include additional
368 * necessary objects (like tags).
370 * Returns the number of refs written, or negative
373 static int write_bundle_refs(int bundle_fd
, struct rev_info
*revs
)
378 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
379 struct object_array_entry
*e
= revs
->pending
.objects
+ i
;
380 struct object_id oid
;
382 const char *display_ref
;
385 if (e
->item
->flags
& UNINTERESTING
)
387 if (dwim_ref(e
->name
, strlen(e
->name
), &oid
, &ref
, 0) != 1)
389 if (read_ref_full(e
->name
, RESOLVE_REF_READING
, &oid
, &flag
))
391 display_ref
= (flag
& REF_ISSYMREF
) ? e
->name
: ref
;
393 if (e
->item
->type
== OBJ_TAG
&&
394 !is_tag_in_date_range(e
->item
, revs
)) {
395 e
->item
->flags
|= UNINTERESTING
;
400 * Make sure the refs we wrote out is correct; --max-count and
401 * other limiting options could have prevented all the tips
402 * from getting output.
404 * Non commit objects such as tags and blobs do not have
405 * this issue as they are not affected by those extra
408 if (!(e
->item
->flags
& SHOWN
) && e
->item
->type
== OBJ_COMMIT
) {
409 warning(_("ref '%s' is excluded by the rev-list options"),
414 * If you run "git bundle create bndl v1.0..v2.0", the
415 * name of the positive ref is "v2.0" but that is the
416 * commit that is referenced by the tag, and not the tag
419 if (!oideq(&oid
, &e
->item
->oid
)) {
421 * Is this the positive end of a range expressed
422 * in terms of a tag (e.g. v2.0 from the range
425 struct commit
*one
= lookup_commit_reference(revs
->repo
, &oid
);
428 if (e
->item
== &(one
->object
)) {
430 * Need to include e->name as an
431 * independent ref to the pack-objects
432 * input, so that the tag is included
433 * in the output; otherwise we would
434 * end up triggering "empty bundle"
437 obj
= parse_object_or_die(&oid
, e
->name
);
439 add_pending_object(revs
, obj
, e
->name
);
445 write_or_die(bundle_fd
, oid_to_hex(&e
->item
->oid
), the_hash_algo
->hexsz
);
446 write_or_die(bundle_fd
, " ", 1);
447 write_or_die(bundle_fd
, display_ref
, strlen(display_ref
));
448 write_or_die(bundle_fd
, "\n", 1);
454 write_or_die(bundle_fd
, "\n", 1);
458 struct bundle_prerequisites_info
{
459 struct object_array
*pending
;
463 static void write_bundle_prerequisites(struct commit
*commit
, void *data
)
465 struct bundle_prerequisites_info
*bpi
= data
;
466 struct object
*object
;
467 struct pretty_print_context ctx
= { 0 };
468 struct strbuf buf
= STRBUF_INIT
;
470 if (!(commit
->object
.flags
& BOUNDARY
))
472 strbuf_addf(&buf
, "-%s ", oid_to_hex(&commit
->object
.oid
));
473 write_or_die(bpi
->fd
, buf
.buf
, buf
.len
);
475 ctx
.fmt
= CMIT_FMT_ONELINE
;
476 ctx
.output_encoding
= get_log_output_encoding();
478 pretty_print_commit(&ctx
, commit
, &buf
);
481 object
= (struct object
*)commit
;
482 object
->flags
|= UNINTERESTING
;
483 add_object_array_with_path(object
, buf
.buf
, bpi
->pending
, S_IFINVALID
,
485 strbuf_addch(&buf
, '\n');
486 write_or_die(bpi
->fd
, buf
.buf
, buf
.len
);
487 strbuf_release(&buf
);
490 int create_bundle(struct repository
*r
, const char *path
,
491 int argc
, const char **argv
, struct strvec
*pack_options
, int version
)
493 struct lock_file lock
= LOCK_INIT
;
495 int bundle_to_stdout
;
497 struct rev_info revs
, revs_copy
;
499 struct bundle_prerequisites_info bpi
;
502 /* init revs to list objects for pack-objects later */
503 save_commit_buffer
= 0;
504 repo_init_revisions(r
, &revs
, NULL
);
507 * Pre-initialize the '--objects' flag so we can parse a
508 * --filter option successfully.
510 revs
.tree_objects
= revs
.blob_objects
= 1;
512 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
515 * Reasons to require version 3:
517 * 1. @object-format is required because our hash algorithm is not
519 * 2. @filter is required because we parsed an object filter.
521 if (the_hash_algo
!= &hash_algos
[GIT_HASH_SHA1
] || revs
.filter
.choice
)
525 error(_("unrecognized argument: %s"), argv
[1]);
529 bundle_to_stdout
= !strcmp(path
, "-");
530 if (bundle_to_stdout
)
533 bundle_fd
= hold_lock_file_for_update(&lock
, path
,
537 version
= min_version
;
539 if (version
< 2 || version
> 3) {
540 die(_("unsupported bundle version %d"), version
);
541 } else if (version
< min_version
) {
542 die(_("cannot write bundle version %d with algorithm %s"), version
, the_hash_algo
->name
);
543 } else if (version
== 2) {
544 write_or_die(bundle_fd
, v2_bundle_signature
, strlen(v2_bundle_signature
));
546 const char *capability
= "@object-format=";
547 write_or_die(bundle_fd
, v3_bundle_signature
, strlen(v3_bundle_signature
));
548 write_or_die(bundle_fd
, capability
, strlen(capability
));
549 write_or_die(bundle_fd
, the_hash_algo
->name
, strlen(the_hash_algo
->name
));
550 write_or_die(bundle_fd
, "\n", 1);
552 if (revs
.filter
.choice
) {
553 const char *value
= expand_list_objects_filter_spec(&revs
.filter
);
554 capability
= "@filter=";
555 write_or_die(bundle_fd
, capability
, strlen(capability
));
556 write_or_die(bundle_fd
, value
, strlen(value
));
557 write_or_die(bundle_fd
, "\n", 1);
561 /* save revs.pending in revs_copy for later use */
562 memcpy(&revs_copy
, &revs
, sizeof(revs
));
563 revs_copy
.pending
.nr
= 0;
564 revs_copy
.pending
.alloc
= 0;
565 revs_copy
.pending
.objects
= NULL
;
566 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
567 struct object_array_entry
*e
= revs
.pending
.objects
+ i
;
569 add_object_array_with_path(e
->item
, e
->name
,
574 /* write prerequisites */
576 if (prepare_revision_walk(&revs
))
577 die("revision walk setup failed");
579 bpi
.pending
= &revs_copy
.pending
;
582 * Remove any object walking here. We only care about commits and
583 * tags here. The revs_copy has the right instances of these values.
585 revs
.blob_objects
= revs
.tree_objects
= 0;
586 traverse_commit_list(&revs
, write_bundle_prerequisites
, NULL
, &bpi
);
587 object_array_remove_duplicates(&revs_copy
.pending
);
589 /* write bundle refs */
590 ref_count
= write_bundle_refs(bundle_fd
, &revs_copy
);
592 die(_("Refusing to create empty bundle."));
593 else if (ref_count
< 0)
597 if (write_pack_data(bundle_fd
, &revs_copy
, pack_options
))
600 if (!bundle_to_stdout
) {
601 if (commit_lock_file(&lock
))
602 die_errno(_("cannot create '%s'"), path
);
606 rollback_lock_file(&lock
);
610 int unbundle(struct repository
*r
, struct bundle_header
*header
,
611 int bundle_fd
, struct strvec
*extra_index_pack_args
,
612 enum verify_bundle_flags flags
)
614 struct child_process ip
= CHILD_PROCESS_INIT
;
616 if (verify_bundle(r
, header
, flags
))
619 strvec_pushl(&ip
.args
, "index-pack", "--fix-thin", "--stdin", NULL
);
621 /* If there is a filter, then we need to create the promisor pack. */
622 if (header
->filter
.choice
)
623 strvec_push(&ip
.args
, "--promisor=from-bundle");
625 if (extra_index_pack_args
) {
626 strvec_pushv(&ip
.args
, extra_index_pack_args
->v
);
627 strvec_clear(extra_index_pack_args
);
633 if (run_command(&ip
))
634 return error(_("index-pack died"));