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