7 #include "parse-options.h"
8 #include "repository.h"
10 static const char *const show_index_usage
[] = {
11 "git show-index [--object-format=<hash-algorithm>]",
15 int cmd_show_index(int argc
, const char **argv
, const char *prefix
)
20 static unsigned int top_index
[256];
22 const char *hash_name
= NULL
;
24 const struct option show_index_options
[] = {
25 OPT_STRING(0, "object-format", &hash_name
, N_("hash-algorithm"),
26 N_("specify the hash algorithm to use")),
30 argc
= parse_options(argc
, argv
, prefix
, show_index_options
, show_index_usage
, 0);
33 hash_algo
= hash_algo_by_name(hash_name
);
34 if (hash_algo
== GIT_HASH_UNKNOWN
)
35 die(_("Unknown hash algorithm"));
36 repo_set_hash_algo(the_repository
, hash_algo
);
39 hashsz
= the_hash_algo
->rawsz
;
41 if (fread(top_index
, 2 * 4, 1, stdin
) != 1)
42 die("unable to read header");
43 if (top_index
[0] == htonl(PACK_IDX_SIGNATURE
)) {
44 version
= ntohl(top_index
[1]);
45 if (version
< 2 || version
> 2)
46 die("unknown index version");
47 if (fread(top_index
, 256 * 4, 1, stdin
) != 1)
48 die("unable to read index");
51 if (fread(&top_index
[2], 254 * 4, 1, stdin
) != 1)
52 die("unable to read index");
55 for (i
= 0; i
< 256; i
++) {
56 unsigned n
= ntohl(top_index
[i
]);
58 die("corrupt index file");
62 for (i
= 0; i
< nr
; i
++) {
63 unsigned int offset
, entry
[(GIT_MAX_RAWSZ
+ 4) / sizeof(unsigned int)];
65 if (fread(entry
, 4 + hashsz
, 1, stdin
) != 1)
66 die("unable to read entry %u/%u", i
, nr
);
67 offset
= ntohl(entry
[0]);
68 printf("%u %s\n", offset
, hash_to_hex((void *)(entry
+1)));
71 unsigned off64_nr
= 0;
77 ALLOC_ARRAY(entries
, nr
);
78 for (i
= 0; i
< nr
; i
++) {
79 if (fread(entries
[i
].oid
.hash
, hashsz
, 1, stdin
) != 1)
80 die("unable to read sha1 %u/%u", i
, nr
);
81 entries
[i
].oid
.algo
= hash_algo_by_ptr(the_hash_algo
);
83 for (i
= 0; i
< nr
; i
++)
84 if (fread(&entries
[i
].crc
, 4, 1, stdin
) != 1)
85 die("unable to read crc %u/%u", i
, nr
);
86 for (i
= 0; i
< nr
; i
++)
87 if (fread(&entries
[i
].off
, 4, 1, stdin
) != 1)
88 die("unable to read 32b offset %u/%u", i
, nr
);
89 for (i
= 0; i
< nr
; i
++) {
91 uint32_t off
= ntohl(entries
[i
].off
);
92 if (!(off
& 0x80000000)) {
96 if ((off
& 0x7fffffff) != off64_nr
)
97 die("inconsistent 64b offset index");
98 if (fread(off64
, 8, 1, stdin
) != 1)
99 die("unable to read 64b offset %u", off64_nr
);
100 offset
= (((uint64_t)ntohl(off64
[0])) << 32) |
104 printf("%" PRIuMAX
" %s (%08"PRIx32
")\n",
106 oid_to_hex(&entries
[i
].oid
),
107 ntohl(entries
[i
].crc
));