f7dab0ec60008decb8d9b48c679d28e994860352
   1/*
   2*
   3* Copyright 2005, Lukas Sandstrom <lukass@etek.chalmers.se>
   4*
   5* This file is licensed under the GPL v2.
   6*
   7*/
   8
   9#include "builtin.h"
  10#include "repository.h"
  11#include "packfile.h"
  12#include "object-store.h"
  13
  14#define BLKSIZE 512
  15
  16static const char pack_redundant_usage[] =
  17"git pack-redundant [--verbose] [--alt-odb] (--all | <filename.pack>...)";
  18
  19static int load_all_packs, verbose, alt_odb;
  20
  21struct llist_item {
  22        struct llist_item *next;
  23        const struct object_id *oid;
  24};
  25static struct llist {
  26        struct llist_item *front;
  27        struct llist_item *back;
  28        size_t size;
  29} *all_objects; /* all objects which must be present in local packfiles */
  30
  31static struct pack_list {
  32        struct pack_list *next;
  33        struct packed_git *pack;
  34        struct llist *unique_objects;
  35        struct llist *all_objects;
  36} *local_packs = NULL, *altodb_packs = NULL;
  37
  38struct pll {
  39        struct pll *next;
  40        struct pack_list *pl;
  41};
  42
  43static struct llist_item *free_nodes;
  44
  45static inline void llist_item_put(struct llist_item *item)
  46{
  47        item->next = free_nodes;
  48        free_nodes = item;
  49}
  50
  51static inline struct llist_item *llist_item_get(void)
  52{
  53        struct llist_item *new_item;
  54        if ( free_nodes ) {
  55                new_item = free_nodes;
  56                free_nodes = free_nodes->next;
  57        } else {
  58                int i = 1;
  59                ALLOC_ARRAY(new_item, BLKSIZE);
  60                for (; i < BLKSIZE; i++)
  61                        llist_item_put(&new_item[i]);
  62        }
  63        return new_item;
  64}
  65
  66static void llist_free(struct llist *list)
  67{
  68        while ((list->back = list->front)) {
  69                list->front = list->front->next;
  70                llist_item_put(list->back);
  71        }
  72        free(list);
  73}
  74
  75static inline void llist_init(struct llist **list)
  76{
  77        *list = xmalloc(sizeof(struct llist));
  78        (*list)->front = (*list)->back = NULL;
  79        (*list)->size = 0;
  80}
  81
  82static struct llist * llist_copy(struct llist *list)
  83{
  84        struct llist *ret;
  85        struct llist_item *new_item, *old_item, *prev;
  86
  87        llist_init(&ret);
  88
  89        if ((ret->size = list->size) == 0)
  90                return ret;
  91
  92        new_item = ret->front = llist_item_get();
  93        new_item->oid = list->front->oid;
  94
  95        old_item = list->front->next;
  96        while (old_item) {
  97                prev = new_item;
  98                new_item = llist_item_get();
  99                prev->next = new_item;
 100                new_item->oid = old_item->oid;
 101                old_item = old_item->next;
 102        }
 103        new_item->next = NULL;
 104        ret->back = new_item;
 105
 106        return ret;
 107}
 108
 109static inline struct llist_item *llist_insert(struct llist *list,
 110                                              struct llist_item *after,
 111                                              const struct object_id *oid)
 112{
 113        struct llist_item *new_item = llist_item_get();
 114        new_item->oid = oid;
 115        new_item->next = NULL;
 116
 117        if (after != NULL) {
 118                new_item->next = after->next;
 119                after->next = new_item;
 120                if (after == list->back)
 121                        list->back = new_item;
 122        } else {/* insert in front */
 123                if (list->size == 0)
 124                        list->back = new_item;
 125                else
 126                        new_item->next = list->front;
 127                list->front = new_item;
 128        }
 129        list->size++;
 130        return new_item;
 131}
 132
 133static inline struct llist_item *llist_insert_back(struct llist *list,
 134                                                   const struct object_id *oid)
 135{
 136        return llist_insert(list, list->back, oid);
 137}
 138
 139static inline struct llist_item *llist_insert_sorted_unique(struct llist *list,
 140                        const struct object_id *oid, struct llist_item *hint)
 141{
 142        struct llist_item *prev = NULL, *l;
 143
 144        l = (hint == NULL) ? list->front : hint;
 145        while (l) {
 146                int cmp = oidcmp(l->oid, oid);
 147                if (cmp > 0) { /* we insert before this entry */
 148                        return llist_insert(list, prev, oid);
 149                }
 150                if (!cmp) { /* already exists */
 151                        return l;
 152                }
 153                prev = l;
 154                l = l->next;
 155        }
 156        /* insert at the end */
 157        return llist_insert_back(list, oid);
 158}
 159
 160/* returns a pointer to an item in front of sha1 */
 161static inline struct llist_item * llist_sorted_remove(struct llist *list, const struct object_id *oid, struct llist_item *hint)
 162{
 163        struct llist_item *prev, *l;
 164
 165redo_from_start:
 166        l = (hint == NULL) ? list->front : hint;
 167        prev = NULL;
 168        while (l) {
 169                int cmp = oidcmp(l->oid, oid);
 170                if (cmp > 0) /* not in list, since sorted */
 171                        return prev;
 172                if (!cmp) { /* found */
 173                        if (prev == NULL) {
 174                                if (hint != NULL && hint != list->front) {
 175                                        /* we don't know the previous element */
 176                                        hint = NULL;
 177                                        goto redo_from_start;
 178                                }
 179                                list->front = l->next;
 180                        } else
 181                                prev->next = l->next;
 182                        if (l == list->back)
 183                                list->back = prev;
 184                        llist_item_put(l);
 185                        list->size--;
 186                        return prev;
 187                }
 188                prev = l;
 189                l = l->next;
 190        }
 191        return prev;
 192}
 193
 194/* computes A\B */
 195static void llist_sorted_difference_inplace(struct llist *A,
 196                                     struct llist *B)
 197{
 198        struct llist_item *hint, *b;
 199
 200        hint = NULL;
 201        b = B->front;
 202
 203        while (b) {
 204                hint = llist_sorted_remove(A, b->oid, hint);
 205                b = b->next;
 206        }
 207}
 208
 209static inline struct pack_list * pack_list_insert(struct pack_list **pl,
 210                                           struct pack_list *entry)
 211{
 212        struct pack_list *p = xmalloc(sizeof(struct pack_list));
 213        memcpy(p, entry, sizeof(struct pack_list));
 214        p->next = *pl;
 215        *pl = p;
 216        return p;
 217}
 218
 219static inline size_t pack_list_size(struct pack_list *pl)
 220{
 221        size_t ret = 0;
 222        while (pl) {
 223                ret++;
 224                pl = pl->next;
 225        }
 226        return ret;
 227}
 228
 229static struct pack_list * pack_list_difference(const struct pack_list *A,
 230                                               const struct pack_list *B)
 231{
 232        struct pack_list *ret;
 233        const struct pack_list *pl;
 234
 235        if (A == NULL)
 236                return NULL;
 237
 238        pl = B;
 239        while (pl != NULL) {
 240                if (A->pack == pl->pack)
 241                        return pack_list_difference(A->next, B);
 242                pl = pl->next;
 243        }
 244        ret = xmalloc(sizeof(struct pack_list));
 245        memcpy(ret, A, sizeof(struct pack_list));
 246        ret->next = pack_list_difference(A->next, B);
 247        return ret;
 248}
 249
 250static void cmp_two_packs(struct pack_list *p1, struct pack_list *p2)
 251{
 252        unsigned long p1_off = 0, p2_off = 0, p1_step, p2_step;
 253        const unsigned char *p1_base, *p2_base;
 254        struct llist_item *p1_hint = NULL, *p2_hint = NULL;
 255        const unsigned int hashsz = the_hash_algo->rawsz;
 256
 257        if (!p1->unique_objects)
 258                p1->unique_objects = llist_copy(p1->all_objects);
 259        if (!p2->unique_objects)
 260                p2->unique_objects = llist_copy(p2->all_objects);
 261
 262        p1_base = p1->pack->index_data;
 263        p2_base = p2->pack->index_data;
 264        p1_base += 256 * 4 + ((p1->pack->index_version < 2) ? 4 : 8);
 265        p2_base += 256 * 4 + ((p2->pack->index_version < 2) ? 4 : 8);
 266        p1_step = hashsz + ((p1->pack->index_version < 2) ? 4 : 0);
 267        p2_step = hashsz + ((p2->pack->index_version < 2) ? 4 : 0);
 268
 269        while (p1_off < p1->pack->num_objects * p1_step &&
 270               p2_off < p2->pack->num_objects * p2_step)
 271        {
 272                int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off);
 273                /* cmp ~ p1 - p2 */
 274                if (cmp == 0) {
 275                        p1_hint = llist_sorted_remove(p1->unique_objects,
 276                                        (const struct object_id *)(p1_base + p1_off),
 277                                        p1_hint);
 278                        p2_hint = llist_sorted_remove(p2->unique_objects,
 279                                        (const struct object_id *)(p1_base + p1_off),
 280                                        p2_hint);
 281                        p1_off += p1_step;
 282                        p2_off += p2_step;
 283                        continue;
 284                }
 285                if (cmp < 0) { /* p1 has the object, p2 doesn't */
 286                        p1_off += p1_step;
 287                } else { /* p2 has the object, p1 doesn't */
 288                        p2_off += p2_step;
 289                }
 290        }
 291}
 292
 293static void pll_free(struct pll *l)
 294{
 295        struct pll *old;
 296        struct pack_list *opl;
 297
 298        while (l) {
 299                old = l;
 300                while (l->pl) {
 301                        opl = l->pl;
 302                        l->pl = opl->next;
 303                        free(opl);
 304                }
 305                l = l->next;
 306                free(old);
 307        }
 308}
 309
 310/* all the permutations have to be free()d at the same time,
 311 * since they refer to each other
 312 */
 313static struct pll * get_permutations(struct pack_list *list, int n)
 314{
 315        struct pll *subset, *ret = NULL, *new_pll = NULL;
 316
 317        if (list == NULL || pack_list_size(list) < n || n == 0)
 318                return NULL;
 319
 320        if (n == 1) {
 321                while (list) {
 322                        new_pll = xmalloc(sizeof(*new_pll));
 323                        new_pll->pl = NULL;
 324                        pack_list_insert(&new_pll->pl, list);
 325                        new_pll->next = ret;
 326                        ret = new_pll;
 327                        list = list->next;
 328                }
 329                return ret;
 330        }
 331
 332        while (list->next) {
 333                subset = get_permutations(list->next, n - 1);
 334                while (subset) {
 335                        new_pll = xmalloc(sizeof(*new_pll));
 336                        new_pll->pl = subset->pl;
 337                        pack_list_insert(&new_pll->pl, list);
 338                        new_pll->next = ret;
 339                        ret = new_pll;
 340                        subset = subset->next;
 341                }
 342                list = list->next;
 343        }
 344        return ret;
 345}
 346
 347static int is_superset(struct pack_list *pl, struct llist *list)
 348{
 349        struct llist *diff;
 350
 351        diff = llist_copy(list);
 352
 353        while (pl) {
 354                llist_sorted_difference_inplace(diff, pl->all_objects);
 355                if (diff->size == 0) { /* we're done */
 356                        llist_free(diff);
 357                        return 1;
 358                }
 359                pl = pl->next;
 360        }
 361        llist_free(diff);
 362        return 0;
 363}
 364
 365static size_t sizeof_union(struct packed_git *p1, struct packed_git *p2)
 366{
 367        size_t ret = 0;
 368        unsigned long p1_off = 0, p2_off = 0, p1_step, p2_step;
 369        const unsigned char *p1_base, *p2_base;
 370        const unsigned int hashsz = the_hash_algo->rawsz;
 371
 372        p1_base = p1->index_data;
 373        p2_base = p2->index_data;
 374        p1_base += 256 * 4 + ((p1->index_version < 2) ? 4 : 8);
 375        p2_base += 256 * 4 + ((p2->index_version < 2) ? 4 : 8);
 376        p1_step = hashsz + ((p1->index_version < 2) ? 4 : 0);
 377        p2_step = hashsz + ((p2->index_version < 2) ? 4 : 0);
 378
 379        while (p1_off < p1->num_objects * p1_step &&
 380               p2_off < p2->num_objects * p2_step)
 381        {
 382                int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off);
 383                /* cmp ~ p1 - p2 */
 384                if (cmp == 0) {
 385                        ret++;
 386                        p1_off += p1_step;
 387                        p2_off += p2_step;
 388                        continue;
 389                }
 390                if (cmp < 0) { /* p1 has the object, p2 doesn't */
 391                        p1_off += p1_step;
 392                } else { /* p2 has the object, p1 doesn't */
 393                        p2_off += p2_step;
 394                }
 395        }
 396        return ret;
 397}
 398
 399/* another O(n^2) function ... */
 400static size_t get_pack_redundancy(struct pack_list *pl)
 401{
 402        struct pack_list *subset;
 403        size_t ret = 0;
 404
 405        if (pl == NULL)
 406                return 0;
 407
 408        while ((subset = pl->next)) {
 409                while (subset) {
 410                        ret += sizeof_union(pl->pack, subset->pack);
 411                        subset = subset->next;
 412                }
 413                pl = pl->next;
 414        }
 415        return ret;
 416}
 417
 418static inline off_t pack_set_bytecount(struct pack_list *pl)
 419{
 420        off_t ret = 0;
 421        while (pl) {
 422                ret += pl->pack->pack_size;
 423                ret += pl->pack->index_size;
 424                pl = pl->next;
 425        }
 426        return ret;
 427}
 428
 429static void minimize(struct pack_list **min)
 430{
 431        struct pack_list *pl, *unique = NULL,
 432                *non_unique = NULL, *min_perm = NULL;
 433        struct pll *perm, *perm_all, *perm_ok = NULL, *new_perm;
 434        struct llist *missing;
 435        off_t min_perm_size = 0, perm_size;
 436        int n;
 437
 438        pl = local_packs;
 439        while (pl) {
 440                if (pl->unique_objects->size)
 441                        pack_list_insert(&unique, pl);
 442                else
 443                        pack_list_insert(&non_unique, pl);
 444                pl = pl->next;
 445        }
 446        /* find out which objects are missing from the set of unique packs */
 447        missing = llist_copy(all_objects);
 448        pl = unique;
 449        while (pl) {
 450                llist_sorted_difference_inplace(missing, pl->all_objects);
 451                pl = pl->next;
 452        }
 453
 454        /* return if there are no objects missing from the unique set */
 455        if (missing->size == 0) {
 456                *min = unique;
 457                free(missing);
 458                return;
 459        }
 460
 461        /* find the permutations which contain all missing objects */
 462        for (n = 1; n <= pack_list_size(non_unique) && !perm_ok; n++) {
 463                perm_all = perm = get_permutations(non_unique, n);
 464                while (perm) {
 465                        if (is_superset(perm->pl, missing)) {
 466                                new_perm = xmalloc(sizeof(struct pll));
 467                                memcpy(new_perm, perm, sizeof(struct pll));
 468                                new_perm->next = perm_ok;
 469                                perm_ok = new_perm;
 470                        }
 471                        perm = perm->next;
 472                }
 473                if (perm_ok)
 474                        break;
 475                pll_free(perm_all);
 476        }
 477        if (perm_ok == NULL)
 478                die("Internal error: No complete sets found!");
 479
 480        /* find the permutation with the smallest size */
 481        perm = perm_ok;
 482        while (perm) {
 483                perm_size = pack_set_bytecount(perm->pl);
 484                if (!min_perm_size || min_perm_size > perm_size) {
 485                        min_perm_size = perm_size;
 486                        min_perm = perm->pl;
 487                }
 488                perm = perm->next;
 489        }
 490        *min = min_perm;
 491        /* add the unique packs to the list */
 492        pl = unique;
 493        while (pl) {
 494                pack_list_insert(min, pl);
 495                pl = pl->next;
 496        }
 497}
 498
 499static void load_all_objects(void)
 500{
 501        struct pack_list *pl = local_packs;
 502        struct llist_item *hint, *l;
 503
 504        llist_init(&all_objects);
 505
 506        while (pl) {
 507                hint = NULL;
 508                l = pl->all_objects->front;
 509                while (l) {
 510                        hint = llist_insert_sorted_unique(all_objects,
 511                                                          l->oid, hint);
 512                        l = l->next;
 513                }
 514                pl = pl->next;
 515        }
 516        /* remove objects present in remote packs */
 517        pl = altodb_packs;
 518        while (pl) {
 519                llist_sorted_difference_inplace(all_objects, pl->all_objects);
 520                pl = pl->next;
 521        }
 522}
 523
 524/* this scales like O(n^2) */
 525static void cmp_local_packs(void)
 526{
 527        struct pack_list *subset, *pl = local_packs;
 528
 529        while ((subset = pl)) {
 530                while ((subset = subset->next))
 531                        cmp_two_packs(pl, subset);
 532                pl = pl->next;
 533        }
 534}
 535
 536static void scan_alt_odb_packs(void)
 537{
 538        struct pack_list *local, *alt;
 539
 540        alt = altodb_packs;
 541        while (alt) {
 542                local = local_packs;
 543                while (local) {
 544                        llist_sorted_difference_inplace(local->all_objects,
 545                                                        alt->all_objects);
 546                        local = local->next;
 547                }
 548                llist_sorted_difference_inplace(all_objects, alt->all_objects);
 549                alt = alt->next;
 550        }
 551}
 552
 553static struct pack_list * add_pack(struct packed_git *p)
 554{
 555        struct pack_list l;
 556        unsigned long off = 0, step;
 557        const unsigned char *base;
 558
 559        if (!p->pack_local && !(alt_odb || verbose))
 560                return NULL;
 561
 562        l.pack = p;
 563        llist_init(&l.all_objects);
 564
 565        if (open_pack_index(p))
 566                return NULL;
 567
 568        base = p->index_data;
 569        base += 256 * 4 + ((p->index_version < 2) ? 4 : 8);
 570        step = the_hash_algo->rawsz + ((p->index_version < 2) ? 4 : 0);
 571        while (off < p->num_objects * step) {
 572                llist_insert_back(l.all_objects, (const struct object_id *)(base + off));
 573                off += step;
 574        }
 575        l.unique_objects = NULL;
 576        if (p->pack_local)
 577                return pack_list_insert(&local_packs, &l);
 578        else
 579                return pack_list_insert(&altodb_packs, &l);
 580}
 581
 582static struct pack_list * add_pack_file(const char *filename)
 583{
 584        struct packed_git *p = get_all_packs(the_repository);
 585
 586        if (strlen(filename) < 40)
 587                die("Bad pack filename: %s", filename);
 588
 589        while (p) {
 590                if (strstr(p->pack_name, filename))
 591                        return add_pack(p);
 592                p = p->next;
 593        }
 594        die("Filename %s not found in packed_git", filename);
 595}
 596
 597static void load_all(void)
 598{
 599        struct packed_git *p = get_all_packs(the_repository);
 600
 601        while (p) {
 602                add_pack(p);
 603                p = p->next;
 604        }
 605}
 606
 607int cmd_pack_redundant(int argc, const char **argv, const char *prefix)
 608{
 609        int i;
 610        struct pack_list *min, *red, *pl;
 611        struct llist *ignore;
 612        struct object_id *oid;
 613        char buf[GIT_MAX_HEXSZ + 2]; /* hex hash + \n + \0 */
 614
 615        if (argc == 2 && !strcmp(argv[1], "-h"))
 616                usage(pack_redundant_usage);
 617
 618        for (i = 1; i < argc; i++) {
 619                const char *arg = argv[i];
 620                if (!strcmp(arg, "--")) {
 621                        i++;
 622                        break;
 623                }
 624                if (!strcmp(arg, "--all")) {
 625                        load_all_packs = 1;
 626                        continue;
 627                }
 628                if (!strcmp(arg, "--verbose")) {
 629                        verbose = 1;
 630                        continue;
 631                }
 632                if (!strcmp(arg, "--alt-odb")) {
 633                        alt_odb = 1;
 634                        continue;
 635                }
 636                if (*arg == '-')
 637                        usage(pack_redundant_usage);
 638                else
 639                        break;
 640        }
 641
 642        if (load_all_packs)
 643                load_all();
 644        else
 645                while (*(argv + i) != NULL)
 646                        add_pack_file(*(argv + i++));
 647
 648        if (local_packs == NULL)
 649                die("Zero packs found!");
 650
 651        load_all_objects();
 652
 653        if (alt_odb)
 654                scan_alt_odb_packs();
 655
 656        /* ignore objects given on stdin */
 657        llist_init(&ignore);
 658        if (!isatty(0)) {
 659                while (fgets(buf, sizeof(buf), stdin)) {
 660                        oid = xmalloc(sizeof(*oid));
 661                        if (get_oid_hex(buf, oid))
 662                                die("Bad object ID on stdin: %s", buf);
 663                        llist_insert_sorted_unique(ignore, oid, NULL);
 664                }
 665        }
 666        llist_sorted_difference_inplace(all_objects, ignore);
 667        pl = local_packs;
 668        while (pl) {
 669                llist_sorted_difference_inplace(pl->all_objects, ignore);
 670                pl = pl->next;
 671        }
 672
 673        cmp_local_packs();
 674
 675        minimize(&min);
 676
 677        if (verbose) {
 678                fprintf(stderr, "There are %lu packs available in alt-odbs.\n",
 679                        (unsigned long)pack_list_size(altodb_packs));
 680                fprintf(stderr, "The smallest (bytewise) set of packs is:\n");
 681                pl = min;
 682                while (pl) {
 683                        fprintf(stderr, "\t%s\n", pl->pack->pack_name);
 684                        pl = pl->next;
 685                }
 686                fprintf(stderr, "containing %lu duplicate objects "
 687                                "with a total size of %lukb.\n",
 688                        (unsigned long)get_pack_redundancy(min),
 689                        (unsigned long)pack_set_bytecount(min)/1024);
 690                fprintf(stderr, "A total of %lu unique objects were considered.\n",
 691                        (unsigned long)all_objects->size);
 692                fprintf(stderr, "Redundant packs (with indexes):\n");
 693        }
 694        pl = red = pack_list_difference(local_packs, min);
 695        while (pl) {
 696                printf("%s\n%s\n",
 697                       sha1_pack_index_name(pl->pack->sha1),
 698                       pl->pack->pack_name);
 699                pl = pl->next;
 700        }
 701        if (verbose)
 702                fprintf(stderr, "%luMB of redundant packs in total.\n",
 703                        (unsigned long)pack_set_bytecount(red)/(1024*1024));
 704
 705        return 0;
 706}