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