at commit f1d88b96
| 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_source *source; |
| 20 | |
| 21 | setup_git_directory(the_repository); |
| 22 | |
| 23 | source = odb_find_source(the_repository->objects, object_dir); |
| 24 | if (source) { |
| 25 | packed = odb_source_files_downcast(source)->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 | fill_midx_entry(m, &oid, &e, NULL); |
| 94 | |
| 95 | printf("%s %"PRIu64"\t%s\n", |
| 96 | oid_to_hex(&oid), e.offset, e.p->pack_name); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | out: |
| 101 | close_midx(tip); |
| 102 | if (packed) |
| 103 | odb_source_free(&packed->base); |
| 104 | |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | static int read_midx_checksum(const char *object_dir) |
| 109 | { |
| 110 | struct odb_source_packed *packed = NULL; |
| 111 | struct multi_pack_index *m; |
| 112 | |
| 113 | m = setup_midx(object_dir, &packed); |
| 114 | if (!m) |
| 115 | return 1; |
| 116 | printf("%s\n", midx_get_checksum_hex(m)); |
| 117 | |
| 118 | close_midx(m); |
| 119 | if (packed) |
| 120 | odb_source_free(&packed->base); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int read_midx_preferred_pack(const char *object_dir) |
| 125 | { |
| 126 | struct odb_source_packed *packed = NULL; |
| 127 | struct multi_pack_index *midx = NULL; |
| 128 | uint32_t preferred_pack; |
| 129 | |
| 130 | midx = setup_midx(object_dir, &packed); |
| 131 | if (!midx) |
| 132 | return 1; |
| 133 | |
| 134 | if (midx_preferred_pack(midx, &preferred_pack) < 0) { |
| 135 | warning(_("could not determine MIDX preferred pack")); |
| 136 | close_midx(midx); |
| 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | printf("%s\n", midx->pack_names[preferred_pack]); |
| 141 | |
| 142 | close_midx(midx); |
| 143 | if (packed) |
| 144 | odb_source_free(&packed->base); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int read_midx_bitmapped_packs(const char *object_dir) |
| 149 | { |
| 150 | struct odb_source_packed *packed = NULL; |
| 151 | struct multi_pack_index *midx = NULL; |
| 152 | struct bitmapped_pack pack; |
| 153 | uint32_t i; |
| 154 | |
| 155 | midx = setup_midx(object_dir, &packed); |
| 156 | if (!midx) |
| 157 | return 1; |
| 158 | |
| 159 | for (i = 0; i < midx->num_packs + midx->num_packs_in_base; i++) { |
| 160 | if (nth_bitmapped_pack(midx, &pack, i) < 0) { |
| 161 | close_midx(midx); |
| 162 | return 1; |
| 163 | } |
| 164 | |
| 165 | printf("%s\n", pack_basename(pack.p)); |
| 166 | printf(" bitmap_pos: %"PRIuMAX"\n", (uintmax_t)pack.bitmap_pos); |
| 167 | printf(" bitmap_nr: %"PRIuMAX"\n", (uintmax_t)pack.bitmap_nr); |
| 168 | } |
| 169 | |
| 170 | close_midx(midx); |
| 171 | if (packed) |
| 172 | odb_source_free(&packed->base); |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | int cmd__read_midx(int argc, const char **argv) |
| 177 | { |
| 178 | if (!(argc == 2 || argc == 3 || argc == 4)) |
| 179 | usage("read-midx [--show-objects|--checksum|--preferred-pack|--bitmap] <object-dir> <checksum>"); |
| 180 | |
| 181 | if (!strcmp(argv[1], "--show-objects")) |
| 182 | return read_midx_file(argv[2], argv[3], 1); |
| 183 | else if (!strcmp(argv[1], "--checksum")) |
| 184 | return read_midx_checksum(argv[2]); |
| 185 | else if (!strcmp(argv[1], "--preferred-pack")) |
| 186 | return read_midx_preferred_pack(argv[2]); |
| 187 | else if (!strcmp(argv[1], "--bitmap")) |
| 188 | return read_midx_bitmapped_packs(argv[2]); |
| 189 | return read_midx_file(argv[1], argv[2], 0); |
| 190 | } |