builtin: stop using `the_index`
[alt-git.git] / t / helper / test-read-midx.c
blob4acae41bb993c883970227f394a61603a6e7152d
1 #include "test-tool.h"
2 #include "hex.h"
3 #include "midx.h"
4 #include "repository.h"
5 #include "object-store-ll.h"
6 #include "pack-bitmap.h"
7 #include "packfile.h"
8 #include "setup.h"
9 #include "gettext.h"
11 static int read_midx_file(const char *object_dir, int show_objects)
13 uint32_t i;
14 struct multi_pack_index *m;
16 setup_git_directory();
17 m = load_multi_pack_index(object_dir, 1);
19 if (!m)
20 return 1;
22 printf("header: %08x %d %d %d %d\n",
23 m->signature,
24 m->version,
25 m->hash_len,
26 m->num_chunks,
27 m->num_packs);
29 printf("chunks:");
31 if (m->chunk_pack_names)
32 printf(" pack-names");
33 if (m->chunk_oid_fanout)
34 printf(" oid-fanout");
35 if (m->chunk_oid_lookup)
36 printf(" oid-lookup");
37 if (m->chunk_object_offsets)
38 printf(" object-offsets");
39 if (m->chunk_large_offsets)
40 printf(" large-offsets");
42 printf("\nnum_objects: %d\n", m->num_objects);
44 printf("packs:\n");
45 for (i = 0; i < m->num_packs; i++)
46 printf("%s\n", m->pack_names[i]);
48 printf("object-dir: %s\n", m->object_dir);
50 if (show_objects) {
51 struct object_id oid;
52 struct pack_entry e;
54 for (i = 0; i < m->num_objects; i++) {
55 nth_midxed_object_oid(&oid, m, i);
56 fill_midx_entry(the_repository, &oid, &e, m);
58 printf("%s %"PRIu64"\t%s\n",
59 oid_to_hex(&oid), e.offset, e.p->pack_name);
63 close_midx(m);
65 return 0;
68 static int read_midx_checksum(const char *object_dir)
70 struct multi_pack_index *m;
72 setup_git_directory();
73 m = load_multi_pack_index(object_dir, 1);
74 if (!m)
75 return 1;
76 printf("%s\n", hash_to_hex(get_midx_checksum(m)));
77 return 0;
80 static int read_midx_preferred_pack(const char *object_dir)
82 struct multi_pack_index *midx = NULL;
83 uint32_t preferred_pack;
85 setup_git_directory();
87 midx = load_multi_pack_index(object_dir, 1);
88 if (!midx)
89 return 1;
91 if (midx_preferred_pack(midx, &preferred_pack) < 0) {
92 warning(_("could not determine MIDX preferred pack"));
93 return 1;
96 printf("%s\n", midx->pack_names[preferred_pack]);
97 return 0;
100 static int read_midx_bitmapped_packs(const char *object_dir)
102 struct multi_pack_index *midx = NULL;
103 struct bitmapped_pack pack;
104 uint32_t i;
106 setup_git_directory();
108 midx = load_multi_pack_index(object_dir, 1);
109 if (!midx)
110 return 1;
112 for (i = 0; i < midx->num_packs; i++) {
113 if (nth_bitmapped_pack(the_repository, midx, &pack, i) < 0)
114 return 1;
116 printf("%s\n", pack_basename(pack.p));
117 printf(" bitmap_pos: %"PRIuMAX"\n", (uintmax_t)pack.bitmap_pos);
118 printf(" bitmap_nr: %"PRIuMAX"\n", (uintmax_t)pack.bitmap_nr);
121 close_midx(midx);
123 return 0;
126 int cmd__read_midx(int argc, const char **argv)
128 if (!(argc == 2 || argc == 3))
129 usage("read-midx [--show-objects|--checksum|--preferred-pack|--bitmap] <object-dir>");
131 if (!strcmp(argv[1], "--show-objects"))
132 return read_midx_file(argv[2], 1);
133 else if (!strcmp(argv[1], "--checksum"))
134 return read_midx_checksum(argv[2]);
135 else if (!strcmp(argv[1], "--preferred-pack"))
136 return read_midx_preferred_pack(argv[2]);
137 else if (!strcmp(argv[1], "--bitmap"))
138 return read_midx_bitmapped_packs(argv[2]);
139 return read_midx_file(argv[1], 0);