]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-read-midx.c
Merge branch 'fc/remove-header-workarounds-for-asciidoc'
[thirdparty/git.git] / t / helper / test-read-midx.c
CommitLineData
4d80560c
DS
1#include "test-tool.h"
2#include "cache.h"
41771fa4 3#include "hex.h"
4d80560c
DS
4#include "midx.h"
5#include "repository.h"
6#include "object-store.h"
6d08b9d4 7#include "pack-bitmap.h"
e38da487 8#include "setup.h"
4d80560c 9
86d174b7 10static int read_midx_file(const char *object_dir, int show_objects)
4d80560c 11{
3227565c 12 uint32_t i;
d9607542
DS
13 struct multi_pack_index *m;
14
15 setup_git_directory();
16 m = load_multi_pack_index(object_dir, 1);
4d80560c
DS
17
18 if (!m)
19 return 1;
20
d9607542 21 printf("header: %08x %d %d %d %d\n",
4d80560c
DS
22 m->signature,
23 m->version,
d9607542 24 m->hash_len,
4d80560c
DS
25 m->num_chunks,
26 m->num_packs);
27
32f3c541
DS
28 printf("chunks:");
29
30 if (m->chunk_pack_names)
31 printf(" pack-names");
d7cacf29
DS
32 if (m->chunk_oid_fanout)
33 printf(" oid-fanout");
0d5b3a5e
DS
34 if (m->chunk_oid_lookup)
35 printf(" oid-lookup");
662148c4
DS
36 if (m->chunk_object_offsets)
37 printf(" object-offsets");
38 if (m->chunk_large_offsets)
39 printf(" large-offsets");
32f3c541 40
d7cacf29 41 printf("\nnum_objects: %d\n", m->num_objects);
32f3c541 42
3227565c
DS
43 printf("packs:\n");
44 for (i = 0; i < m->num_packs; i++)
45 printf("%s\n", m->pack_names[i]);
46
4d80560c
DS
47 printf("object-dir: %s\n", m->object_dir);
48
86d174b7
TB
49 if (show_objects) {
50 struct object_id oid;
51 struct pack_entry e;
52
53 for (i = 0; i < m->num_objects; i++) {
54 nth_midxed_object_oid(&oid, m, i);
55 fill_midx_entry(the_repository, &oid, &e, m);
56
57 printf("%s %"PRIu64"\t%s\n",
58 oid_to_hex(&oid), e.offset, e.p->pack_name);
59 }
86d174b7
TB
60 }
61
7f4c3508
TB
62 close_midx(m);
63
4d80560c
DS
64 return 0;
65}
66
b1b82d1c
TB
67static int read_midx_checksum(const char *object_dir)
68{
69 struct multi_pack_index *m;
70
71 setup_git_directory();
72 m = load_multi_pack_index(object_dir, 1);
73 if (!m)
74 return 1;
75 printf("%s\n", hash_to_hex(get_midx_checksum(m)));
76 return 0;
77}
78
6d08b9d4
TB
79static int read_midx_preferred_pack(const char *object_dir)
80{
81 struct multi_pack_index *midx = NULL;
82 struct bitmap_index *bitmap = NULL;
83
84 setup_git_directory();
85
86 midx = load_multi_pack_index(object_dir, 1);
87 if (!midx)
88 return 1;
89
90 bitmap = prepare_bitmap_git(the_repository);
e861b096 91 if (!bitmap)
6d08b9d4 92 return 1;
e861b096
JK
93 if (!bitmap_is_midx(bitmap)) {
94 free_bitmap_index(bitmap);
95 return 1;
96 }
6d08b9d4
TB
97
98 printf("%s\n", midx->pack_names[midx_preferred_pack(bitmap)]);
e861b096 99 free_bitmap_index(bitmap);
6d08b9d4
TB
100 return 0;
101}
102
4d80560c
DS
103int cmd__read_midx(int argc, const char **argv)
104{
86d174b7 105 if (!(argc == 2 || argc == 3))
6d08b9d4 106 usage("read-midx [--show-objects|--checksum|--preferred-pack] <object-dir>");
4d80560c 107
86d174b7
TB
108 if (!strcmp(argv[1], "--show-objects"))
109 return read_midx_file(argv[2], 1);
b1b82d1c
TB
110 else if (!strcmp(argv[1], "--checksum"))
111 return read_midx_checksum(argv[2]);
6d08b9d4
TB
112 else if (!strcmp(argv[1], "--preferred-pack"))
113 return read_midx_preferred_pack(argv[2]);
86d174b7 114 return read_midx_file(argv[1], 0);
4d80560c 115}