1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
9 #include "parse-options.h"
11 static const char *const show_index_usage
[] = {
12 "git show-index [--object-format=<hash-algorithm>] < <pack-idx-file>",
16 int cmd_show_index(int argc
,
19 struct repository
*repo UNUSED
)
24 static unsigned int top_index
[256];
26 const char *hash_name
= NULL
;
28 const struct option show_index_options
[] = {
29 OPT_STRING(0, "object-format", &hash_name
, N_("hash-algorithm"),
30 N_("specify the hash algorithm to use")),
34 argc
= parse_options(argc
, argv
, prefix
, show_index_options
, show_index_usage
, 0);
37 hash_algo
= hash_algo_by_name(hash_name
);
38 if (hash_algo
== GIT_HASH_UNKNOWN
)
39 die(_("Unknown hash algorithm"));
40 repo_set_hash_algo(the_repository
, hash_algo
);
44 * Fallback to SHA1 if we are running outside of a repository.
46 * TODO: Figure out and implement a way to detect the hash algorithm in use by the
47 * the index file passed in and use that instead.
50 repo_set_hash_algo(the_repository
, GIT_HASH_SHA1
);
52 hashsz
= the_hash_algo
->rawsz
;
54 if (fread(top_index
, 2 * 4, 1, stdin
) != 1)
55 die("unable to read header");
56 if (top_index
[0] == htonl(PACK_IDX_SIGNATURE
)) {
57 version
= ntohl(top_index
[1]);
58 if (version
< 2 || version
> 2)
59 die("unknown index version");
60 if (fread(top_index
, 256 * 4, 1, stdin
) != 1)
61 die("unable to read index");
64 if (fread(&top_index
[2], 254 * 4, 1, stdin
) != 1)
65 die("unable to read index");
68 for (i
= 0; i
< 256; i
++) {
69 unsigned n
= ntohl(top_index
[i
]);
71 die("corrupt index file");
75 for (i
= 0; i
< nr
; i
++) {
76 unsigned int offset
, entry
[(GIT_MAX_RAWSZ
+ 4) / sizeof(unsigned int)];
78 if (fread(entry
, 4 + hashsz
, 1, stdin
) != 1)
79 die("unable to read entry %u/%u", i
, nr
);
80 offset
= ntohl(entry
[0]);
81 printf("%u %s\n", offset
, hash_to_hex((void *)(entry
+1)));
84 unsigned off64_nr
= 0;
90 ALLOC_ARRAY(entries
, nr
);
91 for (i
= 0; i
< nr
; i
++) {
92 if (fread(entries
[i
].oid
.hash
, hashsz
, 1, stdin
) != 1)
93 die("unable to read sha1 %u/%u", i
, nr
);
94 entries
[i
].oid
.algo
= hash_algo_by_ptr(the_hash_algo
);
96 for (i
= 0; i
< nr
; i
++)
97 if (fread(&entries
[i
].crc
, 4, 1, stdin
) != 1)
98 die("unable to read crc %u/%u", i
, nr
);
99 for (i
= 0; i
< nr
; i
++)
100 if (fread(&entries
[i
].off
, 4, 1, stdin
) != 1)
101 die("unable to read 32b offset %u/%u", i
, nr
);
102 for (i
= 0; i
< nr
; i
++) {
104 uint32_t off
= ntohl(entries
[i
].off
);
105 if (!(off
& 0x80000000)) {
109 if ((off
& 0x7fffffff) != off64_nr
)
110 die("inconsistent 64b offset index");
111 if (fread(off64
, 8, 1, stdin
) != 1)
112 die("unable to read 64b offset %u", off64_nr
);
113 offset
= (((uint64_t)ntohl(off64
[0])) << 32) |
117 printf("%" PRIuMAX
" %s (%08"PRIx32
")\n",
119 oid_to_hex(&entries
[i
].oid
),
120 ntohl(entries
[i
].crc
));