1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
7 #include "environment.h"
10 #include "object-store.h"
11 #include "repository.h"
16 #include "list-objects.h"
17 #include "run-command.h"
20 #include "list-objects-filter-options.h"
21 #include "connected.h"
22 #include "write-or-die.h"
24 static const char v2_bundle_signature
[] = "# v2 git bundle\n";
25 static const char v3_bundle_signature
[] = "# v3 git bundle\n";
28 const char *signature
;
30 { 2, v2_bundle_signature
},
31 { 3, v3_bundle_signature
},
34 void bundle_header_init(struct bundle_header
*header
)
36 struct bundle_header blank
= BUNDLE_HEADER_INIT
;
37 memcpy(header
, &blank
, sizeof(*header
));
40 void bundle_header_release(struct bundle_header
*header
)
42 string_list_clear(&header
->prerequisites
, 1);
43 string_list_clear(&header
->references
, 1);
44 list_objects_filter_release(&header
->filter
);
47 static int parse_capability(struct bundle_header
*header
, const char *capability
)
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
];
57 if (skip_prefix(capability
, "filter=", &arg
)) {
58 parse_list_objects_filter(&header
->filter
, arg
);
61 return error(_("unknown capability '%s'"), capability
);
64 static int parse_bundle_signature(struct bundle_header
*header
, const char *line
)
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
;
77 int read_bundle_header_fd(int fd
, struct bundle_header
*header
,
78 const char *report_path
)
80 struct strbuf buf
= STRBUF_INIT
;
83 /* The bundle header begins with the signature */
84 if (strbuf_getwholeline_fd(&buf
, fd
, '\n') ||
85 parse_bundle_signature(header
, buf
.buf
)) {
87 error(_("'%s' does not look like a v2 or v3 bundle file"),
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()`.
98 header
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
100 /* The bundle header ends with an empty line */
101 while (!strbuf_getwholeline_fd(&buf
, fd
, '\n') &&
102 buf
.len
&& buf
.buf
[0] != '\n') {
103 struct object_id oid
;
109 if (header
->version
== 3 && *buf
.buf
== '@') {
110 if (parse_capability(header
, buf
.buf
+ 1)) {
117 if (*buf
.buf
== '-') {
119 strbuf_remove(&buf
, 0, 1);
123 * Tip lines have object name, SP, and refname.
124 * Prerequisites have object name that is optionally
125 * followed by SP and subject line.
127 if (parse_oid_hex_algop(buf
.buf
, &oid
, &p
, header
->hash_algo
) ||
128 (*p
&& !isspace(*p
)) ||
129 (!is_prereq
&& !*p
)) {
131 error(_("unrecognized header: %s%s (%d)"),
132 (is_prereq
? "-" : ""), buf
.buf
, (int)buf
.len
);
136 struct object_id
*dup
= oiddup(&oid
);
138 string_list_append(&header
->prerequisites
, "")->util
= dup
;
140 string_list_append(&header
->references
, p
+ 1)->util
= dup
;
149 strbuf_release(&buf
);
153 int read_bundle_header(const char *path
, struct bundle_header
*header
)
155 int fd
= open(path
, O_RDONLY
);
158 return error(_("could not open '%s'"), path
);
159 return read_bundle_header_fd(fd
, header
, path
);
162 int is_bundle(const char *path
, int quiet
)
164 struct bundle_header header
= BUNDLE_HEADER_INIT
;
165 int fd
= open(path
, O_RDONLY
);
169 fd
= read_bundle_header_fd(fd
, &header
, quiet
? NULL
: path
);
172 bundle_header_release(&header
);
176 static int list_refs(struct string_list
*r
, int argc
, const char **argv
)
180 for (i
= 0; i
< r
->nr
; i
++) {
181 struct object_id
*oid
;
186 for (j
= 1; j
< argc
; j
++)
187 if (!strcmp(r
->items
[i
].string
, argv
[j
]))
193 oid
= r
->items
[i
].util
;
194 name
= r
->items
[i
].string
;
195 printf("%s %s\n", oid_to_hex(oid
), name
);
200 /* Remember to update object flag allocation in object.h */
201 #define PREREQ_MARK (1u<<16)
203 struct string_list_iterator
{
204 struct string_list
*list
;
208 static const struct object_id
*iterate_ref_map(void *cb_data
)
210 struct string_list_iterator
*iter
= cb_data
;
212 if (iter
->cur
>= iter
->list
->nr
)
215 return iter
->list
->items
[iter
->cur
++].util
;
218 int verify_bundle(struct repository
*r
,
219 struct bundle_header
*header
,
220 enum verify_bundle_flags flags
)
223 * Do fast check, then if any prereqs are missing then go line by line
224 * to be verbose about the errors
226 struct string_list
*p
= &header
->prerequisites
;
228 const char *message
= _("Repository lacks these prerequisite commits:");
229 struct string_list_iterator iter
= {
232 struct check_connected_options opts
= {
236 if (!r
|| !r
->objects
|| !r
->objects
->odb
)
237 return error(_("need a repository to verify a bundle"));
239 for (i
= 0; i
< p
->nr
; i
++) {
240 struct string_list_item
*e
= p
->items
+ i
;
241 const char *name
= e
->string
;
242 struct object_id
*oid
= e
->util
;
243 struct object
*o
= parse_object(r
, oid
);
247 if (flags
& VERIFY_BUNDLE_QUIET
)
250 error("%s", message
);
251 error("%s %s", oid_to_hex(oid
), name
);
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"));
260 /* TODO: preserve this verbose language. */
261 if (flags
& VERIFY_BUNDLE_VERBOSE
) {
262 struct string_list
*r
;
264 r
= &header
->references
;
265 printf_ln(Q_("The bundle contains this ref:",
266 "The bundle contains these %"PRIuMAX
" refs:",
269 list_refs(r
, 0, NULL
);
271 r
= &header
->prerequisites
;
273 printf_ln(_("The bundle records a complete history."));
275 printf_ln(Q_("The bundle requires this ref:",
276 "The bundle requires these %"PRIuMAX
" refs:",
279 list_refs(r
, 0, NULL
);
282 printf_ln(_("The bundle uses this hash algorithm: %s"),
283 header
->hash_algo
->name
);
284 if (header
->filter
.choice
)
285 printf_ln(_("The bundle uses this filter: %s"),
286 list_objects_filter_spec(&header
->filter
));
292 int list_bundle_refs(struct bundle_header
*header
, int argc
, const char **argv
)
294 return list_refs(&header
->references
, argc
, argv
);
297 static int is_tag_in_date_range(struct object
*tag
, struct rev_info
*revs
)
300 enum object_type type
;
301 char *buf
= NULL
, *line
, *lineend
;
305 if (revs
->max_age
== -1 && revs
->min_age
== -1)
308 buf
= repo_read_object_file(the_repository
, &tag
->oid
, &type
, &size
);
311 line
= memmem(buf
, size
, "\ntagger ", 8);
314 lineend
= memchr(line
, '\n', buf
+ size
- line
);
315 line
= memchr(line
, '>', lineend
? lineend
- line
: buf
+ size
- line
);
318 date
= parse_timestamp(line
, NULL
, 10);
319 result
= (revs
->max_age
== -1 || revs
->max_age
< date
) &&
320 (revs
->min_age
== -1 || revs
->min_age
> date
);
327 /* Write the pack data to bundle_fd */
328 static int write_pack_data(int bundle_fd
, struct rev_info
*revs
, struct strvec
*pack_options
)
330 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
333 strvec_pushl(&pack_objects
.args
,
335 "--stdout", "--thin", "--delta-base-offset",
337 strvec_pushv(&pack_objects
.args
, pack_options
->v
);
338 if (revs
->filter
.choice
)
339 strvec_pushf(&pack_objects
.args
, "--filter=%s",
340 list_objects_filter_spec(&revs
->filter
));
341 pack_objects
.in
= -1;
342 pack_objects
.out
= bundle_fd
;
343 pack_objects
.git_cmd
= 1;
346 * start_command() will close our descriptor if it's >1. Duplicate it
347 * to avoid surprising the caller.
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
);
358 if (start_command(&pack_objects
))
359 return error(_("Could not spawn pack-objects"));
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);
365 write_or_die(pack_objects
.in
, oid_to_hex(&object
->oid
), the_hash_algo
->hexsz
);
366 write_or_die(pack_objects
.in
, "\n", 1);
368 close(pack_objects
.in
);
369 if (finish_command(&pack_objects
))
370 return error(_("pack-objects died"));
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).
380 * Returns the number of refs written, or negative
383 static int write_bundle_refs(int bundle_fd
, struct rev_info
*revs
)
387 struct strset objects
= STRSET_INIT
;
389 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
390 struct object_array_entry
*e
= revs
->pending
.objects
+ i
;
391 struct object_id oid
;
393 const char *display_ref
;
396 if (e
->item
->flags
& UNINTERESTING
)
398 if (repo_dwim_ref(the_repository
, e
->name
, strlen(e
->name
),
401 if (refs_read_ref_full(get_main_ref_store(the_repository
), e
->name
, RESOLVE_REF_READING
, &oid
, &flag
))
403 display_ref
= (flag
& REF_ISSYMREF
) ? e
->name
: ref
;
405 if (strset_contains(&objects
, display_ref
))
408 if (e
->item
->type
== OBJ_TAG
&&
409 !is_tag_in_date_range(e
->item
, revs
)) {
410 e
->item
->flags
|= UNINTERESTING
;
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.
419 * Non commit objects such as tags and blobs do not have
420 * this issue as they are not affected by those extra
423 if (!(e
->item
->flags
& SHOWN
) && e
->item
->type
== OBJ_COMMIT
) {
424 warning(_("ref '%s' is excluded by the rev-list options"),
430 strset_add(&objects
, display_ref
);
431 write_or_die(bundle_fd
, oid_to_hex(&e
->item
->oid
), the_hash_algo
->hexsz
);
432 write_or_die(bundle_fd
, " ", 1);
433 write_or_die(bundle_fd
, display_ref
, strlen(display_ref
));
434 write_or_die(bundle_fd
, "\n", 1);
439 strset_clear(&objects
);
442 write_or_die(bundle_fd
, "\n", 1);
446 struct bundle_prerequisites_info
{
447 struct object_array
*pending
;
451 static void write_bundle_prerequisites(struct commit
*commit
, void *data
)
453 struct bundle_prerequisites_info
*bpi
= data
;
454 struct object
*object
;
455 struct pretty_print_context ctx
= { 0 };
456 struct strbuf buf
= STRBUF_INIT
;
458 if (!(commit
->object
.flags
& BOUNDARY
))
460 strbuf_addf(&buf
, "-%s ", oid_to_hex(&commit
->object
.oid
));
461 write_or_die(bpi
->fd
, buf
.buf
, buf
.len
);
463 ctx
.fmt
= CMIT_FMT_ONELINE
;
464 ctx
.output_encoding
= get_log_output_encoding();
466 pretty_print_commit(&ctx
, commit
, &buf
);
469 object
= (struct object
*)commit
;
470 object
->flags
|= UNINTERESTING
;
471 add_object_array_with_path(object
, buf
.buf
, bpi
->pending
, S_IFINVALID
,
473 strbuf_addch(&buf
, '\n');
474 write_or_die(bpi
->fd
, buf
.buf
, buf
.len
);
475 strbuf_release(&buf
);
478 int create_bundle(struct repository
*r
, const char *path
,
479 int argc
, const char **argv
, struct strvec
*pack_options
, int version
)
481 struct lock_file lock
= LOCK_INIT
;
483 int bundle_to_stdout
;
485 struct rev_info revs
, revs_copy
;
487 struct bundle_prerequisites_info bpi
;
491 /* init revs to list objects for pack-objects later */
492 save_commit_buffer
= 0;
493 repo_init_revisions(r
, &revs
, NULL
);
496 * Pre-initialize the '--objects' flag so we can parse a
497 * --filter option successfully.
499 revs
.tree_objects
= revs
.blob_objects
= 1;
501 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
504 * Reasons to require version 3:
506 * 1. @object-format is required because our hash algorithm is not
508 * 2. @filter is required because we parsed an object filter.
510 if (the_hash_algo
!= &hash_algos
[GIT_HASH_SHA1
] || revs
.filter
.choice
)
514 ret
= error(_("unrecognized argument: %s"), argv
[1]);
518 bundle_to_stdout
= !strcmp(path
, "-");
519 if (bundle_to_stdout
)
522 bundle_fd
= hold_lock_file_for_update(&lock
, path
,
526 version
= min_version
;
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
));
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);
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);
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
;
558 add_object_array_with_path(e
->item
, e
->name
,
563 /* write prerequisites */
565 if (prepare_revision_walk(&revs
))
566 die("revision walk setup failed");
568 bpi
.pending
= &revs_copy
.pending
;
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.
574 revs
.blob_objects
= revs
.tree_objects
= 0;
575 traverse_commit_list(&revs
, write_bundle_prerequisites
, NULL
, &bpi
);
577 /* write bundle refs */
578 ref_count
= write_bundle_refs(bundle_fd
, &revs_copy
);
580 die(_("Refusing to create empty bundle."));
581 } else if (ref_count
< 0) {
587 if (write_pack_data(bundle_fd
, &revs_copy
, pack_options
)) {
592 if (!bundle_to_stdout
) {
593 if (commit_lock_file(&lock
))
594 die_errno(_("cannot create '%s'"), path
);
600 object_array_clear(&revs_copy
.pending
);
601 release_revisions(&revs
);
602 rollback_lock_file(&lock
);
606 int unbundle(struct repository
*r
, struct bundle_header
*header
,
607 int bundle_fd
, struct strvec
*extra_index_pack_args
,
608 struct unbundle_opts
*opts
)
610 struct child_process ip
= CHILD_PROCESS_INIT
;
611 struct unbundle_opts opts_fallback
= { 0 };
614 opts
= &opts_fallback
;
616 if (verify_bundle(r
, header
, opts
->flags
)) {
621 strvec_pushl(&ip
.args
, "index-pack", "--fix-thin", "--stdin", NULL
);
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");
627 if (opts
->flags
& VERIFY_BUNDLE_FSCK
)
628 strvec_pushf(&ip
.args
, "--fsck-objects%s",
629 opts
->fsck_msg_types
? opts
->fsck_msg_types
: "");
631 if (extra_index_pack_args
)
632 strvec_pushv(&ip
.args
, extra_index_pack_args
->v
);
637 if (run_command(&ip
))
638 return error(_("index-pack died"));