add a force_object_loose() function
[git/spearce.git] / show-index.c
blob7253991fff9f6240ee6413986dfc66cfa3ff184e
1 #include "cache.h"
2 #include "pack.h"
4 int main(int argc, char **argv)
6 int i;
7 unsigned nr;
8 unsigned int version;
9 static unsigned int top_index[256];
11 if (fread(top_index, 2 * 4, 1, stdin) != 1)
12 die("unable to read header");
13 if (top_index[0] == htonl(PACK_IDX_SIGNATURE)) {
14 version = ntohl(top_index[1]);
15 if (version < 2 || version > 2)
16 die("unknown index version");
17 if (fread(top_index, 256 * 4, 1, stdin) != 1)
18 die("unable to read index");
19 } else {
20 version = 1;
21 if (fread(&top_index[2], 254 * 4, 1, stdin) != 1)
22 die("unable to read index");
24 nr = 0;
25 for (i = 0; i < 256; i++) {
26 unsigned n = ntohl(top_index[i]);
27 if (n < nr)
28 die("corrupt index file");
29 nr = n;
31 if (version == 1) {
32 for (i = 0; i < nr; i++) {
33 unsigned int offset, entry[6];
35 if (fread(entry, 4 + 20, 1, stdin) != 1)
36 die("unable to read entry %u/%u", i, nr);
37 offset = ntohl(entry[0]);
38 printf("%u %s\n", offset, sha1_to_hex((void *)(entry+1)));
40 } else {
41 unsigned off64_nr = 0;
42 struct {
43 unsigned char sha1[20];
44 uint32_t crc;
45 uint32_t off;
46 } *entries = xmalloc(nr * sizeof(entries[0]));
47 for (i = 0; i < nr; i++)
48 if (fread(entries[i].sha1, 20, 1, stdin) != 1)
49 die("unable to read sha1 %u/%u", i, nr);
50 for (i = 0; i < nr; i++)
51 if (fread(&entries[i].crc, 4, 1, stdin) != 1)
52 die("unable to read crc %u/%u", i, nr);
53 for (i = 0; i < nr; i++)
54 if (fread(&entries[i].off, 4, 1, stdin) != 1)
55 die("unable to read 32b offset %u/%u", i, nr);
56 for (i = 0; i < nr; i++) {
57 uint64_t offset;
58 uint32_t off = ntohl(entries[i].off);
59 if (!(off & 0x80000000)) {
60 offset = off;
61 } else {
62 uint32_t off64[2];
63 if ((off & 0x7fffffff) != off64_nr)
64 die("inconsistent 64b offset index");
65 if (fread(off64, 8, 1, stdin) != 1)
66 die("unable to read 64b offset %u", off64_nr);
67 offset = (((uint64_t)ntohl(off64[0])) << 32) |
68 ntohl(off64[1]);
69 off64_nr++;
71 printf("%" PRIuMAX " %s (%08x)\n", (uintmax_t) offset,
72 sha1_to_hex(entries[i].sha1),
73 ntohl(entries[i].crc));
75 free(entries);
77 return 0;