4 #include "parse-options.h"
6 static const char *const show_index_usage
[] = {
7 "git show-index [--object-format=<hash-algorithm>]",
11 int cmd_show_index(int argc
, const char **argv
, const char *prefix
)
16 static unsigned int top_index
[256];
18 const char *hash_name
= NULL
;
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")),
26 argc
= parse_options(argc
, argv
, prefix
, show_index_options
, show_index_usage
, 0);
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");
47 if (fread(&top_index
[2], 254 * 4, 1, stdin
) != 1)
48 die("unable to read index");
51 for (i
= 0; i
< 256; i
++) {
52 unsigned n
= ntohl(top_index
[i
]);
54 die("corrupt index file");
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)));
67 unsigned off64_nr
= 0;
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
++) {
87 uint32_t off
= ntohl(entries
[i
].off
);
88 if (!(off
& 0x80000000)) {
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) |
100 printf("%" PRIuMAX
" %s (%08"PRIx32
")\n",
102 oid_to_hex(&entries
[i
].oid
),
103 ntohl(entries
[i
].crc
));