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