780418dcec524fa294064e26a8403d4411cced62
   1#include <ctype.h>
   2#include "cache.h"
   3#include "object.h"
   4#include "delta.h"
   5#include "pack.h"
   6#include "csum-file.h"
   7
   8static const char pack_usage[] = "git-pack-objects [--incremental] [--window=N] [--depth=N] {--stdout | base-name} < object-list";
   9
  10struct object_entry {
  11        unsigned char sha1[20];
  12        unsigned long size;
  13        unsigned long offset;
  14        unsigned int depth;
  15        unsigned int hash;
  16        enum object_type type;
  17        unsigned long delta_size;
  18        struct object_entry *delta;
  19};
  20
  21static int incremental = 0;
  22static struct object_entry **sorted_by_sha, **sorted_by_type;
  23static struct object_entry *objects = NULL;
  24static int nr_objects = 0, nr_alloc = 0;
  25static const char *base_name;
  26static unsigned char pack_file_sha1[20];
  27
  28static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
  29{
  30        unsigned long othersize, delta_size;
  31        char type[10];
  32        void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
  33        void *delta_buf;
  34
  35        if (!otherbuf)
  36                die("unable to read %s", sha1_to_hex(entry->delta->sha1));
  37        delta_buf = diff_delta(otherbuf, othersize,
  38                               buf, size, &delta_size, 0);
  39        if (!delta_buf || delta_size != entry->delta_size)
  40                die("delta size changed");
  41        free(buf);
  42        free(otherbuf);
  43        return delta_buf;
  44}
  45
  46/*
  47 * The per-object header is a pretty dense thing, which is
  48 *  - first byte: low four bits are "size", then three bits of "type",
  49 *    and the high bit is "size continues".
  50 *  - each byte afterwards: low seven bits are size continuation,
  51 *    with the high bit being "size continues"
  52 */
  53static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
  54{
  55        int n = 1;
  56        unsigned char c;
  57
  58        if (type < OBJ_COMMIT || type > OBJ_DELTA)
  59                die("bad type %d", type);
  60
  61        c = (type << 4) | (size & 15);
  62        size >>= 4;
  63        while (size) {
  64                *hdr++ = c | 0x80;
  65                c = size & 0x7f;
  66                size >>= 7;
  67                n++;
  68        }
  69        *hdr = c;
  70        return n;
  71}
  72
  73static unsigned long write_object(struct sha1file *f, struct object_entry *entry)
  74{
  75        unsigned long size;
  76        char type[10];
  77        void *buf = read_sha1_file(entry->sha1, type, &size);
  78        unsigned char header[10];
  79        unsigned hdrlen, datalen;
  80        enum object_type obj_type;
  81
  82        if (!buf)
  83                die("unable to read %s", sha1_to_hex(entry->sha1));
  84        if (size != entry->size)
  85                die("object %s size inconsistency (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
  86
  87        /*
  88         * The object header is a byte of 'type' followed by zero or
  89         * more bytes of length.  For deltas, the 20 bytes of delta sha1
  90         * follows that.
  91         */
  92        obj_type = entry->type;
  93        if (entry->delta) {
  94                buf = delta_against(buf, size, entry);
  95                size = entry->delta_size;
  96                obj_type = OBJ_DELTA;
  97        }
  98        hdrlen = encode_header(obj_type, size, header);
  99        sha1write(f, header, hdrlen);
 100        if (entry->delta) {
 101                sha1write(f, entry->delta, 20);
 102                hdrlen += 20;
 103        }
 104        datalen = sha1write_compressed(f, buf, size);
 105        free(buf);
 106        return hdrlen + datalen;
 107}
 108
 109static unsigned long write_one(struct sha1file *f,
 110                               struct object_entry *e,
 111                               unsigned long offset)
 112{
 113        if (e->offset)
 114                /* offset starts from header size and cannot be zero
 115                 * if it is written already.
 116                 */
 117                return offset;
 118        e->offset = offset;
 119        offset += write_object(f, e);
 120        /* if we are delitified, write out its base object. */
 121        if (e->delta)
 122                offset = write_one(f, e->delta, offset);
 123        return offset;
 124}
 125
 126static void write_pack_file(void)
 127{
 128        int i;
 129        struct sha1file *f;
 130        unsigned long offset;
 131        unsigned long mb;
 132        struct pack_header hdr;
 133
 134        if (!base_name)
 135                f = sha1fd(1, "<stdout>");
 136        else
 137                f = sha1create("%s.%s", base_name, "pack");
 138        hdr.hdr_signature = htonl(PACK_SIGNATURE);
 139        hdr.hdr_version = htonl(PACK_VERSION);
 140        hdr.hdr_entries = htonl(nr_objects);
 141        sha1write(f, &hdr, sizeof(hdr));
 142        offset = sizeof(hdr);
 143        for (i = 0; i < nr_objects; i++)
 144                offset = write_one(f, objects + i, offset);
 145
 146        sha1close(f, pack_file_sha1, 1);
 147        mb = offset >> 20;
 148        offset &= 0xfffff;
 149}
 150
 151static void write_index_file(void)
 152{
 153        int i;
 154        struct sha1file *f = sha1create("%s.%s", base_name, "idx");
 155        struct object_entry **list = sorted_by_sha;
 156        struct object_entry **last = list + nr_objects;
 157        unsigned int array[256];
 158
 159        /*
 160         * Write the first-level table (the list is sorted,
 161         * but we use a 256-entry lookup to be able to avoid
 162         * having to do eight extra binary search iterations).
 163         */
 164        for (i = 0; i < 256; i++) {
 165                struct object_entry **next = list;
 166                while (next < last) {
 167                        struct object_entry *entry = *next;
 168                        if (entry->sha1[0] != i)
 169                                break;
 170                        next++;
 171                }
 172                array[i] = htonl(next - sorted_by_sha);
 173                list = next;
 174        }
 175        sha1write(f, array, 256 * sizeof(int));
 176
 177        /*
 178         * Write the actual SHA1 entries..
 179         */
 180        list = sorted_by_sha;
 181        for (i = 0; i < nr_objects; i++) {
 182                struct object_entry *entry = *list++;
 183                unsigned int offset = htonl(entry->offset);
 184                sha1write(f, &offset, 4);
 185                sha1write(f, entry->sha1, 20);
 186        }
 187        sha1write(f, pack_file_sha1, 20);
 188        sha1close(f, NULL, 1);
 189}
 190
 191static void add_object_entry(unsigned char *sha1, unsigned int hash)
 192{
 193        unsigned int idx = nr_objects;
 194        struct object_entry *entry;
 195
 196        if (incremental && has_sha1_pack(sha1))
 197                return;
 198
 199        if (idx >= nr_alloc) {
 200                unsigned int needed = (idx + 1024) * 3 / 2;
 201                objects = xrealloc(objects, needed * sizeof(*entry));
 202                nr_alloc = needed;
 203        }
 204        entry = objects + idx;
 205        memset(entry, 0, sizeof(*entry));
 206        memcpy(entry->sha1, sha1, 20);
 207        entry->hash = hash;
 208        nr_objects = idx+1;
 209}
 210
 211static void check_object(struct object_entry *entry)
 212{
 213        char type[20];
 214
 215        if (!sha1_object_info(entry->sha1, type, &entry->size)) {
 216                if (!strcmp(type, "commit")) {
 217                        entry->type = OBJ_COMMIT;
 218                } else if (!strcmp(type, "tree")) {
 219                        entry->type = OBJ_TREE;
 220                } else if (!strcmp(type, "blob")) {
 221                        entry->type = OBJ_BLOB;
 222                } else if (!strcmp(type, "tag")) {
 223                        entry->type = OBJ_TAG;
 224                } else
 225                        die("unable to pack object %s of type %s",
 226                            sha1_to_hex(entry->sha1), type);
 227        }
 228        else
 229                die("unable to get type of object %s",
 230                    sha1_to_hex(entry->sha1));
 231}
 232
 233static void get_object_details(void)
 234{
 235        int i;
 236        struct object_entry *entry = objects;
 237
 238        for (i = 0; i < nr_objects; i++)
 239                check_object(entry++);
 240}
 241
 242typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
 243
 244static entry_sort_t current_sort;
 245
 246static int sort_comparator(const void *_a, const void *_b)
 247{
 248        struct object_entry *a = *(struct object_entry **)_a;
 249        struct object_entry *b = *(struct object_entry **)_b;
 250        return current_sort(a,b);
 251}
 252
 253static struct object_entry **create_sorted_list(entry_sort_t sort)
 254{
 255        struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
 256        int i;
 257
 258        for (i = 0; i < nr_objects; i++)
 259                list[i] = objects + i;
 260        current_sort = sort;
 261        qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
 262        return list;
 263}
 264
 265static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
 266{
 267        return memcmp(a->sha1, b->sha1, 20);
 268}
 269
 270static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
 271{
 272        if (a->type < b->type)
 273                return -1;
 274        if (a->type > b->type)
 275                return 1;
 276        if (a->hash < b->hash)
 277                return -1;
 278        if (a->hash > b->hash)
 279                return 1;
 280        if (a->size < b->size)
 281                return -1;
 282        if (a->size > b->size)
 283                return 1;
 284        return a < b ? -1 : (a > b);
 285}
 286
 287struct unpacked {
 288        struct object_entry *entry;
 289        void *data;
 290};
 291
 292/*
 293 * We search for deltas _backwards_ in a list sorted by type and
 294 * by size, so that we see progressively smaller and smaller files.
 295 * That's because we prefer deltas to be from the bigger file
 296 * to the smaller - deletes are potentially cheaper, but perhaps
 297 * more importantly, the bigger file is likely the more recent
 298 * one.
 299 */
 300static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth)
 301{
 302        struct object_entry *cur_entry = cur->entry;
 303        struct object_entry *old_entry = old->entry;
 304        unsigned long size, oldsize, delta_size, sizediff;
 305        long max_size;
 306        void *delta_buf;
 307
 308        /* Don't bother doing diffs between different types */
 309        if (cur_entry->type != old_entry->type)
 310                return -1;
 311
 312        size = cur_entry->size;
 313        if (size < 50)
 314                return -1;
 315        oldsize = old_entry->size;
 316        sizediff = oldsize > size ? oldsize - size : size - oldsize;
 317        if (sizediff > size / 8)
 318                return -1;
 319        if (old_entry->depth >= max_depth)
 320                return 0;
 321
 322        /*
 323         * NOTE!
 324         *
 325         * We always delta from the bigger to the smaller, since that's
 326         * more space-efficient (deletes don't have to say _what_ they
 327         * delete).
 328         */
 329        max_size = size / 2 - 20;
 330        if (cur_entry->delta)
 331                max_size = cur_entry->delta_size-1;
 332        if (sizediff >= max_size)
 333                return -1;
 334        delta_buf = diff_delta(old->data, oldsize,
 335                               cur->data, size, &delta_size, max_size);
 336        if (!delta_buf)
 337                return 0;
 338        cur_entry->delta = old_entry;
 339        cur_entry->delta_size = delta_size;
 340        cur_entry->depth = old_entry->depth + 1;
 341        free(delta_buf);
 342        return 0;
 343}
 344
 345static void find_deltas(struct object_entry **list, int window, int depth)
 346{
 347        int i, idx;
 348        unsigned int array_size = window * sizeof(struct unpacked);
 349        struct unpacked *array = xmalloc(array_size);
 350
 351        memset(array, 0, array_size);
 352        i = nr_objects;
 353        idx = 0;
 354        while (--i >= 0) {
 355                struct object_entry *entry = list[i];
 356                struct unpacked *n = array + idx;
 357                unsigned long size;
 358                char type[10];
 359                int j;
 360
 361                free(n->data);
 362                n->entry = entry;
 363                n->data = read_sha1_file(entry->sha1, type, &size);
 364                if (size != entry->size)
 365                        die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
 366                j = window;
 367                while (--j > 0) {
 368                        unsigned int other_idx = idx + j;
 369                        struct unpacked *m;
 370                        if (other_idx >= window)
 371                                other_idx -= window;
 372                        m = array + other_idx;
 373                        if (!m->entry)
 374                                break;
 375                        if (try_delta(n, m, depth) < 0)
 376                                break;
 377                }
 378                idx++;
 379                if (idx >= window)
 380                        idx = 0;
 381        }
 382}
 383
 384int main(int argc, char **argv)
 385{
 386        char line[PATH_MAX + 20];
 387        int window = 10, depth = 10, pack_to_stdout = 0;
 388        int i;
 389
 390        for (i = 1; i < argc; i++) {
 391                const char *arg = argv[i];
 392
 393                if (*arg == '-') {
 394                        if (!strcmp("--incremental", arg)) {
 395                                incremental = 1;
 396                                continue;
 397                        }
 398                        if (!strncmp("--window=", arg, 9)) {
 399                                char *end;
 400                                window = strtoul(arg+9, &end, 0);
 401                                if (!arg[9] || *end)
 402                                        usage(pack_usage);
 403                                continue;
 404                        }
 405                        if (!strncmp("--depth=", arg, 8)) {
 406                                char *end;
 407                                depth = strtoul(arg+8, &end, 0);
 408                                if (!arg[8] || *end)
 409                                        usage(pack_usage);
 410                                continue;
 411                        }
 412                        if (!strcmp("--stdout", arg)) {
 413                                pack_to_stdout = 1;
 414                                continue;
 415                        }
 416                        usage(pack_usage);
 417                }
 418                if (base_name)
 419                        usage(pack_usage);
 420                base_name = arg;
 421        }
 422
 423        if (pack_to_stdout != !base_name)
 424                usage(pack_usage);
 425
 426        while (fgets(line, sizeof(line), stdin) != NULL) {
 427                unsigned int hash;
 428                char *p;
 429                unsigned char sha1[20];
 430
 431                if (get_sha1_hex(line, sha1))
 432                        die("expected sha1, got garbage");
 433                hash = 0;
 434                p = line+40;
 435                while (*p) {
 436                        unsigned char c = *p++;
 437                        if (isspace(c))
 438                                continue;
 439                        hash = hash * 11 + c;
 440                }
 441                add_object_entry(sha1, hash);
 442        }
 443        get_object_details();
 444
 445        fprintf(stderr, "Packing %d objects\n", nr_objects);
 446
 447        sorted_by_sha = create_sorted_list(sha1_sort);
 448        sorted_by_type = create_sorted_list(type_size_sort);
 449        if (window && depth)
 450                find_deltas(sorted_by_type, window+1, depth);
 451
 452        write_pack_file();
 453        if (!pack_to_stdout)
 454                write_index_file();
 455        return 0;
 456}