6 #include "parse-options.h"
8 static const char *const show_index_usage
[] = {
9 "git show-index [--object-format=<hash-algorithm>]",
13 int cmd_show_index(int argc
, const char **argv
, const char *prefix
)
18 static unsigned int top_index
[256];
20 const char *hash_name
= NULL
;
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")),
28 argc
= parse_options(argc
, argv
, prefix
, show_index_options
, show_index_usage
, 0);
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
);
37 hashsz
= the_hash_algo
->rawsz
;
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");
49 if (fread(&top_index
[2], 254 * 4, 1, stdin
) != 1)
50 die("unable to read index");
53 for (i
= 0; i
< 256; i
++) {
54 unsigned n
= ntohl(top_index
[i
]);
56 die("corrupt index file");
60 for (i
= 0; i
< nr
; i
++) {
61 unsigned int offset
, entry
[(GIT_MAX_RAWSZ
+ 4) / sizeof(unsigned int)];
63 if (fread(entry
, 4 + hashsz
, 1, stdin
) != 1)
64 die("unable to read entry %u/%u", i
, nr
);
65 offset
= ntohl(entry
[0]);
66 printf("%u %s\n", offset
, hash_to_hex((void *)(entry
+1)));
69 unsigned off64_nr
= 0;
75 ALLOC_ARRAY(entries
, nr
);
76 for (i
= 0; i
< nr
; i
++) {
77 if (fread(entries
[i
].oid
.hash
, hashsz
, 1, stdin
) != 1)
78 die("unable to read sha1 %u/%u", i
, nr
);
79 entries
[i
].oid
.algo
= hash_algo_by_ptr(the_hash_algo
);
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
++) {
89 uint32_t off
= ntohl(entries
[i
].off
);
90 if (!(off
& 0x80000000)) {
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) |
102 printf("%" PRIuMAX
" %s (%08"PRIx32
")\n",
104 oid_to_hex(&entries
[i
].oid
),
105 ntohl(entries
[i
].crc
));