]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/show-index.c
The eighth batch
[thirdparty/git.git] / builtin / show-index.c
CommitLineData
ff417260 1#include "builtin.h"
f394e093 2#include "gettext.h"
d1cbe1e6 3#include "hash.h"
41771fa4 4#include "hex.h"
32637cdf 5#include "pack.h"
88a09a55 6#include "parse-options.h"
d1cbe1e6 7#include "repository.h"
0271611e 8
88a09a55 9static const char *const show_index_usage[] = {
10 "git show-index [--object-format=<hash-algorithm>]",
11 NULL
12};
03c5c102 13
ff417260 14int cmd_show_index(int argc, const char **argv, const char *prefix)
0271611e
LT
15{
16 int i;
17 unsigned nr;
32637cdf 18 unsigned int version;
0271611e 19 static unsigned int top_index[256];
88a09a55 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()
27 };
28
29 argc = parse_options(argc, argv, prefix, show_index_options, show_index_usage, 0);
30
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);
36 }
37
38 hashsz = the_hash_algo->rawsz;
0271611e 39
32637cdf
NP
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");
52 }
0271611e
LT
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;
59 }
32637cdf
NP
60 if (version == 1) {
61 for (i = 0; i < nr; i++) {
7962e046 62 unsigned int offset, entry[(GIT_MAX_RAWSZ + 4) / sizeof(unsigned int)];
0271611e 63
7962e046 64 if (fread(entry, 4 + hashsz, 1, stdin) != 1)
32637cdf
NP
65 die("unable to read entry %u/%u", i, nr);
66 offset = ntohl(entry[0]);
be8e172e 67 printf("%u %s\n", offset, hash_to_hex((void *)(entry+1)));
32637cdf
NP
68 }
69 } else {
70 unsigned off64_nr = 0;
71 struct {
7962e046 72 struct object_id oid;
32637cdf
NP
73 uint32_t crc;
74 uint32_t off;
b32fa95f
JK
75 } *entries;
76 ALLOC_ARRAY(entries, nr);
dd15f4f4 77 for (i = 0; i < nr; i++) {
7962e046 78 if (fread(entries[i].oid.hash, hashsz, 1, stdin) != 1)
32637cdf 79 die("unable to read sha1 %u/%u", i, nr);
dd15f4f4 80 entries[i].oid.algo = hash_algo_by_ptr(the_hash_algo);
81 }
32637cdf
NP
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++;
102 }
6e1c2344
RJ
103 printf("%" PRIuMAX " %s (%08"PRIx32")\n",
104 (uintmax_t) offset,
7962e046 105 oid_to_hex(&entries[i].oid),
32637cdf
NP
106 ntohl(entries[i].crc));
107 }
108 free(entries);
0271611e
LT
109 }
110 return 0;
111}