4 #include "environment.h"
7 #include "object-store.h"
8 #include "repository.h"
13 #include "list-objects.h"
14 #include "run-command.h"
17 #include "list-objects-filter-options.h"
18 #include "connected.h"
20 static const char v2_bundle_signature
[] = "# v2 git bundle\n";
21 static const char v3_bundle_signature
[] = "# v3 git bundle\n";
24 const char *signature
;
26 { 2, v2_bundle_signature
},
27 { 3, v3_bundle_signature
},
30 void bundle_header_init(struct bundle_header
*header
)
32 struct bundle_header blank
= BUNDLE_HEADER_INIT
;
33 memcpy(header
, &blank
, sizeof(*header
));
36 void bundle_header_release(struct bundle_header
*header
)
38 string_list_clear(&header
->prerequisites
, 1);
39 string_list_clear(&header
->references
, 1);
40 list_objects_filter_release(&header
->filter
);
43 static int parse_capability(struct bundle_header
*header
, const char *capability
)
46 if (skip_prefix(capability
, "object-format=", &arg
)) {
47 int algo
= hash_algo_by_name(arg
);
48 if (algo
== GIT_HASH_UNKNOWN
)
49 return error(_("unrecognized bundle hash algorithm: %s"), arg
);
50 header
->hash_algo
= &hash_algos
[algo
];
53 if (skip_prefix(capability
, "filter=", &arg
)) {
54 parse_list_objects_filter(&header
->filter
, arg
);
57 return error(_("unknown capability '%s'"), capability
);
60 static int parse_bundle_signature(struct bundle_header
*header
, const char *line
)
64 for (i
= 0; i
< ARRAY_SIZE(bundle_sigs
); i
++) {
65 if (!strcmp(line
, bundle_sigs
[i
].signature
)) {
66 header
->version
= bundle_sigs
[i
].version
;
73 int read_bundle_header_fd(int fd
, struct bundle_header
*header
,
74 const char *report_path
)
76 struct strbuf buf
= STRBUF_INIT
;
79 /* The bundle header begins with the signature */
80 if (strbuf_getwholeline_fd(&buf
, fd
, '\n') ||
81 parse_bundle_signature(header
, buf
.buf
)) {
83 error(_("'%s' does not look like a v2 or v3 bundle file"),
89 header
->hash_algo
= the_hash_algo
;
91 /* The bundle header ends with an empty line */
92 while (!strbuf_getwholeline_fd(&buf
, fd
, '\n') &&
93 buf
.len
&& buf
.buf
[0] != '\n') {
100 if (header
->version
== 3 && *buf
.buf
== '@') {
101 if (parse_capability(header
, buf
.buf
+ 1)) {
108 if (*buf
.buf
== '-') {
110 strbuf_remove(&buf
, 0, 1);
114 * Tip lines have object name, SP, and refname.
115 * Prerequisites have object name that is optionally
116 * followed by SP and subject line.
118 if (parse_oid_hex_algop(buf
.buf
, &oid
, &p
, header
->hash_algo
) ||
119 (*p
&& !isspace(*p
)) ||
120 (!is_prereq
&& !*p
)) {
122 error(_("unrecognized header: %s%s (%d)"),
123 (is_prereq
? "-" : ""), buf
.buf
, (int)buf
.len
);
127 struct object_id
*dup
= oiddup(&oid
);
129 string_list_append(&header
->prerequisites
, "")->util
= dup
;
131 string_list_append(&header
->references
, p
+ 1)->util
= dup
;
140 strbuf_release(&buf
);
144 int read_bundle_header(const char *path
, struct bundle_header
*header
)
146 int fd
= open(path
, O_RDONLY
);
149 return error(_("could not open '%s'"), path
);
150 return read_bundle_header_fd(fd
, header
, path
);
153 int is_bundle(const char *path
, int quiet
)
155 struct bundle_header header
= BUNDLE_HEADER_INIT
;
156 int fd
= open(path
, O_RDONLY
);
160 fd
= read_bundle_header_fd(fd
, &header
, quiet
? NULL
: path
);
163 bundle_header_release(&header
);
167 static int list_refs(struct string_list
*r
, int argc
, const char **argv
)
171 for (i
= 0; i
< r
->nr
; i
++) {
172 struct object_id
*oid
;
177 for (j
= 1; j
< argc
; j
++)
178 if (!strcmp(r
->items
[i
].string
, argv
[j
]))
184 oid
= r
->items
[i
].util
;
185 name
= r
->items
[i
].string
;
186 printf("%s %s\n", oid_to_hex(oid
), name
);
191 /* Remember to update object flag allocation in object.h */
192 #define PREREQ_MARK (1u<<16)
194 struct string_list_iterator
{
195 struct string_list
*list
;
199 static const struct object_id
*iterate_ref_map(void *cb_data
)
201 struct string_list_iterator
*iter
= cb_data
;
203 if (iter
->cur
>= iter
->list
->nr
)
206 return iter
->list
->items
[iter
->cur
++].util
;
209 int verify_bundle(struct repository
*r
,
210 struct bundle_header
*header
,
211 enum verify_bundle_flags flags
)
214 * Do fast check, then if any prereqs are missing then go line by line
215 * to be verbose about the errors
217 struct string_list
*p
= &header
->prerequisites
;
219 const char *message
= _("Repository lacks these prerequisite commits:");
220 struct string_list_iterator iter
= {
223 struct check_connected_options opts
= {
227 if (!r
|| !r
->objects
|| !r
->objects
->odb
)
228 return error(_("need a repository to verify a bundle"));
230 for (i
= 0; i
< p
->nr
; i
++) {
231 struct string_list_item
*e
= p
->items
+ i
;
232 const char *name
= e
->string
;
233 struct object_id
*oid
= e
->util
;
234 struct object
*o
= parse_object(r
, oid
);
238 if (flags
& VERIFY_BUNDLE_QUIET
)
241 error("%s", message
);
242 error("%s %s", oid_to_hex(oid
), name
);
247 if ((ret
= check_connected(iterate_ref_map
, &iter
, &opts
)))
248 error(_("some prerequisite commits exist in the object store, "
249 "but are not connected to the repository's history"));
251 /* TODO: preserve this verbose language. */
252 if (flags
& VERIFY_BUNDLE_VERBOSE
) {
253 struct string_list
*r
;
255 r
= &header
->references
;
256 printf_ln(Q_("The bundle contains this ref:",
257 "The bundle contains these %"PRIuMAX
" refs:",
260 list_refs(r
, 0, NULL
);
262 r
= &header
->prerequisites
;
264 printf_ln(_("The bundle records a complete history."));
266 printf_ln(Q_("The bundle requires this ref:",
267 "The bundle requires these %"PRIuMAX
" refs:",
270 list_refs(r
, 0, NULL
);
273 printf_ln("The bundle uses this hash algorithm: %s",
274 header
->hash_algo
->name
);
275 if (header
->filter
.choice
)
276 printf_ln("The bundle uses this filter: %s",
277 list_objects_filter_spec(&header
->filter
));
283 int list_bundle_refs(struct bundle_header
*header
, int argc
, const char **argv
)
285 return list_refs(&header
->references
, argc
, argv
);
288 static int is_tag_in_date_range(struct object
*tag
, struct rev_info
*revs
)
291 enum object_type type
;
292 char *buf
= NULL
, *line
, *lineend
;
296 if (revs
->max_age
== -1 && revs
->min_age
== -1)
299 buf
= read_object_file(&tag
->oid
, &type
, &size
);
302 line
= memmem(buf
, size
, "\ntagger ", 8);
305 lineend
= memchr(line
, '\n', buf
+ size
- line
);
306 line
= memchr(line
, '>', lineend
? lineend
- line
: buf
+ size
- line
);
309 date
= parse_timestamp(line
, NULL
, 10);
310 result
= (revs
->max_age
== -1 || revs
->max_age
< date
) &&
311 (revs
->min_age
== -1 || revs
->min_age
> date
);
318 /* Write the pack data to bundle_fd */
319 static int write_pack_data(int bundle_fd
, struct rev_info
*revs
, struct strvec
*pack_options
)
321 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
324 strvec_pushl(&pack_objects
.args
,
326 "--stdout", "--thin", "--delta-base-offset",
328 strvec_pushv(&pack_objects
.args
, pack_options
->v
);
329 if (revs
->filter
.choice
)
330 strvec_pushf(&pack_objects
.args
, "--filter=%s",
331 list_objects_filter_spec(&revs
->filter
));
332 pack_objects
.in
= -1;
333 pack_objects
.out
= bundle_fd
;
334 pack_objects
.git_cmd
= 1;
337 * start_command() will close our descriptor if it's >1. Duplicate it
338 * to avoid surprising the caller.
340 if (pack_objects
.out
> 1) {
341 pack_objects
.out
= dup(pack_objects
.out
);
342 if (pack_objects
.out
< 0) {
343 error_errno(_("unable to dup bundle descriptor"));
344 child_process_clear(&pack_objects
);
349 if (start_command(&pack_objects
))
350 return error(_("Could not spawn pack-objects"));
352 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
353 struct object
*object
= revs
->pending
.objects
[i
].item
;
354 if (object
->flags
& UNINTERESTING
)
355 write_or_die(pack_objects
.in
, "^", 1);
356 write_or_die(pack_objects
.in
, oid_to_hex(&object
->oid
), the_hash_algo
->hexsz
);
357 write_or_die(pack_objects
.in
, "\n", 1);
359 close(pack_objects
.in
);
360 if (finish_command(&pack_objects
))
361 return error(_("pack-objects died"));
366 * Write out bundle refs based on the tips already
367 * parsed into revs.pending. As a side effect, may
368 * manipulate revs.pending to include additional
369 * necessary objects (like tags).
371 * Returns the number of refs written, or negative
374 static int write_bundle_refs(int bundle_fd
, struct rev_info
*revs
)
379 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
380 struct object_array_entry
*e
= revs
->pending
.objects
+ i
;
381 struct object_id oid
;
383 const char *display_ref
;
386 if (e
->item
->flags
& UNINTERESTING
)
388 if (dwim_ref(e
->name
, strlen(e
->name
), &oid
, &ref
, 0) != 1)
390 if (read_ref_full(e
->name
, RESOLVE_REF_READING
, &oid
, &flag
))
392 display_ref
= (flag
& REF_ISSYMREF
) ? e
->name
: ref
;
394 if (e
->item
->type
== OBJ_TAG
&&
395 !is_tag_in_date_range(e
->item
, revs
)) {
396 e
->item
->flags
|= UNINTERESTING
;
401 * Make sure the refs we wrote out is correct; --max-count and
402 * other limiting options could have prevented all the tips
403 * from getting output.
405 * Non commit objects such as tags and blobs do not have
406 * this issue as they are not affected by those extra
409 if (!(e
->item
->flags
& SHOWN
) && e
->item
->type
== OBJ_COMMIT
) {
410 warning(_("ref '%s' is excluded by the rev-list options"),
415 * If you run "git bundle create bndl v1.0..v2.0", the
416 * name of the positive ref is "v2.0" but that is the
417 * commit that is referenced by the tag, and not the tag
420 if (!oideq(&oid
, &e
->item
->oid
)) {
422 * Is this the positive end of a range expressed
423 * in terms of a tag (e.g. v2.0 from the range
426 struct commit
*one
= lookup_commit_reference(revs
->repo
, &oid
);
429 if (e
->item
== &(one
->object
)) {
431 * Need to include e->name as an
432 * independent ref to the pack-objects
433 * input, so that the tag is included
434 * in the output; otherwise we would
435 * end up triggering "empty bundle"
438 obj
= parse_object_or_die(&oid
, e
->name
);
440 add_pending_object(revs
, obj
, e
->name
);
446 write_or_die(bundle_fd
, oid_to_hex(&e
->item
->oid
), the_hash_algo
->hexsz
);
447 write_or_die(bundle_fd
, " ", 1);
448 write_or_die(bundle_fd
, display_ref
, strlen(display_ref
));
449 write_or_die(bundle_fd
, "\n", 1);
455 write_or_die(bundle_fd
, "\n", 1);
459 struct bundle_prerequisites_info
{
460 struct object_array
*pending
;
464 static void write_bundle_prerequisites(struct commit
*commit
, void *data
)
466 struct bundle_prerequisites_info
*bpi
= data
;
467 struct object
*object
;
468 struct pretty_print_context ctx
= { 0 };
469 struct strbuf buf
= STRBUF_INIT
;
471 if (!(commit
->object
.flags
& BOUNDARY
))
473 strbuf_addf(&buf
, "-%s ", oid_to_hex(&commit
->object
.oid
));
474 write_or_die(bpi
->fd
, buf
.buf
, buf
.len
);
476 ctx
.fmt
= CMIT_FMT_ONELINE
;
477 ctx
.output_encoding
= get_log_output_encoding();
479 pretty_print_commit(&ctx
, commit
, &buf
);
482 object
= (struct object
*)commit
;
483 object
->flags
|= UNINTERESTING
;
484 add_object_array_with_path(object
, buf
.buf
, bpi
->pending
, S_IFINVALID
,
486 strbuf_addch(&buf
, '\n');
487 write_or_die(bpi
->fd
, buf
.buf
, buf
.len
);
488 strbuf_release(&buf
);
491 int create_bundle(struct repository
*r
, const char *path
,
492 int argc
, const char **argv
, struct strvec
*pack_options
, int version
)
494 struct lock_file lock
= LOCK_INIT
;
496 int bundle_to_stdout
;
498 struct rev_info revs
, revs_copy
;
500 struct bundle_prerequisites_info bpi
;
503 /* init revs to list objects for pack-objects later */
504 save_commit_buffer
= 0;
505 repo_init_revisions(r
, &revs
, NULL
);
508 * Pre-initialize the '--objects' flag so we can parse a
509 * --filter option successfully.
511 revs
.tree_objects
= revs
.blob_objects
= 1;
513 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
516 * Reasons to require version 3:
518 * 1. @object-format is required because our hash algorithm is not
520 * 2. @filter is required because we parsed an object filter.
522 if (the_hash_algo
!= &hash_algos
[GIT_HASH_SHA1
] || revs
.filter
.choice
)
526 error(_("unrecognized argument: %s"), argv
[1]);
530 bundle_to_stdout
= !strcmp(path
, "-");
531 if (bundle_to_stdout
)
534 bundle_fd
= hold_lock_file_for_update(&lock
, path
,
538 version
= min_version
;
540 if (version
< 2 || version
> 3) {
541 die(_("unsupported bundle version %d"), version
);
542 } else if (version
< min_version
) {
543 die(_("cannot write bundle version %d with algorithm %s"), version
, the_hash_algo
->name
);
544 } else if (version
== 2) {
545 write_or_die(bundle_fd
, v2_bundle_signature
, strlen(v2_bundle_signature
));
547 const char *capability
= "@object-format=";
548 write_or_die(bundle_fd
, v3_bundle_signature
, strlen(v3_bundle_signature
));
549 write_or_die(bundle_fd
, capability
, strlen(capability
));
550 write_or_die(bundle_fd
, the_hash_algo
->name
, strlen(the_hash_algo
->name
));
551 write_or_die(bundle_fd
, "\n", 1);
553 if (revs
.filter
.choice
) {
554 const char *value
= expand_list_objects_filter_spec(&revs
.filter
);
555 capability
= "@filter=";
556 write_or_die(bundle_fd
, capability
, strlen(capability
));
557 write_or_die(bundle_fd
, value
, strlen(value
));
558 write_or_die(bundle_fd
, "\n", 1);
562 /* save revs.pending in revs_copy for later use */
563 memcpy(&revs_copy
, &revs
, sizeof(revs
));
564 revs_copy
.pending
.nr
= 0;
565 revs_copy
.pending
.alloc
= 0;
566 revs_copy
.pending
.objects
= NULL
;
567 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
568 struct object_array_entry
*e
= revs
.pending
.objects
+ i
;
570 add_object_array_with_path(e
->item
, e
->name
,
575 /* write prerequisites */
577 if (prepare_revision_walk(&revs
))
578 die("revision walk setup failed");
580 bpi
.pending
= &revs_copy
.pending
;
583 * Remove any object walking here. We only care about commits and
584 * tags here. The revs_copy has the right instances of these values.
586 revs
.blob_objects
= revs
.tree_objects
= 0;
587 traverse_commit_list(&revs
, write_bundle_prerequisites
, NULL
, &bpi
);
588 object_array_remove_duplicates(&revs_copy
.pending
);
590 /* write bundle refs */
591 ref_count
= write_bundle_refs(bundle_fd
, &revs_copy
);
593 die(_("Refusing to create empty bundle."));
594 else if (ref_count
< 0)
598 if (write_pack_data(bundle_fd
, &revs_copy
, pack_options
))
601 if (!bundle_to_stdout
) {
602 if (commit_lock_file(&lock
))
603 die_errno(_("cannot create '%s'"), path
);
607 rollback_lock_file(&lock
);
611 int unbundle(struct repository
*r
, struct bundle_header
*header
,
612 int bundle_fd
, struct strvec
*extra_index_pack_args
,
613 enum verify_bundle_flags flags
)
615 struct child_process ip
= CHILD_PROCESS_INIT
;
617 if (verify_bundle(r
, header
, flags
))
620 strvec_pushl(&ip
.args
, "index-pack", "--fix-thin", "--stdin", NULL
);
622 /* If there is a filter, then we need to create the promisor pack. */
623 if (header
->filter
.choice
)
624 strvec_push(&ip
.args
, "--promisor=from-bundle");
626 if (extra_index_pack_args
) {
627 strvec_pushv(&ip
.args
, extra_index_pack_args
->v
);
628 strvec_clear(extra_index_pack_args
);
634 if (run_command(&ip
))
635 return error(_("index-pack died"));