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