6 #include "parse-options.h"
7 #include "repository.h"
9 static const char *const show_index_usage
[] = {
10 "git show-index [--object-format=<hash-algorithm>]",
14 int cmd_show_index(int argc
, const char **argv
, const char *prefix
)
19 static unsigned int top_index
[256];
21 const char *hash_name
= NULL
;
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")),
29 argc
= parse_options(argc
, argv
, prefix
, show_index_options
, show_index_usage
, 0);
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
);
38 hashsz
= the_hash_algo
->rawsz
;
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");
50 if (fread(&top_index
[2], 254 * 4, 1, stdin
) != 1)
51 die("unable to read index");
54 for (i
= 0; i
< 256; i
++) {
55 unsigned n
= ntohl(top_index
[i
]);
57 die("corrupt index file");
61 for (i
= 0; i
< nr
; i
++) {
62 unsigned int offset
, entry
[(GIT_MAX_RAWSZ
+ 4) / sizeof(unsigned int)];
64 if (fread(entry
, 4 + hashsz
, 1, stdin
) != 1)
65 die("unable to read entry %u/%u", i
, nr
);
66 offset
= ntohl(entry
[0]);
67 printf("%u %s\n", offset
, hash_to_hex((void *)(entry
+1)));
70 unsigned off64_nr
= 0;
76 ALLOC_ARRAY(entries
, nr
);
77 for (i
= 0; i
< nr
; i
++) {
78 if (fread(entries
[i
].oid
.hash
, hashsz
, 1, stdin
) != 1)
79 die("unable to read sha1 %u/%u", i
, nr
);
80 entries
[i
].oid
.algo
= hash_algo_by_ptr(the_hash_algo
);
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
++) {
90 uint32_t off
= ntohl(entries
[i
].off
);
91 if (!(off
& 0x80000000)) {
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) |
103 printf("%" PRIuMAX
" %s (%08"PRIx32
")\n",
105 oid_to_hex(&entries
[i
].oid
),
106 ntohl(entries
[i
].crc
));