git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / repack-filtered.c
| 1 | #include "git-compat-util.h" |
| 2 | #include "repack.h" |
| 3 | #include "repository.h" |
| 4 | #include "run-command.h" |
| 5 | #include "string-list.h" |
| 6 | #include "hex.h" |
| 7 | #include "packfile.h" |
| 8 | #include "list-objects-filter-options.h" |
| 9 | #include "list-objects-filter.h" |
| 10 | #include "odb.h" |
| 11 | #include "promisor-remote.h" |
| 12 | |
| 13 | int write_filtered_pack(const struct write_pack_opts *opts, |
| 14 | struct existing_packs *existing, |
| 15 | struct string_list *names) |
| 16 | { |
| 17 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 18 | struct string_list_item *item; |
| 19 | FILE *in; |
| 20 | int ret; |
| 21 | const char *caret; |
| 22 | const char *pack_prefix = write_pack_opts_pack_prefix(opts); |
| 23 | |
| 24 | prepare_pack_objects(&cmd, opts->po_args, opts->destination); |
| 25 | |
| 26 | strvec_push(&cmd.args, "--stdin-packs"); |
| 27 | |
| 28 | for_each_string_list_item(item, &existing->kept_packs) |
| 29 | strvec_pushf(&cmd.args, "--keep-pack=%s", item->string); |
| 30 | |
| 31 | cmd.in = -1; |
| 32 | |
| 33 | ret = start_command(&cmd); |
| 34 | if (ret) |
| 35 | return ret; |
| 36 | |
| 37 | /* |
| 38 | * Here 'names' contains only the pack(s) that were just |
| 39 | * written, which is exactly the packs we want to keep. Also |
| 40 | * 'existing_kept_packs' already contains the packs in |
| 41 | * 'keep_pack_list'. |
| 42 | */ |
| 43 | in = xfdopen(cmd.in, "w"); |
| 44 | for_each_string_list_item(item, names) |
| 45 | fprintf(in, "^%s-%s.pack\n", pack_prefix, item->string); |
| 46 | for_each_string_list_item(item, &existing->non_kept_packs) |
| 47 | fprintf(in, "%s.pack\n", item->string); |
| 48 | for_each_string_list_item(item, &existing->cruft_packs) |
| 49 | fprintf(in, "%s.pack\n", item->string); |
| 50 | caret = opts->po_args->pack_kept_objects ? "" : "^"; |
| 51 | for_each_string_list_item(item, &existing->kept_packs) |
| 52 | fprintf(in, "%s%s.pack\n", caret, item->string); |
| 53 | fclose(in); |
| 54 | |
| 55 | return finish_pack_objects_cmd(existing->repo->hash_algo, opts, &cmd, |
| 56 | names); |
| 57 | } |
| 58 | |
| 59 | struct collect_cb_data { |
| 60 | struct repository *repo; |
| 61 | struct oidset *set; |
| 62 | }; |
| 63 | |
| 64 | static int collect_promisor_blob(const struct object_id *oid, |
| 65 | struct object_info *oi UNUSED, |
| 66 | void *cb_data) |
| 67 | { |
| 68 | struct collect_cb_data *data = cb_data; |
| 69 | struct object_info info = OBJECT_INFO_INIT; |
| 70 | enum object_type type; |
| 71 | |
| 72 | info.typep = &type; |
| 73 | |
| 74 | /* |
| 75 | * Use OBJECT_INFO_SKIP_FETCH_OBJECT to avoid triggering a |
| 76 | * lazy fetch while collecting promisor blobs. |
| 77 | */ |
| 78 | if (odb_read_object_info_extended(data->repo->objects, oid, &info, |
| 79 | OBJECT_INFO_SKIP_FETCH_OBJECT) < 0) |
| 80 | return 0; |
| 81 | |
| 82 | if (type == OBJ_BLOB) |
| 83 | oidset_insert(data->set, oid); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | int enumerate_promisor_blobs(struct repository *repo, |
| 89 | const struct list_objects_filter_options *filter, |
| 90 | struct oidset *to_drop) |
| 91 | { |
| 92 | struct oidset all_promisor_blobs = OIDSET_INIT; |
| 93 | struct collect_cb_data cb = { |
| 94 | .repo = repo, |
| 95 | .set = &all_promisor_blobs |
| 96 | }; |
| 97 | int ret = 0; |
| 98 | |
| 99 | /* |
| 100 | * The caller (cmd_repack) is responsible for validating that a |
| 101 | * blob:limit filter and a promisor remote are present before |
| 102 | * calling this function. |
| 103 | * |
| 104 | * Walk only promisor objects. Every object visited here is a |
| 105 | * promisor object, so it is recoverable from the promisor remote |
| 106 | * as long as the remote still has it, the same assumption the rest |
| 107 | * of partial clone relies on. |
| 108 | * |
| 109 | * We do not use write_filtered_pack() here because git repack |
| 110 | * routes promisor objects through repack_promisor_objects() |
| 111 | * before the filter machinery runs, so the filtered pack never |
| 112 | * contains promisor blobs. Direct enumeration via |
| 113 | * ODB_FOR_EACH_OBJECT_PROMISOR_ONLY is the correct approach. |
| 114 | */ |
| 115 | ret = odb_for_each_object(repo->objects, NULL, |
| 116 | collect_promisor_blob, &cb, |
| 117 | ODB_FOR_EACH_OBJECT_PROMISOR_ONLY); |
| 118 | if (ret) |
| 119 | goto cleanup; |
| 120 | |
| 121 | /* |
| 122 | * Apply the filter to find which blobs exceed the threshold. |
| 123 | * The caller has to_drop and is responsible for clearing it. |
| 124 | */ |
| 125 | ret = list_objects_filter__filter_oidset(repo, |
| 126 | filter, |
| 127 | &all_promisor_blobs, |
| 128 | to_drop); |
| 129 | |
| 130 | cleanup: |
| 131 | oidset_clear(&all_promisor_blobs); |
| 132 | return ret; |
| 133 | } |