Git 2.46
[git.git] / bundle.c
blobce164c37bc890c2dcbf13f01fa7f0b558164b2e4
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "lockfile.h"
5 #include "bundle.h"
6 #include "environment.h"
7 #include "gettext.h"
8 #include "hex.h"
9 #include "object-store-ll.h"
10 #include "repository.h"
11 #include "object.h"
12 #include "commit.h"
13 #include "diff.h"
14 #include "revision.h"
15 #include "list-objects.h"
16 #include "run-command.h"
17 #include "refs.h"
18 #include "strvec.h"
19 #include "list-objects-filter-options.h"
20 #include "connected.h"
21 #include "write-or-die.h"
23 static const char v2_bundle_signature[] = "# v2 git bundle\n";
24 static const char v3_bundle_signature[] = "# v3 git bundle\n";
25 static struct {
26 int version;
27 const char *signature;
28 } bundle_sigs[] = {
29 { 2, v2_bundle_signature },
30 { 3, v3_bundle_signature },
33 void bundle_header_init(struct bundle_header *header)
35 struct bundle_header blank = BUNDLE_HEADER_INIT;
36 memcpy(header, &blank, sizeof(*header));
39 void bundle_header_release(struct bundle_header *header)
41 string_list_clear(&header->prerequisites, 1);
42 string_list_clear(&header->references, 1);
43 list_objects_filter_release(&header->filter);
46 static int parse_capability(struct bundle_header *header, const char *capability)
48 const char *arg;
49 if (skip_prefix(capability, "object-format=", &arg)) {
50 int algo = hash_algo_by_name(arg);
51 if (algo == GIT_HASH_UNKNOWN)
52 return error(_("unrecognized bundle hash algorithm: %s"), arg);
53 header->hash_algo = &hash_algos[algo];
54 return 0;
56 if (skip_prefix(capability, "filter=", &arg)) {
57 parse_list_objects_filter(&header->filter, arg);
58 return 0;
60 return error(_("unknown capability '%s'"), capability);
63 static int parse_bundle_signature(struct bundle_header *header, const char *line)
65 int i;
67 for (i = 0; i < ARRAY_SIZE(bundle_sigs); i++) {
68 if (!strcmp(line, bundle_sigs[i].signature)) {
69 header->version = bundle_sigs[i].version;
70 return 0;
73 return -1;
76 int read_bundle_header_fd(int fd, struct bundle_header *header,
77 const char *report_path)
79 struct strbuf buf = STRBUF_INIT;
80 int status = 0;
82 /* The bundle header begins with the signature */
83 if (strbuf_getwholeline_fd(&buf, fd, '\n') ||
84 parse_bundle_signature(header, buf.buf)) {
85 if (report_path)
86 error(_("'%s' does not look like a v2 or v3 bundle file"),
87 report_path);
88 status = -1;
89 goto abort;
92 header->hash_algo = the_hash_algo;
94 /* The bundle header ends with an empty line */
95 while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&
96 buf.len && buf.buf[0] != '\n') {
97 struct object_id oid;
98 int is_prereq = 0;
99 const char *p;
101 strbuf_rtrim(&buf);
103 if (header->version == 3 && *buf.buf == '@') {
104 if (parse_capability(header, buf.buf + 1)) {
105 status = -1;
106 break;
108 continue;
111 if (*buf.buf == '-') {
112 is_prereq = 1;
113 strbuf_remove(&buf, 0, 1);
117 * Tip lines have object name, SP, and refname.
118 * Prerequisites have object name that is optionally
119 * followed by SP and subject line.
121 if (parse_oid_hex_algop(buf.buf, &oid, &p, header->hash_algo) ||
122 (*p && !isspace(*p)) ||
123 (!is_prereq && !*p)) {
124 if (report_path)
125 error(_("unrecognized header: %s%s (%d)"),
126 (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
127 status = -1;
128 break;
129 } else {
130 struct object_id *dup = oiddup(&oid);
131 if (is_prereq)
132 string_list_append(&header->prerequisites, "")->util = dup;
133 else
134 string_list_append(&header->references, p + 1)->util = dup;
138 abort:
139 if (status) {
140 close(fd);
141 fd = -1;
143 strbuf_release(&buf);
144 return fd;
147 int read_bundle_header(const char *path, struct bundle_header *header)
149 int fd = open(path, O_RDONLY);
151 if (fd < 0)
152 return error(_("could not open '%s'"), path);
153 return read_bundle_header_fd(fd, header, path);
156 int is_bundle(const char *path, int quiet)
158 struct bundle_header header = BUNDLE_HEADER_INIT;
159 int fd = open(path, O_RDONLY);
161 if (fd < 0)
162 return 0;
163 fd = read_bundle_header_fd(fd, &header, quiet ? NULL : path);
164 if (fd >= 0)
165 close(fd);
166 bundle_header_release(&header);
167 return (fd >= 0);
170 static int list_refs(struct string_list *r, int argc, const char **argv)
172 int i;
174 for (i = 0; i < r->nr; i++) {
175 struct object_id *oid;
176 const char *name;
178 if (argc > 1) {
179 int j;
180 for (j = 1; j < argc; j++)
181 if (!strcmp(r->items[i].string, argv[j]))
182 break;
183 if (j == argc)
184 continue;
187 oid = r->items[i].util;
188 name = r->items[i].string;
189 printf("%s %s\n", oid_to_hex(oid), name);
191 return 0;
194 /* Remember to update object flag allocation in object.h */
195 #define PREREQ_MARK (1u<<16)
197 struct string_list_iterator {
198 struct string_list *list;
199 size_t cur;
202 static const struct object_id *iterate_ref_map(void *cb_data)
204 struct string_list_iterator *iter = cb_data;
206 if (iter->cur >= iter->list->nr)
207 return NULL;
209 return iter->list->items[iter->cur++].util;
212 int verify_bundle(struct repository *r,
213 struct bundle_header *header,
214 enum verify_bundle_flags flags)
217 * Do fast check, then if any prereqs are missing then go line by line
218 * to be verbose about the errors
220 struct string_list *p = &header->prerequisites;
221 int i, ret = 0;
222 const char *message = _("Repository lacks these prerequisite commits:");
223 struct string_list_iterator iter = {
224 .list = p,
226 struct check_connected_options opts = {
227 .quiet = 1,
230 if (!r || !r->objects || !r->objects->odb)
231 return error(_("need a repository to verify a bundle"));
233 for (i = 0; i < p->nr; i++) {
234 struct string_list_item *e = p->items + i;
235 const char *name = e->string;
236 struct object_id *oid = e->util;
237 struct object *o = parse_object(r, oid);
238 if (o)
239 continue;
240 ret++;
241 if (flags & VERIFY_BUNDLE_QUIET)
242 continue;
243 if (ret == 1)
244 error("%s", message);
245 error("%s %s", oid_to_hex(oid), name);
247 if (ret)
248 goto cleanup;
250 if ((ret = check_connected(iterate_ref_map, &iter, &opts)))
251 error(_("some prerequisite commits exist in the object store, "
252 "but are not connected to the repository's history"));
254 /* TODO: preserve this verbose language. */
255 if (flags & VERIFY_BUNDLE_VERBOSE) {
256 struct string_list *r;
258 r = &header->references;
259 printf_ln(Q_("The bundle contains this ref:",
260 "The bundle contains these %"PRIuMAX" refs:",
261 r->nr),
262 (uintmax_t)r->nr);
263 list_refs(r, 0, NULL);
265 r = &header->prerequisites;
266 if (!r->nr) {
267 printf_ln(_("The bundle records a complete history."));
268 } else {
269 printf_ln(Q_("The bundle requires this ref:",
270 "The bundle requires these %"PRIuMAX" refs:",
271 r->nr),
272 (uintmax_t)r->nr);
273 list_refs(r, 0, NULL);
276 printf_ln(_("The bundle uses this hash algorithm: %s"),
277 header->hash_algo->name);
278 if (header->filter.choice)
279 printf_ln(_("The bundle uses this filter: %s"),
280 list_objects_filter_spec(&header->filter));
282 cleanup:
283 return ret;
286 int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
288 return list_refs(&header->references, argc, argv);
291 static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
293 unsigned long size;
294 enum object_type type;
295 char *buf = NULL, *line, *lineend;
296 timestamp_t date;
297 int result = 1;
299 if (revs->max_age == -1 && revs->min_age == -1)
300 goto out;
302 buf = repo_read_object_file(the_repository, &tag->oid, &type, &size);
303 if (!buf)
304 goto out;
305 line = memmem(buf, size, "\ntagger ", 8);
306 if (!line++)
307 goto out;
308 lineend = memchr(line, '\n', buf + size - line);
309 line = memchr(line, '>', lineend ? lineend - line : buf + size - line);
310 if (!line++)
311 goto out;
312 date = parse_timestamp(line, NULL, 10);
313 result = (revs->max_age == -1 || revs->max_age < date) &&
314 (revs->min_age == -1 || revs->min_age > date);
315 out:
316 free(buf);
317 return result;
321 /* Write the pack data to bundle_fd */
322 static int write_pack_data(int bundle_fd, struct rev_info *revs, struct strvec *pack_options)
324 struct child_process pack_objects = CHILD_PROCESS_INIT;
325 int i;
327 strvec_pushl(&pack_objects.args,
328 "pack-objects",
329 "--stdout", "--thin", "--delta-base-offset",
330 NULL);
331 strvec_pushv(&pack_objects.args, pack_options->v);
332 if (revs->filter.choice)
333 strvec_pushf(&pack_objects.args, "--filter=%s",
334 list_objects_filter_spec(&revs->filter));
335 pack_objects.in = -1;
336 pack_objects.out = bundle_fd;
337 pack_objects.git_cmd = 1;
340 * start_command() will close our descriptor if it's >1. Duplicate it
341 * to avoid surprising the caller.
343 if (pack_objects.out > 1) {
344 pack_objects.out = dup(pack_objects.out);
345 if (pack_objects.out < 0) {
346 error_errno(_("unable to dup bundle descriptor"));
347 child_process_clear(&pack_objects);
348 return -1;
352 if (start_command(&pack_objects))
353 return error(_("Could not spawn pack-objects"));
355 for (i = 0; i < revs->pending.nr; i++) {
356 struct object *object = revs->pending.objects[i].item;
357 if (object->flags & UNINTERESTING)
358 write_or_die(pack_objects.in, "^", 1);
359 write_or_die(pack_objects.in, oid_to_hex(&object->oid), the_hash_algo->hexsz);
360 write_or_die(pack_objects.in, "\n", 1);
362 close(pack_objects.in);
363 if (finish_command(&pack_objects))
364 return error(_("pack-objects died"));
365 return 0;
369 * Write out bundle refs based on the tips already
370 * parsed into revs.pending. As a side effect, may
371 * manipulate revs.pending to include additional
372 * necessary objects (like tags).
374 * Returns the number of refs written, or negative
375 * on error.
377 static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
379 int i;
380 int ref_count = 0;
382 for (i = 0; i < revs->pending.nr; i++) {
383 struct object_array_entry *e = revs->pending.objects + i;
384 struct object_id oid;
385 char *ref;
386 const char *display_ref;
387 int flag;
389 if (e->item->flags & UNINTERESTING)
390 continue;
391 if (repo_dwim_ref(the_repository, e->name, strlen(e->name),
392 &oid, &ref, 0) != 1)
393 goto skip_write_ref;
394 if (refs_read_ref_full(get_main_ref_store(the_repository), e->name, RESOLVE_REF_READING, &oid, &flag))
395 flag = 0;
396 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
398 if (e->item->type == OBJ_TAG &&
399 !is_tag_in_date_range(e->item, revs)) {
400 e->item->flags |= UNINTERESTING;
401 goto skip_write_ref;
405 * Make sure the refs we wrote out is correct; --max-count and
406 * other limiting options could have prevented all the tips
407 * from getting output.
409 * Non commit objects such as tags and blobs do not have
410 * this issue as they are not affected by those extra
411 * constraints.
413 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
414 warning(_("ref '%s' is excluded by the rev-list options"),
415 e->name);
416 goto skip_write_ref;
419 * If you run "git bundle create bndl v1.0..v2.0", the
420 * name of the positive ref is "v2.0" but that is the
421 * commit that is referenced by the tag, and not the tag
422 * itself.
424 if (!oideq(&oid, &e->item->oid)) {
426 * Is this the positive end of a range expressed
427 * in terms of a tag (e.g. v2.0 from the range
428 * "v1.0..v2.0")?
430 struct commit *one = lookup_commit_reference(revs->repo, &oid);
431 struct object *obj;
433 if (e->item == &(one->object)) {
435 * Need to include e->name as an
436 * independent ref to the pack-objects
437 * input, so that the tag is included
438 * in the output; otherwise we would
439 * end up triggering "empty bundle"
440 * error.
442 obj = parse_object_or_die(&oid, e->name);
443 obj->flags |= SHOWN;
444 add_pending_object(revs, obj, e->name);
446 goto skip_write_ref;
449 ref_count++;
450 write_or_die(bundle_fd, oid_to_hex(&e->item->oid), the_hash_algo->hexsz);
451 write_or_die(bundle_fd, " ", 1);
452 write_or_die(bundle_fd, display_ref, strlen(display_ref));
453 write_or_die(bundle_fd, "\n", 1);
454 skip_write_ref:
455 free(ref);
458 /* end header */
459 write_or_die(bundle_fd, "\n", 1);
460 return ref_count;
463 struct bundle_prerequisites_info {
464 struct object_array *pending;
465 int fd;
468 static void write_bundle_prerequisites(struct commit *commit, void *data)
470 struct bundle_prerequisites_info *bpi = data;
471 struct object *object;
472 struct pretty_print_context ctx = { 0 };
473 struct strbuf buf = STRBUF_INIT;
475 if (!(commit->object.flags & BOUNDARY))
476 return;
477 strbuf_addf(&buf, "-%s ", oid_to_hex(&commit->object.oid));
478 write_or_die(bpi->fd, buf.buf, buf.len);
480 ctx.fmt = CMIT_FMT_ONELINE;
481 ctx.output_encoding = get_log_output_encoding();
482 strbuf_reset(&buf);
483 pretty_print_commit(&ctx, commit, &buf);
484 strbuf_trim(&buf);
486 object = (struct object *)commit;
487 object->flags |= UNINTERESTING;
488 add_object_array_with_path(object, buf.buf, bpi->pending, S_IFINVALID,
489 NULL);
490 strbuf_addch(&buf, '\n');
491 write_or_die(bpi->fd, buf.buf, buf.len);
492 strbuf_release(&buf);
495 int create_bundle(struct repository *r, const char *path,
496 int argc, const char **argv, struct strvec *pack_options, int version)
498 struct lock_file lock = LOCK_INIT;
499 int bundle_fd = -1;
500 int bundle_to_stdout;
501 int ref_count = 0;
502 struct rev_info revs, revs_copy;
503 int min_version = 2;
504 struct bundle_prerequisites_info bpi;
505 int ret;
506 int i;
508 /* init revs to list objects for pack-objects later */
509 save_commit_buffer = 0;
510 repo_init_revisions(r, &revs, NULL);
513 * Pre-initialize the '--objects' flag so we can parse a
514 * --filter option successfully.
516 revs.tree_objects = revs.blob_objects = 1;
518 argc = setup_revisions(argc, argv, &revs, NULL);
521 * Reasons to require version 3:
523 * 1. @object-format is required because our hash algorithm is not
524 * SHA1.
525 * 2. @filter is required because we parsed an object filter.
527 if (the_hash_algo != &hash_algos[GIT_HASH_SHA1] || revs.filter.choice)
528 min_version = 3;
530 if (argc > 1) {
531 ret = error(_("unrecognized argument: %s"), argv[1]);
532 goto out;
535 bundle_to_stdout = !strcmp(path, "-");
536 if (bundle_to_stdout)
537 bundle_fd = 1;
538 else
539 bundle_fd = hold_lock_file_for_update(&lock, path,
540 LOCK_DIE_ON_ERROR);
542 if (version == -1)
543 version = min_version;
545 if (version < 2 || version > 3) {
546 die(_("unsupported bundle version %d"), version);
547 } else if (version < min_version) {
548 die(_("cannot write bundle version %d with algorithm %s"), version, the_hash_algo->name);
549 } else if (version == 2) {
550 write_or_die(bundle_fd, v2_bundle_signature, strlen(v2_bundle_signature));
551 } else {
552 const char *capability = "@object-format=";
553 write_or_die(bundle_fd, v3_bundle_signature, strlen(v3_bundle_signature));
554 write_or_die(bundle_fd, capability, strlen(capability));
555 write_or_die(bundle_fd, the_hash_algo->name, strlen(the_hash_algo->name));
556 write_or_die(bundle_fd, "\n", 1);
558 if (revs.filter.choice) {
559 const char *value = expand_list_objects_filter_spec(&revs.filter);
560 capability = "@filter=";
561 write_or_die(bundle_fd, capability, strlen(capability));
562 write_or_die(bundle_fd, value, strlen(value));
563 write_or_die(bundle_fd, "\n", 1);
567 /* save revs.pending in revs_copy for later use */
568 memcpy(&revs_copy, &revs, sizeof(revs));
569 revs_copy.pending.nr = 0;
570 revs_copy.pending.alloc = 0;
571 revs_copy.pending.objects = NULL;
572 for (i = 0; i < revs.pending.nr; i++) {
573 struct object_array_entry *e = revs.pending.objects + i;
574 if (e)
575 add_object_array_with_path(e->item, e->name,
576 &revs_copy.pending,
577 e->mode, e->path);
580 /* write prerequisites */
581 revs.boundary = 1;
582 if (prepare_revision_walk(&revs))
583 die("revision walk setup failed");
584 bpi.fd = bundle_fd;
585 bpi.pending = &revs_copy.pending;
588 * Remove any object walking here. We only care about commits and
589 * tags here. The revs_copy has the right instances of these values.
591 revs.blob_objects = revs.tree_objects = 0;
592 traverse_commit_list(&revs, write_bundle_prerequisites, NULL, &bpi);
593 object_array_remove_duplicates(&revs_copy.pending);
595 /* write bundle refs */
596 ref_count = write_bundle_refs(bundle_fd, &revs_copy);
597 if (!ref_count) {
598 die(_("Refusing to create empty bundle."));
599 } else if (ref_count < 0) {
600 ret = -1;
601 goto out;
604 /* write pack */
605 if (write_pack_data(bundle_fd, &revs_copy, pack_options)) {
606 ret = -1;
607 goto out;
610 if (!bundle_to_stdout) {
611 if (commit_lock_file(&lock))
612 die_errno(_("cannot create '%s'"), path);
615 ret = 0;
617 out:
618 object_array_clear(&revs_copy.pending);
619 release_revisions(&revs);
620 rollback_lock_file(&lock);
621 return ret;
624 int unbundle(struct repository *r, struct bundle_header *header,
625 int bundle_fd, struct strvec *extra_index_pack_args,
626 enum verify_bundle_flags flags)
628 struct child_process ip = CHILD_PROCESS_INIT;
630 if (verify_bundle(r, header, flags))
631 return -1;
633 strvec_pushl(&ip.args, "index-pack", "--fix-thin", "--stdin", NULL);
635 /* If there is a filter, then we need to create the promisor pack. */
636 if (header->filter.choice)
637 strvec_push(&ip.args, "--promisor=from-bundle");
639 if (flags & VERIFY_BUNDLE_FSCK)
640 strvec_push(&ip.args, "--fsck-objects");
642 if (extra_index_pack_args) {
643 strvec_pushv(&ip.args, extra_index_pack_args->v);
644 strvec_clear(extra_index_pack_args);
647 ip.in = bundle_fd;
648 ip.no_stdout = 1;
649 ip.git_cmd = 1;
650 if (run_command(&ip))
651 return error(_("index-pack died"));
652 return 0;