refs: add method for initial ref transaction commit
[gitweb.git] / refs / files-backend.c
index fc67cc6ec74725a28d212e525aea5a4ed64fc785..0a511bf5a459c58e029baa443a0a2d8fdfbc5a4d 100644 (file)
@@ -1523,7 +1523,8 @@ static void unlock_ref(struct ref_lock *lock)
  *   avoided, namely if we were successfully able to read the ref
  * - Generate informative error messages in the case of failure
  */
-static int lock_raw_ref(const char *refname, int mustexist,
+static int lock_raw_ref(struct files_ref_store *refs,
+                       const char *refname, int mustexist,
                        const struct string_list *extras,
                        const struct string_list *skip,
                        struct ref_lock **lock_p,
@@ -1531,15 +1532,14 @@ static int lock_raw_ref(const char *refname, int mustexist,
                        unsigned int *type,
                        struct strbuf *err)
 {
-       struct ref_store *ref_store = get_ref_store(NULL);
-       struct files_ref_store *refs =
-               files_downcast(ref_store, 0, "lock_raw_ref");
        struct ref_lock *lock;
        struct strbuf ref_file = STRBUF_INIT;
        int attempts_remaining = 3;
        int ret = TRANSACTION_GENERIC_ERROR;
 
        assert(err);
+       assert_main_repository(&refs->base, "lock_raw_ref");
+
        *type = 0;
 
        /* First lock the file so it can't change out from under us. */
@@ -1623,7 +1623,7 @@ static int lock_raw_ref(const char *refname, int mustexist,
         * fear that its value will change.
         */
 
-       if (files_read_raw_ref(ref_store, refname,
+       if (files_read_raw_ref(&refs->base, refname,
                               lock->old_oid.hash, referent, type)) {
                if (errno == ENOENT) {
                        if (mustexist) {
@@ -1759,9 +1759,10 @@ static enum peel_status peel_entry(struct ref_entry *entry, int repeel)
        return status;
 }
 
-int peel_ref(const char *refname, unsigned char *sha1)
+static int files_peel_ref(struct ref_store *ref_store,
+                         const char *refname, unsigned char *sha1)
 {
-       struct files_ref_store *refs = get_files_ref_store(NULL, "peel_ref");
+       struct files_ref_store *refs = files_downcast(ref_store, 0, "peel_ref");
        int flag;
        unsigned char base[20];
 
@@ -1861,12 +1862,12 @@ static struct ref_iterator_vtable files_ref_iterator_vtable = {
        files_ref_iterator_abort
 };
 
-struct ref_iterator *files_ref_iterator_begin(
-               const char *submodule,
+static struct ref_iterator *files_ref_iterator_begin(
+               struct ref_store *ref_store,
                const char *prefix, unsigned int flags)
 {
        struct files_ref_store *refs =
-               get_files_ref_store(submodule, "ref_iterator_begin");
+               files_downcast(ref_store, 1, "ref_iterator_begin");
        struct ref_dir *loose_dir, *packed_dir;
        struct ref_iterator *loose_iter, *packed_iter;
        struct files_ref_iterator *iter;
@@ -1982,15 +1983,14 @@ static int remove_empty_directories(struct strbuf *path)
  * Locks a ref returning the lock on success and NULL on failure.
  * On failure errno is set to something meaningful.
  */
-static struct ref_lock *lock_ref_sha1_basic(const char *refname,
+static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
+                                           const char *refname,
                                            const unsigned char *old_sha1,
                                            const struct string_list *extras,
                                            const struct string_list *skip,
                                            unsigned int flags, int *type,
                                            struct strbuf *err)
 {
-       struct files_ref_store *refs =
-               get_files_ref_store(NULL, "lock_ref_sha1_basic");
        struct strbuf ref_file = STRBUF_INIT;
        struct ref_lock *lock;
        int last_errno = 0;
@@ -2000,6 +2000,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
        int attempts_remaining = 3;
        int resolved;
 
+       assert_main_repository(&refs->base, "lock_ref_sha1_basic");
        assert(err);
 
        lock = xcalloc(1, sizeof(struct ref_lock));
@@ -2383,14 +2384,14 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
  *
  * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
  */
-static int repack_without_refs(struct string_list *refnames, struct strbuf *err)
+static int repack_without_refs(struct files_ref_store *refs,
+                              struct string_list *refnames, struct strbuf *err)
 {
-       struct files_ref_store *refs =
-               get_files_ref_store(NULL, "repack_without_refs");
        struct ref_dir *packed;
        struct string_list_item *refname;
        int ret, needs_repacking = 0, removed = 0;
 
+       assert_main_repository(&refs->base, "repack_without_refs");
        assert(err);
 
        /* Look for a packed ref */
@@ -2452,13 +2453,15 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
 
 int delete_refs(struct string_list *refnames, unsigned int flags)
 {
+       struct files_ref_store *refs =
+               get_files_ref_store(NULL, "delete_refs");
        struct strbuf err = STRBUF_INIT;
        int i, result = 0;
 
        if (!refnames->nr)
                return 0;
 
-       result = repack_without_refs(refnames, &err);
+       result = repack_without_refs(refs, refnames, &err);
        if (result) {
                /*
                 * If we failed to rewrite the packed-refs file, then
@@ -2571,12 +2574,14 @@ static int files_verify_refname_available(struct ref_store *ref_store,
 
 static int write_ref_to_lockfile(struct ref_lock *lock,
                                 const unsigned char *sha1, struct strbuf *err);
-static int commit_ref_update(struct ref_lock *lock,
+static int commit_ref_update(struct files_ref_store *refs,
+                            struct ref_lock *lock,
                             const unsigned char *sha1, const char *logmsg,
                             struct strbuf *err);
 
 int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)
 {
+       struct files_ref_store *refs = get_files_ref_store(NULL, "rename_ref");
        unsigned char sha1[20], orig_sha1[20];
        int flag = 0, logmoved = 0;
        struct ref_lock *lock;
@@ -2639,8 +2644,8 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 
        logmoved = log;
 
-       lock = lock_ref_sha1_basic(newrefname, NULL, NULL, NULL, REF_NODEREF,
-                                  NULL, &err);
+       lock = lock_ref_sha1_basic(refs, newrefname, NULL, NULL, NULL,
+                                  REF_NODEREF, NULL, &err);
        if (!lock) {
                error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
                strbuf_release(&err);
@@ -2649,7 +2654,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
        hashcpy(lock->old_oid.hash, orig_sha1);
 
        if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
-           commit_ref_update(lock, orig_sha1, logmsg, &err)) {
+           commit_ref_update(refs, lock, orig_sha1, logmsg, &err)) {
                error("unable to write current sha1 into %s: %s", newrefname, err.buf);
                strbuf_release(&err);
                goto rollback;
@@ -2658,8 +2663,8 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
        return 0;
 
  rollback:
-       lock = lock_ref_sha1_basic(oldrefname, NULL, NULL, NULL, REF_NODEREF,
-                                  NULL, &err);
+       lock = lock_ref_sha1_basic(refs, oldrefname, NULL, NULL, NULL,
+                                  REF_NODEREF, NULL, &err);
        if (!lock) {
                error("unable to lock %s for rollback: %s", oldrefname, err.buf);
                strbuf_release(&err);
@@ -2669,7 +2674,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
        flag = log_all_ref_updates;
        log_all_ref_updates = 0;
        if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
-           commit_ref_update(lock, orig_sha1, NULL, &err)) {
+           commit_ref_update(refs, lock, orig_sha1, NULL, &err)) {
                error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
                strbuf_release(&err);
        }
@@ -2772,11 +2777,16 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str
 }
 
 
-int safe_create_reflog(const char *refname, int force_create, struct strbuf *err)
+static int files_create_reflog(struct ref_store *ref_store,
+                              const char *refname, int force_create,
+                              struct strbuf *err)
 {
        int ret;
        struct strbuf sb = STRBUF_INIT;
 
+       /* Check validity (but we don't need the result): */
+       files_downcast(ref_store, 0, "create_reflog");
+
        ret = log_ref_setup(refname, &sb, err, force_create);
        strbuf_release(&sb);
        return ret;
@@ -2905,12 +2915,12 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
  * to the loose reference lockfile. Also update the reflogs if
  * necessary, using the specified lockmsg (which can be NULL).
  */
-static int commit_ref_update(struct ref_lock *lock,
+static int commit_ref_update(struct files_ref_store *refs,
+                            struct ref_lock *lock,
                             const unsigned char *sha1, const char *logmsg,
                             struct strbuf *err)
 {
-       struct files_ref_store *refs =
-               get_files_ref_store(NULL, "commit_ref_update");
+       assert_main_repository(&refs->base, "commit_ref_update");
 
        clear_loose_ref_cache(refs);
        if (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, 0, err)) {
@@ -3015,13 +3025,14 @@ static int files_create_symref(struct ref_store *ref_store,
                               const char *refname, const char *target,
                               const char *logmsg)
 {
+       struct files_ref_store *refs =
+               files_downcast(ref_store, 0, "create_symref");
        struct strbuf err = STRBUF_INIT;
        struct ref_lock *lock;
        int ret;
 
-       files_downcast(ref_store, 0, "create_symref");
-
-       lock = lock_ref_sha1_basic(refname, NULL, NULL, NULL, REF_NODEREF, NULL,
+       lock = lock_ref_sha1_basic(refs, refname, NULL,
+                                  NULL, NULL, REF_NODEREF, NULL,
                                   &err);
        if (!lock) {
                error("%s", err.buf);
@@ -3069,16 +3080,24 @@ int set_worktree_head_symref(const char *gitdir, const char *target)
        return ret;
 }
 
-int reflog_exists(const char *refname)
+static int files_reflog_exists(struct ref_store *ref_store,
+                              const char *refname)
 {
        struct stat st;
 
+       /* Check validity (but we don't need the result): */
+       files_downcast(ref_store, 0, "reflog_exists");
+
        return !lstat(git_path("logs/%s", refname), &st) &&
                S_ISREG(st.st_mode);
 }
 
-int delete_reflog(const char *refname)
+static int files_delete_reflog(struct ref_store *ref_store,
+                              const char *refname)
 {
+       /* Check validity (but we don't need the result): */
+       files_downcast(ref_store, 0, "delete_reflog");
+
        return remove_path(git_path("logs/%s", refname));
 }
 
@@ -3121,13 +3140,19 @@ static char *find_beginning_of_line(char *bob, char *scan)
        return scan;
 }
 
-int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void *cb_data)
+static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
+                                            const char *refname,
+                                            each_reflog_ent_fn fn,
+                                            void *cb_data)
 {
        struct strbuf sb = STRBUF_INIT;
        FILE *logfp;
        long pos;
        int ret = 0, at_tail = 1;
 
+       /* Check validity (but we don't need the result): */
+       files_downcast(ref_store, 0, "for_each_reflog_ent_reverse");
+
        logfp = fopen(git_path("logs/%s", refname), "r");
        if (!logfp)
                return -1;
@@ -3223,12 +3248,17 @@ int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void
        return ret;
 }
 
-int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data)
+static int files_for_each_reflog_ent(struct ref_store *ref_store,
+                                    const char *refname,
+                                    each_reflog_ent_fn fn, void *cb_data)
 {
        FILE *logfp;
        struct strbuf sb = STRBUF_INIT;
        int ret = 0;
 
+       /* Check validity (but we don't need the result): */
+       files_downcast(ref_store, 0, "for_each_reflog_ent");
+
        logfp = fopen(git_path("logs/%s", refname), "r");
        if (!logfp)
                return -1;
@@ -3307,22 +3337,19 @@ static struct ref_iterator_vtable files_reflog_iterator_vtable = {
        files_reflog_iterator_abort
 };
 
-struct ref_iterator *files_reflog_iterator_begin(void)
+static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)
 {
        struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
        struct ref_iterator *ref_iterator = &iter->base;
 
+       /* Check validity (but we don't need the result): */
+       files_downcast(ref_store, 0, "reflog_iterator_begin");
+
        base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
        iter->dir_iterator = dir_iterator_begin(git_path("logs"));
        return ref_iterator;
 }
 
-int for_each_reflog(each_ref_fn fn, void *cb_data)
-{
-       return do_for_each_ref_iterator(files_reflog_iterator_begin(),
-                                       fn, cb_data);
-}
-
 static int ref_update_reject_duplicates(struct string_list *refnames,
                                        struct strbuf *err)
 {
@@ -3395,7 +3422,8 @@ static int split_head_update(struct ref_update *update,
  * Note that the new update will itself be subject to splitting when
  * the iteration gets to it.
  */
-static int split_symref_update(struct ref_update *update,
+static int split_symref_update(struct files_ref_store *refs,
+                              struct ref_update *update,
                               const char *referent,
                               struct ref_transaction *transaction,
                               struct string_list *affected_refnames,
@@ -3477,7 +3505,8 @@ static const char *original_update_refname(struct ref_update *update)
  * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
  *   update of HEAD.
  */
-static int lock_ref_for_update(struct ref_update *update,
+static int lock_ref_for_update(struct files_ref_store *refs,
+                              struct ref_update *update,
                               struct ref_transaction *transaction,
                               const char *head_ref,
                               struct string_list *affected_refnames,
@@ -3489,6 +3518,8 @@ static int lock_ref_for_update(struct ref_update *update,
        int ret;
        struct ref_lock *lock;
 
+       assert_main_repository(&refs->base, "lock_ref_for_update");
+
        if ((update->flags & REF_HAVE_NEW) && is_null_sha1(update->new_sha1))
                update->flags |= REF_DELETING;
 
@@ -3499,7 +3530,7 @@ static int lock_ref_for_update(struct ref_update *update,
                        return ret;
        }
 
-       ret = lock_raw_ref(update->refname, mustexist,
+       ret = lock_raw_ref(refs, update->refname, mustexist,
                           affected_refnames, NULL,
                           &update->lock, &referent,
                           &update->type, err);
@@ -3553,7 +3584,8 @@ static int lock_ref_for_update(struct ref_update *update,
                         * of processing the split-off update, so we
                         * don't have to do it here.
                         */
-                       ret = split_symref_update(update, referent.buf, transaction,
+                       ret = split_symref_update(refs, update,
+                                                 referent.buf, transaction,
                                                  affected_refnames, err);
                        if (ret)
                                return ret;
@@ -3713,8 +3745,8 @@ static int files_transaction_commit(struct ref_store *ref_store,
        for (i = 0; i < transaction->nr; i++) {
                struct ref_update *update = transaction->updates[i];
 
-               ret = lock_ref_for_update(update, transaction, head_ref,
-                                         &affected_refnames, err);
+               ret = lock_ref_for_update(refs, update, transaction,
+                                         head_ref, &affected_refnames, err);
                if (ret)
                        goto cleanup;
        }
@@ -3768,7 +3800,7 @@ static int files_transaction_commit(struct ref_store *ref_store,
                }
        }
 
-       if (repack_without_refs(&refs_to_delete, err)) {
+       if (repack_without_refs(refs, &refs_to_delete, err)) {
                ret = TRANSACTION_GENERIC_ERROR;
                goto cleanup;
        }
@@ -3797,11 +3829,12 @@ static int ref_present(const char *refname,
        return string_list_has_string(affected_refnames, refname);
 }
 
-int initial_ref_transaction_commit(struct ref_transaction *transaction,
-                                  struct strbuf *err)
+static int files_initial_transaction_commit(struct ref_store *ref_store,
+                                           struct ref_transaction *transaction,
+                                           struct strbuf *err)
 {
        struct files_ref_store *refs =
-               get_files_ref_store(NULL, "initial_ref_transaction_commit");
+               files_downcast(ref_store, 0, "initial_ref_transaction_commit");
        int ret = 0, i;
        struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
 
@@ -3914,13 +3947,16 @@ static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
        return 0;
 }
 
-int reflog_expire(const char *refname, const unsigned char *sha1,
-                unsigned int flags,
-                reflog_expiry_prepare_fn prepare_fn,
-                reflog_expiry_should_prune_fn should_prune_fn,
-                reflog_expiry_cleanup_fn cleanup_fn,
-                void *policy_cb_data)
+static int files_reflog_expire(struct ref_store *ref_store,
+                              const char *refname, const unsigned char *sha1,
+                              unsigned int flags,
+                              reflog_expiry_prepare_fn prepare_fn,
+                              reflog_expiry_should_prune_fn should_prune_fn,
+                              reflog_expiry_cleanup_fn cleanup_fn,
+                              void *policy_cb_data)
 {
+       struct files_ref_store *refs =
+               files_downcast(ref_store, 0, "reflog_expire");
        static struct lock_file reflog_lock;
        struct expire_reflog_cb cb;
        struct ref_lock *lock;
@@ -3939,7 +3975,8 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
         * reference itself, plus we might need to update the
         * reference if --updateref was specified:
         */
-       lock = lock_ref_sha1_basic(refname, sha1, NULL, NULL, REF_NODEREF,
+       lock = lock_ref_sha1_basic(refs, refname, sha1,
+                                  NULL, NULL, REF_NODEREF,
                                   &type, &err);
        if (!lock) {
                error("cannot lock ref '%s': %s", refname, err.buf);
@@ -4025,10 +4062,21 @@ struct ref_storage_be refs_be_files = {
        "files",
        files_ref_store_create,
        files_transaction_commit,
+       files_initial_transaction_commit,
 
        files_pack_refs,
+       files_peel_ref,
        files_create_symref,
 
+       files_ref_iterator_begin,
        files_read_raw_ref,
-       files_verify_refname_available
+       files_verify_refname_available,
+
+       files_reflog_iterator_begin,
+       files_for_each_reflog_ent,
+       files_for_each_reflog_ent_reverse,
+       files_reflog_exists,
+       files_create_reflog,
+       files_delete_reflog,
+       files_reflog_expire
 };