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