]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/show-index.c
Merge branch 'fc/remove-header-workarounds-for-asciidoc'
[thirdparty/git.git] / builtin / show-index.c
CommitLineData
ff417260 1#include "builtin.h"
0271611e 2#include "cache.h"
f394e093 3#include "gettext.h"
41771fa4 4#include "hex.h"
32637cdf 5#include "pack.h"
88a09a55 6#include "parse-options.h"
0271611e 7
88a09a55 8static const char *const show_index_usage[] = {
9 "git show-index [--object-format=<hash-algorithm>]",
10 NULL
11};
03c5c102 12
ff417260 13int cmd_show_index(int argc, const char **argv, const char *prefix)
0271611e
LT
14{
15 int i;
16 unsigned nr;
32637cdf 17 unsigned int version;
0271611e 18 static unsigned int top_index[256];
88a09a55 19 unsigned hashsz;
20 const char *hash_name = NULL;
21 int hash_algo;
22 const struct option show_index_options[] = {
23 OPT_STRING(0, "object-format", &hash_name, N_("hash-algorithm"),
24 N_("specify the hash algorithm to use")),
25 OPT_END()
26 };
27
28 argc = parse_options(argc, argv, prefix, show_index_options, show_index_usage, 0);
29
30 if (hash_name) {
31 hash_algo = hash_algo_by_name(hash_name);
32 if (hash_algo == GIT_HASH_UNKNOWN)
33 die(_("Unknown hash algorithm"));
34 repo_set_hash_algo(the_repository, hash_algo);
35 }
36
37 hashsz = the_hash_algo->rawsz;
0271611e 38
32637cdf
NP
39 if (fread(top_index, 2 * 4, 1, stdin) != 1)
40 die("unable to read header");
41 if (top_index[0] == htonl(PACK_IDX_SIGNATURE)) {
42 version = ntohl(top_index[1]);
43 if (version < 2 || version > 2)
44 die("unknown index version");
45 if (fread(top_index, 256 * 4, 1, stdin) != 1)
46 die("unable to read index");
47 } else {
48 version = 1;
49 if (fread(&top_index[2], 254 * 4, 1, stdin) != 1)
50 die("unable to read index");
51 }
0271611e
LT
52 nr = 0;
53 for (i = 0; i < 256; i++) {
54 unsigned n = ntohl(top_index[i]);
55 if (n < nr)
56 die("corrupt index file");
57 nr = n;
58 }
32637cdf
NP
59 if (version == 1) {
60 for (i = 0; i < nr; i++) {
7962e046 61 unsigned int offset, entry[(GIT_MAX_RAWSZ + 4) / sizeof(unsigned int)];
0271611e 62
7962e046 63 if (fread(entry, 4 + hashsz, 1, stdin) != 1)
32637cdf
NP
64 die("unable to read entry %u/%u", i, nr);
65 offset = ntohl(entry[0]);
be8e172e 66 printf("%u %s\n", offset, hash_to_hex((void *)(entry+1)));
32637cdf
NP
67 }
68 } else {
69 unsigned off64_nr = 0;
70 struct {
7962e046 71 struct object_id oid;
32637cdf
NP
72 uint32_t crc;
73 uint32_t off;
b32fa95f
JK
74 } *entries;
75 ALLOC_ARRAY(entries, nr);
dd15f4f4 76 for (i = 0; i < nr; i++) {
7962e046 77 if (fread(entries[i].oid.hash, hashsz, 1, stdin) != 1)
32637cdf 78 die("unable to read sha1 %u/%u", i, nr);
dd15f4f4 79 entries[i].oid.algo = hash_algo_by_ptr(the_hash_algo);
80 }
32637cdf
NP
81 for (i = 0; i < nr; i++)
82 if (fread(&entries[i].crc, 4, 1, stdin) != 1)
83 die("unable to read crc %u/%u", i, nr);
84 for (i = 0; i < nr; i++)
85 if (fread(&entries[i].off, 4, 1, stdin) != 1)
86 die("unable to read 32b offset %u/%u", i, nr);
87 for (i = 0; i < nr; i++) {
88 uint64_t offset;
89 uint32_t off = ntohl(entries[i].off);
90 if (!(off & 0x80000000)) {
91 offset = off;
92 } else {
93 uint32_t off64[2];
94 if ((off & 0x7fffffff) != off64_nr)
95 die("inconsistent 64b offset index");
96 if (fread(off64, 8, 1, stdin) != 1)
97 die("unable to read 64b offset %u", off64_nr);
98 offset = (((uint64_t)ntohl(off64[0])) << 32) |
99 ntohl(off64[1]);
100 off64_nr++;
101 }
6e1c2344
RJ
102 printf("%" PRIuMAX " %s (%08"PRIx32")\n",
103 (uintmax_t) offset,
7962e046 104 oid_to_hex(&entries[i].oid),
32637cdf
NP
105 ntohl(entries[i].crc));
106 }
107 free(entries);
0271611e
LT
108 }
109 return 0;
110}