Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 85 lines (80 sloc) 2.26 kB
0271611 Add a "git-show-index" helper that shows the contents of a pack index
Linus Torvalds authored
1 #include "cache.h"
32637cd show-index.c: learn about index v2
Nicolas Pitre authored
2 #include "pack.h"
0271611 Add a "git-show-index" helper that shows the contents of a pack index
Linus Torvalds authored
3
03c5c10 @jrn Show usage string for 'git show-index -h'
jrn authored
4 static const char show_index_usage[] =
5 "git show-index < <packed archive index>";
6
0271611 Add a "git-show-index" helper that shows the contents of a pack index
Linus Torvalds authored
7 int main(int argc, char **argv)
8 {
9 int i;
10 unsigned nr;
32637cd show-index.c: learn about index v2
Nicolas Pitre authored
11 unsigned int version;
0271611 Add a "git-show-index" helper that shows the contents of a pack index
Linus Torvalds authored
12 static unsigned int top_index[256];
13
03c5c10 @jrn Show usage string for 'git show-index -h'
jrn authored
14 if (argc != 1)
15 usage(show_index_usage);
32637cd show-index.c: learn about index v2
Nicolas Pitre authored
16 if (fread(top_index, 2 * 4, 1, stdin) != 1)
17 die("unable to read header");
18 if (top_index[0] == htonl(PACK_IDX_SIGNATURE)) {
19 version = ntohl(top_index[1]);
20 if (version < 2 || version > 2)
21 die("unknown index version");
22 if (fread(top_index, 256 * 4, 1, stdin) != 1)
23 die("unable to read index");
24 } else {
25 version = 1;
26 if (fread(&top_index[2], 254 * 4, 1, stdin) != 1)
27 die("unable to read index");
28 }
0271611 Add a "git-show-index" helper that shows the contents of a pack index
Linus Torvalds authored
29 nr = 0;
30 for (i = 0; i < 256; i++) {
31 unsigned n = ntohl(top_index[i]);
32 if (n < nr)
33 die("corrupt index file");
34 nr = n;
35 }
32637cd show-index.c: learn about index v2
Nicolas Pitre authored
36 if (version == 1) {
37 for (i = 0; i < nr; i++) {
38 unsigned int offset, entry[6];
0271611 Add a "git-show-index" helper that shows the contents of a pack index
Linus Torvalds authored
39
32637cd show-index.c: learn about index v2
Nicolas Pitre authored
40 if (fread(entry, 4 + 20, 1, stdin) != 1)
41 die("unable to read entry %u/%u", i, nr);
42 offset = ntohl(entry[0]);
43 printf("%u %s\n", offset, sha1_to_hex((void *)(entry+1)));
44 }
45 } else {
46 unsigned off64_nr = 0;
47 struct {
48 unsigned char sha1[20];
49 uint32_t crc;
50 uint32_t off;
51 } *entries = xmalloc(nr * sizeof(entries[0]));
52 for (i = 0; i < nr; i++)
53 if (fread(entries[i].sha1, 20, 1, stdin) != 1)
54 die("unable to read sha1 %u/%u", i, nr);
55 for (i = 0; i < nr; i++)
56 if (fread(&entries[i].crc, 4, 1, stdin) != 1)
57 die("unable to read crc %u/%u", i, nr);
58 for (i = 0; i < nr; i++)
59 if (fread(&entries[i].off, 4, 1, stdin) != 1)
60 die("unable to read 32b offset %u/%u", i, nr);
61 for (i = 0; i < nr; i++) {
62 uint64_t offset;
63 uint32_t off = ntohl(entries[i].off);
64 if (!(off & 0x80000000)) {
65 offset = off;
66 } else {
67 uint32_t off64[2];
68 if ((off & 0x7fffffff) != off64_nr)
69 die("inconsistent 64b offset index");
70 if (fread(off64, 8, 1, stdin) != 1)
71 die("unable to read 64b offset %u", off64_nr);
72 offset = (((uint64_t)ntohl(off64[0])) << 32) |
73 ntohl(off64[1]);
74 off64_nr++;
75 }
6e1c234 Fix some warnings (on cygwin) to allow -Werror
Ramsay Jones authored
76 printf("%" PRIuMAX " %s (%08"PRIx32")\n",
77 (uintmax_t) offset,
32637cd show-index.c: learn about index v2
Nicolas Pitre authored
78 sha1_to_hex(entries[i].sha1),
79 ntohl(entries[i].crc));
80 }
81 free(entries);
0271611 Add a "git-show-index" helper that shows the contents of a pack index
Linus Torvalds authored
82 }
83 return 0;
84 }
Something went wrong with that request. Please try again.