builtin-unpack-objects.con commit gitweb: Drop the href() params which keys are not in %mapping (1149fec)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "object.h"
   4#include "delta.h"
   5#include "pack.h"
   6#include "blob.h"
   7#include "commit.h"
   8#include "tag.h"
   9#include "tree.h"
  10
  11#include <sys/time.h>
  12
  13static int dry_run, quiet;
  14static const char unpack_usage[] = "git-unpack-objects [-n] [-q] < pack-file";
  15
  16/* We always read in 4kB chunks. */
  17static unsigned char buffer[4096];
  18static unsigned long offset, len, eof;
  19static SHA_CTX ctx;
  20
  21/*
  22 * Make sure at least "min" bytes are available in the buffer, and
  23 * return the pointer to the buffer.
  24 */
  25static void * fill(int min)
  26{
  27        if (min <= len)
  28                return buffer + offset;
  29        if (eof)
  30                die("unable to fill input");
  31        if (min > sizeof(buffer))
  32                die("cannot fill %d bytes", min);
  33        if (offset) {
  34                SHA1_Update(&ctx, buffer, offset);
  35                memcpy(buffer, buffer + offset, len);
  36                offset = 0;
  37        }
  38        do {
  39                int ret = xread(0, buffer + len, sizeof(buffer) - len);
  40                if (ret <= 0) {
  41                        if (!ret)
  42                                die("early EOF");
  43                        die("read error on input: %s", strerror(errno));
  44                }
  45                len += ret;
  46        } while (len < min);
  47        return buffer;
  48}
  49
  50static void use(int bytes)
  51{
  52        if (bytes > len)
  53                die("used more bytes than were available");
  54        len -= bytes;
  55        offset += bytes;
  56}
  57
  58static void *get_data(unsigned long size)
  59{
  60        z_stream stream;
  61        void *buf = xmalloc(size);
  62
  63        memset(&stream, 0, sizeof(stream));
  64
  65        stream.next_out = buf;
  66        stream.avail_out = size;
  67        stream.next_in = fill(1);
  68        stream.avail_in = len;
  69        inflateInit(&stream);
  70
  71        for (;;) {
  72                int ret = inflate(&stream, 0);
  73                use(len - stream.avail_in);
  74                if (stream.total_out == size && ret == Z_STREAM_END)
  75                        break;
  76                if (ret != Z_OK)
  77                        die("inflate returned %d\n", ret);
  78                stream.next_in = fill(1);
  79                stream.avail_in = len;
  80        }
  81        inflateEnd(&stream);
  82        return buf;
  83}
  84
  85struct delta_info {
  86        unsigned char base_sha1[20];
  87        unsigned long size;
  88        void *delta;
  89        struct delta_info *next;
  90};
  91
  92static struct delta_info *delta_list;
  93
  94static void add_delta_to_list(unsigned char *base_sha1, void *delta, unsigned long size)
  95{
  96        struct delta_info *info = xmalloc(sizeof(*info));
  97
  98        memcpy(info->base_sha1, base_sha1, 20);
  99        info->size = size;
 100        info->delta = delta;
 101        info->next = delta_list;
 102        delta_list = info;
 103}
 104
 105static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size);
 106
 107static void write_object(void *buf, unsigned long size, const char *type)
 108{
 109        unsigned char sha1[20];
 110        if (write_sha1_file(buf, size, type, sha1) < 0)
 111                die("failed to write object");
 112        added_object(sha1, type, buf, size);
 113}
 114
 115static int resolve_delta(const char *type,
 116        void *base, unsigned long base_size,
 117        void *delta, unsigned long delta_size)
 118{
 119        void *result;
 120        unsigned long result_size;
 121
 122        result = patch_delta(base, base_size,
 123                             delta, delta_size,
 124                             &result_size);
 125        if (!result)
 126                die("failed to apply delta");
 127        free(delta);
 128        write_object(result, result_size, type);
 129        free(result);
 130        return 0;
 131}
 132
 133static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size)
 134{
 135        struct delta_info **p = &delta_list;
 136        struct delta_info *info;
 137
 138        while ((info = *p) != NULL) {
 139                if (!memcmp(info->base_sha1, sha1, 20)) {
 140                        *p = info->next;
 141                        p = &delta_list;
 142                        resolve_delta(type, data, size, info->delta, info->size);
 143                        free(info);
 144                        continue;
 145                }
 146                p = &info->next;
 147        }
 148}
 149
 150static int unpack_non_delta_entry(enum object_type kind, unsigned long size)
 151{
 152        void *buf = get_data(size);
 153        const char *type;
 154
 155        switch (kind) {
 156        case OBJ_COMMIT: type = commit_type; break;
 157        case OBJ_TREE:   type = tree_type; break;
 158        case OBJ_BLOB:   type = blob_type; break;
 159        case OBJ_TAG:    type = tag_type; break;
 160        default: die("bad type %d", kind);
 161        }
 162        if (!dry_run)
 163                write_object(buf, size, type);
 164        free(buf);
 165        return 0;
 166}
 167
 168static int unpack_delta_entry(unsigned long delta_size)
 169{
 170        void *delta_data, *base;
 171        unsigned long base_size;
 172        char type[20];
 173        unsigned char base_sha1[20];
 174        int result;
 175
 176        memcpy(base_sha1, fill(20), 20);
 177        use(20);
 178
 179        delta_data = get_data(delta_size);
 180        if (dry_run) {
 181                free(delta_data);
 182                return 0;
 183        }
 184
 185        if (!has_sha1_file(base_sha1)) {
 186                add_delta_to_list(base_sha1, delta_data, delta_size);
 187                return 0;
 188        }
 189        base = read_sha1_file(base_sha1, type, &base_size);
 190        if (!base)
 191                die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
 192        result = resolve_delta(type, base, base_size, delta_data, delta_size);
 193        free(base);
 194        return result;
 195}
 196
 197static void unpack_one(unsigned nr, unsigned total)
 198{
 199        unsigned shift;
 200        unsigned char *pack, c;
 201        unsigned long size;
 202        enum object_type type;
 203
 204        pack = fill(1);
 205        c = *pack;
 206        use(1);
 207        type = (c >> 4) & 7;
 208        size = (c & 15);
 209        shift = 4;
 210        while (c & 0x80) {
 211                pack = fill(1);
 212                c = *pack++;
 213                use(1);
 214                size += (c & 0x7f) << shift;
 215                shift += 7;
 216        }
 217        if (!quiet) {
 218                static unsigned long last_sec;
 219                static unsigned last_percent;
 220                struct timeval now;
 221                unsigned percentage = (nr * 100) / total;
 222
 223                gettimeofday(&now, NULL);
 224                if (percentage != last_percent || now.tv_sec != last_sec) {
 225                        last_sec = now.tv_sec;
 226                        last_percent = percentage;
 227                        fprintf(stderr, "%4u%% (%u/%u) done\r", percentage, nr, total);
 228                }
 229        }
 230        switch (type) {
 231        case OBJ_COMMIT:
 232        case OBJ_TREE:
 233        case OBJ_BLOB:
 234        case OBJ_TAG:
 235                unpack_non_delta_entry(type, size);
 236                return;
 237        case OBJ_DELTA:
 238                unpack_delta_entry(size);
 239                return;
 240        default:
 241                die("bad object type %d", type);
 242        }
 243}
 244
 245static void unpack_all(void)
 246{
 247        int i;
 248        struct pack_header *hdr = fill(sizeof(struct pack_header));
 249        unsigned nr_objects = ntohl(hdr->hdr_entries);
 250
 251        if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
 252                die("bad pack file");
 253        if (!pack_version_ok(hdr->hdr_version))
 254                die("unknown pack file version %d", ntohl(hdr->hdr_version));
 255        fprintf(stderr, "Unpacking %d objects\n", nr_objects);
 256
 257        use(sizeof(struct pack_header));
 258        for (i = 0; i < nr_objects; i++)
 259                unpack_one(i+1, nr_objects);
 260        if (delta_list)
 261                die("unresolved deltas left after unpacking");
 262}
 263
 264int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
 265{
 266        int i;
 267        unsigned char sha1[20];
 268
 269        git_config(git_default_config);
 270
 271        quiet = !isatty(2);
 272
 273        for (i = 1 ; i < argc; i++) {
 274                const char *arg = argv[i];
 275
 276                if (*arg == '-') {
 277                        if (!strcmp(arg, "-n")) {
 278                                dry_run = 1;
 279                                continue;
 280                        }
 281                        if (!strcmp(arg, "-q")) {
 282                                quiet = 1;
 283                                continue;
 284                        }
 285                        usage(unpack_usage);
 286                }
 287
 288                /* We don't take any non-flag arguments now.. Maybe some day */
 289                usage(unpack_usage);
 290        }
 291        SHA1_Init(&ctx);
 292        unpack_all();
 293        SHA1_Update(&ctx, buffer, offset);
 294        SHA1_Final(sha1, &ctx);
 295        if (memcmp(fill(20), sha1, 20))
 296                die("final sha1 did not match");
 297        use(20);
 298
 299        /* Write the last part of the buffer to stdout */
 300        while (len) {
 301                int ret = xwrite(1, buffer + offset, len);
 302                if (ret <= 0)
 303                        break;
 304                len -= ret;
 305                offset += ret;
 306        }
 307
 308        /* All done */
 309        if (!quiet)
 310                fprintf(stderr, "\n");
 311        return 0;
 312}