154abbd83a5d9e0d54cef8b1f9e7479da0c6d456
   1#include "../cache.h"
   2#include "../config.h"
   3#include "../refs.h"
   4#include "refs-internal.h"
   5#include "ref-cache.h"
   6#include "packed-backend.h"
   7#include "../iterator.h"
   8#include "../lockfile.h"
   9
  10struct packed_ref_store;
  11
  12struct packed_ref_cache {
  13        /*
  14         * A back-pointer to the packed_ref_store with which this
  15         * cache is associated:
  16         */
  17        struct packed_ref_store *refs;
  18
  19        struct ref_cache *cache;
  20
  21        /*
  22         * Count of references to the data structure in this instance,
  23         * including the pointer from files_ref_store::packed if any.
  24         * The data will not be freed as long as the reference count
  25         * is nonzero.
  26         */
  27        unsigned int referrers;
  28
  29        /* The metadata from when this packed-refs cache was read */
  30        struct stat_validity validity;
  31};
  32
  33/*
  34 * Increment the reference count of *packed_refs.
  35 */
  36static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
  37{
  38        packed_refs->referrers++;
  39}
  40
  41/*
  42 * Decrease the reference count of *packed_refs.  If it goes to zero,
  43 * free *packed_refs and return true; otherwise return false.
  44 */
  45static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
  46{
  47        if (!--packed_refs->referrers) {
  48                free_ref_cache(packed_refs->cache);
  49                stat_validity_clear(&packed_refs->validity);
  50                free(packed_refs);
  51                return 1;
  52        } else {
  53                return 0;
  54        }
  55}
  56
  57/*
  58 * A container for `packed-refs`-related data. It is not (yet) a
  59 * `ref_store`.
  60 */
  61struct packed_ref_store {
  62        struct ref_store base;
  63
  64        unsigned int store_flags;
  65
  66        /* The path of the "packed-refs" file: */
  67        char *path;
  68
  69        /*
  70         * A cache of the values read from the `packed-refs` file, if
  71         * it might still be current; otherwise, NULL.
  72         */
  73        struct packed_ref_cache *cache;
  74
  75        /*
  76         * Lock used for the "packed-refs" file. Note that this (and
  77         * thus the enclosing `packed_ref_store`) must not be freed.
  78         */
  79        struct lock_file lock;
  80
  81        /*
  82         * Temporary file used when rewriting new contents to the
  83         * "packed-refs" file. Note that this (and thus the enclosing
  84         * `packed_ref_store`) must not be freed.
  85         */
  86        struct tempfile tempfile;
  87};
  88
  89struct ref_store *packed_ref_store_create(const char *path,
  90                                          unsigned int store_flags)
  91{
  92        struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
  93        struct ref_store *ref_store = (struct ref_store *)refs;
  94
  95        base_ref_store_init(ref_store, &refs_be_packed);
  96        refs->store_flags = store_flags;
  97
  98        refs->path = xstrdup(path);
  99        return ref_store;
 100}
 101
 102/*
 103 * Downcast `ref_store` to `packed_ref_store`. Die if `ref_store` is
 104 * not a `packed_ref_store`. Also die if `packed_ref_store` doesn't
 105 * support at least the flags specified in `required_flags`. `caller`
 106 * is used in any necessary error messages.
 107 */
 108static struct packed_ref_store *packed_downcast(struct ref_store *ref_store,
 109                                                unsigned int required_flags,
 110                                                const char *caller)
 111{
 112        struct packed_ref_store *refs;
 113
 114        if (ref_store->be != &refs_be_packed)
 115                die("BUG: ref_store is type \"%s\" not \"packed\" in %s",
 116                    ref_store->be->name, caller);
 117
 118        refs = (struct packed_ref_store *)ref_store;
 119
 120        if ((refs->store_flags & required_flags) != required_flags)
 121                die("BUG: unallowed operation (%s), requires %x, has %x\n",
 122                    caller, required_flags, refs->store_flags);
 123
 124        return refs;
 125}
 126
 127static void clear_packed_ref_cache(struct packed_ref_store *refs)
 128{
 129        if (refs->cache) {
 130                struct packed_ref_cache *cache = refs->cache;
 131
 132                refs->cache = NULL;
 133                release_packed_ref_cache(cache);
 134        }
 135}
 136
 137/* The length of a peeled reference line in packed-refs, including EOL: */
 138#define PEELED_LINE_LENGTH 42
 139
 140/*
 141 * Parse one line from a packed-refs file.  Write the SHA1 to sha1.
 142 * Return a pointer to the refname within the line (null-terminated),
 143 * or NULL if there was a problem.
 144 */
 145static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
 146{
 147        const char *ref;
 148
 149        if (parse_oid_hex(line->buf, oid, &ref) < 0)
 150                return NULL;
 151        if (!isspace(*ref++))
 152                return NULL;
 153
 154        if (isspace(*ref))
 155                return NULL;
 156
 157        if (line->buf[line->len - 1] != '\n')
 158                return NULL;
 159        line->buf[--line->len] = 0;
 160
 161        return ref;
 162}
 163
 164static NORETURN void die_unterminated_line(const char *path,
 165                                           const char *p, size_t len)
 166{
 167        if (len < 80)
 168                die("unterminated line in %s: %.*s", path, (int)len, p);
 169        else
 170                die("unterminated line in %s: %.75s...", path, p);
 171}
 172
 173static NORETURN void die_invalid_line(const char *path,
 174                                      const char *p, size_t len)
 175{
 176        const char *eol = memchr(p, '\n', len);
 177
 178        if (!eol)
 179                die_unterminated_line(path, p, len);
 180        else if (eol - p < 80)
 181                die("unexpected line in %s: %.*s", path, (int)(eol - p), p);
 182        else
 183                die("unexpected line in %s: %.75s...", path, p);
 184
 185}
 186
 187/*
 188 * Read from the `packed-refs` file into a newly-allocated
 189 * `packed_ref_cache` and return it. The return value will already
 190 * have its reference count incremented.
 191 *
 192 * A comment line of the form "# pack-refs with: " may contain zero or
 193 * more traits. We interpret the traits as follows:
 194 *
 195 *   No traits:
 196 *
 197 *      Probably no references are peeled. But if the file contains a
 198 *      peeled value for a reference, we will use it.
 199 *
 200 *   peeled:
 201 *
 202 *      References under "refs/tags/", if they *can* be peeled, *are*
 203 *      peeled in this file. References outside of "refs/tags/" are
 204 *      probably not peeled even if they could have been, but if we find
 205 *      a peeled value for such a reference we will use it.
 206 *
 207 *   fully-peeled:
 208 *
 209 *      All references in the file that can be peeled are peeled.
 210 *      Inversely (and this is more important), any references in the
 211 *      file for which no peeled value is recorded is not peelable. This
 212 *      trait should typically be written alongside "peeled" for
 213 *      compatibility with older clients, but we do not require it
 214 *      (i.e., "peeled" is a no-op if "fully-peeled" is set).
 215 */
 216static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
 217{
 218        struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
 219        int fd;
 220        struct stat st;
 221        size_t size;
 222        char *buf;
 223        const char *pos, *eol, *eof;
 224        struct ref_entry *last = NULL;
 225        struct strbuf line = STRBUF_INIT;
 226        enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
 227        struct ref_dir *dir;
 228
 229        packed_refs->refs = refs;
 230        acquire_packed_ref_cache(packed_refs);
 231        packed_refs->cache = create_ref_cache(NULL, NULL);
 232        packed_refs->cache->root->flag &= ~REF_INCOMPLETE;
 233
 234        fd = open(refs->path, O_RDONLY);
 235        if (fd < 0) {
 236                if (errno == ENOENT) {
 237                        /*
 238                         * This is OK; it just means that no
 239                         * "packed-refs" file has been written yet,
 240                         * which is equivalent to it being empty.
 241                         */
 242                        return packed_refs;
 243                } else {
 244                        die_errno("couldn't read %s", refs->path);
 245                }
 246        }
 247
 248        stat_validity_update(&packed_refs->validity, fd);
 249
 250        if (fstat(fd, &st) < 0)
 251                die_errno("couldn't stat %s", refs->path);
 252
 253        size = xsize_t(st.st_size);
 254        buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
 255        pos = buf;
 256        eof = buf + size;
 257
 258        dir = get_ref_dir(packed_refs->cache->root);
 259        while (pos < eof) {
 260                struct object_id oid;
 261                const char *refname;
 262                const char *traits;
 263
 264                eol = memchr(pos, '\n', eof - pos);
 265                if (!eol)
 266                        die_unterminated_line(refs->path, pos, eof - pos);
 267
 268                strbuf_add(&line, pos, eol + 1 - pos);
 269
 270                if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
 271                        if (strstr(traits, " fully-peeled "))
 272                                peeled = PEELED_FULLY;
 273                        else if (strstr(traits, " peeled "))
 274                                peeled = PEELED_TAGS;
 275                        /* perhaps other traits later as well */
 276                        goto next_line;
 277                }
 278
 279                refname = parse_ref_line(&line, &oid);
 280                if (refname) {
 281                        int flag = REF_ISPACKED;
 282
 283                        if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
 284                                if (!refname_is_safe(refname))
 285                                        die("packed refname is dangerous: %s", refname);
 286                                oidclr(&oid);
 287                                flag |= REF_BAD_NAME | REF_ISBROKEN;
 288                        }
 289                        last = create_ref_entry(refname, &oid, flag);
 290                        if (peeled == PEELED_FULLY ||
 291                            (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
 292                                last->flag |= REF_KNOWS_PEELED;
 293                        add_ref_entry(dir, last);
 294                } else if (last &&
 295                    line.buf[0] == '^' &&
 296                    line.len == PEELED_LINE_LENGTH &&
 297                    line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
 298                    !get_oid_hex(line.buf + 1, &oid)) {
 299                        oidcpy(&last->u.value.peeled, &oid);
 300                        /*
 301                         * Regardless of what the file header said,
 302                         * we definitely know the value of *this*
 303                         * reference:
 304                         */
 305                        last->flag |= REF_KNOWS_PEELED;
 306                } else {
 307                        die_invalid_line(refs->path, line.buf, line.len);
 308                }
 309
 310        next_line:
 311                /* The "+ 1" is for the LF character. */
 312                pos = eol + 1;
 313                strbuf_reset(&line);
 314        }
 315
 316        if (munmap(buf, size))
 317                die_errno("error ummapping packed-refs file");
 318        close(fd);
 319
 320        strbuf_release(&line);
 321        return packed_refs;
 322}
 323
 324/*
 325 * Check that the packed refs cache (if any) still reflects the
 326 * contents of the file. If not, clear the cache.
 327 */
 328static void validate_packed_ref_cache(struct packed_ref_store *refs)
 329{
 330        if (refs->cache &&
 331            !stat_validity_check(&refs->cache->validity, refs->path))
 332                clear_packed_ref_cache(refs);
 333}
 334
 335/*
 336 * Get the packed_ref_cache for the specified packed_ref_store,
 337 * creating and populating it if it hasn't been read before or if the
 338 * file has been changed (according to its `validity` field) since it
 339 * was last read. On the other hand, if we hold the lock, then assume
 340 * that the file hasn't been changed out from under us, so skip the
 341 * extra `stat()` call in `stat_validity_check()`.
 342 */
 343static struct packed_ref_cache *get_packed_ref_cache(struct packed_ref_store *refs)
 344{
 345        if (!is_lock_file_locked(&refs->lock))
 346                validate_packed_ref_cache(refs);
 347
 348        if (!refs->cache)
 349                refs->cache = read_packed_refs(refs);
 350
 351        return refs->cache;
 352}
 353
 354static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
 355{
 356        return get_ref_dir(packed_ref_cache->cache->root);
 357}
 358
 359static struct ref_dir *get_packed_refs(struct packed_ref_store *refs)
 360{
 361        return get_packed_ref_dir(get_packed_ref_cache(refs));
 362}
 363
 364/*
 365 * Return the ref_entry for the given refname from the packed
 366 * references.  If it does not exist, return NULL.
 367 */
 368static struct ref_entry *get_packed_ref(struct packed_ref_store *refs,
 369                                        const char *refname)
 370{
 371        return find_ref_entry(get_packed_refs(refs), refname);
 372}
 373
 374static int packed_read_raw_ref(struct ref_store *ref_store,
 375                               const char *refname, unsigned char *sha1,
 376                               struct strbuf *referent, unsigned int *type)
 377{
 378        struct packed_ref_store *refs =
 379                packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
 380
 381        struct ref_entry *entry;
 382
 383        *type = 0;
 384
 385        entry = get_packed_ref(refs, refname);
 386        if (!entry) {
 387                errno = ENOENT;
 388                return -1;
 389        }
 390
 391        hashcpy(sha1, entry->u.value.oid.hash);
 392        *type = REF_ISPACKED;
 393        return 0;
 394}
 395
 396static int packed_peel_ref(struct ref_store *ref_store,
 397                           const char *refname, unsigned char *sha1)
 398{
 399        struct packed_ref_store *refs =
 400                packed_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
 401                                "peel_ref");
 402        struct ref_entry *r = get_packed_ref(refs, refname);
 403
 404        if (!r || peel_entry(r, 0))
 405                return -1;
 406
 407        hashcpy(sha1, r->u.value.peeled.hash);
 408        return 0;
 409}
 410
 411struct packed_ref_iterator {
 412        struct ref_iterator base;
 413
 414        struct packed_ref_cache *cache;
 415        struct ref_iterator *iter0;
 416        unsigned int flags;
 417};
 418
 419static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
 420{
 421        struct packed_ref_iterator *iter =
 422                (struct packed_ref_iterator *)ref_iterator;
 423        int ok;
 424
 425        while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
 426                if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
 427                    ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
 428                        continue;
 429
 430                if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
 431                    !ref_resolves_to_object(iter->iter0->refname,
 432                                            iter->iter0->oid,
 433                                            iter->iter0->flags))
 434                        continue;
 435
 436                iter->base.refname = iter->iter0->refname;
 437                iter->base.oid = iter->iter0->oid;
 438                iter->base.flags = iter->iter0->flags;
 439                return ITER_OK;
 440        }
 441
 442        iter->iter0 = NULL;
 443        if (ref_iterator_abort(ref_iterator) != ITER_DONE)
 444                ok = ITER_ERROR;
 445
 446        return ok;
 447}
 448
 449static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator,
 450                                   struct object_id *peeled)
 451{
 452        struct packed_ref_iterator *iter =
 453                (struct packed_ref_iterator *)ref_iterator;
 454
 455        return ref_iterator_peel(iter->iter0, peeled);
 456}
 457
 458static int packed_ref_iterator_abort(struct ref_iterator *ref_iterator)
 459{
 460        struct packed_ref_iterator *iter =
 461                (struct packed_ref_iterator *)ref_iterator;
 462        int ok = ITER_DONE;
 463
 464        if (iter->iter0)
 465                ok = ref_iterator_abort(iter->iter0);
 466
 467        release_packed_ref_cache(iter->cache);
 468        base_ref_iterator_free(ref_iterator);
 469        return ok;
 470}
 471
 472static struct ref_iterator_vtable packed_ref_iterator_vtable = {
 473        packed_ref_iterator_advance,
 474        packed_ref_iterator_peel,
 475        packed_ref_iterator_abort
 476};
 477
 478static struct ref_iterator *packed_ref_iterator_begin(
 479                struct ref_store *ref_store,
 480                const char *prefix, unsigned int flags)
 481{
 482        struct packed_ref_store *refs;
 483        struct packed_ref_iterator *iter;
 484        struct ref_iterator *ref_iterator;
 485        unsigned int required_flags = REF_STORE_READ;
 486
 487        if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
 488                required_flags |= REF_STORE_ODB;
 489        refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin");
 490
 491        iter = xcalloc(1, sizeof(*iter));
 492        ref_iterator = &iter->base;
 493        base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable, 1);
 494
 495        /*
 496         * Note that get_packed_ref_cache() internally checks whether
 497         * the packed-ref cache is up to date with what is on disk,
 498         * and re-reads it if not.
 499         */
 500
 501        iter->cache = get_packed_ref_cache(refs);
 502        acquire_packed_ref_cache(iter->cache);
 503        iter->iter0 = cache_ref_iterator_begin(iter->cache->cache, prefix, 0);
 504
 505        iter->flags = flags;
 506
 507        return ref_iterator;
 508}
 509
 510/*
 511 * Write an entry to the packed-refs file for the specified refname.
 512 * If peeled is non-NULL, write it as the entry's peeled value. On
 513 * error, return a nonzero value and leave errno set at the value left
 514 * by the failing call to `fprintf()`.
 515 */
 516static int write_packed_entry(FILE *fh, const char *refname,
 517                              const unsigned char *sha1,
 518                              const unsigned char *peeled)
 519{
 520        if (fprintf(fh, "%s %s\n", sha1_to_hex(sha1), refname) < 0 ||
 521            (peeled && fprintf(fh, "^%s\n", sha1_to_hex(peeled)) < 0))
 522                return -1;
 523
 524        return 0;
 525}
 526
 527int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
 528{
 529        struct packed_ref_store *refs =
 530                packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
 531                                "packed_refs_lock");
 532        static int timeout_configured = 0;
 533        static int timeout_value = 1000;
 534
 535        if (!timeout_configured) {
 536                git_config_get_int("core.packedrefstimeout", &timeout_value);
 537                timeout_configured = 1;
 538        }
 539
 540        /*
 541         * Note that we close the lockfile immediately because we
 542         * don't write new content to it, but rather to a separate
 543         * tempfile.
 544         */
 545        if (hold_lock_file_for_update_timeout(
 546                            &refs->lock,
 547                            refs->path,
 548                            flags, timeout_value) < 0) {
 549                unable_to_lock_message(refs->path, errno, err);
 550                return -1;
 551        }
 552
 553        if (close_lock_file(&refs->lock)) {
 554                strbuf_addf(err, "unable to close %s: %s", refs->path, strerror(errno));
 555                return -1;
 556        }
 557
 558        /*
 559         * Now that we hold the `packed-refs` lock, make sure that our
 560         * cache matches the current version of the file. Normally
 561         * `get_packed_ref_cache()` does that for us, but that
 562         * function assumes that when the file is locked, any existing
 563         * cache is still valid. We've just locked the file, but it
 564         * might have changed the moment *before* we locked it.
 565         */
 566        validate_packed_ref_cache(refs);
 567
 568        /*
 569         * Now make sure that the packed-refs file as it exists in the
 570         * locked state is loaded into the cache:
 571         */
 572        get_packed_ref_cache(refs);
 573        return 0;
 574}
 575
 576void packed_refs_unlock(struct ref_store *ref_store)
 577{
 578        struct packed_ref_store *refs = packed_downcast(
 579                        ref_store,
 580                        REF_STORE_READ | REF_STORE_WRITE,
 581                        "packed_refs_unlock");
 582
 583        if (!is_lock_file_locked(&refs->lock))
 584                die("BUG: packed_refs_unlock() called when not locked");
 585        rollback_lock_file(&refs->lock);
 586}
 587
 588int packed_refs_is_locked(struct ref_store *ref_store)
 589{
 590        struct packed_ref_store *refs = packed_downcast(
 591                        ref_store,
 592                        REF_STORE_READ | REF_STORE_WRITE,
 593                        "packed_refs_is_locked");
 594
 595        return is_lock_file_locked(&refs->lock);
 596}
 597
 598/*
 599 * The packed-refs header line that we write out.  Perhaps other
 600 * traits will be added later.  The trailing space is required.
 601 */
 602static const char PACKED_REFS_HEADER[] =
 603        "# pack-refs with: peeled fully-peeled \n";
 604
 605static int packed_init_db(struct ref_store *ref_store, struct strbuf *err)
 606{
 607        /* Nothing to do. */
 608        return 0;
 609}
 610
 611/*
 612 * Write the packed-refs from the cache to the packed-refs tempfile,
 613 * incorporating any changes from `updates`. `updates` must be a
 614 * sorted string list whose keys are the refnames and whose util
 615 * values are `struct ref_update *`. On error, rollback the tempfile,
 616 * write an error message to `err`, and return a nonzero value.
 617 *
 618 * The packfile must be locked before calling this function and will
 619 * remain locked when it is done.
 620 */
 621static int write_with_updates(struct packed_ref_store *refs,
 622                              struct string_list *updates,
 623                              struct strbuf *err)
 624{
 625        struct ref_iterator *iter = NULL;
 626        size_t i;
 627        int ok;
 628        FILE *out;
 629        struct strbuf sb = STRBUF_INIT;
 630        char *packed_refs_path;
 631
 632        if (!is_lock_file_locked(&refs->lock))
 633                die("BUG: write_with_updates() called while unlocked");
 634
 635        /*
 636         * If packed-refs is a symlink, we want to overwrite the
 637         * symlinked-to file, not the symlink itself. Also, put the
 638         * staging file next to it:
 639         */
 640        packed_refs_path = get_locked_file_path(&refs->lock);
 641        strbuf_addf(&sb, "%s.new", packed_refs_path);
 642        free(packed_refs_path);
 643        if (create_tempfile(&refs->tempfile, sb.buf) < 0) {
 644                strbuf_addf(err, "unable to create file %s: %s",
 645                            sb.buf, strerror(errno));
 646                strbuf_release(&sb);
 647                return -1;
 648        }
 649        strbuf_release(&sb);
 650
 651        out = fdopen_tempfile(&refs->tempfile, "w");
 652        if (!out) {
 653                strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s",
 654                            strerror(errno));
 655                goto error;
 656        }
 657
 658        if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0)
 659                goto write_error;
 660
 661        /*
 662         * We iterate in parallel through the current list of refs and
 663         * the list of updates, processing an entry from at least one
 664         * of the lists each time through the loop. When the current
 665         * list of refs is exhausted, set iter to NULL. When the list
 666         * of updates is exhausted, leave i set to updates->nr.
 667         */
 668        iter = packed_ref_iterator_begin(&refs->base, "",
 669                                         DO_FOR_EACH_INCLUDE_BROKEN);
 670        if ((ok = ref_iterator_advance(iter)) != ITER_OK)
 671                iter = NULL;
 672
 673        i = 0;
 674
 675        while (iter || i < updates->nr) {
 676                struct ref_update *update = NULL;
 677                int cmp;
 678
 679                if (i >= updates->nr) {
 680                        cmp = -1;
 681                } else {
 682                        update = updates->items[i].util;
 683
 684                        if (!iter)
 685                                cmp = +1;
 686                        else
 687                                cmp = strcmp(iter->refname, update->refname);
 688                }
 689
 690                if (!cmp) {
 691                        /*
 692                         * There is both an old value and an update
 693                         * for this reference. Check the old value if
 694                         * necessary:
 695                         */
 696                        if ((update->flags & REF_HAVE_OLD)) {
 697                                if (is_null_oid(&update->old_oid)) {
 698                                        strbuf_addf(err, "cannot update ref '%s': "
 699                                                    "reference already exists",
 700                                                    update->refname);
 701                                        goto error;
 702                                } else if (oidcmp(&update->old_oid, iter->oid)) {
 703                                        strbuf_addf(err, "cannot update ref '%s': "
 704                                                    "is at %s but expected %s",
 705                                                    update->refname,
 706                                                    oid_to_hex(iter->oid),
 707                                                    oid_to_hex(&update->old_oid));
 708                                        goto error;
 709                                }
 710                        }
 711
 712                        /* Now figure out what to use for the new value: */
 713                        if ((update->flags & REF_HAVE_NEW)) {
 714                                /*
 715                                 * The update takes precedence. Skip
 716                                 * the iterator over the unneeded
 717                                 * value.
 718                                 */
 719                                if ((ok = ref_iterator_advance(iter)) != ITER_OK)
 720                                        iter = NULL;
 721                                cmp = +1;
 722                        } else {
 723                                /*
 724                                 * The update doesn't actually want to
 725                                 * change anything. We're done with it.
 726                                 */
 727                                i++;
 728                                cmp = -1;
 729                        }
 730                } else if (cmp > 0) {
 731                        /*
 732                         * There is no old value but there is an
 733                         * update for this reference. Make sure that
 734                         * the update didn't expect an existing value:
 735                         */
 736                        if ((update->flags & REF_HAVE_OLD) &&
 737                            !is_null_oid(&update->old_oid)) {
 738                                strbuf_addf(err, "cannot update ref '%s': "
 739                                            "reference is missing but expected %s",
 740                                            update->refname,
 741                                            oid_to_hex(&update->old_oid));
 742                                goto error;
 743                        }
 744                }
 745
 746                if (cmp < 0) {
 747                        /* Pass the old reference through. */
 748
 749                        struct object_id peeled;
 750                        int peel_error = ref_iterator_peel(iter, &peeled);
 751
 752                        if (write_packed_entry(out, iter->refname,
 753                                               iter->oid->hash,
 754                                               peel_error ? NULL : peeled.hash))
 755                                goto write_error;
 756
 757                        if ((ok = ref_iterator_advance(iter)) != ITER_OK)
 758                                iter = NULL;
 759                } else if (is_null_oid(&update->new_oid)) {
 760                        /*
 761                         * The update wants to delete the reference,
 762                         * and the reference either didn't exist or we
 763                         * have already skipped it. So we're done with
 764                         * the update (and don't have to write
 765                         * anything).
 766                         */
 767                        i++;
 768                } else {
 769                        struct object_id peeled;
 770                        int peel_error = peel_object(update->new_oid.hash,
 771                                                     peeled.hash);
 772
 773                        if (write_packed_entry(out, update->refname,
 774                                               update->new_oid.hash,
 775                                               peel_error ? NULL : peeled.hash))
 776                                goto write_error;
 777
 778                        i++;
 779                }
 780        }
 781
 782        if (ok != ITER_DONE) {
 783                strbuf_addf(err, "unable to write packed-refs file: "
 784                            "error iterating over old contents");
 785                goto error;
 786        }
 787
 788        if (close_tempfile(&refs->tempfile)) {
 789                strbuf_addf(err, "error closing file %s: %s",
 790                            get_tempfile_path(&refs->tempfile),
 791                            strerror(errno));
 792                strbuf_release(&sb);
 793                return -1;
 794        }
 795
 796        return 0;
 797
 798write_error:
 799        strbuf_addf(err, "error writing to %s: %s",
 800                    get_tempfile_path(&refs->tempfile), strerror(errno));
 801
 802error:
 803        if (iter)
 804                ref_iterator_abort(iter);
 805
 806        delete_tempfile(&refs->tempfile);
 807        return -1;
 808}
 809
 810struct packed_transaction_backend_data {
 811        /* True iff the transaction owns the packed-refs lock. */
 812        int own_lock;
 813
 814        struct string_list updates;
 815};
 816
 817static void packed_transaction_cleanup(struct packed_ref_store *refs,
 818                                       struct ref_transaction *transaction)
 819{
 820        struct packed_transaction_backend_data *data = transaction->backend_data;
 821
 822        if (data) {
 823                string_list_clear(&data->updates, 0);
 824
 825                if (is_tempfile_active(&refs->tempfile))
 826                        delete_tempfile(&refs->tempfile);
 827
 828                if (data->own_lock && is_lock_file_locked(&refs->lock)) {
 829                        packed_refs_unlock(&refs->base);
 830                        data->own_lock = 0;
 831                }
 832
 833                free(data);
 834                transaction->backend_data = NULL;
 835        }
 836
 837        transaction->state = REF_TRANSACTION_CLOSED;
 838}
 839
 840static int packed_transaction_prepare(struct ref_store *ref_store,
 841                                      struct ref_transaction *transaction,
 842                                      struct strbuf *err)
 843{
 844        struct packed_ref_store *refs = packed_downcast(
 845                        ref_store,
 846                        REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
 847                        "ref_transaction_prepare");
 848        struct packed_transaction_backend_data *data;
 849        size_t i;
 850        int ret = TRANSACTION_GENERIC_ERROR;
 851
 852        /*
 853         * Note that we *don't* skip transactions with zero updates,
 854         * because such a transaction might be executed for the side
 855         * effect of ensuring that all of the references are peeled.
 856         * If the caller wants to optimize away empty transactions, it
 857         * should do so itself.
 858         */
 859
 860        data = xcalloc(1, sizeof(*data));
 861        string_list_init(&data->updates, 0);
 862
 863        transaction->backend_data = data;
 864
 865        /*
 866         * Stick the updates in a string list by refname so that we
 867         * can sort them:
 868         */
 869        for (i = 0; i < transaction->nr; i++) {
 870                struct ref_update *update = transaction->updates[i];
 871                struct string_list_item *item =
 872                        string_list_append(&data->updates, update->refname);
 873
 874                /* Store a pointer to update in item->util: */
 875                item->util = update;
 876        }
 877        string_list_sort(&data->updates);
 878
 879        if (ref_update_reject_duplicates(&data->updates, err))
 880                goto failure;
 881
 882        if (!is_lock_file_locked(&refs->lock)) {
 883                if (packed_refs_lock(ref_store, 0, err))
 884                        goto failure;
 885                data->own_lock = 1;
 886        }
 887
 888        if (write_with_updates(refs, &data->updates, err))
 889                goto failure;
 890
 891        transaction->state = REF_TRANSACTION_PREPARED;
 892        return 0;
 893
 894failure:
 895        packed_transaction_cleanup(refs, transaction);
 896        return ret;
 897}
 898
 899static int packed_transaction_abort(struct ref_store *ref_store,
 900                                    struct ref_transaction *transaction,
 901                                    struct strbuf *err)
 902{
 903        struct packed_ref_store *refs = packed_downcast(
 904                        ref_store,
 905                        REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
 906                        "ref_transaction_abort");
 907
 908        packed_transaction_cleanup(refs, transaction);
 909        return 0;
 910}
 911
 912static int packed_transaction_finish(struct ref_store *ref_store,
 913                                     struct ref_transaction *transaction,
 914                                     struct strbuf *err)
 915{
 916        struct packed_ref_store *refs = packed_downcast(
 917                        ref_store,
 918                        REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
 919                        "ref_transaction_finish");
 920        int ret = TRANSACTION_GENERIC_ERROR;
 921        char *packed_refs_path;
 922
 923        packed_refs_path = get_locked_file_path(&refs->lock);
 924        if (rename_tempfile(&refs->tempfile, packed_refs_path)) {
 925                strbuf_addf(err, "error replacing %s: %s",
 926                            refs->path, strerror(errno));
 927                goto cleanup;
 928        }
 929
 930        clear_packed_ref_cache(refs);
 931        ret = 0;
 932
 933cleanup:
 934        free(packed_refs_path);
 935        packed_transaction_cleanup(refs, transaction);
 936        return ret;
 937}
 938
 939static int packed_initial_transaction_commit(struct ref_store *ref_store,
 940                                            struct ref_transaction *transaction,
 941                                            struct strbuf *err)
 942{
 943        return ref_transaction_commit(transaction, err);
 944}
 945
 946static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
 947                             struct string_list *refnames, unsigned int flags)
 948{
 949        struct packed_ref_store *refs =
 950                packed_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
 951        struct strbuf err = STRBUF_INIT;
 952        struct ref_transaction *transaction;
 953        struct string_list_item *item;
 954        int ret;
 955
 956        (void)refs; /* We need the check above, but don't use the variable */
 957
 958        if (!refnames->nr)
 959                return 0;
 960
 961        /*
 962         * Since we don't check the references' old_oids, the
 963         * individual updates can't fail, so we can pack all of the
 964         * updates into a single transaction.
 965         */
 966
 967        transaction = ref_store_transaction_begin(ref_store, &err);
 968        if (!transaction)
 969                return -1;
 970
 971        for_each_string_list_item(item, refnames) {
 972                if (ref_transaction_delete(transaction, item->string, NULL,
 973                                           flags, msg, &err)) {
 974                        warning(_("could not delete reference %s: %s"),
 975                                item->string, err.buf);
 976                        strbuf_reset(&err);
 977                }
 978        }
 979
 980        ret = ref_transaction_commit(transaction, &err);
 981
 982        if (ret) {
 983                if (refnames->nr == 1)
 984                        error(_("could not delete reference %s: %s"),
 985                              refnames->items[0].string, err.buf);
 986                else
 987                        error(_("could not delete references: %s"), err.buf);
 988        }
 989
 990        ref_transaction_free(transaction);
 991        strbuf_release(&err);
 992        return ret;
 993}
 994
 995static int packed_pack_refs(struct ref_store *ref_store, unsigned int flags)
 996{
 997        /*
 998         * Packed refs are already packed. It might be that loose refs
 999         * are packed *into* a packed refs store, but that is done by
1000         * updating the packed references via a transaction.
1001         */
1002        return 0;
1003}
1004
1005static int packed_create_symref(struct ref_store *ref_store,
1006                               const char *refname, const char *target,
1007                               const char *logmsg)
1008{
1009        die("BUG: packed reference store does not support symrefs");
1010}
1011
1012static int packed_rename_ref(struct ref_store *ref_store,
1013                            const char *oldrefname, const char *newrefname,
1014                            const char *logmsg)
1015{
1016        die("BUG: packed reference store does not support renaming references");
1017}
1018
1019static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store)
1020{
1021        return empty_ref_iterator_begin();
1022}
1023
1024static int packed_for_each_reflog_ent(struct ref_store *ref_store,
1025                                      const char *refname,
1026                                      each_reflog_ent_fn fn, void *cb_data)
1027{
1028        return 0;
1029}
1030
1031static int packed_for_each_reflog_ent_reverse(struct ref_store *ref_store,
1032                                              const char *refname,
1033                                              each_reflog_ent_fn fn,
1034                                              void *cb_data)
1035{
1036        return 0;
1037}
1038
1039static int packed_reflog_exists(struct ref_store *ref_store,
1040                               const char *refname)
1041{
1042        return 0;
1043}
1044
1045static int packed_create_reflog(struct ref_store *ref_store,
1046                               const char *refname, int force_create,
1047                               struct strbuf *err)
1048{
1049        die("BUG: packed reference store does not support reflogs");
1050}
1051
1052static int packed_delete_reflog(struct ref_store *ref_store,
1053                               const char *refname)
1054{
1055        return 0;
1056}
1057
1058static int packed_reflog_expire(struct ref_store *ref_store,
1059                                const char *refname, const unsigned char *sha1,
1060                                unsigned int flags,
1061                                reflog_expiry_prepare_fn prepare_fn,
1062                                reflog_expiry_should_prune_fn should_prune_fn,
1063                                reflog_expiry_cleanup_fn cleanup_fn,
1064                                void *policy_cb_data)
1065{
1066        return 0;
1067}
1068
1069struct ref_storage_be refs_be_packed = {
1070        NULL,
1071        "packed",
1072        packed_ref_store_create,
1073        packed_init_db,
1074        packed_transaction_prepare,
1075        packed_transaction_finish,
1076        packed_transaction_abort,
1077        packed_initial_transaction_commit,
1078
1079        packed_pack_refs,
1080        packed_peel_ref,
1081        packed_create_symref,
1082        packed_delete_refs,
1083        packed_rename_ref,
1084
1085        packed_ref_iterator_begin,
1086        packed_read_raw_ref,
1087
1088        packed_reflog_iterator_begin,
1089        packed_for_each_reflog_ent,
1090        packed_for_each_reflog_ent_reverse,
1091        packed_reflog_exists,
1092        packed_create_reflog,
1093        packed_delete_reflog,
1094        packed_reflog_expire
1095};