Merge branch 'ds/commit-graph-merging-fix'
[git] / builtin / show-index.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "pack.h"
4 #include "parse-options.h"
5
6 static const char *const show_index_usage[] = {
7         "git show-index [--object-format=<hash-algorithm>]",
8         NULL
9 };
10
11 int cmd_show_index(int argc, const char **argv, const char *prefix)
12 {
13         int i;
14         unsigned nr;
15         unsigned int version;
16         static unsigned int top_index[256];
17         unsigned hashsz;
18         const char *hash_name = NULL;
19         int hash_algo;
20         const struct option show_index_options[] = {
21                 OPT_STRING(0, "object-format", &hash_name, N_("hash-algorithm"),
22                            N_("specify the hash algorithm to use")),
23                 OPT_END()
24         };
25
26         argc = parse_options(argc, argv, prefix, show_index_options, show_index_usage, 0);
27
28         if (hash_name) {
29                 hash_algo = hash_algo_by_name(hash_name);
30                 if (hash_algo == GIT_HASH_UNKNOWN)
31                         die(_("Unknown hash algorithm"));
32                 repo_set_hash_algo(the_repository, hash_algo);
33         }
34
35         hashsz = the_hash_algo->rawsz;
36
37         if (fread(top_index, 2 * 4, 1, stdin) != 1)
38                 die("unable to read header");
39         if (top_index[0] == htonl(PACK_IDX_SIGNATURE)) {
40                 version = ntohl(top_index[1]);
41                 if (version < 2 || version > 2)
42                         die("unknown index version");
43                 if (fread(top_index, 256 * 4, 1, stdin) != 1)
44                         die("unable to read index");
45         } else {
46                 version = 1;
47                 if (fread(&top_index[2], 254 * 4, 1, stdin) != 1)
48                         die("unable to read index");
49         }
50         nr = 0;
51         for (i = 0; i < 256; i++) {
52                 unsigned n = ntohl(top_index[i]);
53                 if (n < nr)
54                         die("corrupt index file");
55                 nr = n;
56         }
57         if (version == 1) {
58                 for (i = 0; i < nr; i++) {
59                         unsigned int offset, entry[(GIT_MAX_RAWSZ + 4) / sizeof(unsigned int)];
60
61                         if (fread(entry, 4 + hashsz, 1, stdin) != 1)
62                                 die("unable to read entry %u/%u", i, nr);
63                         offset = ntohl(entry[0]);
64                         printf("%u %s\n", offset, hash_to_hex((void *)(entry+1)));
65                 }
66         } else {
67                 unsigned off64_nr = 0;
68                 struct {
69                         struct object_id oid;
70                         uint32_t crc;
71                         uint32_t off;
72                 } *entries;
73                 ALLOC_ARRAY(entries, nr);
74                 for (i = 0; i < nr; i++)
75                         if (fread(entries[i].oid.hash, hashsz, 1, stdin) != 1)
76                                 die("unable to read sha1 %u/%u", i, nr);
77                 for (i = 0; i < nr; i++)
78                         if (fread(&entries[i].crc, 4, 1, stdin) != 1)
79                                 die("unable to read crc %u/%u", i, nr);
80                 for (i = 0; i < nr; i++)
81                         if (fread(&entries[i].off, 4, 1, stdin) != 1)
82                                 die("unable to read 32b offset %u/%u", i, nr);
83                 for (i = 0; i < nr; i++) {
84                         uint64_t offset;
85                         uint32_t off = ntohl(entries[i].off);
86                         if (!(off & 0x80000000)) {
87                                 offset = off;
88                         } else {
89                                 uint32_t off64[2];
90                                 if ((off & 0x7fffffff) != off64_nr)
91                                         die("inconsistent 64b offset index");
92                                 if (fread(off64, 8, 1, stdin) != 1)
93                                         die("unable to read 64b offset %u", off64_nr);
94                                 offset = (((uint64_t)ntohl(off64[0])) << 32) |
95                                                      ntohl(off64[1]);
96                                 off64_nr++;
97                         }
98                         printf("%" PRIuMAX " %s (%08"PRIx32")\n",
99                                (uintmax_t) offset,
100                                oid_to_hex(&entries[i].oid),
101                                ntohl(entries[i].crc));
102                 }
103                 free(entries);
104         }
105         return 0;
106 }