git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / t / helper / test-read-midx.c
| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | |
| 3 | #include "test-tool.h" |
| 4 | #include "hex.h" |
| 5 | #include "midx.h" |
| 6 | #include "repository.h" |
| 7 | #include "odb.h" |
| 8 | #include "odb/source-packed.h" |
| 9 | #include "pack-bitmap.h" |
| 10 | #include "packfile.h" |
| 11 | #include "setup.h" |
| 12 | #include "gettext.h" |
| 13 | #include "pack-revindex.h" |
| 14 | |
| 15 | static struct multi_pack_index *setup_midx(const char *object_dir, |
| 16 | struct odb_source_packed **out) |
| 17 | { |
| 18 | struct odb_source_packed *packed; |
| 19 | struct odb_files_dir *dir; |
| 20 | |
| 21 | setup_git_directory(the_repository); |
| 22 | |
| 23 | dir = odb_source_files_find_dir(the_repository->objects, object_dir); |
| 24 | if (dir) { |
| 25 | packed = dir->packed; |
| 26 | } else { |
| 27 | packed = odb_source_packed_new(the_repository->objects, |
| 28 | object_dir, false); |
| 29 | *out = packed; |
| 30 | } |
| 31 | |
| 32 | return load_multi_pack_index(packed); |
| 33 | } |
| 34 | |
| 35 | static int read_midx_file(const char *object_dir, const char *checksum, |
| 36 | int show_objects) |
| 37 | { |
| 38 | struct odb_source_packed *packed = NULL; |
| 39 | uint32_t i; |
| 40 | struct multi_pack_index *m, *tip; |
| 41 | int ret = 0; |
| 42 | |
| 43 | m = tip = setup_midx(object_dir, &packed); |
| 44 | |
| 45 | if (!m) |
| 46 | return 1; |
| 47 | |
| 48 | if (checksum) { |
| 49 | while (m && strcmp(midx_get_checksum_hex(m), checksum)) |
| 50 | m = m->base_midx; |
| 51 | if (!m) { |
| 52 | ret = error(_("could not find MIDX with checksum %s"), |
| 53 | checksum); |
| 54 | goto out; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | printf("header: %08x %d %d %d %d\n", |
| 59 | m->signature, |
| 60 | m->version, |
| 61 | m->hash_len, |
| 62 | m->num_chunks, |
| 63 | m->num_packs); |
| 64 | |
| 65 | printf("chunks:"); |
| 66 | |
| 67 | if (m->chunk_pack_names) |
| 68 | printf(" pack-names"); |
| 69 | if (m->chunk_oid_fanout) |
| 70 | printf(" oid-fanout"); |
| 71 | if (m->chunk_oid_lookup) |
| 72 | printf(" oid-lookup"); |
| 73 | if (m->chunk_object_offsets) |
| 74 | printf(" object-offsets"); |
| 75 | if (m->chunk_large_offsets) |
| 76 | printf(" large-offsets"); |
| 77 | |
| 78 | printf("\nnum_objects: %d\n", m->num_objects); |
| 79 | |
| 80 | printf("packs:\n"); |
| 81 | for (i = 0; i < m->num_packs; i++) |
| 82 | printf("%s\n", m->pack_names[i]); |
| 83 | |
| 84 | printf("object-dir: %s\n", m->source->base.path); |
| 85 | |
| 86 | if (show_objects) { |
| 87 | struct object_id oid; |
| 88 | struct pack_entry e; |
| 89 | |
| 90 | for (i = 0; i < m->num_objects; i++) { |
| 91 | nth_midxed_object_oid(&oid, m, |
| 92 | i + m->num_objects_in_base); |
| 93 | if (midx_fill_entry(m, &oid, &e, NULL) != |
| 94 | MIDX_FILL_HIT) { |
| 95 | ret = error(_("failed to load pack entry")); |
| 96 | goto out; |
| 97 | } |
| 98 | |
| 99 | printf("%s %"PRIu64"\t%s\n", |
| 100 | oid_to_hex(&oid), e.offset, e.p->pack_name); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | out: |
| 105 | close_midx(tip); |
| 106 | if (packed) |
| 107 | odb_source_free(&packed->base); |
| 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | static int read_midx_checksum(const char *object_dir) |
| 113 | { |
| 114 | struct odb_source_packed *packed = NULL; |
| 115 | struct multi_pack_index *m; |
| 116 | |
| 117 | m = setup_midx(object_dir, &packed); |
| 118 | if (!m) |
| 119 | return 1; |
| 120 | printf("%s\n", midx_get_checksum_hex(m)); |
| 121 | |
| 122 | close_midx(m); |
| 123 | if (packed) |
| 124 | odb_source_free(&packed->base); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static int read_midx_preferred_pack(const char *object_dir) |
| 129 | { |
| 130 | struct odb_source_packed *packed = NULL; |
| 131 | struct multi_pack_index *midx = NULL; |
| 132 | uint32_t preferred_pack; |
| 133 | |
| 134 | midx = setup_midx(object_dir, &packed); |
| 135 | if (!midx) |
| 136 | return 1; |
| 137 | |
| 138 | if (midx_preferred_pack(midx, &preferred_pack) < 0) { |
| 139 | warning(_("could not determine MIDX preferred pack")); |
| 140 | close_midx(midx); |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | printf("%s\n", midx->pack_names[preferred_pack]); |
| 145 | |
| 146 | close_midx(midx); |
| 147 | if (packed) |
| 148 | odb_source_free(&packed->base); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int read_midx_bitmapped_packs(const char *object_dir) |
| 153 | { |
| 154 | struct odb_source_packed *packed = NULL; |
| 155 | struct multi_pack_index *midx = NULL; |
| 156 | struct bitmapped_pack pack; |
| 157 | uint32_t i; |
| 158 | |
| 159 | midx = setup_midx(object_dir, &packed); |
| 160 | if (!midx) |
| 161 | return 1; |
| 162 | |
| 163 | for (i = 0; i < midx->num_packs + midx->num_packs_in_base; i++) { |
| 164 | if (nth_bitmapped_pack(midx, &pack, i) < 0) { |
| 165 | close_midx(midx); |
| 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | printf("%s\n", pack_basename(pack.p)); |
| 170 | printf(" bitmap_pos: %"PRIuMAX"\n", (uintmax_t)pack.bitmap_pos); |
| 171 | printf(" bitmap_nr: %"PRIuMAX"\n", (uintmax_t)pack.bitmap_nr); |
| 172 | } |
| 173 | |
| 174 | close_midx(midx); |
| 175 | if (packed) |
| 176 | odb_source_free(&packed->base); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | int cmd__read_midx(int argc, const char **argv) |
| 181 | { |
| 182 | if (!(argc == 2 || argc == 3 || argc == 4)) |
| 183 | usage("read-midx [--show-objects|--checksum|--preferred-pack|--bitmap] <object-dir> <checksum>"); |
| 184 | |
| 185 | if (!strcmp(argv[1], "--show-objects")) |
| 186 | return read_midx_file(argv[2], argv[3], 1); |
| 187 | else if (!strcmp(argv[1], "--checksum")) |
| 188 | return read_midx_checksum(argv[2]); |
| 189 | else if (!strcmp(argv[1], "--preferred-pack")) |
| 190 | return read_midx_preferred_pack(argv[2]); |
| 191 | else if (!strcmp(argv[1], "--bitmap")) |
| 192 | return read_midx_bitmapped_packs(argv[2]); |
| 193 | return read_midx_file(argv[1], argv[2], 0); |
| 194 | } |