blob: 27072ba94d76005b6f49e04f60f2e697efd0e073 [file] [log] [blame]
Derrick Stolee4d805602018-07-12 15:39:23 -04001#include "test-tool.h"
2#include "cache.h"
3#include "midx.h"
4#include "repository.h"
5#include "object-store.h"
Taylor Blau6d08b9d2021-09-28 21:55:20 -04006#include "pack-bitmap.h"
Derrick Stolee4d805602018-07-12 15:39:23 -04007
Taylor Blau86d174b2021-03-30 11:04:07 -04008static int read_midx_file(const char *object_dir, int show_objects)
Derrick Stolee4d805602018-07-12 15:39:23 -04009{
Derrick Stolee32275652018-07-12 15:39:28 -040010 uint32_t i;
Derrick Stoleed9607542020-08-17 14:04:48 +000011 struct multi_pack_index *m;
12
13 setup_git_directory();
14 m = load_multi_pack_index(object_dir, 1);
Derrick Stolee4d805602018-07-12 15:39:23 -040015
16 if (!m)
17 return 1;
18
Derrick Stoleed9607542020-08-17 14:04:48 +000019 printf("header: %08x %d %d %d %d\n",
Derrick Stolee4d805602018-07-12 15:39:23 -040020 m->signature,
21 m->version,
Derrick Stoleed9607542020-08-17 14:04:48 +000022 m->hash_len,
Derrick Stolee4d805602018-07-12 15:39:23 -040023 m->num_chunks,
24 m->num_packs);
25
Derrick Stolee32f3c542018-07-12 15:39:27 -040026 printf("chunks:");
27
28 if (m->chunk_pack_names)
29 printf(" pack-names");
Derrick Stoleed7cacf22018-07-12 15:39:31 -040030 if (m->chunk_oid_fanout)
31 printf(" oid-fanout");
Derrick Stolee0d5b3a52018-07-12 15:39:30 -040032 if (m->chunk_oid_lookup)
33 printf(" oid-lookup");
Derrick Stolee662148c2018-07-12 15:39:32 -040034 if (m->chunk_object_offsets)
35 printf(" object-offsets");
36 if (m->chunk_large_offsets)
37 printf(" large-offsets");
Derrick Stolee32f3c542018-07-12 15:39:27 -040038
Derrick Stoleed7cacf22018-07-12 15:39:31 -040039 printf("\nnum_objects: %d\n", m->num_objects);
Derrick Stolee32f3c542018-07-12 15:39:27 -040040
Derrick Stolee32275652018-07-12 15:39:28 -040041 printf("packs:\n");
42 for (i = 0; i < m->num_packs; i++)
43 printf("%s\n", m->pack_names[i]);
44
Derrick Stolee4d805602018-07-12 15:39:23 -040045 printf("object-dir: %s\n", m->object_dir);
46
Taylor Blau86d174b2021-03-30 11:04:07 -040047 if (show_objects) {
48 struct object_id oid;
49 struct pack_entry e;
50
51 for (i = 0; i < m->num_objects; i++) {
52 nth_midxed_object_oid(&oid, m, i);
53 fill_midx_entry(the_repository, &oid, &e, m);
54
55 printf("%s %"PRIu64"\t%s\n",
56 oid_to_hex(&oid), e.offset, e.p->pack_name);
57 }
Taylor Blau86d174b2021-03-30 11:04:07 -040058 }
59
Taylor Blau7f4c3502021-10-26 17:01:11 -040060 close_midx(m);
61
Derrick Stolee4d805602018-07-12 15:39:23 -040062 return 0;
63}
64
Taylor Blaub1b82d12021-08-31 16:52:28 -040065static int read_midx_checksum(const char *object_dir)
66{
67 struct multi_pack_index *m;
68
69 setup_git_directory();
70 m = load_multi_pack_index(object_dir, 1);
71 if (!m)
72 return 1;
73 printf("%s\n", hash_to_hex(get_midx_checksum(m)));
74 return 0;
75}
76
Taylor Blau6d08b9d2021-09-28 21:55:20 -040077static int read_midx_preferred_pack(const char *object_dir)
78{
79 struct multi_pack_index *midx = NULL;
80 struct bitmap_index *bitmap = NULL;
81
82 setup_git_directory();
83
84 midx = load_multi_pack_index(object_dir, 1);
85 if (!midx)
86 return 1;
87
88 bitmap = prepare_bitmap_git(the_repository);
Jeff Kinge861b092021-10-06 22:24:40 -040089 if (!bitmap)
Taylor Blau6d08b9d2021-09-28 21:55:20 -040090 return 1;
Jeff Kinge861b092021-10-06 22:24:40 -040091 if (!bitmap_is_midx(bitmap)) {
92 free_bitmap_index(bitmap);
93 return 1;
94 }
Taylor Blau6d08b9d2021-09-28 21:55:20 -040095
96 printf("%s\n", midx->pack_names[midx_preferred_pack(bitmap)]);
Jeff Kinge861b092021-10-06 22:24:40 -040097 free_bitmap_index(bitmap);
Taylor Blau6d08b9d2021-09-28 21:55:20 -040098 return 0;
99}
100
Derrick Stolee4d805602018-07-12 15:39:23 -0400101int cmd__read_midx(int argc, const char **argv)
102{
Taylor Blau86d174b2021-03-30 11:04:07 -0400103 if (!(argc == 2 || argc == 3))
Taylor Blau6d08b9d2021-09-28 21:55:20 -0400104 usage("read-midx [--show-objects|--checksum|--preferred-pack] <object-dir>");
Derrick Stolee4d805602018-07-12 15:39:23 -0400105
Taylor Blau86d174b2021-03-30 11:04:07 -0400106 if (!strcmp(argv[1], "--show-objects"))
107 return read_midx_file(argv[2], 1);
Taylor Blaub1b82d12021-08-31 16:52:28 -0400108 else if (!strcmp(argv[1], "--checksum"))
109 return read_midx_checksum(argv[2]);
Taylor Blau6d08b9d2021-09-28 21:55:20 -0400110 else if (!strcmp(argv[1], "--preferred-pack"))
111 return read_midx_preferred_pack(argv[2]);
Taylor Blau86d174b2021-03-30 11:04:07 -0400112 return read_midx_file(argv[1], 0);
Derrick Stolee4d805602018-07-12 15:39:23 -0400113}