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