]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-read-midx.c
Merge branch 'es/test-cron-safety'
[thirdparty/git.git] / t / helper / test-read-midx.c
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"
10
11 static int read_midx_file(const char *object_dir, int show_objects)
12 {
13 uint32_t i;
14 struct multi_pack_index *m;
15
16 setup_git_directory();
17 m = load_multi_pack_index(object_dir, 1);
18
19 if (!m)
20 return 1;
21
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);
28
29 printf("chunks:");
30
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");
41
42 printf("\nnum_objects: %d\n", m->num_objects);
43
44 printf("packs:\n");
45 for (i = 0; i < m->num_packs; i++)
46 printf("%s\n", m->pack_names[i]);
47
48 printf("object-dir: %s\n", m->object_dir);
49
50 if (show_objects) {
51 struct object_id oid;
52 struct pack_entry e;
53
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);
57
58 printf("%s %"PRIu64"\t%s\n",
59 oid_to_hex(&oid), e.offset, e.p->pack_name);
60 }
61 }
62
63 close_midx(m);
64
65 return 0;
66 }
67
68 static int read_midx_checksum(const char *object_dir)
69 {
70 struct multi_pack_index *m;
71
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;
78 }
79
80 static int read_midx_preferred_pack(const char *object_dir)
81 {
82 struct multi_pack_index *midx = NULL;
83 uint32_t preferred_pack;
84
85 setup_git_directory();
86
87 midx = load_multi_pack_index(object_dir, 1);
88 if (!midx)
89 return 1;
90
91 if (midx_preferred_pack(midx, &preferred_pack) < 0) {
92 warning(_("could not determine MIDX preferred pack"));
93 return 1;
94 }
95
96 printf("%s\n", midx->pack_names[preferred_pack]);
97 return 0;
98 }
99
100 static int read_midx_bitmapped_packs(const char *object_dir)
101 {
102 struct multi_pack_index *midx = NULL;
103 struct bitmapped_pack pack;
104 uint32_t i;
105
106 setup_git_directory();
107
108 midx = load_multi_pack_index(object_dir, 1);
109 if (!midx)
110 return 1;
111
112 for (i = 0; i < midx->num_packs; i++) {
113 if (nth_bitmapped_pack(the_repository, midx, &pack, i) < 0)
114 return 1;
115
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);
119 }
120
121 close_midx(midx);
122
123 return 0;
124 }
125
126 int cmd__read_midx(int argc, const char **argv)
127 {
128 if (!(argc == 2 || argc == 3))
129 usage("read-midx [--show-objects|--checksum|--preferred-pack|--bitmap] <object-dir>");
130
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);
140 }