5c50c223ef9d997ac5f4e27a8adacf7cca60f05f
   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        FILE *f;
 219        struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
 220        struct ref_entry *last = NULL;
 221        struct strbuf line = STRBUF_INIT;
 222        enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
 223        struct ref_dir *dir;
 224
 225        packed_refs->refs = refs;
 226        acquire_packed_ref_cache(packed_refs);
 227        packed_refs->cache = create_ref_cache(NULL, NULL);
 228        packed_refs->cache->root->flag &= ~REF_INCOMPLETE;
 229
 230        f = fopen(refs->path, "r");
 231        if (!f) {
 232                if (errno == ENOENT) {
 233                        /*
 234                         * This is OK; it just means that no
 235                         * "packed-refs" file has been written yet,
 236                         * which is equivalent to it being empty.
 237                         */
 238                        return packed_refs;
 239                } else {
 240                        die_errno("couldn't read %s", refs->path);
 241                }
 242        }
 243
 244        stat_validity_update(&packed_refs->validity, fileno(f));
 245
 246        dir = get_ref_dir(packed_refs->cache->root);
 247        while (strbuf_getwholeline(&line, f, '\n') != EOF) {
 248                struct object_id oid;
 249                const char *refname;
 250                const char *traits;
 251
 252                if (!line.len || line.buf[line.len - 1] != '\n')
 253                        die_unterminated_line(refs->path, line.buf, line.len);
 254
 255                if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
 256                        if (strstr(traits, " fully-peeled "))
 257                                peeled = PEELED_FULLY;
 258                        else if (strstr(traits, " peeled "))
 259                                peeled = PEELED_TAGS;
 260                        /* perhaps other traits later as well */
 261                        continue;
 262                }
 263
 264                refname = parse_ref_line(&line, &oid);
 265                if (refname) {
 266                        int flag = REF_ISPACKED;
 267
 268                        if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
 269                                if (!refname_is_safe(refname))
 270                                        die("packed refname is dangerous: %s", refname);
 271                                oidclr(&oid);
 272                                flag |= REF_BAD_NAME | REF_ISBROKEN;
 273                        }
 274                        last = create_ref_entry(refname, &oid, flag);
 275                        if (peeled == PEELED_FULLY ||
 276                            (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
 277                                last->flag |= REF_KNOWS_PEELED;
 278                        add_ref_entry(dir, last);
 279                } else if (last &&
 280                    line.buf[0] == '^' &&
 281                    line.len == PEELED_LINE_LENGTH &&
 282                    line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
 283                    !get_oid_hex(line.buf + 1, &oid)) {
 284                        oidcpy(&last->u.value.peeled, &oid);
 285                        /*
 286                         * Regardless of what the file header said,
 287                         * we definitely know the value of *this*
 288                         * reference:
 289                         */
 290                        last->flag |= REF_KNOWS_PEELED;
 291                } else {
 292                        die_invalid_line(refs->path, line.buf, line.len);
 293                }
 294        }
 295
 296        fclose(f);
 297        strbuf_release(&line);
 298
 299        return packed_refs;
 300}
 301
 302/*
 303 * Check that the packed refs cache (if any) still reflects the
 304 * contents of the file. If not, clear the cache.
 305 */
 306static void validate_packed_ref_cache(struct packed_ref_store *refs)
 307{
 308        if (refs->cache &&
 309            !stat_validity_check(&refs->cache->validity, refs->path))
 310                clear_packed_ref_cache(refs);
 311}
 312
 313/*
 314 * Get the packed_ref_cache for the specified packed_ref_store,
 315 * creating and populating it if it hasn't been read before or if the
 316 * file has been changed (according to its `validity` field) since it
 317 * was last read. On the other hand, if we hold the lock, then assume
 318 * that the file hasn't been changed out from under us, so skip the
 319 * extra `stat()` call in `stat_validity_check()`.
 320 */
 321static struct packed_ref_cache *get_packed_ref_cache(struct packed_ref_store *refs)
 322{
 323        if (!is_lock_file_locked(&refs->lock))
 324                validate_packed_ref_cache(refs);
 325
 326        if (!refs->cache)
 327                refs->cache = read_packed_refs(refs);
 328
 329        return refs->cache;
 330}
 331
 332static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
 333{
 334        return get_ref_dir(packed_ref_cache->cache->root);
 335}
 336
 337static struct ref_dir *get_packed_refs(struct packed_ref_store *refs)
 338{
 339        return get_packed_ref_dir(get_packed_ref_cache(refs));
 340}
 341
 342/*
 343 * Return the ref_entry for the given refname from the packed
 344 * references.  If it does not exist, return NULL.
 345 */
 346static struct ref_entry *get_packed_ref(struct packed_ref_store *refs,
 347                                        const char *refname)
 348{
 349        return find_ref_entry(get_packed_refs(refs), refname);
 350}
 351
 352static int packed_read_raw_ref(struct ref_store *ref_store,
 353                               const char *refname, unsigned char *sha1,
 354                               struct strbuf *referent, unsigned int *type)
 355{
 356        struct packed_ref_store *refs =
 357                packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
 358
 359        struct ref_entry *entry;
 360
 361        *type = 0;
 362
 363        entry = get_packed_ref(refs, refname);
 364        if (!entry) {
 365                errno = ENOENT;
 366                return -1;
 367        }
 368
 369        hashcpy(sha1, entry->u.value.oid.hash);
 370        *type = REF_ISPACKED;
 371        return 0;
 372}
 373
 374static int packed_peel_ref(struct ref_store *ref_store,
 375                           const char *refname, unsigned char *sha1)
 376{
 377        struct packed_ref_store *refs =
 378                packed_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
 379                                "peel_ref");
 380        struct ref_entry *r = get_packed_ref(refs, refname);
 381
 382        if (!r || peel_entry(r, 0))
 383                return -1;
 384
 385        hashcpy(sha1, r->u.value.peeled.hash);
 386        return 0;
 387}
 388
 389struct packed_ref_iterator {
 390        struct ref_iterator base;
 391
 392        struct packed_ref_cache *cache;
 393        struct ref_iterator *iter0;
 394        unsigned int flags;
 395};
 396
 397static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
 398{
 399        struct packed_ref_iterator *iter =
 400                (struct packed_ref_iterator *)ref_iterator;
 401        int ok;
 402
 403        while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
 404                if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
 405                    ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
 406                        continue;
 407
 408                if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
 409                    !ref_resolves_to_object(iter->iter0->refname,
 410                                            iter->iter0->oid,
 411                                            iter->iter0->flags))
 412                        continue;
 413
 414                iter->base.refname = iter->iter0->refname;
 415                iter->base.oid = iter->iter0->oid;
 416                iter->base.flags = iter->iter0->flags;
 417                return ITER_OK;
 418        }
 419
 420        iter->iter0 = NULL;
 421        if (ref_iterator_abort(ref_iterator) != ITER_DONE)
 422                ok = ITER_ERROR;
 423
 424        return ok;
 425}
 426
 427static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator,
 428                                   struct object_id *peeled)
 429{
 430        struct packed_ref_iterator *iter =
 431                (struct packed_ref_iterator *)ref_iterator;
 432
 433        return ref_iterator_peel(iter->iter0, peeled);
 434}
 435
 436static int packed_ref_iterator_abort(struct ref_iterator *ref_iterator)
 437{
 438        struct packed_ref_iterator *iter =
 439                (struct packed_ref_iterator *)ref_iterator;
 440        int ok = ITER_DONE;
 441
 442        if (iter->iter0)
 443                ok = ref_iterator_abort(iter->iter0);
 444
 445        release_packed_ref_cache(iter->cache);
 446        base_ref_iterator_free(ref_iterator);
 447        return ok;
 448}
 449
 450static struct ref_iterator_vtable packed_ref_iterator_vtable = {
 451        packed_ref_iterator_advance,
 452        packed_ref_iterator_peel,
 453        packed_ref_iterator_abort
 454};
 455
 456static struct ref_iterator *packed_ref_iterator_begin(
 457                struct ref_store *ref_store,
 458                const char *prefix, unsigned int flags)
 459{
 460        struct packed_ref_store *refs;
 461        struct packed_ref_iterator *iter;
 462        struct ref_iterator *ref_iterator;
 463        unsigned int required_flags = REF_STORE_READ;
 464
 465        if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
 466                required_flags |= REF_STORE_ODB;
 467        refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin");
 468
 469        iter = xcalloc(1, sizeof(*iter));
 470        ref_iterator = &iter->base;
 471        base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable, 1);
 472
 473        /*
 474         * Note that get_packed_ref_cache() internally checks whether
 475         * the packed-ref cache is up to date with what is on disk,
 476         * and re-reads it if not.
 477         */
 478
 479        iter->cache = get_packed_ref_cache(refs);
 480        acquire_packed_ref_cache(iter->cache);
 481        iter->iter0 = cache_ref_iterator_begin(iter->cache->cache, prefix, 0);
 482
 483        iter->flags = flags;
 484
 485        return ref_iterator;
 486}
 487
 488/*
 489 * Write an entry to the packed-refs file for the specified refname.
 490 * If peeled is non-NULL, write it as the entry's peeled value. On
 491 * error, return a nonzero value and leave errno set at the value left
 492 * by the failing call to `fprintf()`.
 493 */
 494static int write_packed_entry(FILE *fh, const char *refname,
 495                              const unsigned char *sha1,
 496                              const unsigned char *peeled)
 497{
 498        if (fprintf(fh, "%s %s\n", sha1_to_hex(sha1), refname) < 0 ||
 499            (peeled && fprintf(fh, "^%s\n", sha1_to_hex(peeled)) < 0))
 500                return -1;
 501
 502        return 0;
 503}
 504
 505int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
 506{
 507        struct packed_ref_store *refs =
 508                packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
 509                                "packed_refs_lock");
 510        static int timeout_configured = 0;
 511        static int timeout_value = 1000;
 512
 513        if (!timeout_configured) {
 514                git_config_get_int("core.packedrefstimeout", &timeout_value);
 515                timeout_configured = 1;
 516        }
 517
 518        /*
 519         * Note that we close the lockfile immediately because we
 520         * don't write new content to it, but rather to a separate
 521         * tempfile.
 522         */
 523        if (hold_lock_file_for_update_timeout(
 524                            &refs->lock,
 525                            refs->path,
 526                            flags, timeout_value) < 0) {
 527                unable_to_lock_message(refs->path, errno, err);
 528                return -1;
 529        }
 530
 531        if (close_lock_file(&refs->lock)) {
 532                strbuf_addf(err, "unable to close %s: %s", refs->path, strerror(errno));
 533                return -1;
 534        }
 535
 536        /*
 537         * Now that we hold the `packed-refs` lock, make sure that our
 538         * cache matches the current version of the file. Normally
 539         * `get_packed_ref_cache()` does that for us, but that
 540         * function assumes that when the file is locked, any existing
 541         * cache is still valid. We've just locked the file, but it
 542         * might have changed the moment *before* we locked it.
 543         */
 544        validate_packed_ref_cache(refs);
 545
 546        /*
 547         * Now make sure that the packed-refs file as it exists in the
 548         * locked state is loaded into the cache:
 549         */
 550        get_packed_ref_cache(refs);
 551        return 0;
 552}
 553
 554void packed_refs_unlock(struct ref_store *ref_store)
 555{
 556        struct packed_ref_store *refs = packed_downcast(
 557                        ref_store,
 558                        REF_STORE_READ | REF_STORE_WRITE,
 559                        "packed_refs_unlock");
 560
 561        if (!is_lock_file_locked(&refs->lock))
 562                die("BUG: packed_refs_unlock() called when not locked");
 563        rollback_lock_file(&refs->lock);
 564}
 565
 566int packed_refs_is_locked(struct ref_store *ref_store)
 567{
 568        struct packed_ref_store *refs = packed_downcast(
 569                        ref_store,
 570                        REF_STORE_READ | REF_STORE_WRITE,
 571                        "packed_refs_is_locked");
 572
 573        return is_lock_file_locked(&refs->lock);
 574}
 575
 576/*
 577 * The packed-refs header line that we write out.  Perhaps other
 578 * traits will be added later.  The trailing space is required.
 579 */
 580static const char PACKED_REFS_HEADER[] =
 581        "# pack-refs with: peeled fully-peeled \n";
 582
 583static int packed_init_db(struct ref_store *ref_store, struct strbuf *err)
 584{
 585        /* Nothing to do. */
 586        return 0;
 587}
 588
 589/*
 590 * Write the packed-refs from the cache to the packed-refs tempfile,
 591 * incorporating any changes from `updates`. `updates` must be a
 592 * sorted string list whose keys are the refnames and whose util
 593 * values are `struct ref_update *`. On error, rollback the tempfile,
 594 * write an error message to `err`, and return a nonzero value.
 595 *
 596 * The packfile must be locked before calling this function and will
 597 * remain locked when it is done.
 598 */
 599static int write_with_updates(struct packed_ref_store *refs,
 600                              struct string_list *updates,
 601                              struct strbuf *err)
 602{
 603        struct ref_iterator *iter = NULL;
 604        size_t i;
 605        int ok;
 606        FILE *out;
 607        struct strbuf sb = STRBUF_INIT;
 608        char *packed_refs_path;
 609
 610        if (!is_lock_file_locked(&refs->lock))
 611                die("BUG: write_with_updates() called while unlocked");
 612
 613        /*
 614         * If packed-refs is a symlink, we want to overwrite the
 615         * symlinked-to file, not the symlink itself. Also, put the
 616         * staging file next to it:
 617         */
 618        packed_refs_path = get_locked_file_path(&refs->lock);
 619        strbuf_addf(&sb, "%s.new", packed_refs_path);
 620        free(packed_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                goto write_error;
 638
 639        /*
 640         * We iterate in parallel through the current list of refs and
 641         * the list of updates, processing an entry from at least one
 642         * of the lists each time through the loop. When the current
 643         * list of refs is exhausted, set iter to NULL. When the list
 644         * of updates is exhausted, leave i set to updates->nr.
 645         */
 646        iter = packed_ref_iterator_begin(&refs->base, "",
 647                                         DO_FOR_EACH_INCLUDE_BROKEN);
 648        if ((ok = ref_iterator_advance(iter)) != ITER_OK)
 649                iter = NULL;
 650
 651        i = 0;
 652
 653        while (iter || i < updates->nr) {
 654                struct ref_update *update = NULL;
 655                int cmp;
 656
 657                if (i >= updates->nr) {
 658                        cmp = -1;
 659                } else {
 660                        update = updates->items[i].util;
 661
 662                        if (!iter)
 663                                cmp = +1;
 664                        else
 665                                cmp = strcmp(iter->refname, update->refname);
 666                }
 667
 668                if (!cmp) {
 669                        /*
 670                         * There is both an old value and an update
 671                         * for this reference. Check the old value if
 672                         * necessary:
 673                         */
 674                        if ((update->flags & REF_HAVE_OLD)) {
 675                                if (is_null_oid(&update->old_oid)) {
 676                                        strbuf_addf(err, "cannot update ref '%s': "
 677                                                    "reference already exists",
 678                                                    update->refname);
 679                                        goto error;
 680                                } else if (oidcmp(&update->old_oid, iter->oid)) {
 681                                        strbuf_addf(err, "cannot update ref '%s': "
 682                                                    "is at %s but expected %s",
 683                                                    update->refname,
 684                                                    oid_to_hex(iter->oid),
 685                                                    oid_to_hex(&update->old_oid));
 686                                        goto error;
 687                                }
 688                        }
 689
 690                        /* Now figure out what to use for the new value: */
 691                        if ((update->flags & REF_HAVE_NEW)) {
 692                                /*
 693                                 * The update takes precedence. Skip
 694                                 * the iterator over the unneeded
 695                                 * value.
 696                                 */
 697                                if ((ok = ref_iterator_advance(iter)) != ITER_OK)
 698                                        iter = NULL;
 699                                cmp = +1;
 700                        } else {
 701                                /*
 702                                 * The update doesn't actually want to
 703                                 * change anything. We're done with it.
 704                                 */
 705                                i++;
 706                                cmp = -1;
 707                        }
 708                } else if (cmp > 0) {
 709                        /*
 710                         * There is no old value but there is an
 711                         * update for this reference. Make sure that
 712                         * the update didn't expect an existing value:
 713                         */
 714                        if ((update->flags & REF_HAVE_OLD) &&
 715                            !is_null_oid(&update->old_oid)) {
 716                                strbuf_addf(err, "cannot update ref '%s': "
 717                                            "reference is missing but expected %s",
 718                                            update->refname,
 719                                            oid_to_hex(&update->old_oid));
 720                                goto error;
 721                        }
 722                }
 723
 724                if (cmp < 0) {
 725                        /* Pass the old reference through. */
 726
 727                        struct object_id peeled;
 728                        int peel_error = ref_iterator_peel(iter, &peeled);
 729
 730                        if (write_packed_entry(out, iter->refname,
 731                                               iter->oid->hash,
 732                                               peel_error ? NULL : peeled.hash))
 733                                goto write_error;
 734
 735                        if ((ok = ref_iterator_advance(iter)) != ITER_OK)
 736                                iter = NULL;
 737                } else if (is_null_oid(&update->new_oid)) {
 738                        /*
 739                         * The update wants to delete the reference,
 740                         * and the reference either didn't exist or we
 741                         * have already skipped it. So we're done with
 742                         * the update (and don't have to write
 743                         * anything).
 744                         */
 745                        i++;
 746                } else {
 747                        struct object_id peeled;
 748                        int peel_error = peel_object(update->new_oid.hash,
 749                                                     peeled.hash);
 750
 751                        if (write_packed_entry(out, update->refname,
 752                                               update->new_oid.hash,
 753                                               peel_error ? NULL : peeled.hash))
 754                                goto write_error;
 755
 756                        i++;
 757                }
 758        }
 759
 760        if (ok != ITER_DONE) {
 761                strbuf_addf(err, "unable to write packed-refs file: "
 762                            "error iterating over old contents");
 763                goto error;
 764        }
 765
 766        if (close_tempfile(&refs->tempfile)) {
 767                strbuf_addf(err, "error closing file %s: %s",
 768                            get_tempfile_path(&refs->tempfile),
 769                            strerror(errno));
 770                strbuf_release(&sb);
 771                return -1;
 772        }
 773
 774        return 0;
 775
 776write_error:
 777        strbuf_addf(err, "error writing to %s: %s",
 778                    get_tempfile_path(&refs->tempfile), strerror(errno));
 779
 780error:
 781        if (iter)
 782                ref_iterator_abort(iter);
 783
 784        delete_tempfile(&refs->tempfile);
 785        return -1;
 786}
 787
 788struct packed_transaction_backend_data {
 789        /* True iff the transaction owns the packed-refs lock. */
 790        int own_lock;
 791
 792        struct string_list updates;
 793};
 794
 795static void packed_transaction_cleanup(struct packed_ref_store *refs,
 796                                       struct ref_transaction *transaction)
 797{
 798        struct packed_transaction_backend_data *data = transaction->backend_data;
 799
 800        if (data) {
 801                string_list_clear(&data->updates, 0);
 802
 803                if (is_tempfile_active(&refs->tempfile))
 804                        delete_tempfile(&refs->tempfile);
 805
 806                if (data->own_lock && is_lock_file_locked(&refs->lock)) {
 807                        packed_refs_unlock(&refs->base);
 808                        data->own_lock = 0;
 809                }
 810
 811                free(data);
 812                transaction->backend_data = NULL;
 813        }
 814
 815        transaction->state = REF_TRANSACTION_CLOSED;
 816}
 817
 818static int packed_transaction_prepare(struct ref_store *ref_store,
 819                                      struct ref_transaction *transaction,
 820                                      struct strbuf *err)
 821{
 822        struct packed_ref_store *refs = packed_downcast(
 823                        ref_store,
 824                        REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
 825                        "ref_transaction_prepare");
 826        struct packed_transaction_backend_data *data;
 827        size_t i;
 828        int ret = TRANSACTION_GENERIC_ERROR;
 829
 830        /*
 831         * Note that we *don't* skip transactions with zero updates,
 832         * because such a transaction might be executed for the side
 833         * effect of ensuring that all of the references are peeled.
 834         * If the caller wants to optimize away empty transactions, it
 835         * should do so itself.
 836         */
 837
 838        data = xcalloc(1, sizeof(*data));
 839        string_list_init(&data->updates, 0);
 840
 841        transaction->backend_data = data;
 842
 843        /*
 844         * Stick the updates in a string list by refname so that we
 845         * can sort them:
 846         */
 847        for (i = 0; i < transaction->nr; i++) {
 848                struct ref_update *update = transaction->updates[i];
 849                struct string_list_item *item =
 850                        string_list_append(&data->updates, update->refname);
 851
 852                /* Store a pointer to update in item->util: */
 853                item->util = update;
 854        }
 855        string_list_sort(&data->updates);
 856
 857        if (ref_update_reject_duplicates(&data->updates, err))
 858                goto failure;
 859
 860        if (!is_lock_file_locked(&refs->lock)) {
 861                if (packed_refs_lock(ref_store, 0, err))
 862                        goto failure;
 863                data->own_lock = 1;
 864        }
 865
 866        if (write_with_updates(refs, &data->updates, err))
 867                goto failure;
 868
 869        transaction->state = REF_TRANSACTION_PREPARED;
 870        return 0;
 871
 872failure:
 873        packed_transaction_cleanup(refs, transaction);
 874        return ret;
 875}
 876
 877static int packed_transaction_abort(struct ref_store *ref_store,
 878                                    struct ref_transaction *transaction,
 879                                    struct strbuf *err)
 880{
 881        struct packed_ref_store *refs = packed_downcast(
 882                        ref_store,
 883                        REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
 884                        "ref_transaction_abort");
 885
 886        packed_transaction_cleanup(refs, transaction);
 887        return 0;
 888}
 889
 890static int packed_transaction_finish(struct ref_store *ref_store,
 891                                     struct ref_transaction *transaction,
 892                                     struct strbuf *err)
 893{
 894        struct packed_ref_store *refs = packed_downcast(
 895                        ref_store,
 896                        REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
 897                        "ref_transaction_finish");
 898        int ret = TRANSACTION_GENERIC_ERROR;
 899        char *packed_refs_path;
 900
 901        packed_refs_path = get_locked_file_path(&refs->lock);
 902        if (rename_tempfile(&refs->tempfile, packed_refs_path)) {
 903                strbuf_addf(err, "error replacing %s: %s",
 904                            refs->path, strerror(errno));
 905                goto cleanup;
 906        }
 907
 908        clear_packed_ref_cache(refs);
 909        ret = 0;
 910
 911cleanup:
 912        free(packed_refs_path);
 913        packed_transaction_cleanup(refs, transaction);
 914        return ret;
 915}
 916
 917static int packed_initial_transaction_commit(struct ref_store *ref_store,
 918                                            struct ref_transaction *transaction,
 919                                            struct strbuf *err)
 920{
 921        return ref_transaction_commit(transaction, err);
 922}
 923
 924static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
 925                             struct string_list *refnames, unsigned int flags)
 926{
 927        struct packed_ref_store *refs =
 928                packed_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
 929        struct strbuf err = STRBUF_INIT;
 930        struct ref_transaction *transaction;
 931        struct string_list_item *item;
 932        int ret;
 933
 934        (void)refs; /* We need the check above, but don't use the variable */
 935
 936        if (!refnames->nr)
 937                return 0;
 938
 939        /*
 940         * Since we don't check the references' old_oids, the
 941         * individual updates can't fail, so we can pack all of the
 942         * updates into a single transaction.
 943         */
 944
 945        transaction = ref_store_transaction_begin(ref_store, &err);
 946        if (!transaction)
 947                return -1;
 948
 949        for_each_string_list_item(item, refnames) {
 950                if (ref_transaction_delete(transaction, item->string, NULL,
 951                                           flags, msg, &err)) {
 952                        warning(_("could not delete reference %s: %s"),
 953                                item->string, err.buf);
 954                        strbuf_reset(&err);
 955                }
 956        }
 957
 958        ret = ref_transaction_commit(transaction, &err);
 959
 960        if (ret) {
 961                if (refnames->nr == 1)
 962                        error(_("could not delete reference %s: %s"),
 963                              refnames->items[0].string, err.buf);
 964                else
 965                        error(_("could not delete references: %s"), err.buf);
 966        }
 967
 968        ref_transaction_free(transaction);
 969        strbuf_release(&err);
 970        return ret;
 971}
 972
 973static int packed_pack_refs(struct ref_store *ref_store, unsigned int flags)
 974{
 975        /*
 976         * Packed refs are already packed. It might be that loose refs
 977         * are packed *into* a packed refs store, but that is done by
 978         * updating the packed references via a transaction.
 979         */
 980        return 0;
 981}
 982
 983static int packed_create_symref(struct ref_store *ref_store,
 984                               const char *refname, const char *target,
 985                               const char *logmsg)
 986{
 987        die("BUG: packed reference store does not support symrefs");
 988}
 989
 990static int packed_rename_ref(struct ref_store *ref_store,
 991                            const char *oldrefname, const char *newrefname,
 992                            const char *logmsg)
 993{
 994        die("BUG: packed reference store does not support renaming references");
 995}
 996
 997static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store)
 998{
 999        return empty_ref_iterator_begin();
1000}
1001
1002static int packed_for_each_reflog_ent(struct ref_store *ref_store,
1003                                      const char *refname,
1004                                      each_reflog_ent_fn fn, void *cb_data)
1005{
1006        return 0;
1007}
1008
1009static int packed_for_each_reflog_ent_reverse(struct ref_store *ref_store,
1010                                              const char *refname,
1011                                              each_reflog_ent_fn fn,
1012                                              void *cb_data)
1013{
1014        return 0;
1015}
1016
1017static int packed_reflog_exists(struct ref_store *ref_store,
1018                               const char *refname)
1019{
1020        return 0;
1021}
1022
1023static int packed_create_reflog(struct ref_store *ref_store,
1024                               const char *refname, int force_create,
1025                               struct strbuf *err)
1026{
1027        die("BUG: packed reference store does not support reflogs");
1028}
1029
1030static int packed_delete_reflog(struct ref_store *ref_store,
1031                               const char *refname)
1032{
1033        return 0;
1034}
1035
1036static int packed_reflog_expire(struct ref_store *ref_store,
1037                                const char *refname, const unsigned char *sha1,
1038                                unsigned int flags,
1039                                reflog_expiry_prepare_fn prepare_fn,
1040                                reflog_expiry_should_prune_fn should_prune_fn,
1041                                reflog_expiry_cleanup_fn cleanup_fn,
1042                                void *policy_cb_data)
1043{
1044        return 0;
1045}
1046
1047struct ref_storage_be refs_be_packed = {
1048        NULL,
1049        "packed",
1050        packed_ref_store_create,
1051        packed_init_db,
1052        packed_transaction_prepare,
1053        packed_transaction_finish,
1054        packed_transaction_abort,
1055        packed_initial_transaction_commit,
1056
1057        packed_pack_refs,
1058        packed_peel_ref,
1059        packed_create_symref,
1060        packed_delete_refs,
1061        packed_rename_ref,
1062
1063        packed_ref_iterator_begin,
1064        packed_read_raw_ref,
1065
1066        packed_reflog_iterator_begin,
1067        packed_for_each_reflog_ent,
1068        packed_for_each_reflog_ent_reverse,
1069        packed_reflog_exists,
1070        packed_create_reflog,
1071        packed_delete_reflog,
1072        packed_reflog_expire
1073};