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"
16 static const char v2_bundle_signature
[] = "# v2 git bundle\n";
17 static const char v3_bundle_signature
[] = "# v3 git bundle\n";
20 const char *signature
;
22 { 2, v2_bundle_signature
},
23 { 3, v3_bundle_signature
},
26 void bundle_header_init(struct bundle_header
*header
)
28 struct bundle_header blank
= BUNDLE_HEADER_INIT
;
29 memcpy(header
, &blank
, sizeof(*header
));
32 void bundle_header_release(struct bundle_header
*header
)
34 string_list_clear(&header
->prerequisites
, 1);
35 string_list_clear(&header
->references
, 1);
36 list_objects_filter_release(&header
->filter
);
39 static int parse_capability(struct bundle_header
*header
, const char *capability
)
42 if (skip_prefix(capability
, "object-format=", &arg
)) {
43 int algo
= hash_algo_by_name(arg
);
44 if (algo
== GIT_HASH_UNKNOWN
)
45 return error(_("unrecognized bundle hash algorithm: %s"), arg
);
46 header
->hash_algo
= &hash_algos
[algo
];
49 if (skip_prefix(capability
, "filter=", &arg
)) {
50 parse_list_objects_filter(&header
->filter
, arg
);
53 return error(_("unknown capability '%s'"), capability
);
56 static int parse_bundle_signature(struct bundle_header
*header
, const char *line
)
60 for (i
= 0; i
< ARRAY_SIZE(bundle_sigs
); i
++) {
61 if (!strcmp(line
, bundle_sigs
[i
].signature
)) {
62 header
->version
= bundle_sigs
[i
].version
;
69 int read_bundle_header_fd(int fd
, struct bundle_header
*header
,
70 const char *report_path
)
72 struct strbuf buf
= STRBUF_INIT
;
75 /* The bundle header begins with the signature */
76 if (strbuf_getwholeline_fd(&buf
, fd
, '\n') ||
77 parse_bundle_signature(header
, buf
.buf
)) {
79 error(_("'%s' does not look like a v2 or v3 bundle file"),
85 header
->hash_algo
= the_hash_algo
;
87 /* The bundle header ends with an empty line */
88 while (!strbuf_getwholeline_fd(&buf
, fd
, '\n') &&
89 buf
.len
&& buf
.buf
[0] != '\n') {
96 if (header
->version
== 3 && *buf
.buf
== '@') {
97 if (parse_capability(header
, buf
.buf
+ 1)) {
104 if (*buf
.buf
== '-') {
106 strbuf_remove(&buf
, 0, 1);
110 * Tip lines have object name, SP, and refname.
111 * Prerequisites have object name that is optionally
112 * followed by SP and subject line.
114 if (parse_oid_hex_algop(buf
.buf
, &oid
, &p
, header
->hash_algo
) ||
115 (*p
&& !isspace(*p
)) ||
116 (!is_prereq
&& !*p
)) {
118 error(_("unrecognized header: %s%s (%d)"),
119 (is_prereq
? "-" : ""), buf
.buf
, (int)buf
.len
);
123 struct object_id
*dup
= oiddup(&oid
);
125 string_list_append(&header
->prerequisites
, "")->util
= dup
;
127 string_list_append(&header
->references
, p
+ 1)->util
= dup
;
136 strbuf_release(&buf
);
140 int read_bundle_header(const char *path
, struct bundle_header
*header
)
142 int fd
= open(path
, O_RDONLY
);
145 return error(_("could not open '%s'"), path
);
146 return read_bundle_header_fd(fd
, header
, path
);
149 int is_bundle(const char *path
, int quiet
)
151 struct bundle_header header
= BUNDLE_HEADER_INIT
;
152 int fd
= open(path
, O_RDONLY
);
156 fd
= read_bundle_header_fd(fd
, &header
, quiet
? NULL
: path
);
159 bundle_header_release(&header
);
163 static int list_refs(struct string_list
*r
, int argc
, const char **argv
)
167 for (i
= 0; i
< r
->nr
; i
++) {
168 struct object_id
*oid
;
173 for (j
= 1; j
< argc
; j
++)
174 if (!strcmp(r
->items
[i
].string
, argv
[j
]))
180 oid
= r
->items
[i
].util
;
181 name
= r
->items
[i
].string
;
182 printf("%s %s\n", oid_to_hex(oid
), name
);
187 /* Remember to update object flag allocation in object.h */
188 #define PREREQ_MARK (1u<<16)
190 int verify_bundle(struct repository
*r
,
191 struct bundle_header
*header
,
192 enum verify_bundle_flags flags
)
195 * Do fast check, then if any prereqs are missing then go line by line
196 * to be verbose about the errors
198 struct string_list
*p
= &header
->prerequisites
;
199 struct rev_info revs
= REV_INFO_INIT
;
200 const char *argv
[] = {NULL
, "--all", NULL
};
201 struct commit
*commit
;
202 int i
, ret
= 0, req_nr
;
203 const char *message
= _("Repository lacks these prerequisite commits:");
205 if (!r
|| !r
->objects
|| !r
->objects
->odb
)
206 return error(_("need a repository to verify a bundle"));
208 repo_init_revisions(r
, &revs
, NULL
);
209 for (i
= 0; i
< p
->nr
; i
++) {
210 struct string_list_item
*e
= p
->items
+ i
;
211 const char *name
= e
->string
;
212 struct object_id
*oid
= e
->util
;
213 struct object
*o
= parse_object(r
, oid
);
215 o
->flags
|= PREREQ_MARK
;
216 add_pending_object(&revs
, o
, name
);
220 if (flags
& VERIFY_BUNDLE_QUIET
)
223 error("%s", message
);
224 error("%s %s", oid_to_hex(oid
), name
);
226 if (revs
.pending
.nr
!= p
->nr
)
228 req_nr
= revs
.pending
.nr
;
229 setup_revisions(2, argv
, &revs
, NULL
);
231 list_objects_filter_copy(&revs
.filter
, &header
->filter
);
233 if (prepare_revision_walk(&revs
))
234 die(_("revision walk setup failed"));
237 while (i
&& (commit
= get_revision(&revs
)))
238 if (commit
->object
.flags
& PREREQ_MARK
)
241 for (i
= 0; i
< p
->nr
; i
++) {
242 struct string_list_item
*e
= p
->items
+ i
;
243 const char *name
= e
->string
;
244 const struct object_id
*oid
= e
->util
;
245 struct object
*o
= parse_object(r
, oid
);
246 assert(o
); /* otherwise we'd have returned early */
247 if (o
->flags
& SHOWN
)
250 if (flags
& VERIFY_BUNDLE_QUIET
)
253 error("%s", message
);
254 error("%s %s", oid_to_hex(oid
), name
);
257 if (flags
& VERIFY_BUNDLE_VERBOSE
) {
258 struct string_list
*r
;
260 r
= &header
->references
;
261 printf_ln(Q_("The bundle contains this ref:",
262 "The bundle contains these %"PRIuMAX
" refs:",
265 list_refs(r
, 0, NULL
);
267 r
= &header
->prerequisites
;
269 printf_ln(_("The bundle records a complete history."));
271 printf_ln(Q_("The bundle requires this ref:",
272 "The bundle requires these %"PRIuMAX
" refs:",
275 list_refs(r
, 0, NULL
);
278 printf_ln("The bundle uses this hash algorithm: %s",
279 header
->hash_algo
->name
);
280 if (header
->filter
.choice
)
281 printf_ln("The bundle uses this filter: %s",
282 list_objects_filter_spec(&header
->filter
));
285 /* Clean up objects used, as they will be reused. */
286 for (i
= 0; i
< p
->nr
; i
++) {
287 struct string_list_item
*e
= p
->items
+ i
;
288 struct object_id
*oid
= e
->util
;
289 commit
= lookup_commit_reference_gently(r
, oid
, 1);
291 clear_commit_marks(commit
, ALL_REV_FLAGS
| PREREQ_MARK
);
293 release_revisions(&revs
);
297 int list_bundle_refs(struct bundle_header
*header
, int argc
, const char **argv
)
299 return list_refs(&header
->references
, argc
, argv
);
302 static int is_tag_in_date_range(struct object
*tag
, struct rev_info
*revs
)
305 enum object_type type
;
306 char *buf
= NULL
, *line
, *lineend
;
310 if (revs
->max_age
== -1 && revs
->min_age
== -1)
313 buf
= read_object_file(&tag
->oid
, &type
, &size
);
316 line
= memmem(buf
, size
, "\ntagger ", 8);
319 lineend
= memchr(line
, '\n', buf
+ size
- line
);
320 line
= memchr(line
, '>', lineend
? lineend
- line
: buf
+ size
- line
);
323 date
= parse_timestamp(line
, NULL
, 10);
324 result
= (revs
->max_age
== -1 || revs
->max_age
< date
) &&
325 (revs
->min_age
== -1 || revs
->min_age
> date
);
332 /* Write the pack data to bundle_fd */
333 static int write_pack_data(int bundle_fd
, struct rev_info
*revs
, struct strvec
*pack_options
)
335 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
338 strvec_pushl(&pack_objects
.args
,
340 "--stdout", "--thin", "--delta-base-offset",
342 strvec_pushv(&pack_objects
.args
, pack_options
->v
);
343 if (revs
->filter
.choice
)
344 strvec_pushf(&pack_objects
.args
, "--filter=%s",
345 list_objects_filter_spec(&revs
->filter
));
346 pack_objects
.in
= -1;
347 pack_objects
.out
= bundle_fd
;
348 pack_objects
.git_cmd
= 1;
351 * start_command() will close our descriptor if it's >1. Duplicate it
352 * to avoid surprising the caller.
354 if (pack_objects
.out
> 1) {
355 pack_objects
.out
= dup(pack_objects
.out
);
356 if (pack_objects
.out
< 0) {
357 error_errno(_("unable to dup bundle descriptor"));
358 child_process_clear(&pack_objects
);
363 if (start_command(&pack_objects
))
364 return error(_("Could not spawn pack-objects"));
366 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
367 struct object
*object
= revs
->pending
.objects
[i
].item
;
368 if (object
->flags
& UNINTERESTING
)
369 write_or_die(pack_objects
.in
, "^", 1);
370 write_or_die(pack_objects
.in
, oid_to_hex(&object
->oid
), the_hash_algo
->hexsz
);
371 write_or_die(pack_objects
.in
, "\n", 1);
373 close(pack_objects
.in
);
374 if (finish_command(&pack_objects
))
375 return error(_("pack-objects died"));
380 * Write out bundle refs based on the tips already
381 * parsed into revs.pending. As a side effect, may
382 * manipulate revs.pending to include additional
383 * necessary objects (like tags).
385 * Returns the number of refs written, or negative
388 static int write_bundle_refs(int bundle_fd
, struct rev_info
*revs
)
393 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
394 struct object_array_entry
*e
= revs
->pending
.objects
+ i
;
395 struct object_id oid
;
397 const char *display_ref
;
400 if (e
->item
->flags
& UNINTERESTING
)
402 if (dwim_ref(e
->name
, strlen(e
->name
), &oid
, &ref
, 0) != 1)
404 if (read_ref_full(e
->name
, RESOLVE_REF_READING
, &oid
, &flag
))
406 display_ref
= (flag
& REF_ISSYMREF
) ? e
->name
: 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"),
429 * If you run "git bundle create bndl v1.0..v2.0", the
430 * name of the positive ref is "v2.0" but that is the
431 * commit that is referenced by the tag, and not the tag
434 if (!oideq(&oid
, &e
->item
->oid
)) {
436 * Is this the positive end of a range expressed
437 * in terms of a tag (e.g. v2.0 from the range
440 struct commit
*one
= lookup_commit_reference(revs
->repo
, &oid
);
443 if (e
->item
== &(one
->object
)) {
445 * Need to include e->name as an
446 * independent ref to the pack-objects
447 * input, so that the tag is included
448 * in the output; otherwise we would
449 * end up triggering "empty bundle"
452 obj
= parse_object_or_die(&oid
, e
->name
);
454 add_pending_object(revs
, obj
, e
->name
);
460 write_or_die(bundle_fd
, oid_to_hex(&e
->item
->oid
), the_hash_algo
->hexsz
);
461 write_or_die(bundle_fd
, " ", 1);
462 write_or_die(bundle_fd
, display_ref
, strlen(display_ref
));
463 write_or_die(bundle_fd
, "\n", 1);
469 write_or_die(bundle_fd
, "\n", 1);
473 struct bundle_prerequisites_info
{
474 struct object_array
*pending
;
478 static void write_bundle_prerequisites(struct commit
*commit
, void *data
)
480 struct bundle_prerequisites_info
*bpi
= data
;
481 struct object
*object
;
482 struct pretty_print_context ctx
= { 0 };
483 struct strbuf buf
= STRBUF_INIT
;
485 if (!(commit
->object
.flags
& BOUNDARY
))
487 strbuf_addf(&buf
, "-%s ", oid_to_hex(&commit
->object
.oid
));
488 write_or_die(bpi
->fd
, buf
.buf
, buf
.len
);
490 ctx
.fmt
= CMIT_FMT_ONELINE
;
491 ctx
.output_encoding
= get_log_output_encoding();
493 pretty_print_commit(&ctx
, commit
, &buf
);
496 object
= (struct object
*)commit
;
497 object
->flags
|= UNINTERESTING
;
498 add_object_array_with_path(object
, buf
.buf
, bpi
->pending
, S_IFINVALID
,
500 strbuf_addch(&buf
, '\n');
501 write_or_die(bpi
->fd
, buf
.buf
, buf
.len
);
502 strbuf_release(&buf
);
505 int create_bundle(struct repository
*r
, const char *path
,
506 int argc
, const char **argv
, struct strvec
*pack_options
, int version
)
508 struct lock_file lock
= LOCK_INIT
;
510 int bundle_to_stdout
;
512 struct rev_info revs
, revs_copy
;
514 struct bundle_prerequisites_info bpi
;
517 /* init revs to list objects for pack-objects later */
518 save_commit_buffer
= 0;
519 repo_init_revisions(r
, &revs
, NULL
);
522 * Pre-initialize the '--objects' flag so we can parse a
523 * --filter option successfully.
525 revs
.tree_objects
= revs
.blob_objects
= 1;
527 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
530 * Reasons to require version 3:
532 * 1. @object-format is required because our hash algorithm is not
534 * 2. @filter is required because we parsed an object filter.
536 if (the_hash_algo
!= &hash_algos
[GIT_HASH_SHA1
] || revs
.filter
.choice
)
540 error(_("unrecognized argument: %s"), argv
[1]);
544 bundle_to_stdout
= !strcmp(path
, "-");
545 if (bundle_to_stdout
)
548 bundle_fd
= hold_lock_file_for_update(&lock
, path
,
552 version
= min_version
;
554 if (version
< 2 || version
> 3) {
555 die(_("unsupported bundle version %d"), version
);
556 } else if (version
< min_version
) {
557 die(_("cannot write bundle version %d with algorithm %s"), version
, the_hash_algo
->name
);
558 } else if (version
== 2) {
559 write_or_die(bundle_fd
, v2_bundle_signature
, strlen(v2_bundle_signature
));
561 const char *capability
= "@object-format=";
562 write_or_die(bundle_fd
, v3_bundle_signature
, strlen(v3_bundle_signature
));
563 write_or_die(bundle_fd
, capability
, strlen(capability
));
564 write_or_die(bundle_fd
, the_hash_algo
->name
, strlen(the_hash_algo
->name
));
565 write_or_die(bundle_fd
, "\n", 1);
567 if (revs
.filter
.choice
) {
568 const char *value
= expand_list_objects_filter_spec(&revs
.filter
);
569 capability
= "@filter=";
570 write_or_die(bundle_fd
, capability
, strlen(capability
));
571 write_or_die(bundle_fd
, value
, strlen(value
));
572 write_or_die(bundle_fd
, "\n", 1);
576 /* save revs.pending in revs_copy for later use */
577 memcpy(&revs_copy
, &revs
, sizeof(revs
));
578 revs_copy
.pending
.nr
= 0;
579 revs_copy
.pending
.alloc
= 0;
580 revs_copy
.pending
.objects
= NULL
;
581 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
582 struct object_array_entry
*e
= revs
.pending
.objects
+ i
;
584 add_object_array_with_path(e
->item
, e
->name
,
589 /* write prerequisites */
591 if (prepare_revision_walk(&revs
))
592 die("revision walk setup failed");
594 bpi
.pending
= &revs_copy
.pending
;
597 * Remove any object walking here. We only care about commits and
598 * tags here. The revs_copy has the right instances of these values.
600 revs
.blob_objects
= revs
.tree_objects
= 0;
601 traverse_commit_list(&revs
, write_bundle_prerequisites
, NULL
, &bpi
);
602 object_array_remove_duplicates(&revs_copy
.pending
);
604 /* write bundle refs */
605 ref_count
= write_bundle_refs(bundle_fd
, &revs_copy
);
607 die(_("Refusing to create empty bundle."));
608 else if (ref_count
< 0)
612 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
);
621 rollback_lock_file(&lock
);
625 int unbundle(struct repository
*r
, struct bundle_header
*header
,
626 int bundle_fd
, struct strvec
*extra_index_pack_args
,
627 enum verify_bundle_flags flags
)
629 struct child_process ip
= CHILD_PROCESS_INIT
;
630 strvec_pushl(&ip
.args
, "index-pack", "--fix-thin", "--stdin", NULL
);
632 /* If there is a filter, then we need to create the promisor pack. */
633 if (header
->filter
.choice
)
634 strvec_push(&ip
.args
, "--promisor=from-bundle");
636 if (extra_index_pack_args
) {
637 strvec_pushv(&ip
.args
, extra_index_pack_args
->v
);
638 strvec_clear(extra_index_pack_args
);
641 if (verify_bundle(r
, header
, flags
))
646 if (run_command(&ip
))
647 return error(_("index-pack died"));