a28befbfa3198fcaebf73f6d835943efc316c6aa
   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 (!line.len || line.buf[line.len - 1] != '\n')
 233                        die("unterminated line in %s: %s", packed_refs_file, line.buf);
 234
 235                if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
 236                        if (strstr(traits, " fully-peeled "))
 237                                peeled = PEELED_FULLY;
 238                        else if (strstr(traits, " peeled "))
 239                                peeled = PEELED_TAGS;
 240                        /* perhaps other traits later as well */
 241                        continue;
 242                }
 243
 244                refname = parse_ref_line(&line, &oid);
 245                if (refname) {
 246                        int flag = REF_ISPACKED;
 247
 248                        if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
 249                                if (!refname_is_safe(refname))
 250                                        die("packed refname is dangerous: %s", refname);
 251                                oidclr(&oid);
 252                                flag |= REF_BAD_NAME | REF_ISBROKEN;
 253                        }
 254                        last = create_ref_entry(refname, &oid, flag);
 255                        if (peeled == PEELED_FULLY ||
 256                            (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
 257                                last->flag |= REF_KNOWS_PEELED;
 258                        add_ref_entry(dir, last);
 259                } else if (last &&
 260                    line.buf[0] == '^' &&
 261                    line.len == PEELED_LINE_LENGTH &&
 262                    line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
 263                    !get_oid_hex(line.buf + 1, &oid)) {
 264                        oidcpy(&last->u.value.peeled, &oid);
 265                        /*
 266                         * Regardless of what the file header said,
 267                         * we definitely know the value of *this*
 268                         * reference:
 269                         */
 270                        last->flag |= REF_KNOWS_PEELED;
 271                } else {
 272                        strbuf_setlen(&line, line.len - 1);
 273                        die("unexpected line in %s: %s", packed_refs_file, line.buf);
 274                }
 275        }
 276
 277        fclose(f);
 278        strbuf_release(&line);
 279
 280        return packed_refs;
 281}
 282
 283/*
 284 * Check that the packed refs cache (if any) still reflects the
 285 * contents of the file. If not, clear the cache.
 286 */
 287static void validate_packed_ref_cache(struct packed_ref_store *refs)
 288{
 289        if (refs->cache &&
 290            !stat_validity_check(&refs->cache->validity, refs->path))
 291                clear_packed_ref_cache(refs);
 292}
 293
 294/*
 295 * Get the packed_ref_cache for the specified packed_ref_store,
 296 * creating and populating it if it hasn't been read before or if the
 297 * file has been changed (according to its `validity` field) since it
 298 * was last read. On the other hand, if we hold the lock, then assume
 299 * that the file hasn't been changed out from under us, so skip the
 300 * extra `stat()` call in `stat_validity_check()`.
 301 */
 302static struct packed_ref_cache *get_packed_ref_cache(struct packed_ref_store *refs)
 303{
 304        if (!is_lock_file_locked(&refs->lock))
 305                validate_packed_ref_cache(refs);
 306
 307        if (!refs->cache)
 308                refs->cache = read_packed_refs(refs->path);
 309
 310        return refs->cache;
 311}
 312
 313static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
 314{
 315        return get_ref_dir(packed_ref_cache->cache->root);
 316}
 317
 318static struct ref_dir *get_packed_refs(struct packed_ref_store *refs)
 319{
 320        return get_packed_ref_dir(get_packed_ref_cache(refs));
 321}
 322
 323/*
 324 * Add or overwrite a reference in the in-memory packed reference
 325 * cache. This may only be called while the packed-refs file is locked
 326 * (see packed_refs_lock()). To actually write the packed-refs file,
 327 * call commit_packed_refs().
 328 */
 329void add_packed_ref(struct ref_store *ref_store,
 330                    const char *refname, const struct object_id *oid)
 331{
 332        struct packed_ref_store *refs =
 333                packed_downcast(ref_store, REF_STORE_WRITE,
 334                                "add_packed_ref");
 335        struct ref_dir *packed_refs;
 336        struct ref_entry *packed_entry;
 337
 338        if (!is_lock_file_locked(&refs->lock))
 339                die("BUG: packed refs not locked");
 340
 341        if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
 342                die("Reference has invalid format: '%s'", refname);
 343
 344        packed_refs = get_packed_refs(refs);
 345        packed_entry = find_ref_entry(packed_refs, refname);
 346        if (packed_entry) {
 347                /* Overwrite the existing entry: */
 348                oidcpy(&packed_entry->u.value.oid, oid);
 349                packed_entry->flag = REF_ISPACKED;
 350                oidclr(&packed_entry->u.value.peeled);
 351        } else {
 352                packed_entry = create_ref_entry(refname, oid, REF_ISPACKED);
 353                add_ref_entry(packed_refs, packed_entry);
 354        }
 355}
 356
 357/*
 358 * Return the ref_entry for the given refname from the packed
 359 * references.  If it does not exist, return NULL.
 360 */
 361static struct ref_entry *get_packed_ref(struct packed_ref_store *refs,
 362                                        const char *refname)
 363{
 364        return find_ref_entry(get_packed_refs(refs), refname);
 365}
 366
 367static int packed_read_raw_ref(struct ref_store *ref_store,
 368                               const char *refname, unsigned char *sha1,
 369                               struct strbuf *referent, unsigned int *type)
 370{
 371        struct packed_ref_store *refs =
 372                packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
 373
 374        struct ref_entry *entry;
 375
 376        *type = 0;
 377
 378        entry = get_packed_ref(refs, refname);
 379        if (!entry) {
 380                errno = ENOENT;
 381                return -1;
 382        }
 383
 384        hashcpy(sha1, entry->u.value.oid.hash);
 385        *type = REF_ISPACKED;
 386        return 0;
 387}
 388
 389static int packed_peel_ref(struct ref_store *ref_store,
 390                           const char *refname, unsigned char *sha1)
 391{
 392        struct packed_ref_store *refs =
 393                packed_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
 394                                "peel_ref");
 395        struct ref_entry *r = get_packed_ref(refs, refname);
 396
 397        if (!r || peel_entry(r, 0))
 398                return -1;
 399
 400        hashcpy(sha1, r->u.value.peeled.hash);
 401        return 0;
 402}
 403
 404struct packed_ref_iterator {
 405        struct ref_iterator base;
 406
 407        struct packed_ref_cache *cache;
 408        struct ref_iterator *iter0;
 409        unsigned int flags;
 410};
 411
 412static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
 413{
 414        struct packed_ref_iterator *iter =
 415                (struct packed_ref_iterator *)ref_iterator;
 416        int ok;
 417
 418        while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
 419                if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
 420                    ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
 421                        continue;
 422
 423                if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
 424                    !ref_resolves_to_object(iter->iter0->refname,
 425                                            iter->iter0->oid,
 426                                            iter->iter0->flags))
 427                        continue;
 428
 429                iter->base.refname = iter->iter0->refname;
 430                iter->base.oid = iter->iter0->oid;
 431                iter->base.flags = iter->iter0->flags;
 432                return ITER_OK;
 433        }
 434
 435        iter->iter0 = NULL;
 436        if (ref_iterator_abort(ref_iterator) != ITER_DONE)
 437                ok = ITER_ERROR;
 438
 439        return ok;
 440}
 441
 442static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator,
 443                                   struct object_id *peeled)
 444{
 445        struct packed_ref_iterator *iter =
 446                (struct packed_ref_iterator *)ref_iterator;
 447
 448        return ref_iterator_peel(iter->iter0, peeled);
 449}
 450
 451static int packed_ref_iterator_abort(struct ref_iterator *ref_iterator)
 452{
 453        struct packed_ref_iterator *iter =
 454                (struct packed_ref_iterator *)ref_iterator;
 455        int ok = ITER_DONE;
 456
 457        if (iter->iter0)
 458                ok = ref_iterator_abort(iter->iter0);
 459
 460        release_packed_ref_cache(iter->cache);
 461        base_ref_iterator_free(ref_iterator);
 462        return ok;
 463}
 464
 465static struct ref_iterator_vtable packed_ref_iterator_vtable = {
 466        packed_ref_iterator_advance,
 467        packed_ref_iterator_peel,
 468        packed_ref_iterator_abort
 469};
 470
 471static struct ref_iterator *packed_ref_iterator_begin(
 472                struct ref_store *ref_store,
 473                const char *prefix, unsigned int flags)
 474{
 475        struct packed_ref_store *refs;
 476        struct packed_ref_iterator *iter;
 477        struct ref_iterator *ref_iterator;
 478        unsigned int required_flags = REF_STORE_READ;
 479
 480        if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
 481                required_flags |= REF_STORE_ODB;
 482        refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin");
 483
 484        iter = xcalloc(1, sizeof(*iter));
 485        ref_iterator = &iter->base;
 486        base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable);
 487
 488        /*
 489         * Note that get_packed_ref_cache() internally checks whether
 490         * the packed-ref cache is up to date with what is on disk,
 491         * and re-reads it if not.
 492         */
 493
 494        iter->cache = get_packed_ref_cache(refs);
 495        acquire_packed_ref_cache(iter->cache);
 496        iter->iter0 = cache_ref_iterator_begin(iter->cache->cache, prefix, 0);
 497
 498        iter->flags = flags;
 499
 500        return ref_iterator;
 501}
 502
 503/*
 504 * Write an entry to the packed-refs file for the specified refname.
 505 * If peeled is non-NULL, write it as the entry's peeled value. On
 506 * error, return a nonzero value and leave errno set at the value left
 507 * by the failing call to `fprintf()`.
 508 */
 509static int write_packed_entry(FILE *fh, const char *refname,
 510                              const unsigned char *sha1,
 511                              const unsigned char *peeled)
 512{
 513        if (fprintf(fh, "%s %s\n", sha1_to_hex(sha1), refname) < 0 ||
 514            (peeled && fprintf(fh, "^%s\n", sha1_to_hex(peeled)) < 0))
 515                return -1;
 516
 517        return 0;
 518}
 519
 520int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
 521{
 522        struct packed_ref_store *refs =
 523                packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
 524                                "packed_refs_lock");
 525        static int timeout_configured = 0;
 526        static int timeout_value = 1000;
 527        struct packed_ref_cache *packed_ref_cache;
 528
 529        if (!timeout_configured) {
 530                git_config_get_int("core.packedrefstimeout", &timeout_value);
 531                timeout_configured = 1;
 532        }
 533
 534        /*
 535         * Note that we close the lockfile immediately because we
 536         * don't write new content to it, but rather to a separate
 537         * tempfile.
 538         */
 539        if (hold_lock_file_for_update_timeout(
 540                            &refs->lock,
 541                            refs->path,
 542                            flags, timeout_value) < 0) {
 543                unable_to_lock_message(refs->path, errno, err);
 544                return -1;
 545        }
 546
 547        if (close_lock_file(&refs->lock)) {
 548                strbuf_addf(err, "unable to close %s: %s", refs->path, strerror(errno));
 549                return -1;
 550        }
 551
 552        /*
 553         * Now that we hold the `packed-refs` lock, make sure that our
 554         * cache matches the current version of the file. Normally
 555         * `get_packed_ref_cache()` does that for us, but that
 556         * function assumes that when the file is locked, any existing
 557         * cache is still valid. We've just locked the file, but it
 558         * might have changed the moment *before* we locked it.
 559         */
 560        validate_packed_ref_cache(refs);
 561
 562        packed_ref_cache = get_packed_ref_cache(refs);
 563        /* Increment the reference count to prevent it from being freed: */
 564        acquire_packed_ref_cache(packed_ref_cache);
 565        return 0;
 566}
 567
 568void packed_refs_unlock(struct ref_store *ref_store)
 569{
 570        struct packed_ref_store *refs = packed_downcast(
 571                        ref_store,
 572                        REF_STORE_READ | REF_STORE_WRITE,
 573                        "packed_refs_unlock");
 574
 575        if (!is_lock_file_locked(&refs->lock))
 576                die("BUG: packed_refs_unlock() called when not locked");
 577        rollback_lock_file(&refs->lock);
 578        release_packed_ref_cache(refs->cache);
 579}
 580
 581int packed_refs_is_locked(struct ref_store *ref_store)
 582{
 583        struct packed_ref_store *refs = packed_downcast(
 584                        ref_store,
 585                        REF_STORE_READ | REF_STORE_WRITE,
 586                        "packed_refs_is_locked");
 587
 588        return is_lock_file_locked(&refs->lock);
 589}
 590
 591/*
 592 * The packed-refs header line that we write out.  Perhaps other
 593 * traits will be added later.  The trailing space is required.
 594 */
 595static const char PACKED_REFS_HEADER[] =
 596        "# pack-refs with: peeled fully-peeled \n";
 597
 598/*
 599 * Write the current version of the packed refs cache from memory to
 600 * disk. The packed-refs file must already be locked for writing (see
 601 * packed_refs_lock()). Return zero on success. On errors, rollback
 602 * the lockfile, write an error message to `err`, and return a nonzero
 603 * value.
 604 */
 605int commit_packed_refs(struct ref_store *ref_store, struct strbuf *err)
 606{
 607        struct packed_ref_store *refs =
 608                packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
 609                                "commit_packed_refs");
 610        struct packed_ref_cache *packed_ref_cache =
 611                get_packed_ref_cache(refs);
 612        int ok;
 613        struct strbuf sb = STRBUF_INIT;
 614        FILE *out;
 615        struct ref_iterator *iter;
 616
 617        if (!is_lock_file_locked(&refs->lock))
 618                die("BUG: commit_packed_refs() called when unlocked");
 619
 620        strbuf_addf(&sb, "%s.new", refs->path);
 621        if (create_tempfile(&refs->tempfile, sb.buf) < 0) {
 622                strbuf_addf(err, "unable to create file %s: %s",
 623                            sb.buf, strerror(errno));
 624                strbuf_release(&sb);
 625                return -1;
 626        }
 627        strbuf_release(&sb);
 628
 629        out = fdopen_tempfile(&refs->tempfile, "w");
 630        if (!out) {
 631                strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s",
 632                            strerror(errno));
 633                goto error;
 634        }
 635
 636        if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0) {
 637                strbuf_addf(err, "error writing to %s: %s",
 638                            get_tempfile_path(&refs->tempfile), strerror(errno));
 639                goto error;
 640        }
 641
 642        iter = cache_ref_iterator_begin(packed_ref_cache->cache, NULL, 0);
 643        while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
 644                struct object_id peeled;
 645                int peel_error = ref_iterator_peel(iter, &peeled);
 646
 647                if (write_packed_entry(out, iter->refname, iter->oid->hash,
 648                                       peel_error ? NULL : peeled.hash)) {
 649                        strbuf_addf(err, "error writing to %s: %s",
 650                                    get_tempfile_path(&refs->tempfile),
 651                                    strerror(errno));
 652                        ref_iterator_abort(iter);
 653                        goto error;
 654                }
 655        }
 656
 657        if (ok != ITER_DONE) {
 658                strbuf_addf(err, "unable to rewrite packed-refs file: "
 659                            "error iterating over old contents");
 660                goto error;
 661        }
 662
 663        if (rename_tempfile(&refs->tempfile, refs->path)) {
 664                strbuf_addf(err, "error replacing %s: %s",
 665                            refs->path, strerror(errno));
 666                return -1;
 667        }
 668
 669        return 0;
 670
 671error:
 672        delete_tempfile(&refs->tempfile);
 673        return -1;
 674}
 675
 676/*
 677 * Rewrite the packed-refs file, omitting any refs listed in
 678 * 'refnames'. On error, leave packed-refs unchanged, write an error
 679 * message to 'err', and return a nonzero value. The packed refs lock
 680 * must be held when calling this function; it will still be held when
 681 * the function returns.
 682 *
 683 * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
 684 */
 685int repack_without_refs(struct ref_store *ref_store,
 686                        struct string_list *refnames, struct strbuf *err)
 687{
 688        struct packed_ref_store *refs =
 689                packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
 690                                "repack_without_refs");
 691        struct ref_dir *packed;
 692        struct string_list_item *refname;
 693        int needs_repacking = 0, removed = 0;
 694
 695        packed_assert_main_repository(refs, "repack_without_refs");
 696        assert(err);
 697
 698        if (!is_lock_file_locked(&refs->lock))
 699                die("BUG: repack_without_refs called without holding lock");
 700
 701        /* Look for a packed ref */
 702        for_each_string_list_item(refname, refnames) {
 703                if (get_packed_ref(refs, refname->string)) {
 704                        needs_repacking = 1;
 705                        break;
 706                }
 707        }
 708
 709        /* Avoid locking if we have nothing to do */
 710        if (!needs_repacking)
 711                return 0; /* no refname exists in packed refs */
 712
 713        packed = get_packed_refs(refs);
 714
 715        /* Remove refnames from the cache */
 716        for_each_string_list_item(refname, refnames)
 717                if (remove_entry_from_dir(packed, refname->string) != -1)
 718                        removed = 1;
 719        if (!removed) {
 720                /*
 721                 * All packed entries disappeared while we were
 722                 * acquiring the lock.
 723                 */
 724                clear_packed_ref_cache(refs);
 725                return 0;
 726        }
 727
 728        /* Write what remains */
 729        return commit_packed_refs(&refs->base, err);
 730}
 731
 732static int packed_init_db(struct ref_store *ref_store, struct strbuf *err)
 733{
 734        /* Nothing to do. */
 735        return 0;
 736}
 737
 738static int packed_transaction_prepare(struct ref_store *ref_store,
 739                                      struct ref_transaction *transaction,
 740                                      struct strbuf *err)
 741{
 742        die("BUG: not implemented yet");
 743}
 744
 745static int packed_transaction_abort(struct ref_store *ref_store,
 746                                    struct ref_transaction *transaction,
 747                                    struct strbuf *err)
 748{
 749        die("BUG: not implemented yet");
 750}
 751
 752static int packed_transaction_finish(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_initial_transaction_commit(struct ref_store *ref_store,
 760                                            struct ref_transaction *transaction,
 761                                            struct strbuf *err)
 762{
 763        return ref_transaction_commit(transaction, err);
 764}
 765
 766static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
 767                             struct string_list *refnames, unsigned int flags)
 768{
 769        die("BUG: not implemented yet");
 770}
 771
 772static int packed_pack_refs(struct ref_store *ref_store, unsigned int flags)
 773{
 774        /*
 775         * Packed refs are already packed. It might be that loose refs
 776         * are packed *into* a packed refs store, but that is done by
 777         * updating the packed references via a transaction.
 778         */
 779        return 0;
 780}
 781
 782static int packed_create_symref(struct ref_store *ref_store,
 783                               const char *refname, const char *target,
 784                               const char *logmsg)
 785{
 786        die("BUG: packed reference store does not support symrefs");
 787}
 788
 789static int packed_rename_ref(struct ref_store *ref_store,
 790                            const char *oldrefname, const char *newrefname,
 791                            const char *logmsg)
 792{
 793        die("BUG: packed reference store does not support renaming references");
 794}
 795
 796static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store)
 797{
 798        return empty_ref_iterator_begin();
 799}
 800
 801static int packed_for_each_reflog_ent(struct ref_store *ref_store,
 802                                      const char *refname,
 803                                      each_reflog_ent_fn fn, void *cb_data)
 804{
 805        return 0;
 806}
 807
 808static int packed_for_each_reflog_ent_reverse(struct ref_store *ref_store,
 809                                              const char *refname,
 810                                              each_reflog_ent_fn fn,
 811                                              void *cb_data)
 812{
 813        return 0;
 814}
 815
 816static int packed_reflog_exists(struct ref_store *ref_store,
 817                               const char *refname)
 818{
 819        return 0;
 820}
 821
 822static int packed_create_reflog(struct ref_store *ref_store,
 823                               const char *refname, int force_create,
 824                               struct strbuf *err)
 825{
 826        die("BUG: packed reference store does not support reflogs");
 827}
 828
 829static int packed_delete_reflog(struct ref_store *ref_store,
 830                               const char *refname)
 831{
 832        return 0;
 833}
 834
 835static int packed_reflog_expire(struct ref_store *ref_store,
 836                                const char *refname, const unsigned char *sha1,
 837                                unsigned int flags,
 838                                reflog_expiry_prepare_fn prepare_fn,
 839                                reflog_expiry_should_prune_fn should_prune_fn,
 840                                reflog_expiry_cleanup_fn cleanup_fn,
 841                                void *policy_cb_data)
 842{
 843        return 0;
 844}
 845
 846struct ref_storage_be refs_be_packed = {
 847        NULL,
 848        "packed",
 849        packed_ref_store_create,
 850        packed_init_db,
 851        packed_transaction_prepare,
 852        packed_transaction_finish,
 853        packed_transaction_abort,
 854        packed_initial_transaction_commit,
 855
 856        packed_pack_refs,
 857        packed_peel_ref,
 858        packed_create_symref,
 859        packed_delete_refs,
 860        packed_rename_ref,
 861
 862        packed_ref_iterator_begin,
 863        packed_read_raw_ref,
 864
 865        packed_reflog_iterator_begin,
 866        packed_for_each_reflog_ent,
 867        packed_for_each_reflog_ent_reverse,
 868        packed_reflog_exists,
 869        packed_create_reflog,
 870        packed_delete_reflog,
 871        packed_reflog_expire
 872};