4 #include "object-store.h"
5 #include "repository.h"
10 #include "list-objects.h"
11 #include "run-command.h"
14 #include "list-objects-filter-options.h"
15 #include "connected.h"
17 static const char v2_bundle_signature
[] = "# v2 git bundle\n";
18 static const char v3_bundle_signature
[] = "# v3 git bundle\n";
21 const char *signature
;
23 { 2, v2_bundle_signature
},
24 { 3, v3_bundle_signature
},
27 void bundle_header_init(struct bundle_header
*header
)
29 struct bundle_header blank
= BUNDLE_HEADER_INIT
;
30 memcpy(header
, &blank
, sizeof(*header
));
33 void bundle_header_release(struct bundle_header
*header
)
35 string_list_clear(&header
->prerequisites
, 1);
36 string_list_clear(&header
->references
, 1);
37 list_objects_filter_release(&header
->filter
);
40 static int parse_capability(struct bundle_header
*header
, const char *capability
)
43 if (skip_prefix(capability
, "object-format=", &arg
)) {
44 int algo
= hash_algo_by_name(arg
);
45 if (algo
== GIT_HASH_UNKNOWN
)
46 return error(_("unrecognized bundle hash algorithm: %s"), arg
);
47 header
->hash_algo
= &hash_algos
[algo
];
50 if (skip_prefix(capability
, "filter=", &arg
)) {
51 parse_list_objects_filter(&header
->filter
, arg
);
54 return error(_("unknown capability '%s'"), capability
);
57 static int parse_bundle_signature(struct bundle_header
*header
, const char *line
)
61 for (i
= 0; i
< ARRAY_SIZE(bundle_sigs
); i
++) {
62 if (!strcmp(line
, bundle_sigs
[i
].signature
)) {
63 header
->version
= bundle_sigs
[i
].version
;
70 int read_bundle_header_fd(int fd
, struct bundle_header
*header
,
71 const char *report_path
)
73 struct strbuf buf
= STRBUF_INIT
;
76 /* The bundle header begins with the signature */
77 if (strbuf_getwholeline_fd(&buf
, fd
, '\n') ||
78 parse_bundle_signature(header
, buf
.buf
)) {
80 error(_("'%s' does not look like a v2 or v3 bundle file"),
86 header
->hash_algo
= the_hash_algo
;
88 /* The bundle header ends with an empty line */
89 while (!strbuf_getwholeline_fd(&buf
, fd
, '\n') &&
90 buf
.len
&& buf
.buf
[0] != '\n') {
97 if (header
->version
== 3 && *buf
.buf
== '@') {
98 if (parse_capability(header
, buf
.buf
+ 1)) {
105 if (*buf
.buf
== '-') {
107 strbuf_remove(&buf
, 0, 1);
111 * Tip lines have object name, SP, and refname.
112 * Prerequisites have object name that is optionally
113 * followed by SP and subject line.
115 if (parse_oid_hex_algop(buf
.buf
, &oid
, &p
, header
->hash_algo
) ||
116 (*p
&& !isspace(*p
)) ||
117 (!is_prereq
&& !*p
)) {
119 error(_("unrecognized header: %s%s (%d)"),
120 (is_prereq
? "-" : ""), buf
.buf
, (int)buf
.len
);
124 struct object_id
*dup
= oiddup(&oid
);
126 string_list_append(&header
->prerequisites
, "")->util
= dup
;
128 string_list_append(&header
->references
, p
+ 1)->util
= dup
;
137 strbuf_release(&buf
);
141 int read_bundle_header(const char *path
, struct bundle_header
*header
)
143 int fd
= open(path
, O_RDONLY
);
146 return error(_("could not open '%s'"), path
);
147 return read_bundle_header_fd(fd
, header
, path
);
150 int is_bundle(const char *path
, int quiet
)
152 struct bundle_header header
= BUNDLE_HEADER_INIT
;
153 int fd
= open(path
, O_RDONLY
);
157 fd
= read_bundle_header_fd(fd
, &header
, quiet
? NULL
: path
);
160 bundle_header_release(&header
);
164 static int list_refs(struct string_list
*r
, int argc
, const char **argv
)
168 for (i
= 0; i
< r
->nr
; i
++) {
169 struct object_id
*oid
;
174 for (j
= 1; j
< argc
; j
++)
175 if (!strcmp(r
->items
[i
].string
, argv
[j
]))
181 oid
= r
->items
[i
].util
;
182 name
= r
->items
[i
].string
;
183 printf("%s %s\n", oid_to_hex(oid
), name
);
188 /* Remember to update object flag allocation in object.h */
189 #define PREREQ_MARK (1u<<16)
191 struct string_list_iterator
{
192 struct string_list
*list
;
196 static const struct object_id
*iterate_ref_map(void *cb_data
)
198 struct string_list_iterator
*iter
= cb_data
;
200 if (iter
->cur
>= iter
->list
->nr
)
203 return iter
->list
->items
[iter
->cur
++].util
;
206 int verify_bundle(struct repository
*r
,
207 struct bundle_header
*header
,
208 enum verify_bundle_flags flags
)
211 * Do fast check, then if any prereqs are missing then go line by line
212 * to be verbose about the errors
214 struct string_list
*p
= &header
->prerequisites
;
216 const char *message
= _("Repository lacks these prerequisite commits:");
217 struct string_list_iterator iter
= {
220 struct check_connected_options opts
= {
224 if (!r
|| !r
->objects
|| !r
->objects
->odb
)
225 return error(_("need a repository to verify a bundle"));
227 for (i
= 0; i
< p
->nr
; i
++) {
228 struct string_list_item
*e
= p
->items
+ i
;
229 const char *name
= e
->string
;
230 struct object_id
*oid
= e
->util
;
231 struct object
*o
= parse_object(r
, oid
);
235 if (flags
& VERIFY_BUNDLE_QUIET
)
238 error("%s", message
);
239 error("%s %s", oid_to_hex(oid
), name
);
244 if ((ret
= check_connected(iterate_ref_map
, &iter
, &opts
)))
245 error(_("some prerequisite commits exist in the object store, "
246 "but are not connected to the repository's history"));
248 /* TODO: preserve this verbose language. */
249 if (flags
& VERIFY_BUNDLE_VERBOSE
) {
250 struct string_list
*r
;
252 r
= &header
->references
;
253 printf_ln(Q_("The bundle contains this ref:",
254 "The bundle contains these %"PRIuMAX
" refs:",
257 list_refs(r
, 0, NULL
);
259 r
= &header
->prerequisites
;
261 printf_ln(_("The bundle records a complete history."));
263 printf_ln(Q_("The bundle requires this ref:",
264 "The bundle requires these %"PRIuMAX
" refs:",
267 list_refs(r
, 0, NULL
);
270 printf_ln("The bundle uses this hash algorithm: %s",
271 header
->hash_algo
->name
);
272 if (header
->filter
.choice
)
273 printf_ln("The bundle uses this filter: %s",
274 list_objects_filter_spec(&header
->filter
));
280 int list_bundle_refs(struct bundle_header
*header
, int argc
, const char **argv
)
282 return list_refs(&header
->references
, argc
, argv
);
285 static int is_tag_in_date_range(struct object
*tag
, struct rev_info
*revs
)
288 enum object_type type
;
289 char *buf
= NULL
, *line
, *lineend
;
293 if (revs
->max_age
== -1 && revs
->min_age
== -1)
296 buf
= repo_read_object_file(the_repository
, &tag
->oid
, &type
, &size
);
299 line
= memmem(buf
, size
, "\ntagger ", 8);
302 lineend
= memchr(line
, '\n', buf
+ size
- line
);
303 line
= memchr(line
, '>', lineend
? lineend
- line
: buf
+ size
- line
);
306 date
= parse_timestamp(line
, NULL
, 10);
307 result
= (revs
->max_age
== -1 || revs
->max_age
< date
) &&
308 (revs
->min_age
== -1 || revs
->min_age
> date
);
315 /* Write the pack data to bundle_fd */
316 static int write_pack_data(int bundle_fd
, struct rev_info
*revs
, struct strvec
*pack_options
)
318 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
321 strvec_pushl(&pack_objects
.args
,
323 "--stdout", "--thin", "--delta-base-offset",
325 strvec_pushv(&pack_objects
.args
, pack_options
->v
);
326 if (revs
->filter
.choice
)
327 strvec_pushf(&pack_objects
.args
, "--filter=%s",
328 list_objects_filter_spec(&revs
->filter
));
329 pack_objects
.in
= -1;
330 pack_objects
.out
= bundle_fd
;
331 pack_objects
.git_cmd
= 1;
334 * start_command() will close our descriptor if it's >1. Duplicate it
335 * to avoid surprising the caller.
337 if (pack_objects
.out
> 1) {
338 pack_objects
.out
= dup(pack_objects
.out
);
339 if (pack_objects
.out
< 0) {
340 error_errno(_("unable to dup bundle descriptor"));
341 child_process_clear(&pack_objects
);
346 if (start_command(&pack_objects
))
347 return error(_("Could not spawn pack-objects"));
349 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
350 struct object
*object
= revs
->pending
.objects
[i
].item
;
351 if (object
->flags
& UNINTERESTING
)
352 write_or_die(pack_objects
.in
, "^", 1);
353 write_or_die(pack_objects
.in
, oid_to_hex(&object
->oid
), the_hash_algo
->hexsz
);
354 write_or_die(pack_objects
.in
, "\n", 1);
356 close(pack_objects
.in
);
357 if (finish_command(&pack_objects
))
358 return error(_("pack-objects died"));
363 * Write out bundle refs based on the tips already
364 * parsed into revs.pending. As a side effect, may
365 * manipulate revs.pending to include additional
366 * necessary objects (like tags).
368 * Returns the number of refs written, or negative
371 static int write_bundle_refs(int bundle_fd
, struct rev_info
*revs
)
376 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
377 struct object_array_entry
*e
= revs
->pending
.objects
+ i
;
378 struct object_id oid
;
380 const char *display_ref
;
383 if (e
->item
->flags
& UNINTERESTING
)
385 if (repo_dwim_ref(the_repository
, e
->name
, strlen(e
->name
),
388 if (read_ref_full(e
->name
, RESOLVE_REF_READING
, &oid
, &flag
))
390 display_ref
= (flag
& REF_ISSYMREF
) ? e
->name
: ref
;
392 if (e
->item
->type
== OBJ_TAG
&&
393 !is_tag_in_date_range(e
->item
, revs
)) {
394 e
->item
->flags
|= UNINTERESTING
;
399 * Make sure the refs we wrote out is correct; --max-count and
400 * other limiting options could have prevented all the tips
401 * from getting output.
403 * Non commit objects such as tags and blobs do not have
404 * this issue as they are not affected by those extra
407 if (!(e
->item
->flags
& SHOWN
) && e
->item
->type
== OBJ_COMMIT
) {
408 warning(_("ref '%s' is excluded by the rev-list options"),
413 * If you run "git bundle create bndl v1.0..v2.0", the
414 * name of the positive ref is "v2.0" but that is the
415 * commit that is referenced by the tag, and not the tag
418 if (!oideq(&oid
, &e
->item
->oid
)) {
420 * Is this the positive end of a range expressed
421 * in terms of a tag (e.g. v2.0 from the range
424 struct commit
*one
= lookup_commit_reference(revs
->repo
, &oid
);
427 if (e
->item
== &(one
->object
)) {
429 * Need to include e->name as an
430 * independent ref to the pack-objects
431 * input, so that the tag is included
432 * in the output; otherwise we would
433 * end up triggering "empty bundle"
436 obj
= parse_object_or_die(&oid
, e
->name
);
438 add_pending_object(revs
, obj
, e
->name
);
444 write_or_die(bundle_fd
, oid_to_hex(&e
->item
->oid
), the_hash_algo
->hexsz
);
445 write_or_die(bundle_fd
, " ", 1);
446 write_or_die(bundle_fd
, display_ref
, strlen(display_ref
));
447 write_or_die(bundle_fd
, "\n", 1);
453 write_or_die(bundle_fd
, "\n", 1);
457 struct bundle_prerequisites_info
{
458 struct object_array
*pending
;
462 static void write_bundle_prerequisites(struct commit
*commit
, void *data
)
464 struct bundle_prerequisites_info
*bpi
= data
;
465 struct object
*object
;
466 struct pretty_print_context ctx
= { 0 };
467 struct strbuf buf
= STRBUF_INIT
;
469 if (!(commit
->object
.flags
& BOUNDARY
))
471 strbuf_addf(&buf
, "-%s ", oid_to_hex(&commit
->object
.oid
));
472 write_or_die(bpi
->fd
, buf
.buf
, buf
.len
);
474 ctx
.fmt
= CMIT_FMT_ONELINE
;
475 ctx
.output_encoding
= get_log_output_encoding();
477 pretty_print_commit(&ctx
, commit
, &buf
);
480 object
= (struct object
*)commit
;
481 object
->flags
|= UNINTERESTING
;
482 add_object_array_with_path(object
, buf
.buf
, bpi
->pending
, S_IFINVALID
,
484 strbuf_addch(&buf
, '\n');
485 write_or_die(bpi
->fd
, buf
.buf
, buf
.len
);
486 strbuf_release(&buf
);
489 int create_bundle(struct repository
*r
, const char *path
,
490 int argc
, const char **argv
, struct strvec
*pack_options
, int version
)
492 struct lock_file lock
= LOCK_INIT
;
494 int bundle_to_stdout
;
496 struct rev_info revs
, revs_copy
;
498 struct bundle_prerequisites_info bpi
;
501 /* init revs to list objects for pack-objects later */
502 save_commit_buffer
= 0;
503 repo_init_revisions(r
, &revs
, NULL
);
506 * Pre-initialize the '--objects' flag so we can parse a
507 * --filter option successfully.
509 revs
.tree_objects
= revs
.blob_objects
= 1;
511 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
514 * Reasons to require version 3:
516 * 1. @object-format is required because our hash algorithm is not
518 * 2. @filter is required because we parsed an object filter.
520 if (the_hash_algo
!= &hash_algos
[GIT_HASH_SHA1
] || revs
.filter
.choice
)
524 error(_("unrecognized argument: %s"), argv
[1]);
528 bundle_to_stdout
= !strcmp(path
, "-");
529 if (bundle_to_stdout
)
532 bundle_fd
= hold_lock_file_for_update(&lock
, path
,
536 version
= min_version
;
538 if (version
< 2 || version
> 3) {
539 die(_("unsupported bundle version %d"), version
);
540 } else if (version
< min_version
) {
541 die(_("cannot write bundle version %d with algorithm %s"), version
, the_hash_algo
->name
);
542 } else if (version
== 2) {
543 write_or_die(bundle_fd
, v2_bundle_signature
, strlen(v2_bundle_signature
));
545 const char *capability
= "@object-format=";
546 write_or_die(bundle_fd
, v3_bundle_signature
, strlen(v3_bundle_signature
));
547 write_or_die(bundle_fd
, capability
, strlen(capability
));
548 write_or_die(bundle_fd
, the_hash_algo
->name
, strlen(the_hash_algo
->name
));
549 write_or_die(bundle_fd
, "\n", 1);
551 if (revs
.filter
.choice
) {
552 const char *value
= expand_list_objects_filter_spec(&revs
.filter
);
553 capability
= "@filter=";
554 write_or_die(bundle_fd
, capability
, strlen(capability
));
555 write_or_die(bundle_fd
, value
, strlen(value
));
556 write_or_die(bundle_fd
, "\n", 1);
560 /* save revs.pending in revs_copy for later use */
561 memcpy(&revs_copy
, &revs
, sizeof(revs
));
562 revs_copy
.pending
.nr
= 0;
563 revs_copy
.pending
.alloc
= 0;
564 revs_copy
.pending
.objects
= NULL
;
565 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
566 struct object_array_entry
*e
= revs
.pending
.objects
+ i
;
568 add_object_array_with_path(e
->item
, e
->name
,
573 /* write prerequisites */
575 if (prepare_revision_walk(&revs
))
576 die("revision walk setup failed");
578 bpi
.pending
= &revs_copy
.pending
;
581 * Remove any object walking here. We only care about commits and
582 * tags here. The revs_copy has the right instances of these values.
584 revs
.blob_objects
= revs
.tree_objects
= 0;
585 traverse_commit_list(&revs
, write_bundle_prerequisites
, NULL
, &bpi
);
586 object_array_remove_duplicates(&revs_copy
.pending
);
588 /* write bundle refs */
589 ref_count
= write_bundle_refs(bundle_fd
, &revs_copy
);
591 die(_("Refusing to create empty bundle."));
592 else if (ref_count
< 0)
596 if (write_pack_data(bundle_fd
, &revs_copy
, pack_options
))
599 if (!bundle_to_stdout
) {
600 if (commit_lock_file(&lock
))
601 die_errno(_("cannot create '%s'"), path
);
605 rollback_lock_file(&lock
);
609 int unbundle(struct repository
*r
, struct bundle_header
*header
,
610 int bundle_fd
, struct strvec
*extra_index_pack_args
,
611 enum verify_bundle_flags flags
)
613 struct child_process ip
= CHILD_PROCESS_INIT
;
615 if (verify_bundle(r
, header
, flags
))
618 strvec_pushl(&ip
.args
, "index-pack", "--fix-thin", "--stdin", NULL
);
620 /* If there is a filter, then we need to create the promisor pack. */
621 if (header
->filter
.choice
)
622 strvec_push(&ip
.args
, "--promisor=from-bundle");
624 if (extra_index_pack_args
) {
625 strvec_pushv(&ip
.args
, extra_index_pack_args
->v
);
626 strvec_clear(extra_index_pack_args
);
632 if (run_command(&ip
))
633 return error(_("index-pack died"));