]>
Commit | Line | Data |
---|---|---|
ff417260 | 1 | #include "builtin.h" |
0271611e | 2 | #include "cache.h" |
32637cdf | 3 | #include "pack.h" |
0271611e | 4 | |
03c5c102 | 5 | static const char show_index_usage[] = |
33e8fc87 | 6 | "git show-index"; |
03c5c102 | 7 | |
ff417260 | 8 | int cmd_show_index(int argc, const char **argv, const char *prefix) |
0271611e LT |
9 | { |
10 | int i; | |
11 | unsigned nr; | |
32637cdf | 12 | unsigned int version; |
0271611e | 13 | static unsigned int top_index[256]; |
7962e046 | 14 | const unsigned hashsz = the_hash_algo->rawsz; |
0271611e | 15 | |
03c5c102 JN |
16 | if (argc != 1) |
17 | usage(show_index_usage); | |
32637cdf NP |
18 | if (fread(top_index, 2 * 4, 1, stdin) != 1) |
19 | die("unable to read header"); | |
20 | if (top_index[0] == htonl(PACK_IDX_SIGNATURE)) { | |
21 | version = ntohl(top_index[1]); | |
22 | if (version < 2 || version > 2) | |
23 | die("unknown index version"); | |
24 | if (fread(top_index, 256 * 4, 1, stdin) != 1) | |
25 | die("unable to read index"); | |
26 | } else { | |
27 | version = 1; | |
28 | if (fread(&top_index[2], 254 * 4, 1, stdin) != 1) | |
29 | die("unable to read index"); | |
30 | } | |
0271611e LT |
31 | nr = 0; |
32 | for (i = 0; i < 256; i++) { | |
33 | unsigned n = ntohl(top_index[i]); | |
34 | if (n < nr) | |
35 | die("corrupt index file"); | |
36 | nr = n; | |
37 | } | |
32637cdf NP |
38 | if (version == 1) { |
39 | for (i = 0; i < nr; i++) { | |
7962e046 | 40 | unsigned int offset, entry[(GIT_MAX_RAWSZ + 4) / sizeof(unsigned int)]; |
0271611e | 41 | |
7962e046 | 42 | if (fread(entry, 4 + hashsz, 1, stdin) != 1) |
32637cdf NP |
43 | die("unable to read entry %u/%u", i, nr); |
44 | offset = ntohl(entry[0]); | |
be8e172e | 45 | printf("%u %s\n", offset, hash_to_hex((void *)(entry+1))); |
32637cdf NP |
46 | } |
47 | } else { | |
48 | unsigned off64_nr = 0; | |
49 | struct { | |
7962e046 | 50 | struct object_id oid; |
32637cdf NP |
51 | uint32_t crc; |
52 | uint32_t off; | |
b32fa95f JK |
53 | } *entries; |
54 | ALLOC_ARRAY(entries, nr); | |
32637cdf | 55 | for (i = 0; i < nr; i++) |
7962e046 | 56 | if (fread(entries[i].oid.hash, hashsz, 1, stdin) != 1) |
32637cdf NP |
57 | die("unable to read sha1 %u/%u", i, nr); |
58 | for (i = 0; i < nr; i++) | |
59 | if (fread(&entries[i].crc, 4, 1, stdin) != 1) | |
60 | die("unable to read crc %u/%u", i, nr); | |
61 | for (i = 0; i < nr; i++) | |
62 | if (fread(&entries[i].off, 4, 1, stdin) != 1) | |
63 | die("unable to read 32b offset %u/%u", i, nr); | |
64 | for (i = 0; i < nr; i++) { | |
65 | uint64_t offset; | |
66 | uint32_t off = ntohl(entries[i].off); | |
67 | if (!(off & 0x80000000)) { | |
68 | offset = off; | |
69 | } else { | |
70 | uint32_t off64[2]; | |
71 | if ((off & 0x7fffffff) != off64_nr) | |
72 | die("inconsistent 64b offset index"); | |
73 | if (fread(off64, 8, 1, stdin) != 1) | |
74 | die("unable to read 64b offset %u", off64_nr); | |
75 | offset = (((uint64_t)ntohl(off64[0])) << 32) | | |
76 | ntohl(off64[1]); | |
77 | off64_nr++; | |
78 | } | |
6e1c2344 RJ |
79 | printf("%" PRIuMAX " %s (%08"PRIx32")\n", |
80 | (uintmax_t) offset, | |
7962e046 | 81 | oid_to_hex(&entries[i].oid), |
32637cdf NP |
82 | ntohl(entries[i].crc)); |
83 | } | |
84 | free(entries); | |
0271611e LT |
85 | } |
86 | return 0; | |
87 | } |