Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
3
4 #include "builtin.h"
5 #include "gettext.h"
6 #include "hash.h"
7 #include "hex.h"
8 #include "pack.h"
9 #include "parse-options.h"
10
11 static const char *const show_index_usage[] = {
12 "git show-index [--object-format=<hash-algorithm>] < <pack-idx-file>",
13 NULL
14 };
15
16 int cmd_show_index(int argc,
17 const char **argv,
18 const char *prefix,
19 struct repository *repo UNUSED)
20 {
21 int i;
22 unsigned nr;
23 unsigned int version;
24 static unsigned int top_index[256];
25 unsigned hashsz;
26 const char *hash_name = NULL;
27 int hash_algo;
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")),
31 OPT_END()
32 };
33
34 argc = parse_options(argc, argv, prefix, show_index_options, show_index_usage, 0);
35
36 if (hash_name) {
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);
41 }
42
43 /*
44 * Fallback to SHA1 if we are running outside of a repository.
45 *
46 * TODO: If a future implementation of index file version encodes the hash
47 * algorithm in its header, enable show-index to infer it from the
48 * header rather than relying on repository context or a default fallback.
49 */
50 if (!the_hash_algo) {
51 warning(_("assuming SHA-1; use --object-format to override"));
52 repo_set_hash_algo(the_repository, GIT_HASH_DEFAULT);
53 }
54
55 hashsz = the_hash_algo->rawsz;
56
57 if (fread(top_index, 2 * 4, 1, stdin) != 1)
58 die(_("unable to read header"));
59 if (top_index[0] == htonl(PACK_IDX_SIGNATURE)) {
60 version = ntohl(top_index[1]);
61 if (version < 2 || version > 2)
62 die(_("unknown index version"));
63 if (fread(top_index, 256 * 4, 1, stdin) != 1)
64 die(_("unable to read index"));
65 } else {
66 version = 1;
67 if (fread(&top_index[2], 254 * 4, 1, stdin) != 1)
68 die(_("unable to read index"));
69 }
70 nr = 0;
71 for (i = 0; i < 256; i++) {
72 unsigned n = ntohl(top_index[i]);
73 if (n < nr)
74 die(_("corrupt index file"));
75 nr = n;
76 }
77 if (version == 1) {
78 for (i = 0; i < nr; i++) {
79 unsigned int offset, entry[(GIT_MAX_RAWSZ + 4) / sizeof(unsigned int)];
80
81 if (fread(entry, 4 + hashsz, 1, stdin) != 1)
82 die(_("unable to read entry %u/%u"), i, nr);
83 offset = ntohl(entry[0]);
84 printf("%u %s\n", offset, hash_to_hex((void *)(entry+1)));
85 }
86 } else {
87 unsigned off64_nr = 0;
88 struct {
89 struct object_id oid;
90 uint32_t crc;
91 uint32_t off;
92 } *entries;
93 ALLOC_ARRAY(entries, nr);
94 for (i = 0; i < nr; i++) {
95 if (fread(entries[i].oid.hash, hashsz, 1, stdin) != 1)
96 die(_("unable to read sha1 %u/%u"), i, nr);
97 entries[i].oid.algo = hash_algo_by_ptr(the_hash_algo);
98 }
99 for (i = 0; i < nr; i++)
100 if (fread(&entries[i].crc, 4, 1, stdin) != 1)
101 die(_("unable to read crc %u/%u"), i, nr);
102 for (i = 0; i < nr; i++)
103 if (fread(&entries[i].off, 4, 1, stdin) != 1)
104 die(_("unable to read 32b offset %u/%u"), i, nr);
105 for (i = 0; i < nr; i++) {
106 uint64_t offset;
107 uint32_t off = ntohl(entries[i].off);
108 if (!(off & 0x80000000)) {
109 offset = off;
110 } else {
111 uint32_t off64[2];
112 if ((off & 0x7fffffff) != off64_nr)
113 die(_("inconsistent 64b offset index"));
114 if (fread(off64, 8, 1, stdin) != 1)
115 die(_("unable to read 64b offset %u"), off64_nr);
116 offset = (((uint64_t)ntohl(off64[0])) << 32) |
117 ntohl(off64[1]);
118 off64_nr++;
119 }
120 printf("%" PRIuMAX " %s (%08"PRIx32")\n",
121 (uintmax_t) offset,
122 oid_to_hex(&entries[i].oid),
123 ntohl(entries[i].crc));
124 }
125 free(entries);
126 }
127 return 0;
128 }