Linus Torvalds | 0271611 | 2005-07-05 17:08:02 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Nicolas Pitre | 32637cd | 2007-04-09 01:06:36 -0400 | [diff] [blame] | 2 | #include "pack.h" |
Linus Torvalds | 0271611 | 2005-07-05 17:08:02 -0700 | [diff] [blame] | 3 | |
Jonathan Nieder | 03c5c10 | 2009-11-09 09:04:55 -0600 | [diff] [blame] | 4 | static const char show_index_usage[] = |
| 5 | "git show-index < <packed archive index>"; |
| 6 | |
Linus Torvalds | 0271611 | 2005-07-05 17:08:02 -0700 | [diff] [blame] | 7 | int main(int argc, char **argv) |
| 8 | { |
| 9 | int i; |
| 10 | unsigned nr; |
Nicolas Pitre | 32637cd | 2007-04-09 01:06:36 -0400 | [diff] [blame] | 11 | unsigned int version; |
Linus Torvalds | 0271611 | 2005-07-05 17:08:02 -0700 | [diff] [blame] | 12 | static unsigned int top_index[256]; |
| 13 | |
Ævar Arnfjörð Bjarmason | 5e9637c | 2011-11-18 00:14:42 +0100 | [diff] [blame] | 14 | git_setup_gettext(); |
| 15 | |
Jonathan Nieder | 03c5c10 | 2009-11-09 09:04:55 -0600 | [diff] [blame] | 16 | if (argc != 1) |
| 17 | usage(show_index_usage); |
Nicolas Pitre | 32637cd | 2007-04-09 01:06:36 -0400 | [diff] [blame] | 18 | if (fread(top_index, 2 * 4, 1, stdin) != 1) |
| 19 | die("unable to read header"); |
| 20 | if (top_index[0] == htonl(PACK_IDX_SIGNATURE)) { |
| 21 | version = ntohl(top_index[1]); |
| 22 | if (version < 2 || version > 2) |
| 23 | die("unknown index version"); |
| 24 | if (fread(top_index, 256 * 4, 1, stdin) != 1) |
| 25 | die("unable to read index"); |
| 26 | } else { |
| 27 | version = 1; |
| 28 | if (fread(&top_index[2], 254 * 4, 1, stdin) != 1) |
| 29 | die("unable to read index"); |
| 30 | } |
Linus Torvalds | 0271611 | 2005-07-05 17:08:02 -0700 | [diff] [blame] | 31 | nr = 0; |
| 32 | for (i = 0; i < 256; i++) { |
| 33 | unsigned n = ntohl(top_index[i]); |
| 34 | if (n < nr) |
| 35 | die("corrupt index file"); |
| 36 | nr = n; |
| 37 | } |
Nicolas Pitre | 32637cd | 2007-04-09 01:06:36 -0400 | [diff] [blame] | 38 | if (version == 1) { |
| 39 | for (i = 0; i < nr; i++) { |
| 40 | unsigned int offset, entry[6]; |
Linus Torvalds | 0271611 | 2005-07-05 17:08:02 -0700 | [diff] [blame] | 41 | |
Nicolas Pitre | 32637cd | 2007-04-09 01:06:36 -0400 | [diff] [blame] | 42 | if (fread(entry, 4 + 20, 1, stdin) != 1) |
| 43 | die("unable to read entry %u/%u", i, nr); |
| 44 | offset = ntohl(entry[0]); |
| 45 | printf("%u %s\n", offset, sha1_to_hex((void *)(entry+1))); |
| 46 | } |
| 47 | } else { |
| 48 | unsigned off64_nr = 0; |
| 49 | struct { |
| 50 | unsigned char sha1[20]; |
| 51 | uint32_t crc; |
| 52 | uint32_t off; |
Brandon Casey | 040a655 | 2011-10-06 13:22:22 -0500 | [diff] [blame] | 53 | } *entries = xmalloc(nr * sizeof(entries[0])); |
Nicolas Pitre | 32637cd | 2007-04-09 01:06:36 -0400 | [diff] [blame] | 54 | for (i = 0; i < nr; i++) |
| 55 | if (fread(entries[i].sha1, 20, 1, stdin) != 1) |
| 56 | die("unable to read sha1 %u/%u", i, nr); |
| 57 | for (i = 0; i < nr; i++) |
| 58 | if (fread(&entries[i].crc, 4, 1, stdin) != 1) |
| 59 | die("unable to read crc %u/%u", i, nr); |
| 60 | for (i = 0; i < nr; i++) |
| 61 | if (fread(&entries[i].off, 4, 1, stdin) != 1) |
| 62 | die("unable to read 32b offset %u/%u", i, nr); |
| 63 | for (i = 0; i < nr; i++) { |
| 64 | uint64_t offset; |
| 65 | uint32_t off = ntohl(entries[i].off); |
| 66 | if (!(off & 0x80000000)) { |
| 67 | offset = off; |
| 68 | } else { |
| 69 | uint32_t off64[2]; |
| 70 | if ((off & 0x7fffffff) != off64_nr) |
| 71 | die("inconsistent 64b offset index"); |
| 72 | if (fread(off64, 8, 1, stdin) != 1) |
| 73 | die("unable to read 64b offset %u", off64_nr); |
| 74 | offset = (((uint64_t)ntohl(off64[0])) << 32) | |
| 75 | ntohl(off64[1]); |
| 76 | off64_nr++; |
| 77 | } |
Ramsay Jones | 6e1c234 | 2008-07-03 16:52:09 +0100 | [diff] [blame] | 78 | printf("%" PRIuMAX " %s (%08"PRIx32")\n", |
| 79 | (uintmax_t) offset, |
Nicolas Pitre | 32637cd | 2007-04-09 01:06:36 -0400 | [diff] [blame] | 80 | sha1_to_hex(entries[i].sha1), |
| 81 | ntohl(entries[i].crc)); |
| 82 | } |
| 83 | free(entries); |
Linus Torvalds | 0271611 | 2005-07-05 17:08:02 -0700 | [diff] [blame] | 84 | } |
| 85 | return 0; |
| 86 | } |