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