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