|
| 1 | +#define USE_THE_REPOSITORY_VARIABLE /* for core_apply_sparse_checkout */ |
| 2 | + |
| 3 | +#include "builtin.h" |
| 4 | +#include "git-compat-util.h" |
| 5 | +#include "config.h" |
| 6 | +#include "parse-options.h" |
| 7 | +#include "repository.h" |
| 8 | +#include "commit.h" |
| 9 | +#include "dir.h" |
| 10 | +#include "environment.h" |
| 11 | +#include "hex.h" |
| 12 | +#include "tree.h" |
| 13 | +#include "tree-walk.h" |
| 14 | +#include "object.h" |
| 15 | +#include "object-store-ll.h" |
| 16 | +#include "oid-array.h" |
| 17 | +#include "oidset.h" |
| 18 | +#include "promisor-remote.h" |
| 19 | +#include "strmap.h" |
| 20 | +#include "string-list.h" |
| 21 | +#include "revision.h" |
| 22 | +#include "trace2.h" |
| 23 | +#include "progress.h" |
| 24 | +#include "packfile.h" |
| 25 | +#include "path-walk.h" |
| 26 | + |
| 27 | +static const char * const builtin_backfill_usage[] = { |
| 28 | + N_("(EXPERIMENTAL) git backfill [--batch-size=<n>] [--[no-]sparse]"), |
| 29 | + NULL |
| 30 | +}; |
| 31 | + |
| 32 | +struct backfill_context { |
| 33 | + struct repository *repo; |
| 34 | + struct oid_array current_batch; |
| 35 | + size_t batch_size; |
| 36 | + int sparse; |
| 37 | +}; |
| 38 | + |
| 39 | +static void clear_backfill_context(struct backfill_context *ctx) |
| 40 | +{ |
| 41 | + oid_array_clear(&ctx->current_batch); |
| 42 | +} |
| 43 | + |
| 44 | +static void download_batch(struct backfill_context *ctx) |
| 45 | +{ |
| 46 | + promisor_remote_get_direct(ctx->repo, |
| 47 | + ctx->current_batch.oid, |
| 48 | + ctx->current_batch.nr); |
| 49 | + oid_array_clear(&ctx->current_batch); |
| 50 | + |
| 51 | + /* |
| 52 | + * We likely have a new packfile. Add it to the packed list to |
| 53 | + * avoid possible duplicate downloads of the same objects. |
| 54 | + */ |
| 55 | + reprepare_packed_git(ctx->repo); |
| 56 | +} |
| 57 | + |
| 58 | +static int fill_missing_blobs(const char *path UNUSED, |
| 59 | + struct oid_array *list, |
| 60 | + enum object_type type, |
| 61 | + void *data) |
| 62 | +{ |
| 63 | + struct backfill_context *ctx = data; |
| 64 | + |
| 65 | + if (type != OBJ_BLOB) |
| 66 | + return 0; |
| 67 | + |
| 68 | + for (size_t i = 0; i < list->nr; i++) { |
| 69 | + off_t size = 0; |
| 70 | + struct object_info info = OBJECT_INFO_INIT; |
| 71 | + info.disk_sizep = &size; |
| 72 | + if (oid_object_info_extended(ctx->repo, |
| 73 | + &list->oid[i], |
| 74 | + &info, |
| 75 | + OBJECT_INFO_FOR_PREFETCH) || |
| 76 | + !size) |
| 77 | + oid_array_append(&ctx->current_batch, &list->oid[i]); |
| 78 | + } |
| 79 | + |
| 80 | + if (ctx->current_batch.nr >= ctx->batch_size) |
| 81 | + download_batch(ctx); |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +static int do_backfill(struct backfill_context *ctx) |
| 87 | +{ |
| 88 | + struct rev_info revs; |
| 89 | + struct path_walk_info info = PATH_WALK_INFO_INIT; |
| 90 | + int ret; |
| 91 | + |
| 92 | + if (ctx->sparse) { |
| 93 | + CALLOC_ARRAY(info.pl, 1); |
| 94 | + if (get_sparse_checkout_patterns(info.pl)) { |
| 95 | + clear_pattern_list(info.pl); |
| 96 | + free(info.pl); |
| 97 | + return error(_("problem loading sparse-checkout")); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + repo_init_revisions(ctx->repo, &revs, ""); |
| 102 | + handle_revision_arg("HEAD", &revs, 0, 0); |
| 103 | + |
| 104 | + info.blobs = 1; |
| 105 | + info.tags = info.commits = info.trees = 0; |
| 106 | + |
| 107 | + info.revs = &revs; |
| 108 | + info.path_fn = fill_missing_blobs; |
| 109 | + info.path_fn_data = ctx; |
| 110 | + |
| 111 | + ret = walk_objects_by_path(&info); |
| 112 | + |
| 113 | + /* Download the objects that did not fill a batch. */ |
| 114 | + if (!ret) |
| 115 | + download_batch(ctx); |
| 116 | + |
| 117 | + clear_backfill_context(ctx); |
| 118 | + release_revisions(&revs); |
| 119 | + if (info.pl) { |
| 120 | + clear_pattern_list(info.pl); |
| 121 | + free(info.pl); |
| 122 | + } |
| 123 | + return ret; |
| 124 | +} |
| 125 | + |
| 126 | +int cmd_backfill(int argc, const char **argv, const char *prefix, struct repository *repo) |
| 127 | +{ |
| 128 | + struct backfill_context ctx = { |
| 129 | + .repo = repo, |
| 130 | + .current_batch = OID_ARRAY_INIT, |
| 131 | + .batch_size = 50000, |
| 132 | + .sparse = 0, |
| 133 | + }; |
| 134 | + struct option options[] = { |
| 135 | + OPT_INTEGER(0, "batch-size", &ctx.batch_size, |
| 136 | + N_("Minimun number of objects to request at a time")), |
| 137 | + OPT_BOOL(0, "sparse", &ctx.sparse, |
| 138 | + N_("Restrict the missing objects to the current sparse-checkout")), |
| 139 | + OPT_END(), |
| 140 | + }; |
| 141 | + |
| 142 | + if (argc == 2 && !strcmp(argv[1], "-h")) |
| 143 | + usage_with_options(builtin_backfill_usage, options); |
| 144 | + |
| 145 | + argc = parse_options(argc, argv, prefix, options, builtin_backfill_usage, |
| 146 | + 0); |
| 147 | + |
| 148 | + repo_config(repo, git_default_config, NULL); |
| 149 | + |
| 150 | + if (ctx.sparse < 0) |
| 151 | + ctx.sparse = core_apply_sparse_checkout; |
| 152 | + |
| 153 | + return do_backfill(&ctx); |
| 154 | +} |
0 commit comments