cache.h: remove this no-longer-used header
[alt-git.git] / builtin / show-index.c
blob540dc3dad1c1e3e4308c5058151dfb1dcaee6d21
1 #include "builtin.h"
2 #include "gettext.h"
3 #include "hash.h"
4 #include "hex.h"
5 #include "pack.h"
6 #include "parse-options.h"
7 #include "repository.h"
9 static const char *const show_index_usage[] = {
10 "git show-index [--object-format=<hash-algorithm>]",
11 NULL
14 int cmd_show_index(int argc, const char **argv, const char *prefix)
16 int i;
17 unsigned nr;
18 unsigned int version;
19 static unsigned int top_index[256];
20 unsigned hashsz;
21 const char *hash_name = NULL;
22 int hash_algo;
23 const struct option show_index_options[] = {
24 OPT_STRING(0, "object-format", &hash_name, N_("hash-algorithm"),
25 N_("specify the hash algorithm to use")),
26 OPT_END()
29 argc = parse_options(argc, argv, prefix, show_index_options, show_index_usage, 0);
31 if (hash_name) {
32 hash_algo = hash_algo_by_name(hash_name);
33 if (hash_algo == GIT_HASH_UNKNOWN)
34 die(_("Unknown hash algorithm"));
35 repo_set_hash_algo(the_repository, hash_algo);
38 hashsz = the_hash_algo->rawsz;
40 if (fread(top_index, 2 * 4, 1, stdin) != 1)
41 die("unable to read header");
42 if (top_index[0] == htonl(PACK_IDX_SIGNATURE)) {
43 version = ntohl(top_index[1]);
44 if (version < 2 || version > 2)
45 die("unknown index version");
46 if (fread(top_index, 256 * 4, 1, stdin) != 1)
47 die("unable to read index");
48 } else {
49 version = 1;
50 if (fread(&top_index[2], 254 * 4, 1, stdin) != 1)
51 die("unable to read index");
53 nr = 0;
54 for (i = 0; i < 256; i++) {
55 unsigned n = ntohl(top_index[i]);
56 if (n < nr)
57 die("corrupt index file");
58 nr = n;
60 if (version == 1) {
61 for (i = 0; i < nr; i++) {
62 unsigned int offset, entry[(GIT_MAX_RAWSZ + 4) / sizeof(unsigned int)];
64 if (fread(entry, 4 + hashsz, 1, stdin) != 1)
65 die("unable to read entry %u/%u", i, nr);
66 offset = ntohl(entry[0]);
67 printf("%u %s\n", offset, hash_to_hex((void *)(entry+1)));
69 } else {
70 unsigned off64_nr = 0;
71 struct {
72 struct object_id oid;
73 uint32_t crc;
74 uint32_t off;
75 } *entries;
76 ALLOC_ARRAY(entries, nr);
77 for (i = 0; i < nr; i++) {
78 if (fread(entries[i].oid.hash, hashsz, 1, stdin) != 1)
79 die("unable to read sha1 %u/%u", i, nr);
80 entries[i].oid.algo = hash_algo_by_ptr(the_hash_algo);
82 for (i = 0; i < nr; i++)
83 if (fread(&entries[i].crc, 4, 1, stdin) != 1)
84 die("unable to read crc %u/%u", i, nr);
85 for (i = 0; i < nr; i++)
86 if (fread(&entries[i].off, 4, 1, stdin) != 1)
87 die("unable to read 32b offset %u/%u", i, nr);
88 for (i = 0; i < nr; i++) {
89 uint64_t offset;
90 uint32_t off = ntohl(entries[i].off);
91 if (!(off & 0x80000000)) {
92 offset = off;
93 } else {
94 uint32_t off64[2];
95 if ((off & 0x7fffffff) != off64_nr)
96 die("inconsistent 64b offset index");
97 if (fread(off64, 8, 1, stdin) != 1)
98 die("unable to read 64b offset %u", off64_nr);
99 offset = (((uint64_t)ntohl(off64[0])) << 32) |
100 ntohl(off64[1]);
101 off64_nr++;
103 printf("%" PRIuMAX " %s (%08"PRIx32")\n",
104 (uintmax_t) offset,
105 oid_to_hex(&entries[i].oid),
106 ntohl(entries[i].crc));
108 free(entries);
110 return 0;