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