refs / files-backend.con commit refs_ref_iterator_begin(): handle `GIT_REF_PARANOIA` (0a0865b)
   1#include "../cache.h"
   2#include "../refs.h"
   3#include "refs-internal.h"
   4#include "ref-cache.h"
   5#include "../iterator.h"
   6#include "../dir-iterator.h"
   7#include "../lockfile.h"
   8#include "../object.h"
   9#include "../dir.h"
  10
  11struct ref_lock {
  12        char *ref_name;
  13        struct lock_file *lk;
  14        struct object_id old_oid;
  15};
  16
  17/*
  18 * Return true if refname, which has the specified oid and flags, can
  19 * be resolved to an object in the database. If the referred-to object
  20 * does not exist, emit a warning and return false.
  21 */
  22static int ref_resolves_to_object(const char *refname,
  23                                  const struct object_id *oid,
  24                                  unsigned int flags)
  25{
  26        if (flags & REF_ISBROKEN)
  27                return 0;
  28        if (!has_sha1_file(oid->hash)) {
  29                error("%s does not point to a valid object!", refname);
  30                return 0;
  31        }
  32        return 1;
  33}
  34
  35struct packed_ref_cache {
  36        struct ref_cache *cache;
  37
  38        /*
  39         * Count of references to the data structure in this instance,
  40         * including the pointer from files_ref_store::packed if any.
  41         * The data will not be freed as long as the reference count
  42         * is nonzero.
  43         */
  44        unsigned int referrers;
  45
  46        /* The metadata from when this packed-refs cache was read */
  47        struct stat_validity validity;
  48};
  49
  50/*
  51 * Future: need to be in "struct repository"
  52 * when doing a full libification.
  53 */
  54struct files_ref_store {
  55        struct ref_store base;
  56        unsigned int store_flags;
  57
  58        char *gitdir;
  59        char *gitcommondir;
  60        char *packed_refs_path;
  61
  62        struct ref_cache *loose;
  63        struct packed_ref_cache *packed;
  64
  65        /*
  66         * Lock used for the "packed-refs" file. Note that this (and
  67         * thus the enclosing `files_ref_store`) must not be freed.
  68         */
  69        struct lock_file packed_refs_lock;
  70};
  71
  72/*
  73 * Increment the reference count of *packed_refs.
  74 */
  75static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
  76{
  77        packed_refs->referrers++;
  78}
  79
  80/*
  81 * Decrease the reference count of *packed_refs.  If it goes to zero,
  82 * free *packed_refs and return true; otherwise return false.
  83 */
  84static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
  85{
  86        if (!--packed_refs->referrers) {
  87                free_ref_cache(packed_refs->cache);
  88                stat_validity_clear(&packed_refs->validity);
  89                free(packed_refs);
  90                return 1;
  91        } else {
  92                return 0;
  93        }
  94}
  95
  96static void clear_packed_ref_cache(struct files_ref_store *refs)
  97{
  98        if (refs->packed) {
  99                struct packed_ref_cache *packed_refs = refs->packed;
 100
 101                if (is_lock_file_locked(&refs->packed_refs_lock))
 102                        die("BUG: packed-ref cache cleared while locked");
 103                refs->packed = NULL;
 104                release_packed_ref_cache(packed_refs);
 105        }
 106}
 107
 108static void clear_loose_ref_cache(struct files_ref_store *refs)
 109{
 110        if (refs->loose) {
 111                free_ref_cache(refs->loose);
 112                refs->loose = NULL;
 113        }
 114}
 115
 116/*
 117 * Create a new submodule ref cache and add it to the internal
 118 * set of caches.
 119 */
 120static struct ref_store *files_ref_store_create(const char *gitdir,
 121                                                unsigned int flags)
 122{
 123        struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
 124        struct ref_store *ref_store = (struct ref_store *)refs;
 125        struct strbuf sb = STRBUF_INIT;
 126
 127        base_ref_store_init(ref_store, &refs_be_files);
 128        refs->store_flags = flags;
 129
 130        refs->gitdir = xstrdup(gitdir);
 131        get_common_dir_noenv(&sb, gitdir);
 132        refs->gitcommondir = strbuf_detach(&sb, NULL);
 133        strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir);
 134        refs->packed_refs_path = strbuf_detach(&sb, NULL);
 135
 136        return ref_store;
 137}
 138
 139/*
 140 * Die if refs is not the main ref store. caller is used in any
 141 * necessary error messages.
 142 */
 143static void files_assert_main_repository(struct files_ref_store *refs,
 144                                         const char *caller)
 145{
 146        if (refs->store_flags & REF_STORE_MAIN)
 147                return;
 148
 149        die("BUG: operation %s only allowed for main ref store", caller);
 150}
 151
 152/*
 153 * Downcast ref_store to files_ref_store. Die if ref_store is not a
 154 * files_ref_store. required_flags is compared with ref_store's
 155 * store_flags to ensure the ref_store has all required capabilities.
 156 * "caller" is used in any necessary error messages.
 157 */
 158static struct files_ref_store *files_downcast(struct ref_store *ref_store,
 159                                              unsigned int required_flags,
 160                                              const char *caller)
 161{
 162        struct files_ref_store *refs;
 163
 164        if (ref_store->be != &refs_be_files)
 165                die("BUG: ref_store is type \"%s\" not \"files\" in %s",
 166                    ref_store->be->name, caller);
 167
 168        refs = (struct files_ref_store *)ref_store;
 169
 170        if ((refs->store_flags & required_flags) != required_flags)
 171                die("BUG: operation %s requires abilities 0x%x, but only have 0x%x",
 172                    caller, required_flags, refs->store_flags);
 173
 174        return refs;
 175}
 176
 177/* The length of a peeled reference line in packed-refs, including EOL: */
 178#define PEELED_LINE_LENGTH 42
 179
 180/*
 181 * The packed-refs header line that we write out.  Perhaps other
 182 * traits will be added later.  The trailing space is required.
 183 */
 184static const char PACKED_REFS_HEADER[] =
 185        "# pack-refs with: peeled fully-peeled \n";
 186
 187/*
 188 * Parse one line from a packed-refs file.  Write the SHA1 to sha1.
 189 * Return a pointer to the refname within the line (null-terminated),
 190 * or NULL if there was a problem.
 191 */
 192static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
 193{
 194        const char *ref;
 195
 196        if (parse_oid_hex(line->buf, oid, &ref) < 0)
 197                return NULL;
 198        if (!isspace(*ref++))
 199                return NULL;
 200
 201        if (isspace(*ref))
 202                return NULL;
 203
 204        if (line->buf[line->len - 1] != '\n')
 205                return NULL;
 206        line->buf[--line->len] = 0;
 207
 208        return ref;
 209}
 210
 211/*
 212 * Read from `packed_refs_file` into a newly-allocated
 213 * `packed_ref_cache` and return it. The return value will already
 214 * have its reference count incremented.
 215 *
 216 * A comment line of the form "# pack-refs with: " may contain zero or
 217 * more traits. We interpret the traits as follows:
 218 *
 219 *   No traits:
 220 *
 221 *      Probably no references are peeled. But if the file contains a
 222 *      peeled value for a reference, we will use it.
 223 *
 224 *   peeled:
 225 *
 226 *      References under "refs/tags/", if they *can* be peeled, *are*
 227 *      peeled in this file. References outside of "refs/tags/" are
 228 *      probably not peeled even if they could have been, but if we find
 229 *      a peeled value for such a reference we will use it.
 230 *
 231 *   fully-peeled:
 232 *
 233 *      All references in the file that can be peeled are peeled.
 234 *      Inversely (and this is more important), any references in the
 235 *      file for which no peeled value is recorded is not peelable. This
 236 *      trait should typically be written alongside "peeled" for
 237 *      compatibility with older clients, but we do not require it
 238 *      (i.e., "peeled" is a no-op if "fully-peeled" is set).
 239 */
 240static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file)
 241{
 242        FILE *f;
 243        struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
 244        struct ref_entry *last = NULL;
 245        struct strbuf line = STRBUF_INIT;
 246        enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
 247        struct ref_dir *dir;
 248
 249        acquire_packed_ref_cache(packed_refs);
 250        packed_refs->cache = create_ref_cache(NULL, NULL);
 251        packed_refs->cache->root->flag &= ~REF_INCOMPLETE;
 252
 253        f = fopen(packed_refs_file, "r");
 254        if (!f) {
 255                if (errno == ENOENT) {
 256                        /*
 257                         * This is OK; it just means that no
 258                         * "packed-refs" file has been written yet,
 259                         * which is equivalent to it being empty.
 260                         */
 261                        return packed_refs;
 262                } else {
 263                        die_errno("couldn't read %s", packed_refs_file);
 264                }
 265        }
 266
 267        stat_validity_update(&packed_refs->validity, fileno(f));
 268
 269        dir = get_ref_dir(packed_refs->cache->root);
 270        while (strbuf_getwholeline(&line, f, '\n') != EOF) {
 271                struct object_id oid;
 272                const char *refname;
 273                const char *traits;
 274
 275                if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
 276                        if (strstr(traits, " fully-peeled "))
 277                                peeled = PEELED_FULLY;
 278                        else if (strstr(traits, " peeled "))
 279                                peeled = PEELED_TAGS;
 280                        /* perhaps other traits later as well */
 281                        continue;
 282                }
 283
 284                refname = parse_ref_line(&line, &oid);
 285                if (refname) {
 286                        int flag = REF_ISPACKED;
 287
 288                        if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
 289                                if (!refname_is_safe(refname))
 290                                        die("packed refname is dangerous: %s", refname);
 291                                oidclr(&oid);
 292                                flag |= REF_BAD_NAME | REF_ISBROKEN;
 293                        }
 294                        last = create_ref_entry(refname, &oid, flag, 0);
 295                        if (peeled == PEELED_FULLY ||
 296                            (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
 297                                last->flag |= REF_KNOWS_PEELED;
 298                        add_ref_entry(dir, last);
 299                        continue;
 300                }
 301                if (last &&
 302                    line.buf[0] == '^' &&
 303                    line.len == PEELED_LINE_LENGTH &&
 304                    line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
 305                    !get_oid_hex(line.buf + 1, &oid)) {
 306                        oidcpy(&last->u.value.peeled, &oid);
 307                        /*
 308                         * Regardless of what the file header said,
 309                         * we definitely know the value of *this*
 310                         * reference:
 311                         */
 312                        last->flag |= REF_KNOWS_PEELED;
 313                }
 314        }
 315
 316        fclose(f);
 317        strbuf_release(&line);
 318
 319        return packed_refs;
 320}
 321
 322static const char *files_packed_refs_path(struct files_ref_store *refs)
 323{
 324        return refs->packed_refs_path;
 325}
 326
 327static void files_reflog_path(struct files_ref_store *refs,
 328                              struct strbuf *sb,
 329                              const char *refname)
 330{
 331        if (!refname) {
 332                /*
 333                 * FIXME: of course this is wrong in multi worktree
 334                 * setting. To be fixed real soon.
 335                 */
 336                strbuf_addf(sb, "%s/logs", refs->gitcommondir);
 337                return;
 338        }
 339
 340        switch (ref_type(refname)) {
 341        case REF_TYPE_PER_WORKTREE:
 342        case REF_TYPE_PSEUDOREF:
 343                strbuf_addf(sb, "%s/logs/%s", refs->gitdir, refname);
 344                break;
 345        case REF_TYPE_NORMAL:
 346                strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, refname);
 347                break;
 348        default:
 349                die("BUG: unknown ref type %d of ref %s",
 350                    ref_type(refname), refname);
 351        }
 352}
 353
 354static void files_ref_path(struct files_ref_store *refs,
 355                           struct strbuf *sb,
 356                           const char *refname)
 357{
 358        switch (ref_type(refname)) {
 359        case REF_TYPE_PER_WORKTREE:
 360        case REF_TYPE_PSEUDOREF:
 361                strbuf_addf(sb, "%s/%s", refs->gitdir, refname);
 362                break;
 363        case REF_TYPE_NORMAL:
 364                strbuf_addf(sb, "%s/%s", refs->gitcommondir, refname);
 365                break;
 366        default:
 367                die("BUG: unknown ref type %d of ref %s",
 368                    ref_type(refname), refname);
 369        }
 370}
 371
 372/*
 373 * Get the packed_ref_cache for the specified files_ref_store,
 374 * creating and populating it if it hasn't been read before or if the
 375 * file has been changed (according to its `validity` field) since it
 376 * was last read. On the other hand, if we hold the lock, then assume
 377 * that the file hasn't been changed out from under us, so skip the
 378 * extra `stat()` call in `stat_validity_check()`.
 379 */
 380static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *refs)
 381{
 382        const char *packed_refs_file = files_packed_refs_path(refs);
 383
 384        if (refs->packed &&
 385            !is_lock_file_locked(&refs->packed_refs_lock) &&
 386            !stat_validity_check(&refs->packed->validity, packed_refs_file))
 387                clear_packed_ref_cache(refs);
 388
 389        if (!refs->packed)
 390                refs->packed = read_packed_refs(packed_refs_file);
 391
 392        return refs->packed;
 393}
 394
 395static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
 396{
 397        return get_ref_dir(packed_ref_cache->cache->root);
 398}
 399
 400static struct ref_dir *get_packed_refs(struct files_ref_store *refs)
 401{
 402        return get_packed_ref_dir(get_packed_ref_cache(refs));
 403}
 404
 405/*
 406 * Add a reference to the in-memory packed reference cache.  This may
 407 * only be called while the packed-refs file is locked (see
 408 * lock_packed_refs()).  To actually write the packed-refs file, call
 409 * commit_packed_refs().
 410 */
 411static void add_packed_ref(struct files_ref_store *refs,
 412                           const char *refname, const struct object_id *oid)
 413{
 414        struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);
 415
 416        if (!is_lock_file_locked(&refs->packed_refs_lock))
 417                die("BUG: packed refs not locked");
 418        add_ref_entry(get_packed_ref_dir(packed_ref_cache),
 419                      create_ref_entry(refname, oid, REF_ISPACKED, 1));
 420}
 421
 422/*
 423 * Read the loose references from the namespace dirname into dir
 424 * (without recursing).  dirname must end with '/'.  dir must be the
 425 * directory entry corresponding to dirname.
 426 */
 427static void loose_fill_ref_dir(struct ref_store *ref_store,
 428                               struct ref_dir *dir, const char *dirname)
 429{
 430        struct files_ref_store *refs =
 431                files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir");
 432        DIR *d;
 433        struct dirent *de;
 434        int dirnamelen = strlen(dirname);
 435        struct strbuf refname;
 436        struct strbuf path = STRBUF_INIT;
 437        size_t path_baselen;
 438
 439        files_ref_path(refs, &path, dirname);
 440        path_baselen = path.len;
 441
 442        d = opendir(path.buf);
 443        if (!d) {
 444                strbuf_release(&path);
 445                return;
 446        }
 447
 448        strbuf_init(&refname, dirnamelen + 257);
 449        strbuf_add(&refname, dirname, dirnamelen);
 450
 451        while ((de = readdir(d)) != NULL) {
 452                struct object_id oid;
 453                struct stat st;
 454                int flag;
 455
 456                if (de->d_name[0] == '.')
 457                        continue;
 458                if (ends_with(de->d_name, ".lock"))
 459                        continue;
 460                strbuf_addstr(&refname, de->d_name);
 461                strbuf_addstr(&path, de->d_name);
 462                if (stat(path.buf, &st) < 0) {
 463                        ; /* silently ignore */
 464                } else if (S_ISDIR(st.st_mode)) {
 465                        strbuf_addch(&refname, '/');
 466                        add_entry_to_dir(dir,
 467                                         create_dir_entry(dir->cache, refname.buf,
 468                                                          refname.len, 1));
 469                } else {
 470                        if (!refs_resolve_ref_unsafe(&refs->base,
 471                                                     refname.buf,
 472                                                     RESOLVE_REF_READING,
 473                                                     oid.hash, &flag)) {
 474                                oidclr(&oid);
 475                                flag |= REF_ISBROKEN;
 476                        } else if (is_null_oid(&oid)) {
 477                                /*
 478                                 * It is so astronomically unlikely
 479                                 * that NULL_SHA1 is the SHA-1 of an
 480                                 * actual object that we consider its
 481                                 * appearance in a loose reference
 482                                 * file to be repo corruption
 483                                 * (probably due to a software bug).
 484                                 */
 485                                flag |= REF_ISBROKEN;
 486                        }
 487
 488                        if (check_refname_format(refname.buf,
 489                                                 REFNAME_ALLOW_ONELEVEL)) {
 490                                if (!refname_is_safe(refname.buf))
 491                                        die("loose refname is dangerous: %s", refname.buf);
 492                                oidclr(&oid);
 493                                flag |= REF_BAD_NAME | REF_ISBROKEN;
 494                        }
 495                        add_entry_to_dir(dir,
 496                                         create_ref_entry(refname.buf, &oid, flag, 0));
 497                }
 498                strbuf_setlen(&refname, dirnamelen);
 499                strbuf_setlen(&path, path_baselen);
 500        }
 501        strbuf_release(&refname);
 502        strbuf_release(&path);
 503        closedir(d);
 504
 505        /*
 506         * Manually add refs/bisect, which, being per-worktree, might
 507         * not appear in the directory listing for refs/ in the main
 508         * repo.
 509         */
 510        if (!strcmp(dirname, "refs/")) {
 511                int pos = search_ref_dir(dir, "refs/bisect/", 12);
 512
 513                if (pos < 0) {
 514                        struct ref_entry *child_entry = create_dir_entry(
 515                                        dir->cache, "refs/bisect/", 12, 1);
 516                        add_entry_to_dir(dir, child_entry);
 517                }
 518        }
 519}
 520
 521static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
 522{
 523        if (!refs->loose) {
 524                /*
 525                 * Mark the top-level directory complete because we
 526                 * are about to read the only subdirectory that can
 527                 * hold references:
 528                 */
 529                refs->loose = create_ref_cache(&refs->base, loose_fill_ref_dir);
 530
 531                /* We're going to fill the top level ourselves: */
 532                refs->loose->root->flag &= ~REF_INCOMPLETE;
 533
 534                /*
 535                 * Add an incomplete entry for "refs/" (to be filled
 536                 * lazily):
 537                 */
 538                add_entry_to_dir(get_ref_dir(refs->loose->root),
 539                                 create_dir_entry(refs->loose, "refs/", 5, 1));
 540        }
 541        return refs->loose;
 542}
 543
 544/*
 545 * Return the ref_entry for the given refname from the packed
 546 * references.  If it does not exist, return NULL.
 547 */
 548static struct ref_entry *get_packed_ref(struct files_ref_store *refs,
 549                                        const char *refname)
 550{
 551        return find_ref_entry(get_packed_refs(refs), refname);
 552}
 553
 554/*
 555 * A loose ref file doesn't exist; check for a packed ref.
 556 */
 557static int resolve_packed_ref(struct files_ref_store *refs,
 558                              const char *refname,
 559                              unsigned char *sha1, unsigned int *flags)
 560{
 561        struct ref_entry *entry;
 562
 563        /*
 564         * The loose reference file does not exist; check for a packed
 565         * reference.
 566         */
 567        entry = get_packed_ref(refs, refname);
 568        if (entry) {
 569                hashcpy(sha1, entry->u.value.oid.hash);
 570                *flags |= REF_ISPACKED;
 571                return 0;
 572        }
 573        /* refname is not a packed reference. */
 574        return -1;
 575}
 576
 577static int files_read_raw_ref(struct ref_store *ref_store,
 578                              const char *refname, unsigned char *sha1,
 579                              struct strbuf *referent, unsigned int *type)
 580{
 581        struct files_ref_store *refs =
 582                files_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
 583        struct strbuf sb_contents = STRBUF_INIT;
 584        struct strbuf sb_path = STRBUF_INIT;
 585        const char *path;
 586        const char *buf;
 587        struct stat st;
 588        int fd;
 589        int ret = -1;
 590        int save_errno;
 591        int remaining_retries = 3;
 592
 593        *type = 0;
 594        strbuf_reset(&sb_path);
 595
 596        files_ref_path(refs, &sb_path, refname);
 597
 598        path = sb_path.buf;
 599
 600stat_ref:
 601        /*
 602         * We might have to loop back here to avoid a race
 603         * condition: first we lstat() the file, then we try
 604         * to read it as a link or as a file.  But if somebody
 605         * changes the type of the file (file <-> directory
 606         * <-> symlink) between the lstat() and reading, then
 607         * we don't want to report that as an error but rather
 608         * try again starting with the lstat().
 609         *
 610         * We'll keep a count of the retries, though, just to avoid
 611         * any confusing situation sending us into an infinite loop.
 612         */
 613
 614        if (remaining_retries-- <= 0)
 615                goto out;
 616
 617        if (lstat(path, &st) < 0) {
 618                if (errno != ENOENT)
 619                        goto out;
 620                if (resolve_packed_ref(refs, refname, sha1, type)) {
 621                        errno = ENOENT;
 622                        goto out;
 623                }
 624                ret = 0;
 625                goto out;
 626        }
 627
 628        /* Follow "normalized" - ie "refs/.." symlinks by hand */
 629        if (S_ISLNK(st.st_mode)) {
 630                strbuf_reset(&sb_contents);
 631                if (strbuf_readlink(&sb_contents, path, 0) < 0) {
 632                        if (errno == ENOENT || errno == EINVAL)
 633                                /* inconsistent with lstat; retry */
 634                                goto stat_ref;
 635                        else
 636                                goto out;
 637                }
 638                if (starts_with(sb_contents.buf, "refs/") &&
 639                    !check_refname_format(sb_contents.buf, 0)) {
 640                        strbuf_swap(&sb_contents, referent);
 641                        *type |= REF_ISSYMREF;
 642                        ret = 0;
 643                        goto out;
 644                }
 645                /*
 646                 * It doesn't look like a refname; fall through to just
 647                 * treating it like a non-symlink, and reading whatever it
 648                 * points to.
 649                 */
 650        }
 651
 652        /* Is it a directory? */
 653        if (S_ISDIR(st.st_mode)) {
 654                /*
 655                 * Even though there is a directory where the loose
 656                 * ref is supposed to be, there could still be a
 657                 * packed ref:
 658                 */
 659                if (resolve_packed_ref(refs, refname, sha1, type)) {
 660                        errno = EISDIR;
 661                        goto out;
 662                }
 663                ret = 0;
 664                goto out;
 665        }
 666
 667        /*
 668         * Anything else, just open it and try to use it as
 669         * a ref
 670         */
 671        fd = open(path, O_RDONLY);
 672        if (fd < 0) {
 673                if (errno == ENOENT && !S_ISLNK(st.st_mode))
 674                        /* inconsistent with lstat; retry */
 675                        goto stat_ref;
 676                else
 677                        goto out;
 678        }
 679        strbuf_reset(&sb_contents);
 680        if (strbuf_read(&sb_contents, fd, 256) < 0) {
 681                int save_errno = errno;
 682                close(fd);
 683                errno = save_errno;
 684                goto out;
 685        }
 686        close(fd);
 687        strbuf_rtrim(&sb_contents);
 688        buf = sb_contents.buf;
 689        if (starts_with(buf, "ref:")) {
 690                buf += 4;
 691                while (isspace(*buf))
 692                        buf++;
 693
 694                strbuf_reset(referent);
 695                strbuf_addstr(referent, buf);
 696                *type |= REF_ISSYMREF;
 697                ret = 0;
 698                goto out;
 699        }
 700
 701        /*
 702         * Please note that FETCH_HEAD has additional
 703         * data after the sha.
 704         */
 705        if (get_sha1_hex(buf, sha1) ||
 706            (buf[40] != '\0' && !isspace(buf[40]))) {
 707                *type |= REF_ISBROKEN;
 708                errno = EINVAL;
 709                goto out;
 710        }
 711
 712        ret = 0;
 713
 714out:
 715        save_errno = errno;
 716        strbuf_release(&sb_path);
 717        strbuf_release(&sb_contents);
 718        errno = save_errno;
 719        return ret;
 720}
 721
 722static void unlock_ref(struct ref_lock *lock)
 723{
 724        /* Do not free lock->lk -- atexit() still looks at them */
 725        if (lock->lk)
 726                rollback_lock_file(lock->lk);
 727        free(lock->ref_name);
 728        free(lock);
 729}
 730
 731/*
 732 * Lock refname, without following symrefs, and set *lock_p to point
 733 * at a newly-allocated lock object. Fill in lock->old_oid, referent,
 734 * and type similarly to read_raw_ref().
 735 *
 736 * The caller must verify that refname is a "safe" reference name (in
 737 * the sense of refname_is_safe()) before calling this function.
 738 *
 739 * If the reference doesn't already exist, verify that refname doesn't
 740 * have a D/F conflict with any existing references. extras and skip
 741 * are passed to refs_verify_refname_available() for this check.
 742 *
 743 * If mustexist is not set and the reference is not found or is
 744 * broken, lock the reference anyway but clear sha1.
 745 *
 746 * Return 0 on success. On failure, write an error message to err and
 747 * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR.
 748 *
 749 * Implementation note: This function is basically
 750 *
 751 *     lock reference
 752 *     read_raw_ref()
 753 *
 754 * but it includes a lot more code to
 755 * - Deal with possible races with other processes
 756 * - Avoid calling refs_verify_refname_available() when it can be
 757 *   avoided, namely if we were successfully able to read the ref
 758 * - Generate informative error messages in the case of failure
 759 */
 760static int lock_raw_ref(struct files_ref_store *refs,
 761                        const char *refname, int mustexist,
 762                        const struct string_list *extras,
 763                        const struct string_list *skip,
 764                        struct ref_lock **lock_p,
 765                        struct strbuf *referent,
 766                        unsigned int *type,
 767                        struct strbuf *err)
 768{
 769        struct ref_lock *lock;
 770        struct strbuf ref_file = STRBUF_INIT;
 771        int attempts_remaining = 3;
 772        int ret = TRANSACTION_GENERIC_ERROR;
 773
 774        assert(err);
 775        files_assert_main_repository(refs, "lock_raw_ref");
 776
 777        *type = 0;
 778
 779        /* First lock the file so it can't change out from under us. */
 780
 781        *lock_p = lock = xcalloc(1, sizeof(*lock));
 782
 783        lock->ref_name = xstrdup(refname);
 784        files_ref_path(refs, &ref_file, refname);
 785
 786retry:
 787        switch (safe_create_leading_directories(ref_file.buf)) {
 788        case SCLD_OK:
 789                break; /* success */
 790        case SCLD_EXISTS:
 791                /*
 792                 * Suppose refname is "refs/foo/bar". We just failed
 793                 * to create the containing directory, "refs/foo",
 794                 * because there was a non-directory in the way. This
 795                 * indicates a D/F conflict, probably because of
 796                 * another reference such as "refs/foo". There is no
 797                 * reason to expect this error to be transitory.
 798                 */
 799                if (refs_verify_refname_available(&refs->base, refname,
 800                                                  extras, skip, err)) {
 801                        if (mustexist) {
 802                                /*
 803                                 * To the user the relevant error is
 804                                 * that the "mustexist" reference is
 805                                 * missing:
 806                                 */
 807                                strbuf_reset(err);
 808                                strbuf_addf(err, "unable to resolve reference '%s'",
 809                                            refname);
 810                        } else {
 811                                /*
 812                                 * The error message set by
 813                                 * refs_verify_refname_available() is
 814                                 * OK.
 815                                 */
 816                                ret = TRANSACTION_NAME_CONFLICT;
 817                        }
 818                } else {
 819                        /*
 820                         * The file that is in the way isn't a loose
 821                         * reference. Report it as a low-level
 822                         * failure.
 823                         */
 824                        strbuf_addf(err, "unable to create lock file %s.lock; "
 825                                    "non-directory in the way",
 826                                    ref_file.buf);
 827                }
 828                goto error_return;
 829        case SCLD_VANISHED:
 830                /* Maybe another process was tidying up. Try again. */
 831                if (--attempts_remaining > 0)
 832                        goto retry;
 833                /* fall through */
 834        default:
 835                strbuf_addf(err, "unable to create directory for %s",
 836                            ref_file.buf);
 837                goto error_return;
 838        }
 839
 840        if (!lock->lk)
 841                lock->lk = xcalloc(1, sizeof(struct lock_file));
 842
 843        if (hold_lock_file_for_update(lock->lk, ref_file.buf, LOCK_NO_DEREF) < 0) {
 844                if (errno == ENOENT && --attempts_remaining > 0) {
 845                        /*
 846                         * Maybe somebody just deleted one of the
 847                         * directories leading to ref_file.  Try
 848                         * again:
 849                         */
 850                        goto retry;
 851                } else {
 852                        unable_to_lock_message(ref_file.buf, errno, err);
 853                        goto error_return;
 854                }
 855        }
 856
 857        /*
 858         * Now we hold the lock and can read the reference without
 859         * fear that its value will change.
 860         */
 861
 862        if (files_read_raw_ref(&refs->base, refname,
 863                               lock->old_oid.hash, referent, type)) {
 864                if (errno == ENOENT) {
 865                        if (mustexist) {
 866                                /* Garden variety missing reference. */
 867                                strbuf_addf(err, "unable to resolve reference '%s'",
 868                                            refname);
 869                                goto error_return;
 870                        } else {
 871                                /*
 872                                 * Reference is missing, but that's OK. We
 873                                 * know that there is not a conflict with
 874                                 * another loose reference because
 875                                 * (supposing that we are trying to lock
 876                                 * reference "refs/foo/bar"):
 877                                 *
 878                                 * - We were successfully able to create
 879                                 *   the lockfile refs/foo/bar.lock, so we
 880                                 *   know there cannot be a loose reference
 881                                 *   named "refs/foo".
 882                                 *
 883                                 * - We got ENOENT and not EISDIR, so we
 884                                 *   know that there cannot be a loose
 885                                 *   reference named "refs/foo/bar/baz".
 886                                 */
 887                        }
 888                } else if (errno == EISDIR) {
 889                        /*
 890                         * There is a directory in the way. It might have
 891                         * contained references that have been deleted. If
 892                         * we don't require that the reference already
 893                         * exists, try to remove the directory so that it
 894                         * doesn't cause trouble when we want to rename the
 895                         * lockfile into place later.
 896                         */
 897                        if (mustexist) {
 898                                /* Garden variety missing reference. */
 899                                strbuf_addf(err, "unable to resolve reference '%s'",
 900                                            refname);
 901                                goto error_return;
 902                        } else if (remove_dir_recursively(&ref_file,
 903                                                          REMOVE_DIR_EMPTY_ONLY)) {
 904                                if (refs_verify_refname_available(
 905                                                    &refs->base, refname,
 906                                                    extras, skip, err)) {
 907                                        /*
 908                                         * The error message set by
 909                                         * verify_refname_available() is OK.
 910                                         */
 911                                        ret = TRANSACTION_NAME_CONFLICT;
 912                                        goto error_return;
 913                                } else {
 914                                        /*
 915                                         * We can't delete the directory,
 916                                         * but we also don't know of any
 917                                         * references that it should
 918                                         * contain.
 919                                         */
 920                                        strbuf_addf(err, "there is a non-empty directory '%s' "
 921                                                    "blocking reference '%s'",
 922                                                    ref_file.buf, refname);
 923                                        goto error_return;
 924                                }
 925                        }
 926                } else if (errno == EINVAL && (*type & REF_ISBROKEN)) {
 927                        strbuf_addf(err, "unable to resolve reference '%s': "
 928                                    "reference broken", refname);
 929                        goto error_return;
 930                } else {
 931                        strbuf_addf(err, "unable to resolve reference '%s': %s",
 932                                    refname, strerror(errno));
 933                        goto error_return;
 934                }
 935
 936                /*
 937                 * If the ref did not exist and we are creating it,
 938                 * make sure there is no existing ref that conflicts
 939                 * with refname:
 940                 */
 941                if (refs_verify_refname_available(
 942                                    &refs->base, refname,
 943                                    extras, skip, err))
 944                        goto error_return;
 945        }
 946
 947        ret = 0;
 948        goto out;
 949
 950error_return:
 951        unlock_ref(lock);
 952        *lock_p = NULL;
 953
 954out:
 955        strbuf_release(&ref_file);
 956        return ret;
 957}
 958
 959static int files_peel_ref(struct ref_store *ref_store,
 960                          const char *refname, unsigned char *sha1)
 961{
 962        struct files_ref_store *refs =
 963                files_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
 964                               "peel_ref");
 965        int flag;
 966        unsigned char base[20];
 967
 968        if (current_ref_iter && current_ref_iter->refname == refname) {
 969                struct object_id peeled;
 970
 971                if (ref_iterator_peel(current_ref_iter, &peeled))
 972                        return -1;
 973                hashcpy(sha1, peeled.hash);
 974                return 0;
 975        }
 976
 977        if (refs_read_ref_full(ref_store, refname,
 978                               RESOLVE_REF_READING, base, &flag))
 979                return -1;
 980
 981        /*
 982         * If the reference is packed, read its ref_entry from the
 983         * cache in the hope that we already know its peeled value.
 984         * We only try this optimization on packed references because
 985         * (a) forcing the filling of the loose reference cache could
 986         * be expensive and (b) loose references anyway usually do not
 987         * have REF_KNOWS_PEELED.
 988         */
 989        if (flag & REF_ISPACKED) {
 990                struct ref_entry *r = get_packed_ref(refs, refname);
 991                if (r) {
 992                        if (peel_entry(r, 0))
 993                                return -1;
 994                        hashcpy(sha1, r->u.value.peeled.hash);
 995                        return 0;
 996                }
 997        }
 998
 999        return peel_object(base, sha1);
1000}
1001
1002struct files_ref_iterator {
1003        struct ref_iterator base;
1004
1005        struct packed_ref_cache *packed_ref_cache;
1006        struct ref_iterator *iter0;
1007        unsigned int flags;
1008};
1009
1010static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)
1011{
1012        struct files_ref_iterator *iter =
1013                (struct files_ref_iterator *)ref_iterator;
1014        int ok;
1015
1016        while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
1017                if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
1018                    ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
1019                        continue;
1020
1021                if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
1022                    !ref_resolves_to_object(iter->iter0->refname,
1023                                            iter->iter0->oid,
1024                                            iter->iter0->flags))
1025                        continue;
1026
1027                iter->base.refname = iter->iter0->refname;
1028                iter->base.oid = iter->iter0->oid;
1029                iter->base.flags = iter->iter0->flags;
1030                return ITER_OK;
1031        }
1032
1033        iter->iter0 = NULL;
1034        if (ref_iterator_abort(ref_iterator) != ITER_DONE)
1035                ok = ITER_ERROR;
1036
1037        return ok;
1038}
1039
1040static int files_ref_iterator_peel(struct ref_iterator *ref_iterator,
1041                                   struct object_id *peeled)
1042{
1043        struct files_ref_iterator *iter =
1044                (struct files_ref_iterator *)ref_iterator;
1045
1046        return ref_iterator_peel(iter->iter0, peeled);
1047}
1048
1049static int files_ref_iterator_abort(struct ref_iterator *ref_iterator)
1050{
1051        struct files_ref_iterator *iter =
1052                (struct files_ref_iterator *)ref_iterator;
1053        int ok = ITER_DONE;
1054
1055        if (iter->iter0)
1056                ok = ref_iterator_abort(iter->iter0);
1057
1058        release_packed_ref_cache(iter->packed_ref_cache);
1059        base_ref_iterator_free(ref_iterator);
1060        return ok;
1061}
1062
1063static struct ref_iterator_vtable files_ref_iterator_vtable = {
1064        files_ref_iterator_advance,
1065        files_ref_iterator_peel,
1066        files_ref_iterator_abort
1067};
1068
1069static struct ref_iterator *files_ref_iterator_begin(
1070                struct ref_store *ref_store,
1071                const char *prefix, unsigned int flags)
1072{
1073        struct files_ref_store *refs;
1074        struct ref_iterator *loose_iter, *packed_iter;
1075        struct files_ref_iterator *iter;
1076        struct ref_iterator *ref_iterator;
1077        unsigned int required_flags = REF_STORE_READ;
1078
1079        if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
1080                required_flags |= REF_STORE_ODB;
1081
1082        refs = files_downcast(ref_store, required_flags, "ref_iterator_begin");
1083
1084        iter = xcalloc(1, sizeof(*iter));
1085        ref_iterator = &iter->base;
1086        base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);
1087
1088        /*
1089         * We must make sure that all loose refs are read before
1090         * accessing the packed-refs file; this avoids a race
1091         * condition if loose refs are migrated to the packed-refs
1092         * file by a simultaneous process, but our in-memory view is
1093         * from before the migration. We ensure this as follows:
1094         * First, we call start the loose refs iteration with its
1095         * `prime_ref` argument set to true. This causes the loose
1096         * references in the subtree to be pre-read into the cache.
1097         * (If they've already been read, that's OK; we only need to
1098         * guarantee that they're read before the packed refs, not
1099         * *how much* before.) After that, we call
1100         * get_packed_ref_cache(), which internally checks whether the
1101         * packed-ref cache is up to date with what is on disk, and
1102         * re-reads it if not.
1103         */
1104
1105        loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
1106                                              prefix, 1);
1107
1108        iter->packed_ref_cache = get_packed_ref_cache(refs);
1109        acquire_packed_ref_cache(iter->packed_ref_cache);
1110        packed_iter = cache_ref_iterator_begin(iter->packed_ref_cache->cache,
1111                                               prefix, 0);
1112
1113        iter->iter0 = overlay_ref_iterator_begin(loose_iter, packed_iter);
1114        iter->flags = flags;
1115
1116        return ref_iterator;
1117}
1118
1119/*
1120 * Verify that the reference locked by lock has the value old_sha1.
1121 * Fail if the reference doesn't exist and mustexist is set. Return 0
1122 * on success. On error, write an error message to err, set errno, and
1123 * return a negative value.
1124 */
1125static int verify_lock(struct ref_store *ref_store, struct ref_lock *lock,
1126                       const unsigned char *old_sha1, int mustexist,
1127                       struct strbuf *err)
1128{
1129        assert(err);
1130
1131        if (refs_read_ref_full(ref_store, lock->ref_name,
1132                               mustexist ? RESOLVE_REF_READING : 0,
1133                               lock->old_oid.hash, NULL)) {
1134                if (old_sha1) {
1135                        int save_errno = errno;
1136                        strbuf_addf(err, "can't verify ref '%s'", lock->ref_name);
1137                        errno = save_errno;
1138                        return -1;
1139                } else {
1140                        oidclr(&lock->old_oid);
1141                        return 0;
1142                }
1143        }
1144        if (old_sha1 && hashcmp(lock->old_oid.hash, old_sha1)) {
1145                strbuf_addf(err, "ref '%s' is at %s but expected %s",
1146                            lock->ref_name,
1147                            oid_to_hex(&lock->old_oid),
1148                            sha1_to_hex(old_sha1));
1149                errno = EBUSY;
1150                return -1;
1151        }
1152        return 0;
1153}
1154
1155static int remove_empty_directories(struct strbuf *path)
1156{
1157        /*
1158         * we want to create a file but there is a directory there;
1159         * if that is an empty directory (or a directory that contains
1160         * only empty directories), remove them.
1161         */
1162        return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);
1163}
1164
1165static int create_reflock(const char *path, void *cb)
1166{
1167        struct lock_file *lk = cb;
1168
1169        return hold_lock_file_for_update(lk, path, LOCK_NO_DEREF) < 0 ? -1 : 0;
1170}
1171
1172/*
1173 * Locks a ref returning the lock on success and NULL on failure.
1174 * On failure errno is set to something meaningful.
1175 */
1176static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
1177                                            const char *refname,
1178                                            const unsigned char *old_sha1,
1179                                            const struct string_list *extras,
1180                                            const struct string_list *skip,
1181                                            unsigned int flags, int *type,
1182                                            struct strbuf *err)
1183{
1184        struct strbuf ref_file = STRBUF_INIT;
1185        struct ref_lock *lock;
1186        int last_errno = 0;
1187        int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
1188        int resolve_flags = RESOLVE_REF_NO_RECURSE;
1189        int resolved;
1190
1191        files_assert_main_repository(refs, "lock_ref_sha1_basic");
1192        assert(err);
1193
1194        lock = xcalloc(1, sizeof(struct ref_lock));
1195
1196        if (mustexist)
1197                resolve_flags |= RESOLVE_REF_READING;
1198        if (flags & REF_DELETING)
1199                resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
1200
1201        files_ref_path(refs, &ref_file, refname);
1202        resolved = !!refs_resolve_ref_unsafe(&refs->base,
1203                                             refname, resolve_flags,
1204                                             lock->old_oid.hash, type);
1205        if (!resolved && errno == EISDIR) {
1206                /*
1207                 * we are trying to lock foo but we used to
1208                 * have foo/bar which now does not exist;
1209                 * it is normal for the empty directory 'foo'
1210                 * to remain.
1211                 */
1212                if (remove_empty_directories(&ref_file)) {
1213                        last_errno = errno;
1214                        if (!refs_verify_refname_available(
1215                                            &refs->base,
1216                                            refname, extras, skip, err))
1217                                strbuf_addf(err, "there are still refs under '%s'",
1218                                            refname);
1219                        goto error_return;
1220                }
1221                resolved = !!refs_resolve_ref_unsafe(&refs->base,
1222                                                     refname, resolve_flags,
1223                                                     lock->old_oid.hash, type);
1224        }
1225        if (!resolved) {
1226                last_errno = errno;
1227                if (last_errno != ENOTDIR ||
1228                    !refs_verify_refname_available(&refs->base, refname,
1229                                                   extras, skip, err))
1230                        strbuf_addf(err, "unable to resolve reference '%s': %s",
1231                                    refname, strerror(last_errno));
1232
1233                goto error_return;
1234        }
1235
1236        /*
1237         * If the ref did not exist and we are creating it, make sure
1238         * there is no existing packed ref whose name begins with our
1239         * refname, nor a packed ref whose name is a proper prefix of
1240         * our refname.
1241         */
1242        if (is_null_oid(&lock->old_oid) &&
1243            refs_verify_refname_available(&refs->base, refname,
1244                                          extras, skip, err)) {
1245                last_errno = ENOTDIR;
1246                goto error_return;
1247        }
1248
1249        lock->lk = xcalloc(1, sizeof(struct lock_file));
1250
1251        lock->ref_name = xstrdup(refname);
1252
1253        if (raceproof_create_file(ref_file.buf, create_reflock, lock->lk)) {
1254                last_errno = errno;
1255                unable_to_lock_message(ref_file.buf, errno, err);
1256                goto error_return;
1257        }
1258
1259        if (verify_lock(&refs->base, lock, old_sha1, mustexist, err)) {
1260                last_errno = errno;
1261                goto error_return;
1262        }
1263        goto out;
1264
1265 error_return:
1266        unlock_ref(lock);
1267        lock = NULL;
1268
1269 out:
1270        strbuf_release(&ref_file);
1271        errno = last_errno;
1272        return lock;
1273}
1274
1275/*
1276 * Write an entry to the packed-refs file for the specified refname.
1277 * If peeled is non-NULL, write it as the entry's peeled value.
1278 */
1279static void write_packed_entry(FILE *fh, const char *refname,
1280                               const unsigned char *sha1,
1281                               const unsigned char *peeled)
1282{
1283        fprintf_or_die(fh, "%s %s\n", sha1_to_hex(sha1), refname);
1284        if (peeled)
1285                fprintf_or_die(fh, "^%s\n", sha1_to_hex(peeled));
1286}
1287
1288/*
1289 * Lock the packed-refs file for writing. Flags is passed to
1290 * hold_lock_file_for_update(). Return 0 on success. On errors, set
1291 * errno appropriately and return a nonzero value.
1292 */
1293static int lock_packed_refs(struct files_ref_store *refs, int flags)
1294{
1295        static int timeout_configured = 0;
1296        static int timeout_value = 1000;
1297        struct packed_ref_cache *packed_ref_cache;
1298
1299        files_assert_main_repository(refs, "lock_packed_refs");
1300
1301        if (!timeout_configured) {
1302                git_config_get_int("core.packedrefstimeout", &timeout_value);
1303                timeout_configured = 1;
1304        }
1305
1306        if (hold_lock_file_for_update_timeout(
1307                            &refs->packed_refs_lock, files_packed_refs_path(refs),
1308                            flags, timeout_value) < 0)
1309                return -1;
1310        /*
1311         * Get the current packed-refs while holding the lock. It is
1312         * important that we call `get_packed_ref_cache()` before
1313         * setting `packed_ref_cache->lock`, because otherwise the
1314         * former will see that the file is locked and assume that the
1315         * cache can't be stale.
1316         */
1317        packed_ref_cache = get_packed_ref_cache(refs);
1318        /* Increment the reference count to prevent it from being freed: */
1319        acquire_packed_ref_cache(packed_ref_cache);
1320        return 0;
1321}
1322
1323/*
1324 * Write the current version of the packed refs cache from memory to
1325 * disk. The packed-refs file must already be locked for writing (see
1326 * lock_packed_refs()). Return zero on success. On errors, set errno
1327 * and return a nonzero value
1328 */
1329static int commit_packed_refs(struct files_ref_store *refs)
1330{
1331        struct packed_ref_cache *packed_ref_cache =
1332                get_packed_ref_cache(refs);
1333        int ok, error = 0;
1334        int save_errno = 0;
1335        FILE *out;
1336        struct ref_iterator *iter;
1337
1338        files_assert_main_repository(refs, "commit_packed_refs");
1339
1340        if (!is_lock_file_locked(&refs->packed_refs_lock))
1341                die("BUG: packed-refs not locked");
1342
1343        out = fdopen_lock_file(&refs->packed_refs_lock, "w");
1344        if (!out)
1345                die_errno("unable to fdopen packed-refs descriptor");
1346
1347        fprintf_or_die(out, "%s", PACKED_REFS_HEADER);
1348
1349        iter = cache_ref_iterator_begin(packed_ref_cache->cache, NULL, 0);
1350        while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
1351                struct object_id peeled;
1352                int peel_error = ref_iterator_peel(iter, &peeled);
1353
1354                write_packed_entry(out, iter->refname, iter->oid->hash,
1355                                   peel_error ? NULL : peeled.hash);
1356        }
1357
1358        if (ok != ITER_DONE)
1359                die("error while iterating over references");
1360
1361        if (commit_lock_file(&refs->packed_refs_lock)) {
1362                save_errno = errno;
1363                error = -1;
1364        }
1365        release_packed_ref_cache(packed_ref_cache);
1366        errno = save_errno;
1367        return error;
1368}
1369
1370/*
1371 * Rollback the lockfile for the packed-refs file, and discard the
1372 * in-memory packed reference cache.  (The packed-refs file will be
1373 * read anew if it is needed again after this function is called.)
1374 */
1375static void rollback_packed_refs(struct files_ref_store *refs)
1376{
1377        struct packed_ref_cache *packed_ref_cache =
1378                get_packed_ref_cache(refs);
1379
1380        files_assert_main_repository(refs, "rollback_packed_refs");
1381
1382        if (!is_lock_file_locked(&refs->packed_refs_lock))
1383                die("BUG: packed-refs not locked");
1384        rollback_lock_file(&refs->packed_refs_lock);
1385        release_packed_ref_cache(packed_ref_cache);
1386        clear_packed_ref_cache(refs);
1387}
1388
1389struct ref_to_prune {
1390        struct ref_to_prune *next;
1391        unsigned char sha1[20];
1392        char name[FLEX_ARRAY];
1393};
1394
1395enum {
1396        REMOVE_EMPTY_PARENTS_REF = 0x01,
1397        REMOVE_EMPTY_PARENTS_REFLOG = 0x02
1398};
1399
1400/*
1401 * Remove empty parent directories associated with the specified
1402 * reference and/or its reflog, but spare [logs/]refs/ and immediate
1403 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
1404 * REMOVE_EMPTY_PARENTS_REFLOG.
1405 */
1406static void try_remove_empty_parents(struct files_ref_store *refs,
1407                                     const char *refname,
1408                                     unsigned int flags)
1409{
1410        struct strbuf buf = STRBUF_INIT;
1411        struct strbuf sb = STRBUF_INIT;
1412        char *p, *q;
1413        int i;
1414
1415        strbuf_addstr(&buf, refname);
1416        p = buf.buf;
1417        for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
1418                while (*p && *p != '/')
1419                        p++;
1420                /* tolerate duplicate slashes; see check_refname_format() */
1421                while (*p == '/')
1422                        p++;
1423        }
1424        q = buf.buf + buf.len;
1425        while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) {
1426                while (q > p && *q != '/')
1427                        q--;
1428                while (q > p && *(q-1) == '/')
1429                        q--;
1430                if (q == p)
1431                        break;
1432                strbuf_setlen(&buf, q - buf.buf);
1433
1434                strbuf_reset(&sb);
1435                files_ref_path(refs, &sb, buf.buf);
1436                if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf))
1437                        flags &= ~REMOVE_EMPTY_PARENTS_REF;
1438
1439                strbuf_reset(&sb);
1440                files_reflog_path(refs, &sb, buf.buf);
1441                if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf))
1442                        flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;
1443        }
1444        strbuf_release(&buf);
1445        strbuf_release(&sb);
1446}
1447
1448/* make sure nobody touched the ref, and unlink */
1449static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
1450{
1451        struct ref_transaction *transaction;
1452        struct strbuf err = STRBUF_INIT;
1453
1454        if (check_refname_format(r->name, 0))
1455                return;
1456
1457        transaction = ref_store_transaction_begin(&refs->base, &err);
1458        if (!transaction ||
1459            ref_transaction_delete(transaction, r->name, r->sha1,
1460                                   REF_ISPRUNING | REF_NODEREF, NULL, &err) ||
1461            ref_transaction_commit(transaction, &err)) {
1462                ref_transaction_free(transaction);
1463                error("%s", err.buf);
1464                strbuf_release(&err);
1465                return;
1466        }
1467        ref_transaction_free(transaction);
1468        strbuf_release(&err);
1469}
1470
1471static void prune_refs(struct files_ref_store *refs, struct ref_to_prune *r)
1472{
1473        while (r) {
1474                prune_ref(refs, r);
1475                r = r->next;
1476        }
1477}
1478
1479/*
1480 * Return true if the specified reference should be packed.
1481 */
1482static int should_pack_ref(const char *refname,
1483                           const struct object_id *oid, unsigned int ref_flags,
1484                           unsigned int pack_flags)
1485{
1486        /* Do not pack per-worktree refs: */
1487        if (ref_type(refname) != REF_TYPE_NORMAL)
1488                return 0;
1489
1490        /* Do not pack non-tags unless PACK_REFS_ALL is set: */
1491        if (!(pack_flags & PACK_REFS_ALL) && !starts_with(refname, "refs/tags/"))
1492                return 0;
1493
1494        /* Do not pack symbolic refs: */
1495        if (ref_flags & REF_ISSYMREF)
1496                return 0;
1497
1498        /* Do not pack broken refs: */
1499        if (!ref_resolves_to_object(refname, oid, ref_flags))
1500                return 0;
1501
1502        return 1;
1503}
1504
1505static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
1506{
1507        struct files_ref_store *refs =
1508                files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
1509                               "pack_refs");
1510        struct ref_iterator *iter;
1511        struct ref_dir *packed_refs;
1512        int ok;
1513        struct ref_to_prune *refs_to_prune = NULL;
1514
1515        lock_packed_refs(refs, LOCK_DIE_ON_ERROR);
1516        packed_refs = get_packed_refs(refs);
1517
1518        iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL, 0);
1519        while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
1520                /*
1521                 * If the loose reference can be packed, add an entry
1522                 * in the packed ref cache. If the reference should be
1523                 * pruned, also add it to refs_to_prune.
1524                 */
1525                struct ref_entry *packed_entry;
1526
1527                if (!should_pack_ref(iter->refname, iter->oid, iter->flags,
1528                                     flags))
1529                        continue;
1530
1531                /*
1532                 * Create an entry in the packed-refs cache equivalent
1533                 * to the one from the loose ref cache, except that
1534                 * we don't copy the peeled status, because we want it
1535                 * to be re-peeled.
1536                 */
1537                packed_entry = find_ref_entry(packed_refs, iter->refname);
1538                if (packed_entry) {
1539                        /* Overwrite existing packed entry with info from loose entry */
1540                        packed_entry->flag = REF_ISPACKED;
1541                        oidcpy(&packed_entry->u.value.oid, iter->oid);
1542                } else {
1543                        packed_entry = create_ref_entry(iter->refname, iter->oid,
1544                                                        REF_ISPACKED, 0);
1545                        add_ref_entry(packed_refs, packed_entry);
1546                }
1547                oidclr(&packed_entry->u.value.peeled);
1548
1549                /* Schedule the loose reference for pruning if requested. */
1550                if ((flags & PACK_REFS_PRUNE)) {
1551                        struct ref_to_prune *n;
1552                        FLEX_ALLOC_STR(n, name, iter->refname);
1553                        hashcpy(n->sha1, iter->oid->hash);
1554                        n->next = refs_to_prune;
1555                        refs_to_prune = n;
1556                }
1557        }
1558        if (ok != ITER_DONE)
1559                die("error while iterating over references");
1560
1561        if (commit_packed_refs(refs))
1562                die_errno("unable to overwrite old ref-pack file");
1563
1564        prune_refs(refs, refs_to_prune);
1565        return 0;
1566}
1567
1568/*
1569 * Rewrite the packed-refs file, omitting any refs listed in
1570 * 'refnames'. On error, leave packed-refs unchanged, write an error
1571 * message to 'err', and return a nonzero value.
1572 *
1573 * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
1574 */
1575static int repack_without_refs(struct files_ref_store *refs,
1576                               struct string_list *refnames, struct strbuf *err)
1577{
1578        struct ref_dir *packed;
1579        struct string_list_item *refname;
1580        int ret, needs_repacking = 0, removed = 0;
1581
1582        files_assert_main_repository(refs, "repack_without_refs");
1583        assert(err);
1584
1585        /* Look for a packed ref */
1586        for_each_string_list_item(refname, refnames) {
1587                if (get_packed_ref(refs, refname->string)) {
1588                        needs_repacking = 1;
1589                        break;
1590                }
1591        }
1592
1593        /* Avoid locking if we have nothing to do */
1594        if (!needs_repacking)
1595                return 0; /* no refname exists in packed refs */
1596
1597        if (lock_packed_refs(refs, 0)) {
1598                unable_to_lock_message(files_packed_refs_path(refs), errno, err);
1599                return -1;
1600        }
1601        packed = get_packed_refs(refs);
1602
1603        /* Remove refnames from the cache */
1604        for_each_string_list_item(refname, refnames)
1605                if (remove_entry_from_dir(packed, refname->string) != -1)
1606                        removed = 1;
1607        if (!removed) {
1608                /*
1609                 * All packed entries disappeared while we were
1610                 * acquiring the lock.
1611                 */
1612                rollback_packed_refs(refs);
1613                return 0;
1614        }
1615
1616        /* Write what remains */
1617        ret = commit_packed_refs(refs);
1618        if (ret)
1619                strbuf_addf(err, "unable to overwrite old ref-pack file: %s",
1620                            strerror(errno));
1621        return ret;
1622}
1623
1624static int files_delete_refs(struct ref_store *ref_store, const char *msg,
1625                             struct string_list *refnames, unsigned int flags)
1626{
1627        struct files_ref_store *refs =
1628                files_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
1629        struct strbuf err = STRBUF_INIT;
1630        int i, result = 0;
1631
1632        if (!refnames->nr)
1633                return 0;
1634
1635        result = repack_without_refs(refs, refnames, &err);
1636        if (result) {
1637                /*
1638                 * If we failed to rewrite the packed-refs file, then
1639                 * it is unsafe to try to remove loose refs, because
1640                 * doing so might expose an obsolete packed value for
1641                 * a reference that might even point at an object that
1642                 * has been garbage collected.
1643                 */
1644                if (refnames->nr == 1)
1645                        error(_("could not delete reference %s: %s"),
1646                              refnames->items[0].string, err.buf);
1647                else
1648                        error(_("could not delete references: %s"), err.buf);
1649
1650                goto out;
1651        }
1652
1653        for (i = 0; i < refnames->nr; i++) {
1654                const char *refname = refnames->items[i].string;
1655
1656                if (refs_delete_ref(&refs->base, msg, refname, NULL, flags))
1657                        result |= error(_("could not remove reference %s"), refname);
1658        }
1659
1660out:
1661        strbuf_release(&err);
1662        return result;
1663}
1664
1665/*
1666 * People using contrib's git-new-workdir have .git/logs/refs ->
1667 * /some/other/path/.git/logs/refs, and that may live on another device.
1668 *
1669 * IOW, to avoid cross device rename errors, the temporary renamed log must
1670 * live into logs/refs.
1671 */
1672#define TMP_RENAMED_LOG  "refs/.tmp-renamed-log"
1673
1674struct rename_cb {
1675        const char *tmp_renamed_log;
1676        int true_errno;
1677};
1678
1679static int rename_tmp_log_callback(const char *path, void *cb_data)
1680{
1681        struct rename_cb *cb = cb_data;
1682
1683        if (rename(cb->tmp_renamed_log, path)) {
1684                /*
1685                 * rename(a, b) when b is an existing directory ought
1686                 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
1687                 * Sheesh. Record the true errno for error reporting,
1688                 * but report EISDIR to raceproof_create_file() so
1689                 * that it knows to retry.
1690                 */
1691                cb->true_errno = errno;
1692                if (errno == ENOTDIR)
1693                        errno = EISDIR;
1694                return -1;
1695        } else {
1696                return 0;
1697        }
1698}
1699
1700static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
1701{
1702        struct strbuf path = STRBUF_INIT;
1703        struct strbuf tmp = STRBUF_INIT;
1704        struct rename_cb cb;
1705        int ret;
1706
1707        files_reflog_path(refs, &path, newrefname);
1708        files_reflog_path(refs, &tmp, TMP_RENAMED_LOG);
1709        cb.tmp_renamed_log = tmp.buf;
1710        ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb);
1711        if (ret) {
1712                if (errno == EISDIR)
1713                        error("directory not empty: %s", path.buf);
1714                else
1715                        error("unable to move logfile %s to %s: %s",
1716                              tmp.buf, path.buf,
1717                              strerror(cb.true_errno));
1718        }
1719
1720        strbuf_release(&path);
1721        strbuf_release(&tmp);
1722        return ret;
1723}
1724
1725static int write_ref_to_lockfile(struct ref_lock *lock,
1726                                 const struct object_id *oid, struct strbuf *err);
1727static int commit_ref_update(struct files_ref_store *refs,
1728                             struct ref_lock *lock,
1729                             const struct object_id *oid, const char *logmsg,
1730                             struct strbuf *err);
1731
1732static int files_rename_ref(struct ref_store *ref_store,
1733                            const char *oldrefname, const char *newrefname,
1734                            const char *logmsg)
1735{
1736        struct files_ref_store *refs =
1737                files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
1738        struct object_id oid, orig_oid;
1739        int flag = 0, logmoved = 0;
1740        struct ref_lock *lock;
1741        struct stat loginfo;
1742        struct strbuf sb_oldref = STRBUF_INIT;
1743        struct strbuf sb_newref = STRBUF_INIT;
1744        struct strbuf tmp_renamed_log = STRBUF_INIT;
1745        int log, ret;
1746        struct strbuf err = STRBUF_INIT;
1747
1748        files_reflog_path(refs, &sb_oldref, oldrefname);
1749        files_reflog_path(refs, &sb_newref, newrefname);
1750        files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);
1751
1752        log = !lstat(sb_oldref.buf, &loginfo);
1753        if (log && S_ISLNK(loginfo.st_mode)) {
1754                ret = error("reflog for %s is a symlink", oldrefname);
1755                goto out;
1756        }
1757
1758        if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,
1759                                     RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1760                                orig_oid.hash, &flag)) {
1761                ret = error("refname %s not found", oldrefname);
1762                goto out;
1763        }
1764
1765        if (flag & REF_ISSYMREF) {
1766                ret = error("refname %s is a symbolic ref, renaming it is not supported",
1767                            oldrefname);
1768                goto out;
1769        }
1770        if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {
1771                ret = 1;
1772                goto out;
1773        }
1774
1775        if (log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {
1776                ret = error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
1777                            oldrefname, strerror(errno));
1778                goto out;
1779        }
1780
1781        if (refs_delete_ref(&refs->base, logmsg, oldrefname,
1782                            orig_oid.hash, REF_NODEREF)) {
1783                error("unable to delete old %s", oldrefname);
1784                goto rollback;
1785        }
1786
1787        /*
1788         * Since we are doing a shallow lookup, oid is not the
1789         * correct value to pass to delete_ref as old_oid. But that
1790         * doesn't matter, because an old_oid check wouldn't add to
1791         * the safety anyway; we want to delete the reference whatever
1792         * its current value.
1793         */
1794        if (!refs_read_ref_full(&refs->base, newrefname,
1795                                RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1796                                oid.hash, NULL) &&
1797            refs_delete_ref(&refs->base, NULL, newrefname,
1798                            NULL, REF_NODEREF)) {
1799                if (errno == EISDIR) {
1800                        struct strbuf path = STRBUF_INIT;
1801                        int result;
1802
1803                        files_ref_path(refs, &path, newrefname);
1804                        result = remove_empty_directories(&path);
1805                        strbuf_release(&path);
1806
1807                        if (result) {
1808                                error("Directory not empty: %s", newrefname);
1809                                goto rollback;
1810                        }
1811                } else {
1812                        error("unable to delete existing %s", newrefname);
1813                        goto rollback;
1814                }
1815        }
1816
1817        if (log && rename_tmp_log(refs, newrefname))
1818                goto rollback;
1819
1820        logmoved = log;
1821
1822        lock = lock_ref_sha1_basic(refs, newrefname, NULL, NULL, NULL,
1823                                   REF_NODEREF, NULL, &err);
1824        if (!lock) {
1825                error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
1826                strbuf_release(&err);
1827                goto rollback;
1828        }
1829        oidcpy(&lock->old_oid, &orig_oid);
1830
1831        if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
1832            commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) {
1833                error("unable to write current sha1 into %s: %s", newrefname, err.buf);
1834                strbuf_release(&err);
1835                goto rollback;
1836        }
1837
1838        ret = 0;
1839        goto out;
1840
1841 rollback:
1842        lock = lock_ref_sha1_basic(refs, oldrefname, NULL, NULL, NULL,
1843                                   REF_NODEREF, NULL, &err);
1844        if (!lock) {
1845                error("unable to lock %s for rollback: %s", oldrefname, err.buf);
1846                strbuf_release(&err);
1847                goto rollbacklog;
1848        }
1849
1850        flag = log_all_ref_updates;
1851        log_all_ref_updates = LOG_REFS_NONE;
1852        if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
1853            commit_ref_update(refs, lock, &orig_oid, NULL, &err)) {
1854                error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
1855                strbuf_release(&err);
1856        }
1857        log_all_ref_updates = flag;
1858
1859 rollbacklog:
1860        if (logmoved && rename(sb_newref.buf, sb_oldref.buf))
1861                error("unable to restore logfile %s from %s: %s",
1862                        oldrefname, newrefname, strerror(errno));
1863        if (!logmoved && log &&
1864            rename(tmp_renamed_log.buf, sb_oldref.buf))
1865                error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG": %s",
1866                        oldrefname, strerror(errno));
1867        ret = 1;
1868 out:
1869        strbuf_release(&sb_newref);
1870        strbuf_release(&sb_oldref);
1871        strbuf_release(&tmp_renamed_log);
1872
1873        return ret;
1874}
1875
1876static int close_ref(struct ref_lock *lock)
1877{
1878        if (close_lock_file(lock->lk))
1879                return -1;
1880        return 0;
1881}
1882
1883static int commit_ref(struct ref_lock *lock)
1884{
1885        char *path = get_locked_file_path(lock->lk);
1886        struct stat st;
1887
1888        if (!lstat(path, &st) && S_ISDIR(st.st_mode)) {
1889                /*
1890                 * There is a directory at the path we want to rename
1891                 * the lockfile to. Hopefully it is empty; try to
1892                 * delete it.
1893                 */
1894                size_t len = strlen(path);
1895                struct strbuf sb_path = STRBUF_INIT;
1896
1897                strbuf_attach(&sb_path, path, len, len);
1898
1899                /*
1900                 * If this fails, commit_lock_file() will also fail
1901                 * and will report the problem.
1902                 */
1903                remove_empty_directories(&sb_path);
1904                strbuf_release(&sb_path);
1905        } else {
1906                free(path);
1907        }
1908
1909        if (commit_lock_file(lock->lk))
1910                return -1;
1911        return 0;
1912}
1913
1914static int open_or_create_logfile(const char *path, void *cb)
1915{
1916        int *fd = cb;
1917
1918        *fd = open(path, O_APPEND | O_WRONLY | O_CREAT, 0666);
1919        return (*fd < 0) ? -1 : 0;
1920}
1921
1922/*
1923 * Create a reflog for a ref. If force_create = 0, only create the
1924 * reflog for certain refs (those for which should_autocreate_reflog
1925 * returns non-zero). Otherwise, create it regardless of the reference
1926 * name. If the logfile already existed or was created, return 0 and
1927 * set *logfd to the file descriptor opened for appending to the file.
1928 * If no logfile exists and we decided not to create one, return 0 and
1929 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
1930 * return -1.
1931 */
1932static int log_ref_setup(struct files_ref_store *refs,
1933                         const char *refname, int force_create,
1934                         int *logfd, struct strbuf *err)
1935{
1936        struct strbuf logfile_sb = STRBUF_INIT;
1937        char *logfile;
1938
1939        files_reflog_path(refs, &logfile_sb, refname);
1940        logfile = strbuf_detach(&logfile_sb, NULL);
1941
1942        if (force_create || should_autocreate_reflog(refname)) {
1943                if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {
1944                        if (errno == ENOENT)
1945                                strbuf_addf(err, "unable to create directory for '%s': "
1946                                            "%s", logfile, strerror(errno));
1947                        else if (errno == EISDIR)
1948                                strbuf_addf(err, "there are still logs under '%s'",
1949                                            logfile);
1950                        else
1951                                strbuf_addf(err, "unable to append to '%s': %s",
1952                                            logfile, strerror(errno));
1953
1954                        goto error;
1955                }
1956        } else {
1957                *logfd = open(logfile, O_APPEND | O_WRONLY, 0666);
1958                if (*logfd < 0) {
1959                        if (errno == ENOENT || errno == EISDIR) {
1960                                /*
1961                                 * The logfile doesn't already exist,
1962                                 * but that is not an error; it only
1963                                 * means that we won't write log
1964                                 * entries to it.
1965                                 */
1966                                ;
1967                        } else {
1968                                strbuf_addf(err, "unable to append to '%s': %s",
1969                                            logfile, strerror(errno));
1970                                goto error;
1971                        }
1972                }
1973        }
1974
1975        if (*logfd >= 0)
1976                adjust_shared_perm(logfile);
1977
1978        free(logfile);
1979        return 0;
1980
1981error:
1982        free(logfile);
1983        return -1;
1984}
1985
1986static int files_create_reflog(struct ref_store *ref_store,
1987                               const char *refname, int force_create,
1988                               struct strbuf *err)
1989{
1990        struct files_ref_store *refs =
1991                files_downcast(ref_store, REF_STORE_WRITE, "create_reflog");
1992        int fd;
1993
1994        if (log_ref_setup(refs, refname, force_create, &fd, err))
1995                return -1;
1996
1997        if (fd >= 0)
1998                close(fd);
1999
2000        return 0;
2001}
2002
2003static int log_ref_write_fd(int fd, const struct object_id *old_oid,
2004                            const struct object_id *new_oid,
2005                            const char *committer, const char *msg)
2006{
2007        int msglen, written;
2008        unsigned maxlen, len;
2009        char *logrec;
2010
2011        msglen = msg ? strlen(msg) : 0;
2012        maxlen = strlen(committer) + msglen + 100;
2013        logrec = xmalloc(maxlen);
2014        len = xsnprintf(logrec, maxlen, "%s %s %s\n",
2015                        oid_to_hex(old_oid),
2016                        oid_to_hex(new_oid),
2017                        committer);
2018        if (msglen)
2019                len += copy_reflog_msg(logrec + len - 1, msg) - 1;
2020
2021        written = len <= maxlen ? write_in_full(fd, logrec, len) : -1;
2022        free(logrec);
2023        if (written != len)
2024                return -1;
2025
2026        return 0;
2027}
2028
2029static int files_log_ref_write(struct files_ref_store *refs,
2030                               const char *refname, const struct object_id *old_oid,
2031                               const struct object_id *new_oid, const char *msg,
2032                               int flags, struct strbuf *err)
2033{
2034        int logfd, result;
2035
2036        if (log_all_ref_updates == LOG_REFS_UNSET)
2037                log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
2038
2039        result = log_ref_setup(refs, refname,
2040                               flags & REF_FORCE_CREATE_REFLOG,
2041                               &logfd, err);
2042
2043        if (result)
2044                return result;
2045
2046        if (logfd < 0)
2047                return 0;
2048        result = log_ref_write_fd(logfd, old_oid, new_oid,
2049                                  git_committer_info(0), msg);
2050        if (result) {
2051                struct strbuf sb = STRBUF_INIT;
2052                int save_errno = errno;
2053
2054                files_reflog_path(refs, &sb, refname);
2055                strbuf_addf(err, "unable to append to '%s': %s",
2056                            sb.buf, strerror(save_errno));
2057                strbuf_release(&sb);
2058                close(logfd);
2059                return -1;
2060        }
2061        if (close(logfd)) {
2062                struct strbuf sb = STRBUF_INIT;
2063                int save_errno = errno;
2064
2065                files_reflog_path(refs, &sb, refname);
2066                strbuf_addf(err, "unable to append to '%s': %s",
2067                            sb.buf, strerror(save_errno));
2068                strbuf_release(&sb);
2069                return -1;
2070        }
2071        return 0;
2072}
2073
2074/*
2075 * Write sha1 into the open lockfile, then close the lockfile. On
2076 * errors, rollback the lockfile, fill in *err and
2077 * return -1.
2078 */
2079static int write_ref_to_lockfile(struct ref_lock *lock,
2080                                 const struct object_id *oid, struct strbuf *err)
2081{
2082        static char term = '\n';
2083        struct object *o;
2084        int fd;
2085
2086        o = parse_object(oid);
2087        if (!o) {
2088                strbuf_addf(err,
2089                            "trying to write ref '%s' with nonexistent object %s",
2090                            lock->ref_name, oid_to_hex(oid));
2091                unlock_ref(lock);
2092                return -1;
2093        }
2094        if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
2095                strbuf_addf(err,
2096                            "trying to write non-commit object %s to branch '%s'",
2097                            oid_to_hex(oid), lock->ref_name);
2098                unlock_ref(lock);
2099                return -1;
2100        }
2101        fd = get_lock_file_fd(lock->lk);
2102        if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||
2103            write_in_full(fd, &term, 1) != 1 ||
2104            close_ref(lock) < 0) {
2105                strbuf_addf(err,
2106                            "couldn't write '%s'", get_lock_file_path(lock->lk));
2107                unlock_ref(lock);
2108                return -1;
2109        }
2110        return 0;
2111}
2112
2113/*
2114 * Commit a change to a loose reference that has already been written
2115 * to the loose reference lockfile. Also update the reflogs if
2116 * necessary, using the specified lockmsg (which can be NULL).
2117 */
2118static int commit_ref_update(struct files_ref_store *refs,
2119                             struct ref_lock *lock,
2120                             const struct object_id *oid, const char *logmsg,
2121                             struct strbuf *err)
2122{
2123        files_assert_main_repository(refs, "commit_ref_update");
2124
2125        clear_loose_ref_cache(refs);
2126        if (files_log_ref_write(refs, lock->ref_name,
2127                                &lock->old_oid, oid,
2128                                logmsg, 0, err)) {
2129                char *old_msg = strbuf_detach(err, NULL);
2130                strbuf_addf(err, "cannot update the ref '%s': %s",
2131                            lock->ref_name, old_msg);
2132                free(old_msg);
2133                unlock_ref(lock);
2134                return -1;
2135        }
2136
2137        if (strcmp(lock->ref_name, "HEAD") != 0) {
2138                /*
2139                 * Special hack: If a branch is updated directly and HEAD
2140                 * points to it (may happen on the remote side of a push
2141                 * for example) then logically the HEAD reflog should be
2142                 * updated too.
2143                 * A generic solution implies reverse symref information,
2144                 * but finding all symrefs pointing to the given branch
2145                 * would be rather costly for this rare event (the direct
2146                 * update of a branch) to be worth it.  So let's cheat and
2147                 * check with HEAD only which should cover 99% of all usage
2148                 * scenarios (even 100% of the default ones).
2149                 */
2150                struct object_id head_oid;
2151                int head_flag;
2152                const char *head_ref;
2153
2154                head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
2155                                                   RESOLVE_REF_READING,
2156                                                   head_oid.hash, &head_flag);
2157                if (head_ref && (head_flag & REF_ISSYMREF) &&
2158                    !strcmp(head_ref, lock->ref_name)) {
2159                        struct strbuf log_err = STRBUF_INIT;
2160                        if (files_log_ref_write(refs, "HEAD",
2161                                                &lock->old_oid, oid,
2162                                                logmsg, 0, &log_err)) {
2163                                error("%s", log_err.buf);
2164                                strbuf_release(&log_err);
2165                        }
2166                }
2167        }
2168
2169        if (commit_ref(lock)) {
2170                strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
2171                unlock_ref(lock);
2172                return -1;
2173        }
2174
2175        unlock_ref(lock);
2176        return 0;
2177}
2178
2179static int create_ref_symlink(struct ref_lock *lock, const char *target)
2180{
2181        int ret = -1;
2182#ifndef NO_SYMLINK_HEAD
2183        char *ref_path = get_locked_file_path(lock->lk);
2184        unlink(ref_path);
2185        ret = symlink(target, ref_path);
2186        free(ref_path);
2187
2188        if (ret)
2189                fprintf(stderr, "no symlink - falling back to symbolic ref\n");
2190#endif
2191        return ret;
2192}
2193
2194static void update_symref_reflog(struct files_ref_store *refs,
2195                                 struct ref_lock *lock, const char *refname,
2196                                 const char *target, const char *logmsg)
2197{
2198        struct strbuf err = STRBUF_INIT;
2199        struct object_id new_oid;
2200        if (logmsg &&
2201            !refs_read_ref_full(&refs->base, target,
2202                                RESOLVE_REF_READING, new_oid.hash, NULL) &&
2203            files_log_ref_write(refs, refname, &lock->old_oid,
2204                                &new_oid, logmsg, 0, &err)) {
2205                error("%s", err.buf);
2206                strbuf_release(&err);
2207        }
2208}
2209
2210static int create_symref_locked(struct files_ref_store *refs,
2211                                struct ref_lock *lock, const char *refname,
2212                                const char *target, const char *logmsg)
2213{
2214        if (prefer_symlink_refs && !create_ref_symlink(lock, target)) {
2215                update_symref_reflog(refs, lock, refname, target, logmsg);
2216                return 0;
2217        }
2218
2219        if (!fdopen_lock_file(lock->lk, "w"))
2220                return error("unable to fdopen %s: %s",
2221                             lock->lk->tempfile.filename.buf, strerror(errno));
2222
2223        update_symref_reflog(refs, lock, refname, target, logmsg);
2224
2225        /* no error check; commit_ref will check ferror */
2226        fprintf(lock->lk->tempfile.fp, "ref: %s\n", target);
2227        if (commit_ref(lock) < 0)
2228                return error("unable to write symref for %s: %s", refname,
2229                             strerror(errno));
2230        return 0;
2231}
2232
2233static int files_create_symref(struct ref_store *ref_store,
2234                               const char *refname, const char *target,
2235                               const char *logmsg)
2236{
2237        struct files_ref_store *refs =
2238                files_downcast(ref_store, REF_STORE_WRITE, "create_symref");
2239        struct strbuf err = STRBUF_INIT;
2240        struct ref_lock *lock;
2241        int ret;
2242
2243        lock = lock_ref_sha1_basic(refs, refname, NULL,
2244                                   NULL, NULL, REF_NODEREF, NULL,
2245                                   &err);
2246        if (!lock) {
2247                error("%s", err.buf);
2248                strbuf_release(&err);
2249                return -1;
2250        }
2251
2252        ret = create_symref_locked(refs, lock, refname, target, logmsg);
2253        unlock_ref(lock);
2254        return ret;
2255}
2256
2257static int files_reflog_exists(struct ref_store *ref_store,
2258                               const char *refname)
2259{
2260        struct files_ref_store *refs =
2261                files_downcast(ref_store, REF_STORE_READ, "reflog_exists");
2262        struct strbuf sb = STRBUF_INIT;
2263        struct stat st;
2264        int ret;
2265
2266        files_reflog_path(refs, &sb, refname);
2267        ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode);
2268        strbuf_release(&sb);
2269        return ret;
2270}
2271
2272static int files_delete_reflog(struct ref_store *ref_store,
2273                               const char *refname)
2274{
2275        struct files_ref_store *refs =
2276                files_downcast(ref_store, REF_STORE_WRITE, "delete_reflog");
2277        struct strbuf sb = STRBUF_INIT;
2278        int ret;
2279
2280        files_reflog_path(refs, &sb, refname);
2281        ret = remove_path(sb.buf);
2282        strbuf_release(&sb);
2283        return ret;
2284}
2285
2286static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)
2287{
2288        struct object_id ooid, noid;
2289        char *email_end, *message;
2290        timestamp_t timestamp;
2291        int tz;
2292        const char *p = sb->buf;
2293
2294        /* old SP new SP name <email> SP time TAB msg LF */
2295        if (!sb->len || sb->buf[sb->len - 1] != '\n' ||
2296            parse_oid_hex(p, &ooid, &p) || *p++ != ' ' ||
2297            parse_oid_hex(p, &noid, &p) || *p++ != ' ' ||
2298            !(email_end = strchr(p, '>')) ||
2299            email_end[1] != ' ' ||
2300            !(timestamp = parse_timestamp(email_end + 2, &message, 10)) ||
2301            !message || message[0] != ' ' ||
2302            (message[1] != '+' && message[1] != '-') ||
2303            !isdigit(message[2]) || !isdigit(message[3]) ||
2304            !isdigit(message[4]) || !isdigit(message[5]))
2305                return 0; /* corrupt? */
2306        email_end[1] = '\0';
2307        tz = strtol(message + 1, NULL, 10);
2308        if (message[6] != '\t')
2309                message += 6;
2310        else
2311                message += 7;
2312        return fn(&ooid, &noid, p, timestamp, tz, message, cb_data);
2313}
2314
2315static char *find_beginning_of_line(char *bob, char *scan)
2316{
2317        while (bob < scan && *(--scan) != '\n')
2318                ; /* keep scanning backwards */
2319        /*
2320         * Return either beginning of the buffer, or LF at the end of
2321         * the previous line.
2322         */
2323        return scan;
2324}
2325
2326static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
2327                                             const char *refname,
2328                                             each_reflog_ent_fn fn,
2329                                             void *cb_data)
2330{
2331        struct files_ref_store *refs =
2332                files_downcast(ref_store, REF_STORE_READ,
2333                               "for_each_reflog_ent_reverse");
2334        struct strbuf sb = STRBUF_INIT;
2335        FILE *logfp;
2336        long pos;
2337        int ret = 0, at_tail = 1;
2338
2339        files_reflog_path(refs, &sb, refname);
2340        logfp = fopen(sb.buf, "r");
2341        strbuf_release(&sb);
2342        if (!logfp)
2343                return -1;
2344
2345        /* Jump to the end */
2346        if (fseek(logfp, 0, SEEK_END) < 0)
2347                ret = error("cannot seek back reflog for %s: %s",
2348                            refname, strerror(errno));
2349        pos = ftell(logfp);
2350        while (!ret && 0 < pos) {
2351                int cnt;
2352                size_t nread;
2353                char buf[BUFSIZ];
2354                char *endp, *scanp;
2355
2356                /* Fill next block from the end */
2357                cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
2358                if (fseek(logfp, pos - cnt, SEEK_SET)) {
2359                        ret = error("cannot seek back reflog for %s: %s",
2360                                    refname, strerror(errno));
2361                        break;
2362                }
2363                nread = fread(buf, cnt, 1, logfp);
2364                if (nread != 1) {
2365                        ret = error("cannot read %d bytes from reflog for %s: %s",
2366                                    cnt, refname, strerror(errno));
2367                        break;
2368                }
2369                pos -= cnt;
2370
2371                scanp = endp = buf + cnt;
2372                if (at_tail && scanp[-1] == '\n')
2373                        /* Looking at the final LF at the end of the file */
2374                        scanp--;
2375                at_tail = 0;
2376
2377                while (buf < scanp) {
2378                        /*
2379                         * terminating LF of the previous line, or the beginning
2380                         * of the buffer.
2381                         */
2382                        char *bp;
2383
2384                        bp = find_beginning_of_line(buf, scanp);
2385
2386                        if (*bp == '\n') {
2387                                /*
2388                                 * The newline is the end of the previous line,
2389                                 * so we know we have complete line starting
2390                                 * at (bp + 1). Prefix it onto any prior data
2391                                 * we collected for the line and process it.
2392                                 */
2393                                strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1));
2394                                scanp = bp;
2395                                endp = bp + 1;
2396                                ret = show_one_reflog_ent(&sb, fn, cb_data);
2397                                strbuf_reset(&sb);
2398                                if (ret)
2399                                        break;
2400                        } else if (!pos) {
2401                                /*
2402                                 * We are at the start of the buffer, and the
2403                                 * start of the file; there is no previous
2404                                 * line, and we have everything for this one.
2405                                 * Process it, and we can end the loop.
2406                                 */
2407                                strbuf_splice(&sb, 0, 0, buf, endp - buf);
2408                                ret = show_one_reflog_ent(&sb, fn, cb_data);
2409                                strbuf_reset(&sb);
2410                                break;
2411                        }
2412
2413                        if (bp == buf) {
2414                                /*
2415                                 * We are at the start of the buffer, and there
2416                                 * is more file to read backwards. Which means
2417                                 * we are in the middle of a line. Note that we
2418                                 * may get here even if *bp was a newline; that
2419                                 * just means we are at the exact end of the
2420                                 * previous line, rather than some spot in the
2421                                 * middle.
2422                                 *
2423                                 * Save away what we have to be combined with
2424                                 * the data from the next read.
2425                                 */
2426                                strbuf_splice(&sb, 0, 0, buf, endp - buf);
2427                                break;
2428                        }
2429                }
2430
2431        }
2432        if (!ret && sb.len)
2433                die("BUG: reverse reflog parser had leftover data");
2434
2435        fclose(logfp);
2436        strbuf_release(&sb);
2437        return ret;
2438}
2439
2440static int files_for_each_reflog_ent(struct ref_store *ref_store,
2441                                     const char *refname,
2442                                     each_reflog_ent_fn fn, void *cb_data)
2443{
2444        struct files_ref_store *refs =
2445                files_downcast(ref_store, REF_STORE_READ,
2446                               "for_each_reflog_ent");
2447        FILE *logfp;
2448        struct strbuf sb = STRBUF_INIT;
2449        int ret = 0;
2450
2451        files_reflog_path(refs, &sb, refname);
2452        logfp = fopen(sb.buf, "r");
2453        strbuf_release(&sb);
2454        if (!logfp)
2455                return -1;
2456
2457        while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))
2458                ret = show_one_reflog_ent(&sb, fn, cb_data);
2459        fclose(logfp);
2460        strbuf_release(&sb);
2461        return ret;
2462}
2463
2464struct files_reflog_iterator {
2465        struct ref_iterator base;
2466
2467        struct ref_store *ref_store;
2468        struct dir_iterator *dir_iterator;
2469        struct object_id oid;
2470};
2471
2472static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
2473{
2474        struct files_reflog_iterator *iter =
2475                (struct files_reflog_iterator *)ref_iterator;
2476        struct dir_iterator *diter = iter->dir_iterator;
2477        int ok;
2478
2479        while ((ok = dir_iterator_advance(diter)) == ITER_OK) {
2480                int flags;
2481
2482                if (!S_ISREG(diter->st.st_mode))
2483                        continue;
2484                if (diter->basename[0] == '.')
2485                        continue;
2486                if (ends_with(diter->basename, ".lock"))
2487                        continue;
2488
2489                if (refs_read_ref_full(iter->ref_store,
2490                                       diter->relative_path, 0,
2491                                       iter->oid.hash, &flags)) {
2492                        error("bad ref for %s", diter->path.buf);
2493                        continue;
2494                }
2495
2496                iter->base.refname = diter->relative_path;
2497                iter->base.oid = &iter->oid;
2498                iter->base.flags = flags;
2499                return ITER_OK;
2500        }
2501
2502        iter->dir_iterator = NULL;
2503        if (ref_iterator_abort(ref_iterator) == ITER_ERROR)
2504                ok = ITER_ERROR;
2505        return ok;
2506}
2507
2508static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator,
2509                                   struct object_id *peeled)
2510{
2511        die("BUG: ref_iterator_peel() called for reflog_iterator");
2512}
2513
2514static int files_reflog_iterator_abort(struct ref_iterator *ref_iterator)
2515{
2516        struct files_reflog_iterator *iter =
2517                (struct files_reflog_iterator *)ref_iterator;
2518        int ok = ITER_DONE;
2519
2520        if (iter->dir_iterator)
2521                ok = dir_iterator_abort(iter->dir_iterator);
2522
2523        base_ref_iterator_free(ref_iterator);
2524        return ok;
2525}
2526
2527static struct ref_iterator_vtable files_reflog_iterator_vtable = {
2528        files_reflog_iterator_advance,
2529        files_reflog_iterator_peel,
2530        files_reflog_iterator_abort
2531};
2532
2533static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)
2534{
2535        struct files_ref_store *refs =
2536                files_downcast(ref_store, REF_STORE_READ,
2537                               "reflog_iterator_begin");
2538        struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
2539        struct ref_iterator *ref_iterator = &iter->base;
2540        struct strbuf sb = STRBUF_INIT;
2541
2542        base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
2543        files_reflog_path(refs, &sb, NULL);
2544        iter->dir_iterator = dir_iterator_begin(sb.buf);
2545        iter->ref_store = ref_store;
2546        strbuf_release(&sb);
2547        return ref_iterator;
2548}
2549
2550/*
2551 * If update is a direct update of head_ref (the reference pointed to
2552 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
2553 */
2554static int split_head_update(struct ref_update *update,
2555                             struct ref_transaction *transaction,
2556                             const char *head_ref,
2557                             struct string_list *affected_refnames,
2558                             struct strbuf *err)
2559{
2560        struct string_list_item *item;
2561        struct ref_update *new_update;
2562
2563        if ((update->flags & REF_LOG_ONLY) ||
2564            (update->flags & REF_ISPRUNING) ||
2565            (update->flags & REF_UPDATE_VIA_HEAD))
2566                return 0;
2567
2568        if (strcmp(update->refname, head_ref))
2569                return 0;
2570
2571        /*
2572         * First make sure that HEAD is not already in the
2573         * transaction. This insertion is O(N) in the transaction
2574         * size, but it happens at most once per transaction.
2575         */
2576        item = string_list_insert(affected_refnames, "HEAD");
2577        if (item->util) {
2578                /* An entry already existed */
2579                strbuf_addf(err,
2580                            "multiple updates for 'HEAD' (including one "
2581                            "via its referent '%s') are not allowed",
2582                            update->refname);
2583                return TRANSACTION_NAME_CONFLICT;
2584        }
2585
2586        new_update = ref_transaction_add_update(
2587                        transaction, "HEAD",
2588                        update->flags | REF_LOG_ONLY | REF_NODEREF,
2589                        update->new_oid.hash, update->old_oid.hash,
2590                        update->msg);
2591
2592        item->util = new_update;
2593
2594        return 0;
2595}
2596
2597/*
2598 * update is for a symref that points at referent and doesn't have
2599 * REF_NODEREF set. Split it into two updates:
2600 * - The original update, but with REF_LOG_ONLY and REF_NODEREF set
2601 * - A new, separate update for the referent reference
2602 * Note that the new update will itself be subject to splitting when
2603 * the iteration gets to it.
2604 */
2605static int split_symref_update(struct files_ref_store *refs,
2606                               struct ref_update *update,
2607                               const char *referent,
2608                               struct ref_transaction *transaction,
2609                               struct string_list *affected_refnames,
2610                               struct strbuf *err)
2611{
2612        struct string_list_item *item;
2613        struct ref_update *new_update;
2614        unsigned int new_flags;
2615
2616        /*
2617         * First make sure that referent is not already in the
2618         * transaction. This insertion is O(N) in the transaction
2619         * size, but it happens at most once per symref in a
2620         * transaction.
2621         */
2622        item = string_list_insert(affected_refnames, referent);
2623        if (item->util) {
2624                /* An entry already existed */
2625                strbuf_addf(err,
2626                            "multiple updates for '%s' (including one "
2627                            "via symref '%s') are not allowed",
2628                            referent, update->refname);
2629                return TRANSACTION_NAME_CONFLICT;
2630        }
2631
2632        new_flags = update->flags;
2633        if (!strcmp(update->refname, "HEAD")) {
2634                /*
2635                 * Record that the new update came via HEAD, so that
2636                 * when we process it, split_head_update() doesn't try
2637                 * to add another reflog update for HEAD. Note that
2638                 * this bit will be propagated if the new_update
2639                 * itself needs to be split.
2640                 */
2641                new_flags |= REF_UPDATE_VIA_HEAD;
2642        }
2643
2644        new_update = ref_transaction_add_update(
2645                        transaction, referent, new_flags,
2646                        update->new_oid.hash, update->old_oid.hash,
2647                        update->msg);
2648
2649        new_update->parent_update = update;
2650
2651        /*
2652         * Change the symbolic ref update to log only. Also, it
2653         * doesn't need to check its old SHA-1 value, as that will be
2654         * done when new_update is processed.
2655         */
2656        update->flags |= REF_LOG_ONLY | REF_NODEREF;
2657        update->flags &= ~REF_HAVE_OLD;
2658
2659        item->util = new_update;
2660
2661        return 0;
2662}
2663
2664/*
2665 * Return the refname under which update was originally requested.
2666 */
2667static const char *original_update_refname(struct ref_update *update)
2668{
2669        while (update->parent_update)
2670                update = update->parent_update;
2671
2672        return update->refname;
2673}
2674
2675/*
2676 * Check whether the REF_HAVE_OLD and old_oid values stored in update
2677 * are consistent with oid, which is the reference's current value. If
2678 * everything is OK, return 0; otherwise, write an error message to
2679 * err and return -1.
2680 */
2681static int check_old_oid(struct ref_update *update, struct object_id *oid,
2682                         struct strbuf *err)
2683{
2684        if (!(update->flags & REF_HAVE_OLD) ||
2685                   !oidcmp(oid, &update->old_oid))
2686                return 0;
2687
2688        if (is_null_oid(&update->old_oid))
2689                strbuf_addf(err, "cannot lock ref '%s': "
2690                            "reference already exists",
2691                            original_update_refname(update));
2692        else if (is_null_oid(oid))
2693                strbuf_addf(err, "cannot lock ref '%s': "
2694                            "reference is missing but expected %s",
2695                            original_update_refname(update),
2696                            oid_to_hex(&update->old_oid));
2697        else
2698                strbuf_addf(err, "cannot lock ref '%s': "
2699                            "is at %s but expected %s",
2700                            original_update_refname(update),
2701                            oid_to_hex(oid),
2702                            oid_to_hex(&update->old_oid));
2703
2704        return -1;
2705}
2706
2707/*
2708 * Prepare for carrying out update:
2709 * - Lock the reference referred to by update.
2710 * - Read the reference under lock.
2711 * - Check that its old SHA-1 value (if specified) is correct, and in
2712 *   any case record it in update->lock->old_oid for later use when
2713 *   writing the reflog.
2714 * - If it is a symref update without REF_NODEREF, split it up into a
2715 *   REF_LOG_ONLY update of the symref and add a separate update for
2716 *   the referent to transaction.
2717 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
2718 *   update of HEAD.
2719 */
2720static int lock_ref_for_update(struct files_ref_store *refs,
2721                               struct ref_update *update,
2722                               struct ref_transaction *transaction,
2723                               const char *head_ref,
2724                               struct string_list *affected_refnames,
2725                               struct strbuf *err)
2726{
2727        struct strbuf referent = STRBUF_INIT;
2728        int mustexist = (update->flags & REF_HAVE_OLD) &&
2729                !is_null_oid(&update->old_oid);
2730        int ret;
2731        struct ref_lock *lock;
2732
2733        files_assert_main_repository(refs, "lock_ref_for_update");
2734
2735        if ((update->flags & REF_HAVE_NEW) && is_null_oid(&update->new_oid))
2736                update->flags |= REF_DELETING;
2737
2738        if (head_ref) {
2739                ret = split_head_update(update, transaction, head_ref,
2740                                        affected_refnames, err);
2741                if (ret)
2742                        return ret;
2743        }
2744
2745        ret = lock_raw_ref(refs, update->refname, mustexist,
2746                           affected_refnames, NULL,
2747                           &lock, &referent,
2748                           &update->type, err);
2749        if (ret) {
2750                char *reason;
2751
2752                reason = strbuf_detach(err, NULL);
2753                strbuf_addf(err, "cannot lock ref '%s': %s",
2754                            original_update_refname(update), reason);
2755                free(reason);
2756                return ret;
2757        }
2758
2759        update->backend_data = lock;
2760
2761        if (update->type & REF_ISSYMREF) {
2762                if (update->flags & REF_NODEREF) {
2763                        /*
2764                         * We won't be reading the referent as part of
2765                         * the transaction, so we have to read it here
2766                         * to record and possibly check old_sha1:
2767                         */
2768                        if (refs_read_ref_full(&refs->base,
2769                                               referent.buf, 0,
2770                                               lock->old_oid.hash, NULL)) {
2771                                if (update->flags & REF_HAVE_OLD) {
2772                                        strbuf_addf(err, "cannot lock ref '%s': "
2773                                                    "error reading reference",
2774                                                    original_update_refname(update));
2775                                        return -1;
2776                                }
2777                        } else if (check_old_oid(update, &lock->old_oid, err)) {
2778                                return TRANSACTION_GENERIC_ERROR;
2779                        }
2780                } else {
2781                        /*
2782                         * Create a new update for the reference this
2783                         * symref is pointing at. Also, we will record
2784                         * and verify old_sha1 for this update as part
2785                         * of processing the split-off update, so we
2786                         * don't have to do it here.
2787                         */
2788                        ret = split_symref_update(refs, update,
2789                                                  referent.buf, transaction,
2790                                                  affected_refnames, err);
2791                        if (ret)
2792                                return ret;
2793                }
2794        } else {
2795                struct ref_update *parent_update;
2796
2797                if (check_old_oid(update, &lock->old_oid, err))
2798                        return TRANSACTION_GENERIC_ERROR;
2799
2800                /*
2801                 * If this update is happening indirectly because of a
2802                 * symref update, record the old SHA-1 in the parent
2803                 * update:
2804                 */
2805                for (parent_update = update->parent_update;
2806                     parent_update;
2807                     parent_update = parent_update->parent_update) {
2808                        struct ref_lock *parent_lock = parent_update->backend_data;
2809                        oidcpy(&parent_lock->old_oid, &lock->old_oid);
2810                }
2811        }
2812
2813        if ((update->flags & REF_HAVE_NEW) &&
2814            !(update->flags & REF_DELETING) &&
2815            !(update->flags & REF_LOG_ONLY)) {
2816                if (!(update->type & REF_ISSYMREF) &&
2817                    !oidcmp(&lock->old_oid, &update->new_oid)) {
2818                        /*
2819                         * The reference already has the desired
2820                         * value, so we don't need to write it.
2821                         */
2822                } else if (write_ref_to_lockfile(lock, &update->new_oid,
2823                                                 err)) {
2824                        char *write_err = strbuf_detach(err, NULL);
2825
2826                        /*
2827                         * The lock was freed upon failure of
2828                         * write_ref_to_lockfile():
2829                         */
2830                        update->backend_data = NULL;
2831                        strbuf_addf(err,
2832                                    "cannot update ref '%s': %s",
2833                                    update->refname, write_err);
2834                        free(write_err);
2835                        return TRANSACTION_GENERIC_ERROR;
2836                } else {
2837                        update->flags |= REF_NEEDS_COMMIT;
2838                }
2839        }
2840        if (!(update->flags & REF_NEEDS_COMMIT)) {
2841                /*
2842                 * We didn't call write_ref_to_lockfile(), so
2843                 * the lockfile is still open. Close it to
2844                 * free up the file descriptor:
2845                 */
2846                if (close_ref(lock)) {
2847                        strbuf_addf(err, "couldn't close '%s.lock'",
2848                                    update->refname);
2849                        return TRANSACTION_GENERIC_ERROR;
2850                }
2851        }
2852        return 0;
2853}
2854
2855/*
2856 * Unlock any references in `transaction` that are still locked, and
2857 * mark the transaction closed.
2858 */
2859static void files_transaction_cleanup(struct ref_transaction *transaction)
2860{
2861        size_t i;
2862
2863        for (i = 0; i < transaction->nr; i++) {
2864                struct ref_update *update = transaction->updates[i];
2865                struct ref_lock *lock = update->backend_data;
2866
2867                if (lock) {
2868                        unlock_ref(lock);
2869                        update->backend_data = NULL;
2870                }
2871        }
2872
2873        transaction->state = REF_TRANSACTION_CLOSED;
2874}
2875
2876static int files_transaction_prepare(struct ref_store *ref_store,
2877                                     struct ref_transaction *transaction,
2878                                     struct strbuf *err)
2879{
2880        struct files_ref_store *refs =
2881                files_downcast(ref_store, REF_STORE_WRITE,
2882                               "ref_transaction_prepare");
2883        size_t i;
2884        int ret = 0;
2885        struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
2886        char *head_ref = NULL;
2887        int head_type;
2888        struct object_id head_oid;
2889
2890        assert(err);
2891
2892        if (!transaction->nr)
2893                goto cleanup;
2894
2895        /*
2896         * Fail if a refname appears more than once in the
2897         * transaction. (If we end up splitting up any updates using
2898         * split_symref_update() or split_head_update(), those
2899         * functions will check that the new updates don't have the
2900         * same refname as any existing ones.)
2901         */
2902        for (i = 0; i < transaction->nr; i++) {
2903                struct ref_update *update = transaction->updates[i];
2904                struct string_list_item *item =
2905                        string_list_append(&affected_refnames, update->refname);
2906
2907                /*
2908                 * We store a pointer to update in item->util, but at
2909                 * the moment we never use the value of this field
2910                 * except to check whether it is non-NULL.
2911                 */
2912                item->util = update;
2913        }
2914        string_list_sort(&affected_refnames);
2915        if (ref_update_reject_duplicates(&affected_refnames, err)) {
2916                ret = TRANSACTION_GENERIC_ERROR;
2917                goto cleanup;
2918        }
2919
2920        /*
2921         * Special hack: If a branch is updated directly and HEAD
2922         * points to it (may happen on the remote side of a push
2923         * for example) then logically the HEAD reflog should be
2924         * updated too.
2925         *
2926         * A generic solution would require reverse symref lookups,
2927         * but finding all symrefs pointing to a given branch would be
2928         * rather costly for this rare event (the direct update of a
2929         * branch) to be worth it. So let's cheat and check with HEAD
2930         * only, which should cover 99% of all usage scenarios (even
2931         * 100% of the default ones).
2932         *
2933         * So if HEAD is a symbolic reference, then record the name of
2934         * the reference that it points to. If we see an update of
2935         * head_ref within the transaction, then split_head_update()
2936         * arranges for the reflog of HEAD to be updated, too.
2937         */
2938        head_ref = refs_resolve_refdup(ref_store, "HEAD",
2939                                       RESOLVE_REF_NO_RECURSE,
2940                                       head_oid.hash, &head_type);
2941
2942        if (head_ref && !(head_type & REF_ISSYMREF)) {
2943                free(head_ref);
2944                head_ref = NULL;
2945        }
2946
2947        /*
2948         * Acquire all locks, verify old values if provided, check
2949         * that new values are valid, and write new values to the
2950         * lockfiles, ready to be activated. Only keep one lockfile
2951         * open at a time to avoid running out of file descriptors.
2952         * Note that lock_ref_for_update() might append more updates
2953         * to the transaction.
2954         */
2955        for (i = 0; i < transaction->nr; i++) {
2956                struct ref_update *update = transaction->updates[i];
2957
2958                ret = lock_ref_for_update(refs, update, transaction,
2959                                          head_ref, &affected_refnames, err);
2960                if (ret)
2961                        break;
2962        }
2963
2964cleanup:
2965        free(head_ref);
2966        string_list_clear(&affected_refnames, 0);
2967
2968        if (ret)
2969                files_transaction_cleanup(transaction);
2970        else
2971                transaction->state = REF_TRANSACTION_PREPARED;
2972
2973        return ret;
2974}
2975
2976static int files_transaction_finish(struct ref_store *ref_store,
2977                                    struct ref_transaction *transaction,
2978                                    struct strbuf *err)
2979{
2980        struct files_ref_store *refs =
2981                files_downcast(ref_store, 0, "ref_transaction_finish");
2982        size_t i;
2983        int ret = 0;
2984        struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;
2985        struct string_list_item *ref_to_delete;
2986        struct strbuf sb = STRBUF_INIT;
2987
2988        assert(err);
2989
2990        if (!transaction->nr) {
2991                transaction->state = REF_TRANSACTION_CLOSED;
2992                return 0;
2993        }
2994
2995        /* Perform updates first so live commits remain referenced */
2996        for (i = 0; i < transaction->nr; i++) {
2997                struct ref_update *update = transaction->updates[i];
2998                struct ref_lock *lock = update->backend_data;
2999
3000                if (update->flags & REF_NEEDS_COMMIT ||
3001                    update->flags & REF_LOG_ONLY) {
3002                        if (files_log_ref_write(refs,
3003                                                lock->ref_name,
3004                                                &lock->old_oid,
3005                                                &update->new_oid,
3006                                                update->msg, update->flags,
3007                                                err)) {
3008                                char *old_msg = strbuf_detach(err, NULL);
3009
3010                                strbuf_addf(err, "cannot update the ref '%s': %s",
3011                                            lock->ref_name, old_msg);
3012                                free(old_msg);
3013                                unlock_ref(lock);
3014                                update->backend_data = NULL;
3015                                ret = TRANSACTION_GENERIC_ERROR;
3016                                goto cleanup;
3017                        }
3018                }
3019                if (update->flags & REF_NEEDS_COMMIT) {
3020                        clear_loose_ref_cache(refs);
3021                        if (commit_ref(lock)) {
3022                                strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
3023                                unlock_ref(lock);
3024                                update->backend_data = NULL;
3025                                ret = TRANSACTION_GENERIC_ERROR;
3026                                goto cleanup;
3027                        }
3028                }
3029        }
3030        /* Perform deletes now that updates are safely completed */
3031        for (i = 0; i < transaction->nr; i++) {
3032                struct ref_update *update = transaction->updates[i];
3033                struct ref_lock *lock = update->backend_data;
3034
3035                if (update->flags & REF_DELETING &&
3036                    !(update->flags & REF_LOG_ONLY)) {
3037                        if (!(update->type & REF_ISPACKED) ||
3038                            update->type & REF_ISSYMREF) {
3039                                /* It is a loose reference. */
3040                                strbuf_reset(&sb);
3041                                files_ref_path(refs, &sb, lock->ref_name);
3042                                if (unlink_or_msg(sb.buf, err)) {
3043                                        ret = TRANSACTION_GENERIC_ERROR;
3044                                        goto cleanup;
3045                                }
3046                                update->flags |= REF_DELETED_LOOSE;
3047                        }
3048
3049                        if (!(update->flags & REF_ISPRUNING))
3050                                string_list_append(&refs_to_delete,
3051                                                   lock->ref_name);
3052                }
3053        }
3054
3055        if (repack_without_refs(refs, &refs_to_delete, err)) {
3056                ret = TRANSACTION_GENERIC_ERROR;
3057                goto cleanup;
3058        }
3059
3060        /* Delete the reflogs of any references that were deleted: */
3061        for_each_string_list_item(ref_to_delete, &refs_to_delete) {
3062                strbuf_reset(&sb);
3063                files_reflog_path(refs, &sb, ref_to_delete->string);
3064                if (!unlink_or_warn(sb.buf))
3065                        try_remove_empty_parents(refs, ref_to_delete->string,
3066                                                 REMOVE_EMPTY_PARENTS_REFLOG);
3067        }
3068
3069        clear_loose_ref_cache(refs);
3070
3071cleanup:
3072        files_transaction_cleanup(transaction);
3073
3074        for (i = 0; i < transaction->nr; i++) {
3075                struct ref_update *update = transaction->updates[i];
3076
3077                if (update->flags & REF_DELETED_LOOSE) {
3078                        /*
3079                         * The loose reference was deleted. Delete any
3080                         * empty parent directories. (Note that this
3081                         * can only work because we have already
3082                         * removed the lockfile.)
3083                         */
3084                        try_remove_empty_parents(refs, update->refname,
3085                                                 REMOVE_EMPTY_PARENTS_REF);
3086                }
3087        }
3088
3089        strbuf_release(&sb);
3090        string_list_clear(&refs_to_delete, 0);
3091        return ret;
3092}
3093
3094static int files_transaction_abort(struct ref_store *ref_store,
3095                                   struct ref_transaction *transaction,
3096                                   struct strbuf *err)
3097{
3098        files_transaction_cleanup(transaction);
3099        return 0;
3100}
3101
3102static int ref_present(const char *refname,
3103                       const struct object_id *oid, int flags, void *cb_data)
3104{
3105        struct string_list *affected_refnames = cb_data;
3106
3107        return string_list_has_string(affected_refnames, refname);
3108}
3109
3110static int files_initial_transaction_commit(struct ref_store *ref_store,
3111                                            struct ref_transaction *transaction,
3112                                            struct strbuf *err)
3113{
3114        struct files_ref_store *refs =
3115                files_downcast(ref_store, REF_STORE_WRITE,
3116                               "initial_ref_transaction_commit");
3117        size_t i;
3118        int ret = 0;
3119        struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
3120
3121        assert(err);
3122
3123        if (transaction->state != REF_TRANSACTION_OPEN)
3124                die("BUG: commit called for transaction that is not open");
3125
3126        /* Fail if a refname appears more than once in the transaction: */
3127        for (i = 0; i < transaction->nr; i++)
3128                string_list_append(&affected_refnames,
3129                                   transaction->updates[i]->refname);
3130        string_list_sort(&affected_refnames);
3131        if (ref_update_reject_duplicates(&affected_refnames, err)) {
3132                ret = TRANSACTION_GENERIC_ERROR;
3133                goto cleanup;
3134        }
3135
3136        /*
3137         * It's really undefined to call this function in an active
3138         * repository or when there are existing references: we are
3139         * only locking and changing packed-refs, so (1) any
3140         * simultaneous processes might try to change a reference at
3141         * the same time we do, and (2) any existing loose versions of
3142         * the references that we are setting would have precedence
3143         * over our values. But some remote helpers create the remote
3144         * "HEAD" and "master" branches before calling this function,
3145         * so here we really only check that none of the references
3146         * that we are creating already exists.
3147         */
3148        if (refs_for_each_rawref(&refs->base, ref_present,
3149                                 &affected_refnames))
3150                die("BUG: initial ref transaction called with existing refs");
3151
3152        for (i = 0; i < transaction->nr; i++) {
3153                struct ref_update *update = transaction->updates[i];
3154
3155                if ((update->flags & REF_HAVE_OLD) &&
3156                    !is_null_oid(&update->old_oid))
3157                        die("BUG: initial ref transaction with old_sha1 set");
3158                if (refs_verify_refname_available(&refs->base, update->refname,
3159                                                  &affected_refnames, NULL,
3160                                                  err)) {
3161                        ret = TRANSACTION_NAME_CONFLICT;
3162                        goto cleanup;
3163                }
3164        }
3165
3166        if (lock_packed_refs(refs, 0)) {
3167                strbuf_addf(err, "unable to lock packed-refs file: %s",
3168                            strerror(errno));
3169                ret = TRANSACTION_GENERIC_ERROR;
3170                goto cleanup;
3171        }
3172
3173        for (i = 0; i < transaction->nr; i++) {
3174                struct ref_update *update = transaction->updates[i];
3175
3176                if ((update->flags & REF_HAVE_NEW) &&
3177                    !is_null_oid(&update->new_oid))
3178                        add_packed_ref(refs, update->refname,
3179                                       &update->new_oid);
3180        }
3181
3182        if (commit_packed_refs(refs)) {
3183                strbuf_addf(err, "unable to commit packed-refs file: %s",
3184                            strerror(errno));
3185                ret = TRANSACTION_GENERIC_ERROR;
3186                goto cleanup;
3187        }
3188
3189cleanup:
3190        transaction->state = REF_TRANSACTION_CLOSED;
3191        string_list_clear(&affected_refnames, 0);
3192        return ret;
3193}
3194
3195struct expire_reflog_cb {
3196        unsigned int flags;
3197        reflog_expiry_should_prune_fn *should_prune_fn;
3198        void *policy_cb;
3199        FILE *newlog;
3200        struct object_id last_kept_oid;
3201};
3202
3203static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
3204                             const char *email, timestamp_t timestamp, int tz,
3205                             const char *message, void *cb_data)
3206{
3207        struct expire_reflog_cb *cb = cb_data;
3208        struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;
3209
3210        if (cb->flags & EXPIRE_REFLOGS_REWRITE)
3211                ooid = &cb->last_kept_oid;
3212
3213        if ((*cb->should_prune_fn)(ooid, noid, email, timestamp, tz,
3214                                   message, policy_cb)) {
3215                if (!cb->newlog)
3216                        printf("would prune %s", message);
3217                else if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
3218                        printf("prune %s", message);
3219        } else {
3220                if (cb->newlog) {
3221                        fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s",
3222                                oid_to_hex(ooid), oid_to_hex(noid),
3223                                email, timestamp, tz, message);
3224                        oidcpy(&cb->last_kept_oid, noid);
3225                }
3226                if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
3227                        printf("keep %s", message);
3228        }
3229        return 0;
3230}
3231
3232static int files_reflog_expire(struct ref_store *ref_store,
3233                               const char *refname, const unsigned char *sha1,
3234                               unsigned int flags,
3235                               reflog_expiry_prepare_fn prepare_fn,
3236                               reflog_expiry_should_prune_fn should_prune_fn,
3237                               reflog_expiry_cleanup_fn cleanup_fn,
3238                               void *policy_cb_data)
3239{
3240        struct files_ref_store *refs =
3241                files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire");
3242        static struct lock_file reflog_lock;
3243        struct expire_reflog_cb cb;
3244        struct ref_lock *lock;
3245        struct strbuf log_file_sb = STRBUF_INIT;
3246        char *log_file;
3247        int status = 0;
3248        int type;
3249        struct strbuf err = STRBUF_INIT;
3250        struct object_id oid;
3251
3252        memset(&cb, 0, sizeof(cb));
3253        cb.flags = flags;
3254        cb.policy_cb = policy_cb_data;
3255        cb.should_prune_fn = should_prune_fn;
3256
3257        /*
3258         * The reflog file is locked by holding the lock on the
3259         * reference itself, plus we might need to update the
3260         * reference if --updateref was specified:
3261         */
3262        lock = lock_ref_sha1_basic(refs, refname, sha1,
3263                                   NULL, NULL, REF_NODEREF,
3264                                   &type, &err);
3265        if (!lock) {
3266                error("cannot lock ref '%s': %s", refname, err.buf);
3267                strbuf_release(&err);
3268                return -1;
3269        }
3270        if (!refs_reflog_exists(ref_store, refname)) {
3271                unlock_ref(lock);
3272                return 0;
3273        }
3274
3275        files_reflog_path(refs, &log_file_sb, refname);
3276        log_file = strbuf_detach(&log_file_sb, NULL);
3277        if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3278                /*
3279                 * Even though holding $GIT_DIR/logs/$reflog.lock has
3280                 * no locking implications, we use the lock_file
3281                 * machinery here anyway because it does a lot of the
3282                 * work we need, including cleaning up if the program
3283                 * exits unexpectedly.
3284                 */
3285                if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {
3286                        struct strbuf err = STRBUF_INIT;
3287                        unable_to_lock_message(log_file, errno, &err);
3288                        error("%s", err.buf);
3289                        strbuf_release(&err);
3290                        goto failure;
3291                }
3292                cb.newlog = fdopen_lock_file(&reflog_lock, "w");
3293                if (!cb.newlog) {
3294                        error("cannot fdopen %s (%s)",
3295                              get_lock_file_path(&reflog_lock), strerror(errno));
3296                        goto failure;
3297                }
3298        }
3299
3300        hashcpy(oid.hash, sha1);
3301
3302        (*prepare_fn)(refname, &oid, cb.policy_cb);
3303        refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);
3304        (*cleanup_fn)(cb.policy_cb);
3305
3306        if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3307                /*
3308                 * It doesn't make sense to adjust a reference pointed
3309                 * to by a symbolic ref based on expiring entries in
3310                 * the symbolic reference's reflog. Nor can we update
3311                 * a reference if there are no remaining reflog
3312                 * entries.
3313                 */
3314                int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) &&
3315                        !(type & REF_ISSYMREF) &&
3316                        !is_null_oid(&cb.last_kept_oid);
3317
3318                if (close_lock_file(&reflog_lock)) {
3319                        status |= error("couldn't write %s: %s", log_file,
3320                                        strerror(errno));
3321                } else if (update &&
3322                           (write_in_full(get_lock_file_fd(lock->lk),
3323                                oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||
3324                            write_str_in_full(get_lock_file_fd(lock->lk), "\n") != 1 ||
3325                            close_ref(lock) < 0)) {
3326                        status |= error("couldn't write %s",
3327                                        get_lock_file_path(lock->lk));
3328                        rollback_lock_file(&reflog_lock);
3329                } else if (commit_lock_file(&reflog_lock)) {
3330                        status |= error("unable to write reflog '%s' (%s)",
3331                                        log_file, strerror(errno));
3332                } else if (update && commit_ref(lock)) {
3333                        status |= error("couldn't set %s", lock->ref_name);
3334                }
3335        }
3336        free(log_file);
3337        unlock_ref(lock);
3338        return status;
3339
3340 failure:
3341        rollback_lock_file(&reflog_lock);
3342        free(log_file);
3343        unlock_ref(lock);
3344        return -1;
3345}
3346
3347static int files_init_db(struct ref_store *ref_store, struct strbuf *err)
3348{
3349        struct files_ref_store *refs =
3350                files_downcast(ref_store, REF_STORE_WRITE, "init_db");
3351        struct strbuf sb = STRBUF_INIT;
3352
3353        /*
3354         * Create .git/refs/{heads,tags}
3355         */
3356        files_ref_path(refs, &sb, "refs/heads");
3357        safe_create_dir(sb.buf, 1);
3358
3359        strbuf_reset(&sb);
3360        files_ref_path(refs, &sb, "refs/tags");
3361        safe_create_dir(sb.buf, 1);
3362
3363        strbuf_release(&sb);
3364        return 0;
3365}
3366
3367struct ref_storage_be refs_be_files = {
3368        NULL,
3369        "files",
3370        files_ref_store_create,
3371        files_init_db,
3372        files_transaction_prepare,
3373        files_transaction_finish,
3374        files_transaction_abort,
3375        files_initial_transaction_commit,
3376
3377        files_pack_refs,
3378        files_peel_ref,
3379        files_create_symref,
3380        files_delete_refs,
3381        files_rename_ref,
3382
3383        files_ref_iterator_begin,
3384        files_read_raw_ref,
3385
3386        files_reflog_iterator_begin,
3387        files_for_each_reflog_ent,
3388        files_for_each_reflog_ent_reverse,
3389        files_reflog_exists,
3390        files_create_reflog,
3391        files_delete_reflog,
3392        files_reflog_expire
3393};