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 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);
38 static int parse_capability(struct bundle_header
*header
, const char *capability
)
41 if (skip_prefix(capability
, "object-format=", &arg
)) {
42 int algo
= hash_algo_by_name(arg
);
43 if (algo
== GIT_HASH_UNKNOWN
)
44 return error(_("unrecognized bundle hash algorithm: %s"), arg
);
45 header
->hash_algo
= &hash_algos
[algo
];
48 return error(_("unknown capability '%s'"), capability
);
51 static int parse_bundle_signature(struct bundle_header
*header
, const char *line
)
55 for (i
= 0; i
< ARRAY_SIZE(bundle_sigs
); i
++) {
56 if (!strcmp(line
, bundle_sigs
[i
].signature
)) {
57 header
->version
= bundle_sigs
[i
].version
;
64 static int parse_bundle_header(int fd
, struct bundle_header
*header
,
65 const char *report_path
)
67 struct strbuf buf
= STRBUF_INIT
;
70 /* The bundle header begins with the signature */
71 if (strbuf_getwholeline_fd(&buf
, fd
, '\n') ||
72 parse_bundle_signature(header
, buf
.buf
)) {
74 error(_("'%s' does not look like a v2 or v3 bundle file"),
80 header
->hash_algo
= the_hash_algo
;
82 /* The bundle header ends with an empty line */
83 while (!strbuf_getwholeline_fd(&buf
, fd
, '\n') &&
84 buf
.len
&& buf
.buf
[0] != '\n') {
91 if (header
->version
== 3 && *buf
.buf
== '@') {
92 if (parse_capability(header
, buf
.buf
+ 1)) {
99 if (*buf
.buf
== '-') {
101 strbuf_remove(&buf
, 0, 1);
105 * Tip lines have object name, SP, and refname.
106 * Prerequisites have object name that is optionally
107 * followed by SP and subject line.
109 if (parse_oid_hex_algop(buf
.buf
, &oid
, &p
, header
->hash_algo
) ||
110 (*p
&& !isspace(*p
)) ||
111 (!is_prereq
&& !*p
)) {
113 error(_("unrecognized header: %s%s (%d)"),
114 (is_prereq
? "-" : ""), buf
.buf
, (int)buf
.len
);
118 struct object_id
*dup
= oiddup(&oid
);
120 string_list_append(&header
->prerequisites
, "")->util
= dup
;
122 string_list_append(&header
->references
, p
+ 1)->util
= dup
;
131 strbuf_release(&buf
);
135 int read_bundle_header(const char *path
, struct bundle_header
*header
)
137 int fd
= open(path
, O_RDONLY
);
140 return error(_("could not open '%s'"), path
);
141 return parse_bundle_header(fd
, header
, path
);
144 int is_bundle(const char *path
, int quiet
)
146 struct bundle_header header
= BUNDLE_HEADER_INIT
;
147 int fd
= open(path
, O_RDONLY
);
151 fd
= parse_bundle_header(fd
, &header
, quiet
? NULL
: path
);
154 bundle_header_release(&header
);
158 static int list_refs(struct string_list
*r
, int argc
, const char **argv
)
162 for (i
= 0; i
< r
->nr
; i
++) {
163 struct object_id
*oid
;
168 for (j
= 1; j
< argc
; j
++)
169 if (!strcmp(r
->items
[i
].string
, argv
[j
]))
175 oid
= r
->items
[i
].util
;
176 name
= r
->items
[i
].string
;
177 printf("%s %s\n", oid_to_hex(oid
), name
);
182 /* Remember to update object flag allocation in object.h */
183 #define PREREQ_MARK (1u<<16)
185 int verify_bundle(struct repository
*r
,
186 struct bundle_header
*header
,
190 * Do fast check, then if any prereqs are missing then go line by line
191 * to be verbose about the errors
193 struct string_list
*p
= &header
->prerequisites
;
194 struct rev_info revs
;
195 const char *argv
[] = {NULL
, "--all", NULL
};
196 struct commit
*commit
;
197 int i
, ret
= 0, req_nr
;
198 const char *message
= _("Repository lacks these prerequisite commits:");
200 if (!r
|| !r
->objects
|| !r
->objects
->odb
)
201 return error(_("need a repository to verify a bundle"));
203 repo_init_revisions(r
, &revs
, NULL
);
204 for (i
= 0; i
< p
->nr
; i
++) {
205 struct string_list_item
*e
= p
->items
+ i
;
206 const char *name
= e
->string
;
207 struct object_id
*oid
= e
->util
;
208 struct object
*o
= parse_object(r
, oid
);
210 o
->flags
|= PREREQ_MARK
;
211 add_pending_object(&revs
, o
, name
);
215 error("%s", message
);
216 error("%s %s", oid_to_hex(oid
), name
);
218 if (revs
.pending
.nr
!= p
->nr
)
220 req_nr
= revs
.pending
.nr
;
221 setup_revisions(2, argv
, &revs
, NULL
);
223 if (prepare_revision_walk(&revs
))
224 die(_("revision walk setup failed"));
227 while (i
&& (commit
= get_revision(&revs
)))
228 if (commit
->object
.flags
& PREREQ_MARK
)
231 for (i
= 0; i
< p
->nr
; i
++) {
232 struct string_list_item
*e
= p
->items
+ i
;
233 const char *name
= e
->string
;
234 const struct object_id
*oid
= e
->util
;
235 struct object
*o
= parse_object(r
, oid
);
236 assert(o
); /* otherwise we'd have returned early */
237 if (o
->flags
& SHOWN
)
240 error("%s", message
);
241 error("%s %s", oid_to_hex(oid
), name
);
244 /* Clean up objects used, as they will be reused. */
245 for (i
= 0; i
< p
->nr
; i
++) {
246 struct string_list_item
*e
= p
->items
+ i
;
247 struct object_id
*oid
= e
->util
;
248 commit
= lookup_commit_reference_gently(r
, oid
, 1);
250 clear_commit_marks(commit
, ALL_REV_FLAGS
);
254 struct string_list
*r
;
256 r
= &header
->references
;
257 printf_ln(Q_("The bundle contains this ref:",
258 "The bundle contains these %d refs:",
261 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 %d refs:",
270 list_refs(r
, 0, NULL
);
276 int list_bundle_refs(struct bundle_header
*header
, int argc
, const char **argv
)
278 return list_refs(&header
->references
, argc
, argv
);
281 static int is_tag_in_date_range(struct object
*tag
, struct rev_info
*revs
)
284 enum object_type type
;
285 char *buf
= NULL
, *line
, *lineend
;
289 if (revs
->max_age
== -1 && revs
->min_age
== -1)
292 buf
= read_object_file(&tag
->oid
, &type
, &size
);
295 line
= memmem(buf
, size
, "\ntagger ", 8);
298 lineend
= memchr(line
, '\n', buf
+ size
- line
);
299 line
= memchr(line
, '>', lineend
? lineend
- line
: buf
+ size
- line
);
302 date
= parse_timestamp(line
, NULL
, 10);
303 result
= (revs
->max_age
== -1 || revs
->max_age
< date
) &&
304 (revs
->min_age
== -1 || revs
->min_age
> date
);
311 /* Write the pack data to bundle_fd */
312 static int write_pack_data(int bundle_fd
, struct rev_info
*revs
, struct strvec
*pack_options
)
314 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
317 strvec_pushl(&pack_objects
.args
,
319 "--stdout", "--thin", "--delta-base-offset",
321 strvec_pushv(&pack_objects
.args
, pack_options
->v
);
322 pack_objects
.in
= -1;
323 pack_objects
.out
= bundle_fd
;
324 pack_objects
.git_cmd
= 1;
327 * start_command() will close our descriptor if it's >1. Duplicate it
328 * to avoid surprising the caller.
330 if (pack_objects
.out
> 1) {
331 pack_objects
.out
= dup(pack_objects
.out
);
332 if (pack_objects
.out
< 0) {
333 error_errno(_("unable to dup bundle descriptor"));
334 child_process_clear(&pack_objects
);
339 if (start_command(&pack_objects
))
340 return error(_("Could not spawn pack-objects"));
342 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
343 struct object
*object
= revs
->pending
.objects
[i
].item
;
344 if (object
->flags
& UNINTERESTING
)
345 write_or_die(pack_objects
.in
, "^", 1);
346 write_or_die(pack_objects
.in
, oid_to_hex(&object
->oid
), the_hash_algo
->hexsz
);
347 write_or_die(pack_objects
.in
, "\n", 1);
349 close(pack_objects
.in
);
350 if (finish_command(&pack_objects
))
351 return error(_("pack-objects died"));
356 * Write out bundle refs based on the tips already
357 * parsed into revs.pending. As a side effect, may
358 * manipulate revs.pending to include additional
359 * necessary objects (like tags).
361 * Returns the number of refs written, or negative
364 static int write_bundle_refs(int bundle_fd
, struct rev_info
*revs
)
369 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
370 struct object_array_entry
*e
= revs
->pending
.objects
+ i
;
371 struct object_id oid
;
373 const char *display_ref
;
376 if (e
->item
->flags
& UNINTERESTING
)
378 if (dwim_ref(e
->name
, strlen(e
->name
), &oid
, &ref
, 0) != 1)
380 if (read_ref_full(e
->name
, RESOLVE_REF_READING
, &oid
, &flag
))
382 display_ref
= (flag
& REF_ISSYMREF
) ? e
->name
: ref
;
384 if (e
->item
->type
== OBJ_TAG
&&
385 !is_tag_in_date_range(e
->item
, revs
)) {
386 e
->item
->flags
|= UNINTERESTING
;
391 * Make sure the refs we wrote out is correct; --max-count and
392 * other limiting options could have prevented all the tips
393 * from getting output.
395 * Non commit objects such as tags and blobs do not have
396 * this issue as they are not affected by those extra
399 if (!(e
->item
->flags
& SHOWN
) && e
->item
->type
== OBJ_COMMIT
) {
400 warning(_("ref '%s' is excluded by the rev-list options"),
405 * If you run "git bundle create bndl v1.0..v2.0", the
406 * name of the positive ref is "v2.0" but that is the
407 * commit that is referenced by the tag, and not the tag
410 if (!oideq(&oid
, &e
->item
->oid
)) {
412 * Is this the positive end of a range expressed
413 * in terms of a tag (e.g. v2.0 from the range
416 struct commit
*one
= lookup_commit_reference(revs
->repo
, &oid
);
419 if (e
->item
== &(one
->object
)) {
421 * Need to include e->name as an
422 * independent ref to the pack-objects
423 * input, so that the tag is included
424 * in the output; otherwise we would
425 * end up triggering "empty bundle"
428 obj
= parse_object_or_die(&oid
, e
->name
);
430 add_pending_object(revs
, obj
, e
->name
);
436 write_or_die(bundle_fd
, oid_to_hex(&e
->item
->oid
), the_hash_algo
->hexsz
);
437 write_or_die(bundle_fd
, " ", 1);
438 write_or_die(bundle_fd
, display_ref
, strlen(display_ref
));
439 write_or_die(bundle_fd
, "\n", 1);
445 write_or_die(bundle_fd
, "\n", 1);
449 struct bundle_prerequisites_info
{
450 struct object_array
*pending
;
454 static void write_bundle_prerequisites(struct commit
*commit
, void *data
)
456 struct bundle_prerequisites_info
*bpi
= data
;
457 struct object
*object
;
458 struct pretty_print_context ctx
= { 0 };
459 struct strbuf buf
= STRBUF_INIT
;
461 if (!(commit
->object
.flags
& BOUNDARY
))
463 strbuf_addf(&buf
, "-%s ", oid_to_hex(&commit
->object
.oid
));
464 write_or_die(bpi
->fd
, buf
.buf
, buf
.len
);
466 ctx
.fmt
= CMIT_FMT_ONELINE
;
467 ctx
.output_encoding
= get_log_output_encoding();
469 pretty_print_commit(&ctx
, commit
, &buf
);
472 object
= (struct object
*)commit
;
473 object
->flags
|= UNINTERESTING
;
474 add_object_array_with_path(object
, buf
.buf
, bpi
->pending
, S_IFINVALID
,
476 strbuf_addch(&buf
, '\n');
477 write_or_die(bpi
->fd
, buf
.buf
, buf
.len
);
478 strbuf_release(&buf
);
481 int create_bundle(struct repository
*r
, const char *path
,
482 int argc
, const char **argv
, struct strvec
*pack_options
, int version
)
484 struct lock_file lock
= LOCK_INIT
;
486 int bundle_to_stdout
;
488 struct rev_info revs
, revs_copy
;
489 int min_version
= the_hash_algo
== &hash_algos
[GIT_HASH_SHA1
] ? 2 : 3;
490 struct bundle_prerequisites_info bpi
;
493 bundle_to_stdout
= !strcmp(path
, "-");
494 if (bundle_to_stdout
)
497 bundle_fd
= hold_lock_file_for_update(&lock
, path
,
501 version
= min_version
;
503 if (version
< 2 || version
> 3) {
504 die(_("unsupported bundle version %d"), version
);
505 } else if (version
< min_version
) {
506 die(_("cannot write bundle version %d with algorithm %s"), version
, the_hash_algo
->name
);
507 } else if (version
== 2) {
508 write_or_die(bundle_fd
, v2_bundle_signature
, strlen(v2_bundle_signature
));
510 const char *capability
= "@object-format=";
511 write_or_die(bundle_fd
, v3_bundle_signature
, strlen(v3_bundle_signature
));
512 write_or_die(bundle_fd
, capability
, strlen(capability
));
513 write_or_die(bundle_fd
, the_hash_algo
->name
, strlen(the_hash_algo
->name
));
514 write_or_die(bundle_fd
, "\n", 1);
517 /* init revs to list objects for pack-objects later */
518 save_commit_buffer
= 0;
519 repo_init_revisions(r
, &revs
, NULL
);
521 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
524 error(_("unrecognized argument: %s"), argv
[1]);
528 /* save revs.pending in revs_copy for later use */
529 memcpy(&revs_copy
, &revs
, sizeof(revs
));
530 revs_copy
.pending
.nr
= 0;
531 revs_copy
.pending
.alloc
= 0;
532 revs_copy
.pending
.objects
= NULL
;
533 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
534 struct object_array_entry
*e
= revs
.pending
.objects
+ i
;
536 add_object_array_with_path(e
->item
, e
->name
,
541 /* write prerequisites */
543 if (prepare_revision_walk(&revs
))
544 die("revision walk setup failed");
546 bpi
.pending
= &revs_copy
.pending
;
547 traverse_commit_list(&revs
, write_bundle_prerequisites
, NULL
, &bpi
);
548 object_array_remove_duplicates(&revs_copy
.pending
);
550 /* write bundle refs */
551 ref_count
= write_bundle_refs(bundle_fd
, &revs_copy
);
553 die(_("Refusing to create empty bundle."));
554 else if (ref_count
< 0)
558 if (write_pack_data(bundle_fd
, &revs_copy
, pack_options
))
561 if (!bundle_to_stdout
) {
562 if (commit_lock_file(&lock
))
563 die_errno(_("cannot create '%s'"), path
);
567 rollback_lock_file(&lock
);
571 int unbundle(struct repository
*r
, struct bundle_header
*header
,
572 int bundle_fd
, struct strvec
*extra_index_pack_args
)
574 struct child_process ip
= CHILD_PROCESS_INIT
;
575 strvec_pushl(&ip
.args
, "index-pack", "--fix-thin", "--stdin", NULL
);
577 if (extra_index_pack_args
) {
578 strvec_pushv(&ip
.args
, extra_index_pack_args
->v
);
579 strvec_clear(extra_index_pack_args
);
582 if (verify_bundle(r
, header
, 0))
587 if (run_command(&ip
))
588 return error(_("index-pack died"));