4 #include "object-store.h"
5 #include "repository.h"
10 #include "list-objects.h"
11 #include "run-command.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 static void add_to_ref_list(const struct object_id
*oid
, const char *name
,
27 struct ref_list
*list
)
29 ALLOC_GROW(list
->list
, list
->nr
+ 1, list
->alloc
);
30 oidcpy(&list
->list
[list
->nr
].oid
, oid
);
31 list
->list
[list
->nr
].name
= xstrdup(name
);
35 static int parse_capability(struct bundle_header
*header
, const char *capability
)
38 if (skip_prefix(capability
, "object-format=", &arg
)) {
39 int algo
= hash_algo_by_name(arg
);
40 if (algo
== GIT_HASH_UNKNOWN
)
41 return error(_("unrecognized bundle hash algorithm: %s"), arg
);
42 header
->hash_algo
= &hash_algos
[algo
];
45 return error(_("unknown capability '%s'"), capability
);
48 static int parse_bundle_signature(struct bundle_header
*header
, const char *line
)
52 for (i
= 0; i
< ARRAY_SIZE(bundle_sigs
); i
++) {
53 if (!strcmp(line
, bundle_sigs
[i
].signature
)) {
54 header
->version
= bundle_sigs
[i
].version
;
61 static int parse_bundle_header(int fd
, struct bundle_header
*header
,
62 const char *report_path
)
64 struct strbuf buf
= STRBUF_INIT
;
67 /* The bundle header begins with the signature */
68 if (strbuf_getwholeline_fd(&buf
, fd
, '\n') ||
69 parse_bundle_signature(header
, buf
.buf
)) {
71 error(_("'%s' does not look like a v2 or v3 bundle file"),
77 header
->hash_algo
= the_hash_algo
;
79 /* The bundle header ends with an empty line */
80 while (!strbuf_getwholeline_fd(&buf
, fd
, '\n') &&
81 buf
.len
&& buf
.buf
[0] != '\n') {
88 if (header
->version
== 3 && *buf
.buf
== '@') {
89 if (parse_capability(header
, buf
.buf
+ 1)) {
96 if (*buf
.buf
== '-') {
98 strbuf_remove(&buf
, 0, 1);
102 * Tip lines have object name, SP, and refname.
103 * Prerequisites have object name that is optionally
104 * followed by SP and subject line.
106 if (parse_oid_hex_algop(buf
.buf
, &oid
, &p
, header
->hash_algo
) ||
107 (*p
&& !isspace(*p
)) ||
108 (!is_prereq
&& !*p
)) {
110 error(_("unrecognized header: %s%s (%d)"),
111 (is_prereq
? "-" : ""), buf
.buf
, (int)buf
.len
);
116 add_to_ref_list(&oid
, "", &header
->prerequisites
);
118 add_to_ref_list(&oid
, p
+ 1, &header
->references
);
127 strbuf_release(&buf
);
131 int read_bundle_header(const char *path
, struct bundle_header
*header
)
133 int fd
= open(path
, O_RDONLY
);
136 return error(_("could not open '%s'"), path
);
137 return parse_bundle_header(fd
, header
, path
);
140 int is_bundle(const char *path
, int quiet
)
142 struct bundle_header header
;
143 int fd
= open(path
, O_RDONLY
);
147 memset(&header
, 0, sizeof(header
));
148 fd
= parse_bundle_header(fd
, &header
, quiet
? NULL
: path
);
154 static int list_refs(struct ref_list
*r
, int argc
, const char **argv
)
158 for (i
= 0; i
< r
->nr
; i
++) {
161 for (j
= 1; j
< argc
; j
++)
162 if (!strcmp(r
->list
[i
].name
, argv
[j
]))
167 printf("%s %s\n", oid_to_hex(&r
->list
[i
].oid
),
173 /* Remember to update object flag allocation in object.h */
174 #define PREREQ_MARK (1u<<16)
176 int verify_bundle(struct repository
*r
,
177 struct bundle_header
*header
,
181 * Do fast check, then if any prereqs are missing then go line by line
182 * to be verbose about the errors
184 struct ref_list
*p
= &header
->prerequisites
;
185 struct rev_info revs
;
186 const char *argv
[] = {NULL
, "--all", NULL
};
187 struct commit
*commit
;
188 int i
, ret
= 0, req_nr
;
189 const char *message
= _("Repository lacks these prerequisite commits:");
191 if (!r
|| !r
->objects
|| !r
->objects
->odb
)
192 return error(_("need a repository to verify a bundle"));
194 repo_init_revisions(r
, &revs
, NULL
);
195 for (i
= 0; i
< p
->nr
; i
++) {
196 struct ref_list_entry
*e
= p
->list
+ i
;
197 struct object
*o
= parse_object(r
, &e
->oid
);
199 o
->flags
|= PREREQ_MARK
;
200 add_pending_object(&revs
, o
, e
->name
);
204 error("%s", message
);
205 error("%s %s", oid_to_hex(&e
->oid
), e
->name
);
207 if (revs
.pending
.nr
!= p
->nr
)
209 req_nr
= revs
.pending
.nr
;
210 setup_revisions(2, argv
, &revs
, NULL
);
212 if (prepare_revision_walk(&revs
))
213 die(_("revision walk setup failed"));
216 while (i
&& (commit
= get_revision(&revs
)))
217 if (commit
->object
.flags
& PREREQ_MARK
)
220 for (i
= 0; i
< p
->nr
; i
++) {
221 struct ref_list_entry
*e
= p
->list
+ i
;
222 struct object
*o
= parse_object(r
, &e
->oid
);
223 assert(o
); /* otherwise we'd have returned early */
224 if (o
->flags
& SHOWN
)
227 error("%s", message
);
228 error("%s %s", oid_to_hex(&e
->oid
), e
->name
);
231 /* Clean up objects used, as they will be reused. */
232 for (i
= 0; i
< p
->nr
; i
++) {
233 struct ref_list_entry
*e
= p
->list
+ i
;
234 commit
= lookup_commit_reference_gently(r
, &e
->oid
, 1);
236 clear_commit_marks(commit
, ALL_REV_FLAGS
);
242 r
= &header
->references
;
243 printf_ln(Q_("The bundle contains this ref:",
244 "The bundle contains these %d refs:",
247 list_refs(r
, 0, NULL
);
248 r
= &header
->prerequisites
;
250 printf_ln(_("The bundle records a complete history."));
252 printf_ln(Q_("The bundle requires this ref:",
253 "The bundle requires these %d refs:",
256 list_refs(r
, 0, NULL
);
262 int list_bundle_refs(struct bundle_header
*header
, int argc
, const char **argv
)
264 return list_refs(&header
->references
, argc
, argv
);
267 static int is_tag_in_date_range(struct object
*tag
, struct rev_info
*revs
)
270 enum object_type type
;
271 char *buf
= NULL
, *line
, *lineend
;
275 if (revs
->max_age
== -1 && revs
->min_age
== -1)
278 buf
= read_object_file(&tag
->oid
, &type
, &size
);
281 line
= memmem(buf
, size
, "\ntagger ", 8);
284 lineend
= memchr(line
, '\n', buf
+ size
- line
);
285 line
= memchr(line
, '>', lineend
? lineend
- line
: buf
+ size
- line
);
288 date
= parse_timestamp(line
, NULL
, 10);
289 result
= (revs
->max_age
== -1 || revs
->max_age
< date
) &&
290 (revs
->min_age
== -1 || revs
->min_age
> date
);
297 /* Write the pack data to bundle_fd */
298 static int write_pack_data(int bundle_fd
, struct rev_info
*revs
, struct strvec
*pack_options
)
300 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
303 strvec_pushl(&pack_objects
.args
,
305 "--stdout", "--thin", "--delta-base-offset",
307 strvec_pushv(&pack_objects
.args
, pack_options
->v
);
308 pack_objects
.in
= -1;
309 pack_objects
.out
= bundle_fd
;
310 pack_objects
.git_cmd
= 1;
313 * start_command() will close our descriptor if it's >1. Duplicate it
314 * to avoid surprising the caller.
316 if (pack_objects
.out
> 1) {
317 pack_objects
.out
= dup(pack_objects
.out
);
318 if (pack_objects
.out
< 0) {
319 error_errno(_("unable to dup bundle descriptor"));
320 child_process_clear(&pack_objects
);
325 if (start_command(&pack_objects
))
326 return error(_("Could not spawn pack-objects"));
328 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
329 struct object
*object
= revs
->pending
.objects
[i
].item
;
330 if (object
->flags
& UNINTERESTING
)
331 write_or_die(pack_objects
.in
, "^", 1);
332 write_or_die(pack_objects
.in
, oid_to_hex(&object
->oid
), the_hash_algo
->hexsz
);
333 write_or_die(pack_objects
.in
, "\n", 1);
335 close(pack_objects
.in
);
336 if (finish_command(&pack_objects
))
337 return error(_("pack-objects died"));
342 * Write out bundle refs based on the tips already
343 * parsed into revs.pending. As a side effect, may
344 * manipulate revs.pending to include additional
345 * necessary objects (like tags).
347 * Returns the number of refs written, or negative
350 static int write_bundle_refs(int bundle_fd
, struct rev_info
*revs
)
355 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
356 struct object_array_entry
*e
= revs
->pending
.objects
+ i
;
357 struct object_id oid
;
359 const char *display_ref
;
362 if (e
->item
->flags
& UNINTERESTING
)
364 if (dwim_ref(e
->name
, strlen(e
->name
), &oid
, &ref
, 0) != 1)
366 if (read_ref_full(e
->name
, RESOLVE_REF_READING
, &oid
, &flag
))
368 display_ref
= (flag
& REF_ISSYMREF
) ? e
->name
: ref
;
370 if (e
->item
->type
== OBJ_TAG
&&
371 !is_tag_in_date_range(e
->item
, revs
)) {
372 e
->item
->flags
|= UNINTERESTING
;
377 * Make sure the refs we wrote out is correct; --max-count and
378 * other limiting options could have prevented all the tips
379 * from getting output.
381 * Non commit objects such as tags and blobs do not have
382 * this issue as they are not affected by those extra
385 if (!(e
->item
->flags
& SHOWN
) && e
->item
->type
== OBJ_COMMIT
) {
386 warning(_("ref '%s' is excluded by the rev-list options"),
391 * If you run "git bundle create bndl v1.0..v2.0", the
392 * name of the positive ref is "v2.0" but that is the
393 * commit that is referenced by the tag, and not the tag
396 if (!oideq(&oid
, &e
->item
->oid
)) {
398 * Is this the positive end of a range expressed
399 * in terms of a tag (e.g. v2.0 from the range
402 struct commit
*one
= lookup_commit_reference(revs
->repo
, &oid
);
405 if (e
->item
== &(one
->object
)) {
407 * Need to include e->name as an
408 * independent ref to the pack-objects
409 * input, so that the tag is included
410 * in the output; otherwise we would
411 * end up triggering "empty bundle"
414 obj
= parse_object_or_die(&oid
, e
->name
);
416 add_pending_object(revs
, obj
, e
->name
);
422 write_or_die(bundle_fd
, oid_to_hex(&e
->item
->oid
), the_hash_algo
->hexsz
);
423 write_or_die(bundle_fd
, " ", 1);
424 write_or_die(bundle_fd
, display_ref
, strlen(display_ref
));
425 write_or_die(bundle_fd
, "\n", 1);
431 write_or_die(bundle_fd
, "\n", 1);
435 struct bundle_prerequisites_info
{
436 struct object_array
*pending
;
440 static void write_bundle_prerequisites(struct commit
*commit
, void *data
)
442 struct bundle_prerequisites_info
*bpi
= data
;
443 struct object
*object
;
444 struct pretty_print_context ctx
= { 0 };
445 struct strbuf buf
= STRBUF_INIT
;
447 if (!(commit
->object
.flags
& BOUNDARY
))
449 strbuf_addf(&buf
, "-%s ", oid_to_hex(&commit
->object
.oid
));
450 write_or_die(bpi
->fd
, buf
.buf
, buf
.len
);
452 ctx
.fmt
= CMIT_FMT_ONELINE
;
453 ctx
.output_encoding
= get_log_output_encoding();
455 pretty_print_commit(&ctx
, commit
, &buf
);
458 object
= (struct object
*)commit
;
459 object
->flags
|= UNINTERESTING
;
460 add_object_array_with_path(object
, buf
.buf
, bpi
->pending
, S_IFINVALID
,
462 strbuf_addch(&buf
, '\n');
463 write_or_die(bpi
->fd
, buf
.buf
, buf
.len
);
464 strbuf_release(&buf
);
467 int create_bundle(struct repository
*r
, const char *path
,
468 int argc
, const char **argv
, struct strvec
*pack_options
, int version
)
470 struct lock_file lock
= LOCK_INIT
;
472 int bundle_to_stdout
;
474 struct rev_info revs
, revs_copy
;
475 int min_version
= the_hash_algo
== &hash_algos
[GIT_HASH_SHA1
] ? 2 : 3;
476 struct bundle_prerequisites_info bpi
;
479 bundle_to_stdout
= !strcmp(path
, "-");
480 if (bundle_to_stdout
)
483 bundle_fd
= hold_lock_file_for_update(&lock
, path
,
487 version
= min_version
;
489 if (version
< 2 || version
> 3) {
490 die(_("unsupported bundle version %d"), version
);
491 } else if (version
< min_version
) {
492 die(_("cannot write bundle version %d with algorithm %s"), version
, the_hash_algo
->name
);
493 } else if (version
== 2) {
494 write_or_die(bundle_fd
, v2_bundle_signature
, strlen(v2_bundle_signature
));
496 const char *capability
= "@object-format=";
497 write_or_die(bundle_fd
, v3_bundle_signature
, strlen(v3_bundle_signature
));
498 write_or_die(bundle_fd
, capability
, strlen(capability
));
499 write_or_die(bundle_fd
, the_hash_algo
->name
, strlen(the_hash_algo
->name
));
500 write_or_die(bundle_fd
, "\n", 1);
503 /* init revs to list objects for pack-objects later */
504 save_commit_buffer
= 0;
505 repo_init_revisions(r
, &revs
, NULL
);
507 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
510 error(_("unrecognized argument: %s"), argv
[1]);
514 /* save revs.pending in revs_copy for later use */
515 memcpy(&revs_copy
, &revs
, sizeof(revs
));
516 revs_copy
.pending
.nr
= 0;
517 revs_copy
.pending
.alloc
= 0;
518 revs_copy
.pending
.objects
= NULL
;
519 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
520 struct object_array_entry
*e
= revs
.pending
.objects
+ i
;
522 add_object_array_with_path(e
->item
, e
->name
,
527 /* write prerequisites */
529 if (prepare_revision_walk(&revs
))
530 die("revision walk setup failed");
532 bpi
.pending
= &revs_copy
.pending
;
533 traverse_commit_list(&revs
, write_bundle_prerequisites
, NULL
, &bpi
);
534 object_array_remove_duplicates(&revs_copy
.pending
);
536 /* write bundle refs */
537 ref_count
= write_bundle_refs(bundle_fd
, &revs_copy
);
539 die(_("Refusing to create empty bundle."));
540 else if (ref_count
< 0)
544 if (write_pack_data(bundle_fd
, &revs_copy
, pack_options
))
547 if (!bundle_to_stdout
) {
548 if (commit_lock_file(&lock
))
549 die_errno(_("cannot create '%s'"), path
);
553 rollback_lock_file(&lock
);
557 int unbundle(struct repository
*r
, struct bundle_header
*header
,
558 int bundle_fd
, int flags
)
560 const char *argv_index_pack
[] = {"index-pack",
561 "--fix-thin", "--stdin", NULL
, NULL
};
562 struct child_process ip
= CHILD_PROCESS_INIT
;
564 if (flags
& BUNDLE_VERBOSE
)
565 argv_index_pack
[3] = "-v";
567 if (verify_bundle(r
, header
, 0))
569 ip
.argv
= argv_index_pack
;
573 if (run_command(&ip
))
574 return error(_("index-pack died"));