1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
6 #include "environment.h"
9 #include "object-store-ll.h"
10 #include "repository.h"
15 #include "list-objects.h"
16 #include "run-command.h"
19 #include "list-objects-filter-options.h"
20 #include "connected.h"
21 #include "write-or-die.h"
23 static const char v2_bundle_signature
[] = "# v2 git bundle\n";
24 static const char v3_bundle_signature
[] = "# v3 git bundle\n";
27 const char *signature
;
29 { 2, v2_bundle_signature
},
30 { 3, v3_bundle_signature
},
33 void bundle_header_init(struct bundle_header
*header
)
35 struct bundle_header blank
= BUNDLE_HEADER_INIT
;
36 memcpy(header
, &blank
, sizeof(*header
));
39 void bundle_header_release(struct bundle_header
*header
)
41 string_list_clear(&header
->prerequisites
, 1);
42 string_list_clear(&header
->references
, 1);
43 list_objects_filter_release(&header
->filter
);
46 static int parse_capability(struct bundle_header
*header
, const char *capability
)
49 if (skip_prefix(capability
, "object-format=", &arg
)) {
50 int algo
= hash_algo_by_name(arg
);
51 if (algo
== GIT_HASH_UNKNOWN
)
52 return error(_("unrecognized bundle hash algorithm: %s"), arg
);
53 header
->hash_algo
= &hash_algos
[algo
];
56 if (skip_prefix(capability
, "filter=", &arg
)) {
57 parse_list_objects_filter(&header
->filter
, arg
);
60 return error(_("unknown capability '%s'"), capability
);
63 static int parse_bundle_signature(struct bundle_header
*header
, const char *line
)
67 for (i
= 0; i
< ARRAY_SIZE(bundle_sigs
); i
++) {
68 if (!strcmp(line
, bundle_sigs
[i
].signature
)) {
69 header
->version
= bundle_sigs
[i
].version
;
76 int read_bundle_header_fd(int fd
, struct bundle_header
*header
,
77 const char *report_path
)
79 struct strbuf buf
= STRBUF_INIT
;
82 /* The bundle header begins with the signature */
83 if (strbuf_getwholeline_fd(&buf
, fd
, '\n') ||
84 parse_bundle_signature(header
, buf
.buf
)) {
86 error(_("'%s' does not look like a v2 or v3 bundle file"),
93 * The default hash format for bundles is SHA1, unless told otherwise
94 * by an "object-format=" capability, which is being handled in
95 * `parse_capability()`.
97 header
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
99 /* The bundle header ends with an empty line */
100 while (!strbuf_getwholeline_fd(&buf
, fd
, '\n') &&
101 buf
.len
&& buf
.buf
[0] != '\n') {
102 struct object_id oid
;
108 if (header
->version
== 3 && *buf
.buf
== '@') {
109 if (parse_capability(header
, buf
.buf
+ 1)) {
116 if (*buf
.buf
== '-') {
118 strbuf_remove(&buf
, 0, 1);
122 * Tip lines have object name, SP, and refname.
123 * Prerequisites have object name that is optionally
124 * followed by SP and subject line.
126 if (parse_oid_hex_algop(buf
.buf
, &oid
, &p
, header
->hash_algo
) ||
127 (*p
&& !isspace(*p
)) ||
128 (!is_prereq
&& !*p
)) {
130 error(_("unrecognized header: %s%s (%d)"),
131 (is_prereq
? "-" : ""), buf
.buf
, (int)buf
.len
);
135 struct object_id
*dup
= oiddup(&oid
);
137 string_list_append(&header
->prerequisites
, "")->util
= dup
;
139 string_list_append(&header
->references
, p
+ 1)->util
= dup
;
148 strbuf_release(&buf
);
152 int read_bundle_header(const char *path
, struct bundle_header
*header
)
154 int fd
= open(path
, O_RDONLY
);
157 return error(_("could not open '%s'"), path
);
158 return read_bundle_header_fd(fd
, header
, path
);
161 int is_bundle(const char *path
, int quiet
)
163 struct bundle_header header
= BUNDLE_HEADER_INIT
;
164 int fd
= open(path
, O_RDONLY
);
168 fd
= read_bundle_header_fd(fd
, &header
, quiet
? NULL
: path
);
171 bundle_header_release(&header
);
175 static int list_refs(struct string_list
*r
, int argc
, const char **argv
)
179 for (i
= 0; i
< r
->nr
; i
++) {
180 struct object_id
*oid
;
185 for (j
= 1; j
< argc
; j
++)
186 if (!strcmp(r
->items
[i
].string
, argv
[j
]))
192 oid
= r
->items
[i
].util
;
193 name
= r
->items
[i
].string
;
194 printf("%s %s\n", oid_to_hex(oid
), name
);
199 /* Remember to update object flag allocation in object.h */
200 #define PREREQ_MARK (1u<<16)
202 struct string_list_iterator
{
203 struct string_list
*list
;
207 static const struct object_id
*iterate_ref_map(void *cb_data
)
209 struct string_list_iterator
*iter
= cb_data
;
211 if (iter
->cur
>= iter
->list
->nr
)
214 return iter
->list
->items
[iter
->cur
++].util
;
217 int verify_bundle(struct repository
*r
,
218 struct bundle_header
*header
,
219 enum verify_bundle_flags flags
)
222 * Do fast check, then if any prereqs are missing then go line by line
223 * to be verbose about the errors
225 struct string_list
*p
= &header
->prerequisites
;
227 const char *message
= _("Repository lacks these prerequisite commits:");
228 struct string_list_iterator iter
= {
231 struct check_connected_options opts
= {
235 if (!r
|| !r
->objects
|| !r
->objects
->odb
)
236 return error(_("need a repository to verify a bundle"));
238 for (i
= 0; i
< p
->nr
; i
++) {
239 struct string_list_item
*e
= p
->items
+ i
;
240 const char *name
= e
->string
;
241 struct object_id
*oid
= e
->util
;
242 struct object
*o
= parse_object(r
, oid
);
246 if (flags
& VERIFY_BUNDLE_QUIET
)
249 error("%s", message
);
250 error("%s %s", oid_to_hex(oid
), name
);
255 if ((ret
= check_connected(iterate_ref_map
, &iter
, &opts
)))
256 error(_("some prerequisite commits exist in the object store, "
257 "but are not connected to the repository's history"));
259 /* TODO: preserve this verbose language. */
260 if (flags
& VERIFY_BUNDLE_VERBOSE
) {
261 struct string_list
*r
;
263 r
= &header
->references
;
264 printf_ln(Q_("The bundle contains this ref:",
265 "The bundle contains these %"PRIuMAX
" refs:",
268 list_refs(r
, 0, NULL
);
270 r
= &header
->prerequisites
;
272 printf_ln(_("The bundle records a complete history."));
274 printf_ln(Q_("The bundle requires this ref:",
275 "The bundle requires these %"PRIuMAX
" refs:",
278 list_refs(r
, 0, NULL
);
281 printf_ln(_("The bundle uses this hash algorithm: %s"),
282 header
->hash_algo
->name
);
283 if (header
->filter
.choice
)
284 printf_ln(_("The bundle uses this filter: %s"),
285 list_objects_filter_spec(&header
->filter
));
291 int list_bundle_refs(struct bundle_header
*header
, int argc
, const char **argv
)
293 return list_refs(&header
->references
, argc
, argv
);
296 static int is_tag_in_date_range(struct object
*tag
, struct rev_info
*revs
)
299 enum object_type type
;
300 char *buf
= NULL
, *line
, *lineend
;
304 if (revs
->max_age
== -1 && revs
->min_age
== -1)
307 buf
= repo_read_object_file(the_repository
, &tag
->oid
, &type
, &size
);
310 line
= memmem(buf
, size
, "\ntagger ", 8);
313 lineend
= memchr(line
, '\n', buf
+ size
- line
);
314 line
= memchr(line
, '>', lineend
? lineend
- line
: buf
+ size
- line
);
317 date
= parse_timestamp(line
, NULL
, 10);
318 result
= (revs
->max_age
== -1 || revs
->max_age
< date
) &&
319 (revs
->min_age
== -1 || revs
->min_age
> date
);
326 /* Write the pack data to bundle_fd */
327 static int write_pack_data(int bundle_fd
, struct rev_info
*revs
, struct strvec
*pack_options
)
329 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
332 strvec_pushl(&pack_objects
.args
,
334 "--stdout", "--thin", "--delta-base-offset",
336 strvec_pushv(&pack_objects
.args
, pack_options
->v
);
337 if (revs
->filter
.choice
)
338 strvec_pushf(&pack_objects
.args
, "--filter=%s",
339 list_objects_filter_spec(&revs
->filter
));
340 pack_objects
.in
= -1;
341 pack_objects
.out
= bundle_fd
;
342 pack_objects
.git_cmd
= 1;
345 * start_command() will close our descriptor if it's >1. Duplicate it
346 * to avoid surprising the caller.
348 if (pack_objects
.out
> 1) {
349 pack_objects
.out
= dup(pack_objects
.out
);
350 if (pack_objects
.out
< 0) {
351 error_errno(_("unable to dup bundle descriptor"));
352 child_process_clear(&pack_objects
);
357 if (start_command(&pack_objects
))
358 return error(_("Could not spawn pack-objects"));
360 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
361 struct object
*object
= revs
->pending
.objects
[i
].item
;
362 if (object
->flags
& UNINTERESTING
)
363 write_or_die(pack_objects
.in
, "^", 1);
364 write_or_die(pack_objects
.in
, oid_to_hex(&object
->oid
), the_hash_algo
->hexsz
);
365 write_or_die(pack_objects
.in
, "\n", 1);
367 close(pack_objects
.in
);
368 if (finish_command(&pack_objects
))
369 return error(_("pack-objects died"));
374 * Write out bundle refs based on the tips already
375 * parsed into revs.pending. As a side effect, may
376 * manipulate revs.pending to include additional
377 * necessary objects (like tags).
379 * Returns the number of refs written, or negative
382 static int write_bundle_refs(int bundle_fd
, struct rev_info
*revs
)
387 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
388 struct object_array_entry
*e
= revs
->pending
.objects
+ i
;
389 struct object_id oid
;
391 const char *display_ref
;
394 if (e
->item
->flags
& UNINTERESTING
)
396 if (repo_dwim_ref(the_repository
, e
->name
, strlen(e
->name
),
399 if (refs_read_ref_full(get_main_ref_store(the_repository
), e
->name
, RESOLVE_REF_READING
, &oid
, &flag
))
401 display_ref
= (flag
& REF_ISSYMREF
) ? e
->name
: ref
;
403 if (e
->item
->type
== OBJ_TAG
&&
404 !is_tag_in_date_range(e
->item
, revs
)) {
405 e
->item
->flags
|= UNINTERESTING
;
410 * Make sure the refs we wrote out is correct; --max-count and
411 * other limiting options could have prevented all the tips
412 * from getting output.
414 * Non commit objects such as tags and blobs do not have
415 * this issue as they are not affected by those extra
418 if (!(e
->item
->flags
& SHOWN
) && e
->item
->type
== OBJ_COMMIT
) {
419 warning(_("ref '%s' is excluded by the rev-list options"),
424 * If you run "git bundle create bndl v1.0..v2.0", the
425 * name of the positive ref is "v2.0" but that is the
426 * commit that is referenced by the tag, and not the tag
429 if (!oideq(&oid
, &e
->item
->oid
)) {
431 * Is this the positive end of a range expressed
432 * in terms of a tag (e.g. v2.0 from the range
435 struct commit
*one
= lookup_commit_reference(revs
->repo
, &oid
);
438 if (e
->item
== &(one
->object
)) {
440 * Need to include e->name as an
441 * independent ref to the pack-objects
442 * input, so that the tag is included
443 * in the output; otherwise we would
444 * end up triggering "empty bundle"
447 obj
= parse_object_or_die(&oid
, e
->name
);
449 add_pending_object(revs
, obj
, e
->name
);
455 write_or_die(bundle_fd
, oid_to_hex(&e
->item
->oid
), the_hash_algo
->hexsz
);
456 write_or_die(bundle_fd
, " ", 1);
457 write_or_die(bundle_fd
, display_ref
, strlen(display_ref
));
458 write_or_die(bundle_fd
, "\n", 1);
464 write_or_die(bundle_fd
, "\n", 1);
468 struct bundle_prerequisites_info
{
469 struct object_array
*pending
;
473 static void write_bundle_prerequisites(struct commit
*commit
, void *data
)
475 struct bundle_prerequisites_info
*bpi
= data
;
476 struct object
*object
;
477 struct pretty_print_context ctx
= { 0 };
478 struct strbuf buf
= STRBUF_INIT
;
480 if (!(commit
->object
.flags
& BOUNDARY
))
482 strbuf_addf(&buf
, "-%s ", oid_to_hex(&commit
->object
.oid
));
483 write_or_die(bpi
->fd
, buf
.buf
, buf
.len
);
485 ctx
.fmt
= CMIT_FMT_ONELINE
;
486 ctx
.output_encoding
= get_log_output_encoding();
488 pretty_print_commit(&ctx
, commit
, &buf
);
491 object
= (struct object
*)commit
;
492 object
->flags
|= UNINTERESTING
;
493 add_object_array_with_path(object
, buf
.buf
, bpi
->pending
, S_IFINVALID
,
495 strbuf_addch(&buf
, '\n');
496 write_or_die(bpi
->fd
, buf
.buf
, buf
.len
);
497 strbuf_release(&buf
);
500 int create_bundle(struct repository
*r
, const char *path
,
501 int argc
, const char **argv
, struct strvec
*pack_options
, int version
)
503 struct lock_file lock
= LOCK_INIT
;
505 int bundle_to_stdout
;
507 struct rev_info revs
, revs_copy
;
509 struct bundle_prerequisites_info bpi
;
513 /* init revs to list objects for pack-objects later */
514 save_commit_buffer
= 0;
515 repo_init_revisions(r
, &revs
, NULL
);
518 * Pre-initialize the '--objects' flag so we can parse a
519 * --filter option successfully.
521 revs
.tree_objects
= revs
.blob_objects
= 1;
523 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
526 * Reasons to require version 3:
528 * 1. @object-format is required because our hash algorithm is not
530 * 2. @filter is required because we parsed an object filter.
532 if (the_hash_algo
!= &hash_algos
[GIT_HASH_SHA1
] || revs
.filter
.choice
)
536 ret
= error(_("unrecognized argument: %s"), argv
[1]);
540 bundle_to_stdout
= !strcmp(path
, "-");
541 if (bundle_to_stdout
)
544 bundle_fd
= hold_lock_file_for_update(&lock
, path
,
548 version
= min_version
;
550 if (version
< 2 || version
> 3) {
551 die(_("unsupported bundle version %d"), version
);
552 } else if (version
< min_version
) {
553 die(_("cannot write bundle version %d with algorithm %s"), version
, the_hash_algo
->name
);
554 } else if (version
== 2) {
555 write_or_die(bundle_fd
, v2_bundle_signature
, strlen(v2_bundle_signature
));
557 const char *capability
= "@object-format=";
558 write_or_die(bundle_fd
, v3_bundle_signature
, strlen(v3_bundle_signature
));
559 write_or_die(bundle_fd
, capability
, strlen(capability
));
560 write_or_die(bundle_fd
, the_hash_algo
->name
, strlen(the_hash_algo
->name
));
561 write_or_die(bundle_fd
, "\n", 1);
563 if (revs
.filter
.choice
) {
564 const char *value
= expand_list_objects_filter_spec(&revs
.filter
);
565 capability
= "@filter=";
566 write_or_die(bundle_fd
, capability
, strlen(capability
));
567 write_or_die(bundle_fd
, value
, strlen(value
));
568 write_or_die(bundle_fd
, "\n", 1);
572 /* save revs.pending in revs_copy for later use */
573 memcpy(&revs_copy
, &revs
, sizeof(revs
));
574 revs_copy
.pending
.nr
= 0;
575 revs_copy
.pending
.alloc
= 0;
576 revs_copy
.pending
.objects
= NULL
;
577 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
578 struct object_array_entry
*e
= revs
.pending
.objects
+ i
;
580 add_object_array_with_path(e
->item
, e
->name
,
585 /* write prerequisites */
587 if (prepare_revision_walk(&revs
))
588 die("revision walk setup failed");
590 bpi
.pending
= &revs_copy
.pending
;
593 * Remove any object walking here. We only care about commits and
594 * tags here. The revs_copy has the right instances of these values.
596 revs
.blob_objects
= revs
.tree_objects
= 0;
597 traverse_commit_list(&revs
, write_bundle_prerequisites
, NULL
, &bpi
);
598 object_array_remove_duplicates(&revs_copy
.pending
);
600 /* write bundle refs */
601 ref_count
= write_bundle_refs(bundle_fd
, &revs_copy
);
603 die(_("Refusing to create empty bundle."));
604 } else if (ref_count
< 0) {
610 if (write_pack_data(bundle_fd
, &revs_copy
, pack_options
)) {
615 if (!bundle_to_stdout
) {
616 if (commit_lock_file(&lock
))
617 die_errno(_("cannot create '%s'"), path
);
623 object_array_clear(&revs_copy
.pending
);
624 release_revisions(&revs
);
625 rollback_lock_file(&lock
);
629 int unbundle(struct repository
*r
, struct bundle_header
*header
,
630 int bundle_fd
, struct strvec
*extra_index_pack_args
,
631 enum verify_bundle_flags flags
)
633 struct child_process ip
= CHILD_PROCESS_INIT
;
635 if (verify_bundle(r
, header
, flags
))
638 strvec_pushl(&ip
.args
, "index-pack", "--fix-thin", "--stdin", NULL
);
640 /* If there is a filter, then we need to create the promisor pack. */
641 if (header
->filter
.choice
)
642 strvec_push(&ip
.args
, "--promisor=from-bundle");
644 if (flags
& VERIFY_BUNDLE_FSCK
)
645 strvec_push(&ip
.args
, "--fsck-objects");
647 if (extra_index_pack_args
)
648 strvec_pushv(&ip
.args
, extra_index_pack_args
->v
);
653 if (run_command(&ip
))
654 return error(_("index-pack died"));