show-index.con commit Add an option for using any HTTP authentication scheme, not only basic (b8ac923)
   1#include "cache.h"
   2#include "pack.h"
   3
   4static const char show_index_usage[] =
   5"git show-index < <packed archive index>";
   6
   7int main(int argc, 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 = xmalloc(nr * sizeof(entries[0]));
  52                for (i = 0; i < nr; i++)
  53                        if (fread(entries[i].sha1, 20, 1, stdin) != 1)
  54                                die("unable to read sha1 %u/%u", i, nr);
  55                for (i = 0; i < nr; i++)
  56                        if (fread(&entries[i].crc, 4, 1, stdin) != 1)
  57                                die("unable to read crc %u/%u", i, nr);
  58                for (i = 0; i < nr; i++)
  59                        if (fread(&entries[i].off, 4, 1, stdin) != 1)
  60                                die("unable to read 32b offset %u/%u", i, nr);
  61                for (i = 0; i < nr; i++) {
  62                        uint64_t offset;
  63                        uint32_t off = ntohl(entries[i].off);
  64                        if (!(off & 0x80000000)) {
  65                                offset = off;
  66                        } else {
  67                                uint32_t off64[2];
  68                                if ((off & 0x7fffffff) != off64_nr)
  69                                        die("inconsistent 64b offset index");
  70                                if (fread(off64, 8, 1, stdin) != 1)
  71                                        die("unable to read 64b offset %u", off64_nr);
  72                                offset = (((uint64_t)ntohl(off64[0])) << 32) |
  73                                                     ntohl(off64[1]);
  74                                off64_nr++;
  75                        }
  76                        printf("%" PRIuMAX " %s (%08"PRIx32")\n",
  77                               (uintmax_t) offset,
  78                               sha1_to_hex(entries[i].sha1),
  79                               ntohl(entries[i].crc));
  80                }
  81                free(entries);
  82        }
  83        return 0;
  84}