Merge branch 'ah/filter-branch-setup'
[git] / show-index.c
1 #include "cache.h"
2 #include "pack.h"
3
4 static const char show_index_usage[] =
5 "git show-index";
6
7 int cmd_main(int argc, const char **argv)
8 {
9         int i;
10         unsigned nr;
11         unsigned int version;
12         static unsigned int top_index[256];
13
14         if (argc != 1)
15                 usage(show_index_usage);
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         }
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         }
36         if (version == 1) {
37                 for (i = 0; i < nr; i++) {
38                         unsigned int offset, entry[6];
39
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;
52                 ALLOC_ARRAY(entries, nr);
53                 for (i = 0; i < nr; i++)
54                         if (fread(entries[i].sha1, 20, 1, stdin) != 1)
55                                 die("unable to read sha1 %u/%u", i, nr);
56                 for (i = 0; i < nr; i++)
57                         if (fread(&entries[i].crc, 4, 1, stdin) != 1)
58                                 die("unable to read crc %u/%u", i, nr);
59                 for (i = 0; i < nr; i++)
60                         if (fread(&entries[i].off, 4, 1, stdin) != 1)
61                                 die("unable to read 32b offset %u/%u", i, nr);
62                 for (i = 0; i < nr; i++) {
63                         uint64_t offset;
64                         uint32_t off = ntohl(entries[i].off);
65                         if (!(off & 0x80000000)) {
66                                 offset = off;
67                         } else {
68                                 uint32_t off64[2];
69                                 if ((off & 0x7fffffff) != off64_nr)
70                                         die("inconsistent 64b offset index");
71                                 if (fread(off64, 8, 1, stdin) != 1)
72                                         die("unable to read 64b offset %u", off64_nr);
73                                 offset = (((uint64_t)ntohl(off64[0])) << 32) |
74                                                      ntohl(off64[1]);
75                                 off64_nr++;
76                         }
77                         printf("%" PRIuMAX " %s (%08"PRIx32")\n",
78                                (uintmax_t) offset,
79                                sha1_to_hex(entries[i].sha1),
80                                ntohl(entries[i].crc));
81                 }
82                 free(entries);
83         }
84         return 0;
85 }