18ce47fcb7b788941d36e65f32b333ce672a8765
   1#include "../cache.h"
   2#include "../refs.h"
   3#include "refs-internal.h"
   4#include "ref-cache.h"
   5#include "packed-backend.h"
   6#include "../iterator.h"
   7#include "../lockfile.h"
   8
   9struct packed_ref_cache {
  10        struct ref_cache *cache;
  11
  12        /*
  13         * Count of references to the data structure in this instance,
  14         * including the pointer from files_ref_store::packed if any.
  15         * The data will not be freed as long as the reference count
  16         * is nonzero.
  17         */
  18        unsigned int referrers;
  19
  20        /* The metadata from when this packed-refs cache was read */
  21        struct stat_validity validity;
  22};
  23
  24/*
  25 * Increment the reference count of *packed_refs.
  26 */
  27static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
  28{
  29        packed_refs->referrers++;
  30}
  31
  32/*
  33 * Decrease the reference count of *packed_refs.  If it goes to zero,
  34 * free *packed_refs and return true; otherwise return false.
  35 */
  36static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
  37{
  38        if (!--packed_refs->referrers) {
  39                free_ref_cache(packed_refs->cache);
  40                stat_validity_clear(&packed_refs->validity);
  41                free(packed_refs);
  42                return 1;
  43        } else {
  44                return 0;
  45        }
  46}
  47
  48/*
  49 * A container for `packed-refs`-related data. It is not (yet) a
  50 * `ref_store`.
  51 */
  52struct packed_ref_store {
  53        struct ref_store base;
  54
  55        unsigned int store_flags;
  56
  57        /* The path of the "packed-refs" file: */
  58        char *path;
  59
  60        /*
  61         * A cache of the values read from the `packed-refs` file, if
  62         * it might still be current; otherwise, NULL.
  63         */
  64        struct packed_ref_cache *cache;
  65
  66        /*
  67         * Lock used for the "packed-refs" file. Note that this (and
  68         * thus the enclosing `packed_ref_store`) must not be freed.
  69         */
  70        struct lock_file lock;
  71};
  72
  73struct ref_store *packed_ref_store_create(const char *path,
  74                                          unsigned int store_flags)
  75{
  76        struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
  77        struct ref_store *ref_store = (struct ref_store *)refs;
  78
  79        base_ref_store_init(ref_store, &refs_be_packed);
  80        refs->store_flags = store_flags;
  81
  82        refs->path = xstrdup(path);
  83        return ref_store;
  84}
  85
  86/*
  87 * Die if refs is not the main ref store. caller is used in any
  88 * necessary error messages.
  89 */
  90static void packed_assert_main_repository(struct packed_ref_store *refs,
  91                                          const char *caller)
  92{
  93        if (refs->store_flags & REF_STORE_MAIN)
  94                return;
  95
  96        die("BUG: operation %s only allowed for main ref store", caller);
  97}
  98
  99/*
 100 * Downcast `ref_store` to `packed_ref_store`. Die if `ref_store` is
 101 * not a `packed_ref_store`. Also die if `packed_ref_store` doesn't
 102 * support at least the flags specified in `required_flags`. `caller`
 103 * is used in any necessary error messages.
 104 */
 105static struct packed_ref_store *packed_downcast(struct ref_store *ref_store,
 106                                                unsigned int required_flags,
 107                                                const char *caller)
 108{
 109        struct packed_ref_store *refs;
 110
 111        if (ref_store->be != &refs_be_packed)
 112                die("BUG: ref_store is type \"%s\" not \"packed\" in %s",
 113                    ref_store->be->name, caller);
 114
 115        refs = (struct packed_ref_store *)ref_store;
 116
 117        if ((refs->store_flags & required_flags) != required_flags)
 118                die("BUG: unallowed operation (%s), requires %x, has %x\n",
 119                    caller, required_flags, refs->store_flags);
 120
 121        return refs;
 122}
 123
 124static void clear_packed_ref_cache(struct packed_ref_store *refs)
 125{
 126        if (refs->cache) {
 127                struct packed_ref_cache *cache = refs->cache;
 128
 129                if (is_lock_file_locked(&refs->lock))
 130                        die("BUG: packed-ref cache cleared while locked");
 131                refs->cache = NULL;
 132                release_packed_ref_cache(cache);
 133        }
 134}
 135
 136/* The length of a peeled reference line in packed-refs, including EOL: */
 137#define PEELED_LINE_LENGTH 42
 138
 139/*
 140 * Parse one line from a packed-refs file.  Write the SHA1 to sha1.
 141 * Return a pointer to the refname within the line (null-terminated),
 142 * or NULL if there was a problem.
 143 */
 144static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
 145{
 146        const char *ref;
 147
 148        if (parse_oid_hex(line->buf, oid, &ref) < 0)
 149                return NULL;
 150        if (!isspace(*ref++))
 151                return NULL;
 152
 153        if (isspace(*ref))
 154                return NULL;
 155
 156        if (line->buf[line->len - 1] != '\n')
 157                return NULL;
 158        line->buf[--line->len] = 0;
 159
 160        return ref;
 161}
 162
 163/*
 164 * Read from `packed_refs_file` into a newly-allocated
 165 * `packed_ref_cache` and return it. The return value will already
 166 * have its reference count incremented.
 167 *
 168 * A comment line of the form "# pack-refs with: " may contain zero or
 169 * more traits. We interpret the traits as follows:
 170 *
 171 *   No traits:
 172 *
 173 *      Probably no references are peeled. But if the file contains a
 174 *      peeled value for a reference, we will use it.
 175 *
 176 *   peeled:
 177 *
 178 *      References under "refs/tags/", if they *can* be peeled, *are*
 179 *      peeled in this file. References outside of "refs/tags/" are
 180 *      probably not peeled even if they could have been, but if we find
 181 *      a peeled value for such a reference we will use it.
 182 *
 183 *   fully-peeled:
 184 *
 185 *      All references in the file that can be peeled are peeled.
 186 *      Inversely (and this is more important), any references in the
 187 *      file for which no peeled value is recorded is not peelable. This
 188 *      trait should typically be written alongside "peeled" for
 189 *      compatibility with older clients, but we do not require it
 190 *      (i.e., "peeled" is a no-op if "fully-peeled" is set).
 191 */
 192static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file)
 193{
 194        FILE *f;
 195        struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
 196        struct ref_entry *last = NULL;
 197        struct strbuf line = STRBUF_INIT;
 198        enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
 199        struct ref_dir *dir;
 200
 201        acquire_packed_ref_cache(packed_refs);
 202        packed_refs->cache = create_ref_cache(NULL, NULL);
 203        packed_refs->cache->root->flag &= ~REF_INCOMPLETE;
 204
 205        f = fopen(packed_refs_file, "r");
 206        if (!f) {
 207                if (errno == ENOENT) {
 208                        /*
 209                         * This is OK; it just means that no
 210                         * "packed-refs" file has been written yet,
 211                         * which is equivalent to it being empty.
 212                         */
 213                        return packed_refs;
 214                } else {
 215                        die_errno("couldn't read %s", packed_refs_file);
 216                }
 217        }
 218
 219        stat_validity_update(&packed_refs->validity, fileno(f));
 220
 221        dir = get_ref_dir(packed_refs->cache->root);
 222        while (strbuf_getwholeline(&line, f, '\n') != EOF) {
 223                struct object_id oid;
 224                const char *refname;
 225                const char *traits;
 226
 227                if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
 228                        if (strstr(traits, " fully-peeled "))
 229                                peeled = PEELED_FULLY;
 230                        else if (strstr(traits, " peeled "))
 231                                peeled = PEELED_TAGS;
 232                        /* perhaps other traits later as well */
 233                        continue;
 234                }
 235
 236                refname = parse_ref_line(&line, &oid);
 237                if (refname) {
 238                        int flag = REF_ISPACKED;
 239
 240                        if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
 241                                if (!refname_is_safe(refname))
 242                                        die("packed refname is dangerous: %s", refname);
 243                                oidclr(&oid);
 244                                flag |= REF_BAD_NAME | REF_ISBROKEN;
 245                        }
 246                        last = create_ref_entry(refname, &oid, flag);
 247                        if (peeled == PEELED_FULLY ||
 248                            (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
 249                                last->flag |= REF_KNOWS_PEELED;
 250                        add_ref_entry(dir, last);
 251                        continue;
 252                }
 253                if (last &&
 254                    line.buf[0] == '^' &&
 255                    line.len == PEELED_LINE_LENGTH &&
 256                    line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
 257                    !get_oid_hex(line.buf + 1, &oid)) {
 258                        oidcpy(&last->u.value.peeled, &oid);
 259                        /*
 260                         * Regardless of what the file header said,
 261                         * we definitely know the value of *this*
 262                         * reference:
 263                         */
 264                        last->flag |= REF_KNOWS_PEELED;
 265                }
 266        }
 267
 268        fclose(f);
 269        strbuf_release(&line);
 270
 271        return packed_refs;
 272}
 273
 274/*
 275 * Check that the packed refs cache (if any) still reflects the
 276 * contents of the file. If not, clear the cache.
 277 */
 278static void validate_packed_ref_cache(struct packed_ref_store *refs)
 279{
 280        if (refs->cache &&
 281            !stat_validity_check(&refs->cache->validity, refs->path))
 282                clear_packed_ref_cache(refs);
 283}
 284
 285/*
 286 * Get the packed_ref_cache for the specified packed_ref_store,
 287 * creating and populating it if it hasn't been read before or if the
 288 * file has been changed (according to its `validity` field) since it
 289 * was last read. On the other hand, if we hold the lock, then assume
 290 * that the file hasn't been changed out from under us, so skip the
 291 * extra `stat()` call in `stat_validity_check()`.
 292 */
 293static struct packed_ref_cache *get_packed_ref_cache(struct packed_ref_store *refs)
 294{
 295        if (!is_lock_file_locked(&refs->lock))
 296                validate_packed_ref_cache(refs);
 297
 298        if (!refs->cache)
 299                refs->cache = read_packed_refs(refs->path);
 300
 301        return refs->cache;
 302}
 303
 304static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
 305{
 306        return get_ref_dir(packed_ref_cache->cache->root);
 307}
 308
 309static struct ref_dir *get_packed_refs(struct packed_ref_store *refs)
 310{
 311        return get_packed_ref_dir(get_packed_ref_cache(refs));
 312}
 313
 314/*
 315 * Add or overwrite a reference in the in-memory packed reference
 316 * cache. This may only be called while the packed-refs file is locked
 317 * (see lock_packed_refs()). To actually write the packed-refs file,
 318 * call commit_packed_refs().
 319 */
 320void add_packed_ref(struct ref_store *ref_store,
 321                    const char *refname, const struct object_id *oid)
 322{
 323        struct packed_ref_store *refs =
 324                packed_downcast(ref_store, REF_STORE_WRITE,
 325                                "add_packed_ref");
 326        struct ref_dir *packed_refs;
 327        struct ref_entry *packed_entry;
 328
 329        if (!is_lock_file_locked(&refs->lock))
 330                die("BUG: packed refs not locked");
 331
 332        if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
 333                die("Reference has invalid format: '%s'", refname);
 334
 335        packed_refs = get_packed_refs(refs);
 336        packed_entry = find_ref_entry(packed_refs, refname);
 337        if (packed_entry) {
 338                /* Overwrite the existing entry: */
 339                oidcpy(&packed_entry->u.value.oid, oid);
 340                packed_entry->flag = REF_ISPACKED;
 341                oidclr(&packed_entry->u.value.peeled);
 342        } else {
 343                packed_entry = create_ref_entry(refname, oid, REF_ISPACKED);
 344                add_ref_entry(packed_refs, packed_entry);
 345        }
 346}
 347
 348/*
 349 * Return the ref_entry for the given refname from the packed
 350 * references.  If it does not exist, return NULL.
 351 */
 352static struct ref_entry *get_packed_ref(struct packed_ref_store *refs,
 353                                        const char *refname)
 354{
 355        return find_ref_entry(get_packed_refs(refs), refname);
 356}
 357
 358static int packed_read_raw_ref(struct ref_store *ref_store,
 359                               const char *refname, unsigned char *sha1,
 360                               struct strbuf *referent, unsigned int *type)
 361{
 362        struct packed_ref_store *refs =
 363                packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
 364
 365        struct ref_entry *entry;
 366
 367        *type = 0;
 368
 369        entry = get_packed_ref(refs, refname);
 370        if (!entry) {
 371                errno = ENOENT;
 372                return -1;
 373        }
 374
 375        hashcpy(sha1, entry->u.value.oid.hash);
 376        *type = REF_ISPACKED;
 377        return 0;
 378}
 379
 380static int packed_peel_ref(struct ref_store *ref_store,
 381                           const char *refname, unsigned char *sha1)
 382{
 383        struct packed_ref_store *refs =
 384                packed_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
 385                                "peel_ref");
 386        struct ref_entry *r = get_packed_ref(refs, refname);
 387
 388        if (!r || peel_entry(r, 0))
 389                return -1;
 390
 391        hashcpy(sha1, r->u.value.peeled.hash);
 392        return 0;
 393}
 394
 395struct packed_ref_iterator {
 396        struct ref_iterator base;
 397
 398        struct packed_ref_cache *cache;
 399        struct ref_iterator *iter0;
 400        unsigned int flags;
 401};
 402
 403static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
 404{
 405        struct packed_ref_iterator *iter =
 406                (struct packed_ref_iterator *)ref_iterator;
 407        int ok;
 408
 409        while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
 410                if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
 411                    ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
 412                        continue;
 413
 414                if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
 415                    !ref_resolves_to_object(iter->iter0->refname,
 416                                            iter->iter0->oid,
 417                                            iter->iter0->flags))
 418                        continue;
 419
 420                iter->base.refname = iter->iter0->refname;
 421                iter->base.oid = iter->iter0->oid;
 422                iter->base.flags = iter->iter0->flags;
 423                return ITER_OK;
 424        }
 425
 426        iter->iter0 = NULL;
 427        if (ref_iterator_abort(ref_iterator) != ITER_DONE)
 428                ok = ITER_ERROR;
 429
 430        return ok;
 431}
 432
 433static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator,
 434                                   struct object_id *peeled)
 435{
 436        struct packed_ref_iterator *iter =
 437                (struct packed_ref_iterator *)ref_iterator;
 438
 439        return ref_iterator_peel(iter->iter0, peeled);
 440}
 441
 442static int packed_ref_iterator_abort(struct ref_iterator *ref_iterator)
 443{
 444        struct packed_ref_iterator *iter =
 445                (struct packed_ref_iterator *)ref_iterator;
 446        int ok = ITER_DONE;
 447
 448        if (iter->iter0)
 449                ok = ref_iterator_abort(iter->iter0);
 450
 451        release_packed_ref_cache(iter->cache);
 452        base_ref_iterator_free(ref_iterator);
 453        return ok;
 454}
 455
 456static struct ref_iterator_vtable packed_ref_iterator_vtable = {
 457        packed_ref_iterator_advance,
 458        packed_ref_iterator_peel,
 459        packed_ref_iterator_abort
 460};
 461
 462static struct ref_iterator *packed_ref_iterator_begin(
 463                struct ref_store *ref_store,
 464                const char *prefix, unsigned int flags)
 465{
 466        struct packed_ref_store *refs;
 467        struct packed_ref_iterator *iter;
 468        struct ref_iterator *ref_iterator;
 469        unsigned int required_flags = REF_STORE_READ;
 470
 471        if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
 472                required_flags |= REF_STORE_ODB;
 473        refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin");
 474
 475        iter = xcalloc(1, sizeof(*iter));
 476        ref_iterator = &iter->base;
 477        base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable);
 478
 479        /*
 480         * Note that get_packed_ref_cache() internally checks whether
 481         * the packed-ref cache is up to date with what is on disk,
 482         * and re-reads it if not.
 483         */
 484
 485        iter->cache = get_packed_ref_cache(refs);
 486        acquire_packed_ref_cache(iter->cache);
 487        iter->iter0 = cache_ref_iterator_begin(iter->cache->cache, prefix, 0);
 488
 489        iter->flags = flags;
 490
 491        return ref_iterator;
 492}
 493
 494/*
 495 * Write an entry to the packed-refs file for the specified refname.
 496 * If peeled is non-NULL, write it as the entry's peeled value. On
 497 * error, return a nonzero value and leave errno set at the value left
 498 * by the failing call to `fprintf()`.
 499 */
 500static int write_packed_entry(FILE *fh, const char *refname,
 501                              const unsigned char *sha1,
 502                              const unsigned char *peeled)
 503{
 504        if (fprintf(fh, "%s %s\n", sha1_to_hex(sha1), refname) < 0 ||
 505            (peeled && fprintf(fh, "^%s\n", sha1_to_hex(peeled)) < 0))
 506                return -1;
 507
 508        return 0;
 509}
 510
 511int lock_packed_refs(struct ref_store *ref_store, int flags)
 512{
 513        struct packed_ref_store *refs =
 514                packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
 515                                "lock_packed_refs");
 516        static int timeout_configured = 0;
 517        static int timeout_value = 1000;
 518        struct packed_ref_cache *packed_ref_cache;
 519
 520        if (!timeout_configured) {
 521                git_config_get_int("core.packedrefstimeout", &timeout_value);
 522                timeout_configured = 1;
 523        }
 524
 525        if (hold_lock_file_for_update_timeout(
 526                            &refs->lock,
 527                            refs->path,
 528                            flags, timeout_value) < 0)
 529                return -1;
 530
 531        /*
 532         * Now that we hold the `packed-refs` lock, make sure that our
 533         * cache matches the current version of the file. Normally
 534         * `get_packed_ref_cache()` does that for us, but that
 535         * function assumes that when the file is locked, any existing
 536         * cache is still valid. We've just locked the file, but it
 537         * might have changed the moment *before* we locked it.
 538         */
 539        validate_packed_ref_cache(refs);
 540
 541        packed_ref_cache = get_packed_ref_cache(refs);
 542        /* Increment the reference count to prevent it from being freed: */
 543        acquire_packed_ref_cache(packed_ref_cache);
 544        return 0;
 545}
 546
 547/*
 548 * The packed-refs header line that we write out.  Perhaps other
 549 * traits will be added later.  The trailing space is required.
 550 */
 551static const char PACKED_REFS_HEADER[] =
 552        "# pack-refs with: peeled fully-peeled \n";
 553
 554/*
 555 * Write the current version of the packed refs cache from memory to
 556 * disk. The packed-refs file must already be locked for writing (see
 557 * lock_packed_refs()). Return zero on success. On errors, rollback
 558 * the lockfile, write an error message to `err`, and return a nonzero
 559 * value.
 560 */
 561int commit_packed_refs(struct ref_store *ref_store, struct strbuf *err)
 562{
 563        struct packed_ref_store *refs =
 564                packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
 565                                "commit_packed_refs");
 566        struct packed_ref_cache *packed_ref_cache =
 567                get_packed_ref_cache(refs);
 568        int ok;
 569        int ret = -1;
 570        FILE *out;
 571        struct ref_iterator *iter;
 572
 573        if (!is_lock_file_locked(&refs->lock))
 574                die("BUG: commit_packed_refs() called when unlocked");
 575
 576        out = fdopen_lock_file(&refs->lock, "w");
 577        if (!out) {
 578                strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s",
 579                            strerror(errno));
 580                goto error;
 581        }
 582
 583        if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0) {
 584                strbuf_addf(err, "error writing to %s: %s",
 585                            get_lock_file_path(&refs->lock), strerror(errno));
 586                goto error;
 587        }
 588
 589        iter = cache_ref_iterator_begin(packed_ref_cache->cache, NULL, 0);
 590        while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
 591                struct object_id peeled;
 592                int peel_error = ref_iterator_peel(iter, &peeled);
 593
 594                if (write_packed_entry(out, iter->refname, iter->oid->hash,
 595                                       peel_error ? NULL : peeled.hash)) {
 596                        strbuf_addf(err, "error writing to %s: %s",
 597                                    get_lock_file_path(&refs->lock),
 598                                    strerror(errno));
 599                        ref_iterator_abort(iter);
 600                        goto error;
 601                }
 602        }
 603
 604        if (ok != ITER_DONE) {
 605                strbuf_addf(err, "unable to write packed-refs file: "
 606                            "error iterating over old contents");
 607                goto error;
 608        }
 609
 610        if (commit_lock_file(&refs->lock)) {
 611                strbuf_addf(err, "error overwriting %s: %s",
 612                            refs->path, strerror(errno));
 613                goto out;
 614        }
 615
 616        ret = 0;
 617        goto out;
 618
 619error:
 620        rollback_lock_file(&refs->lock);
 621
 622out:
 623        release_packed_ref_cache(packed_ref_cache);
 624        return ret;
 625}
 626
 627/*
 628 * Rollback the lockfile for the packed-refs file, and discard the
 629 * in-memory packed reference cache.  (The packed-refs file will be
 630 * read anew if it is needed again after this function is called.)
 631 */
 632static void rollback_packed_refs(struct packed_ref_store *refs)
 633{
 634        struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);
 635
 636        packed_assert_main_repository(refs, "rollback_packed_refs");
 637
 638        if (!is_lock_file_locked(&refs->lock))
 639                die("BUG: packed-refs not locked");
 640        rollback_lock_file(&refs->lock);
 641        release_packed_ref_cache(packed_ref_cache);
 642        clear_packed_ref_cache(refs);
 643}
 644
 645/*
 646 * Rewrite the packed-refs file, omitting any refs listed in
 647 * 'refnames'. On error, leave packed-refs unchanged, write an error
 648 * message to 'err', and return a nonzero value.
 649 *
 650 * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
 651 */
 652int repack_without_refs(struct ref_store *ref_store,
 653                        struct string_list *refnames, struct strbuf *err)
 654{
 655        struct packed_ref_store *refs =
 656                packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
 657                                "repack_without_refs");
 658        struct ref_dir *packed;
 659        struct string_list_item *refname;
 660        int needs_repacking = 0, removed = 0;
 661
 662        packed_assert_main_repository(refs, "repack_without_refs");
 663        assert(err);
 664
 665        /* Look for a packed ref */
 666        for_each_string_list_item(refname, refnames) {
 667                if (get_packed_ref(refs, refname->string)) {
 668                        needs_repacking = 1;
 669                        break;
 670                }
 671        }
 672
 673        /* Avoid locking if we have nothing to do */
 674        if (!needs_repacking)
 675                return 0; /* no refname exists in packed refs */
 676
 677        if (lock_packed_refs(&refs->base, 0)) {
 678                unable_to_lock_message(refs->path, errno, err);
 679                return -1;
 680        }
 681        packed = get_packed_refs(refs);
 682
 683        /* Remove refnames from the cache */
 684        for_each_string_list_item(refname, refnames)
 685                if (remove_entry_from_dir(packed, refname->string) != -1)
 686                        removed = 1;
 687        if (!removed) {
 688                /*
 689                 * All packed entries disappeared while we were
 690                 * acquiring the lock.
 691                 */
 692                rollback_packed_refs(refs);
 693                return 0;
 694        }
 695
 696        /* Write what remains */
 697        return commit_packed_refs(&refs->base, err);
 698}
 699
 700static int packed_init_db(struct ref_store *ref_store, struct strbuf *err)
 701{
 702        /* Nothing to do. */
 703        return 0;
 704}
 705
 706static int packed_transaction_prepare(struct ref_store *ref_store,
 707                                      struct ref_transaction *transaction,
 708                                      struct strbuf *err)
 709{
 710        die("BUG: not implemented yet");
 711}
 712
 713static int packed_transaction_abort(struct ref_store *ref_store,
 714                                    struct ref_transaction *transaction,
 715                                    struct strbuf *err)
 716{
 717        die("BUG: not implemented yet");
 718}
 719
 720static int packed_transaction_finish(struct ref_store *ref_store,
 721                                     struct ref_transaction *transaction,
 722                                     struct strbuf *err)
 723{
 724        die("BUG: not implemented yet");
 725}
 726
 727static int packed_initial_transaction_commit(struct ref_store *ref_store,
 728                                            struct ref_transaction *transaction,
 729                                            struct strbuf *err)
 730{
 731        return ref_transaction_commit(transaction, err);
 732}
 733
 734static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
 735                             struct string_list *refnames, unsigned int flags)
 736{
 737        die("BUG: not implemented yet");
 738}
 739
 740static int packed_pack_refs(struct ref_store *ref_store, unsigned int flags)
 741{
 742        /*
 743         * Packed refs are already packed. It might be that loose refs
 744         * are packed *into* a packed refs store, but that is done by
 745         * updating the packed references via a transaction.
 746         */
 747        return 0;
 748}
 749
 750static int packed_create_symref(struct ref_store *ref_store,
 751                               const char *refname, const char *target,
 752                               const char *logmsg)
 753{
 754        die("BUG: packed reference store does not support symrefs");
 755}
 756
 757static int packed_rename_ref(struct ref_store *ref_store,
 758                            const char *oldrefname, const char *newrefname,
 759                            const char *logmsg)
 760{
 761        die("BUG: packed reference store does not support renaming references");
 762}
 763
 764static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store)
 765{
 766        return empty_ref_iterator_begin();
 767}
 768
 769static int packed_for_each_reflog_ent(struct ref_store *ref_store,
 770                                      const char *refname,
 771                                      each_reflog_ent_fn fn, void *cb_data)
 772{
 773        return 0;
 774}
 775
 776static int packed_for_each_reflog_ent_reverse(struct ref_store *ref_store,
 777                                              const char *refname,
 778                                              each_reflog_ent_fn fn,
 779                                              void *cb_data)
 780{
 781        return 0;
 782}
 783
 784static int packed_reflog_exists(struct ref_store *ref_store,
 785                               const char *refname)
 786{
 787        return 0;
 788}
 789
 790static int packed_create_reflog(struct ref_store *ref_store,
 791                               const char *refname, int force_create,
 792                               struct strbuf *err)
 793{
 794        die("BUG: packed reference store does not support reflogs");
 795}
 796
 797static int packed_delete_reflog(struct ref_store *ref_store,
 798                               const char *refname)
 799{
 800        return 0;
 801}
 802
 803static int packed_reflog_expire(struct ref_store *ref_store,
 804                                const char *refname, const unsigned char *sha1,
 805                                unsigned int flags,
 806                                reflog_expiry_prepare_fn prepare_fn,
 807                                reflog_expiry_should_prune_fn should_prune_fn,
 808                                reflog_expiry_cleanup_fn cleanup_fn,
 809                                void *policy_cb_data)
 810{
 811        return 0;
 812}
 813
 814struct ref_storage_be refs_be_packed = {
 815        NULL,
 816        "packed",
 817        packed_ref_store_create,
 818        packed_init_db,
 819        packed_transaction_prepare,
 820        packed_transaction_finish,
 821        packed_transaction_abort,
 822        packed_initial_transaction_commit,
 823
 824        packed_pack_refs,
 825        packed_peel_ref,
 826        packed_create_symref,
 827        packed_delete_refs,
 828        packed_rename_ref,
 829
 830        packed_ref_iterator_begin,
 831        packed_read_raw_ref,
 832
 833        packed_reflog_iterator_begin,
 834        packed_for_each_reflog_ent,
 835        packed_for_each_reflog_ent_reverse,
 836        packed_reflog_exists,
 837        packed_create_reflog,
 838        packed_delete_reflog,
 839        packed_reflog_expire
 840};