files-backend: avoid ref api targeting main ref store

A small step towards making files-backend work as a non-main ref store using the newly added store-aware API. For the record, `join` and `nm` on refs.o and files-backend.o tell me that files-backend no longer uses functions that default to get_main_ref_store(). I'm not yet comfortable at the idea of removing files_assert_main_repository() (or converting REF_STORE_MAIN to REF_STORE_WRITE). More staring and testing is required before that can happen. Well, except peel_ref(). I'm pretty sure that function is safe. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Mar 26, 2017 at 09:42 UTC 2f40e954723b861cb4a921d39d1ef0465410247e
1 file changed +49 -35
refs/files-backend.c
+49 -35
@@ -1829,8 +1829,6 @@ static int files_peel_ref(struct ref_store *ref_store,
1829 int flag;
1830 unsigned char base[20];
1831
1832 - files_assert_main_repository(refs, "peel_ref");
1833 -
1832 if (current_ref_iter && current_ref_iter->refname == refname) {
1833 struct object_id peeled;
1834
@@ -1840,7 +1838,8 @@ static int files_peel_ref(struct ref_store *ref_store,
1838 return 0;
1839 }
1840
1843 - if (read_ref_full(refname, RESOLVE_REF_READING, base, &flag))
1841 + if (refs_read_ref_full(ref_store, refname,
1842 + RESOLVE_REF_READING, base, &flag))
1843 return -1;
1844
1845 /*
@@ -2008,15 +2007,15 @@ static struct ref_iterator *files_ref_iterator_begin(
2007 * on success. On error, write an error message to err, set errno, and
2008 * return a negative value.
2009 */
2011 -static int verify_lock(struct ref_lock *lock,
2010 +static int verify_lock(struct ref_store *ref_store, struct ref_lock *lock,
2011 const unsigned char *old_sha1, int mustexist,
2012 struct strbuf *err)
2013 {
2014 assert(err);
2015
2017 - if (read_ref_full(lock->ref_name,
2018 - mustexist ? RESOLVE_REF_READING : 0,
2019 - lock->old_oid.hash, NULL)) {
2016 + if (refs_read_ref_full(ref_store, lock->ref_name,
2017 + mustexist ? RESOLVE_REF_READING : 0,
2018 + lock->old_oid.hash, NULL)) {
2019 if (old_sha1) {
2020 int save_errno = errno;
2021 strbuf_addf(err, "can't verify ref '%s'", lock->ref_name);
@@ -2085,8 +2084,9 @@ static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
2084 resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
2085
2086 files_ref_path(refs, &ref_file, refname);
2088 - resolved = !!resolve_ref_unsafe(refname, resolve_flags,
2089 - lock->old_oid.hash, type);
2087 + resolved = !!refs_resolve_ref_unsafe(&refs->base,
2088 + refname, resolve_flags,
2089 + lock->old_oid.hash, type);
2090 if (!resolved && errno == EISDIR) {
2091 /*
2092 * we are trying to lock foo but we used to
@@ -2103,8 +2103,9 @@ static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
2103 refname);
2104 goto error_return;
2105 }
2106 - resolved = !!resolve_ref_unsafe(refname, resolve_flags,
2107 - lock->old_oid.hash, type);
2106 + resolved = !!refs_resolve_ref_unsafe(&refs->base,
2107 + refname, resolve_flags,
2108 + lock->old_oid.hash, type);
2109 }
2110 if (!resolved) {
2111 last_errno = errno;
@@ -2142,7 +2143,7 @@ static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
2143 goto error_return;
2144 }
2145
2145 - if (verify_lock(lock, old_sha1, mustexist, err)) {
2146 + if (verify_lock(&refs->base, lock, old_sha1, mustexist, err)) {
2147 last_errno = errno;
2148 goto error_return;
2149 }
@@ -2397,7 +2398,7 @@ static void try_remove_empty_parents(struct files_ref_store *refs,
2398 }
2399
2400 /* make sure nobody touched the ref, and unlink */
2400 -static void prune_ref(struct ref_to_prune *r)
2401 +static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
2402 {
2403 struct ref_transaction *transaction;
2404 struct strbuf err = STRBUF_INIT;
@@ -2405,7 +2406,7 @@ static void prune_ref(struct ref_to_prune *r)
2406 if (check_refname_format(r->name, 0))
2407 return;
2408
2408 - transaction = ref_transaction_begin(&err);
2409 + transaction = ref_store_transaction_begin(&refs->base, &err);
2410 if (!transaction ||
2411 ref_transaction_delete(transaction, r->name, r->sha1,
2412 REF_ISPRUNING | REF_NODEREF, NULL, &err) ||
@@ -2419,10 +2420,10 @@ static void prune_ref(struct ref_to_prune *r)
2420 strbuf_release(&err);
2421 }
2422
2422 -static void prune_refs(struct ref_to_prune *r)
2423 +static void prune_refs(struct files_ref_store *refs, struct ref_to_prune *r)
2424 {
2425 while (r) {
2425 - prune_ref(r);
2426 + prune_ref(refs, r);
2427 r = r->next;
2428 }
2429 }
@@ -2446,7 +2447,7 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
2447 if (commit_packed_refs(refs))
2448 die_errno("unable to overwrite old ref-pack file");
2449
2449 - prune_refs(cbdata.ref_to_prune);
2450 + prune_refs(refs, cbdata.ref_to_prune);
2451 return 0;
2452 }
2453
@@ -2538,7 +2539,7 @@ static int files_delete_refs(struct ref_store *ref_store,
2539 for (i = 0; i < refnames->nr; i++) {
2540 const char *refname = refnames->items[i].string;
2541
2541 - if (delete_ref(NULL, refname, NULL, flags))
2542 + if (refs_delete_ref(&refs->base, NULL, refname, NULL, flags))
2543 result |= error(_("could not remove reference %s"), refname);
2544 }
2545
@@ -2660,7 +2661,8 @@ static int files_rename_ref(struct ref_store *ref_store,
2661 goto out;
2662 }
2663
2663 - if (!resolve_ref_unsafe(oldrefname, RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
2664 + if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,
2665 + RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
2666 orig_sha1, &flag)) {
2667 ret = error("refname %s not found", oldrefname);
2668 goto out;
@@ -2682,7 +2684,8 @@ static int files_rename_ref(struct ref_store *ref_store,
2684 goto out;
2685 }
2686
2685 - if (delete_ref(logmsg, oldrefname, orig_sha1, REF_NODEREF)) {
2687 + if (refs_delete_ref(&refs->base, logmsg, oldrefname,
2688 + orig_sha1, REF_NODEREF)) {
2689 error("unable to delete old %s", oldrefname);
2690 goto rollback;
2691 }
@@ -2694,9 +2697,11 @@ static int files_rename_ref(struct ref_store *ref_store,
2697 * the safety anyway; we want to delete the reference whatever
2698 * its current value.
2699 */
2697 - if (!read_ref_full(newrefname, RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
2698 - sha1, NULL) &&
2699 - delete_ref(NULL, newrefname, NULL, REF_NODEREF)) {
2700 + if (!refs_read_ref_full(&refs->base, newrefname,
2701 + RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
2702 + sha1, NULL) &&
2703 + refs_delete_ref(&refs->base, NULL, newrefname,
2704 + NULL, REF_NODEREF)) {
2705 if (errno == EISDIR) {
2706 struct strbuf path = STRBUF_INIT;
2707 int result;
@@ -3052,8 +3057,9 @@ static int commit_ref_update(struct files_ref_store *refs,
3057 int head_flag;
3058 const char *head_ref;
3059
3055 - head_ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
3056 - head_sha1, &head_flag);
3060 + head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
3061 + RESOLVE_REF_READING,
3062 + head_sha1, &head_flag);
3063 if (head_ref && (head_flag & REF_ISSYMREF) &&
3064 !strcmp(head_ref, lock->ref_name)) {
3065 struct strbuf log_err = STRBUF_INIT;
@@ -3097,7 +3103,9 @@ static void update_symref_reflog(struct files_ref_store *refs,
3103 {
3104 struct strbuf err = STRBUF_INIT;
3105 unsigned char new_sha1[20];
3100 - if (logmsg && !read_ref(target, new_sha1) &&
3106 + if (logmsg &&
3107 + !refs_read_ref_full(&refs->base, target,
3108 + RESOLVE_REF_READING, new_sha1, NULL) &&
3109 files_log_ref_write(refs, refname, lock->old_oid.hash,
3110 new_sha1, logmsg, 0, &err)) {
3111 error("%s", err.buf);
@@ -3402,6 +3410,7 @@ static int files_for_each_reflog_ent(struct ref_store *ref_store,
3410 struct files_reflog_iterator {
3411 struct ref_iterator base;
3412
3413 + struct ref_store *ref_store;
3414 struct dir_iterator *dir_iterator;
3415 struct object_id oid;
3416 };
@@ -3423,8 +3432,9 @@ static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
3432 if (ends_with(diter->basename, ".lock"))
3433 continue;
3434
3426 - if (read_ref_full(diter->relative_path, 0,
3427 - iter->oid.hash, &flags)) {
3435 + if (refs_read_ref_full(iter->ref_store,
3436 + diter->relative_path, 0,
3437 + iter->oid.hash, &flags)) {
3438 error("bad ref for %s", diter->path.buf);
3439 continue;
3440 }
@@ -3478,6 +3488,7 @@ static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_st
3488 base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
3489 files_reflog_path(refs, &sb, NULL);
3490 iter->dir_iterator = dir_iterator_begin(sb.buf);
3491 + iter->ref_store = ref_store;
3492 strbuf_release(&sb);
3493 return ref_iterator;
3494 }
@@ -3717,8 +3728,9 @@ static int lock_ref_for_update(struct files_ref_store *refs,
3728 * the transaction, so we have to read it here
3729 * to record and possibly check old_sha1:
3730 */
3720 - if (read_ref_full(referent.buf, 0,
3721 - lock->old_oid.hash, NULL)) {
3731 + if (refs_read_ref_full(&refs->base,
3732 + referent.buf, 0,
3733 + lock->old_oid.hash, NULL)) {
3734 if (update->flags & REF_HAVE_OLD) {
3735 strbuf_addf(err, "cannot lock ref '%s': "
3736 "error reading reference",
@@ -3872,8 +3884,9 @@ static int files_transaction_commit(struct ref_store *ref_store,
3884 * head_ref within the transaction, then split_head_update()
3885 * arranges for the reflog of HEAD to be updated, too.
3886 */
3875 - head_ref = resolve_refdup("HEAD", RESOLVE_REF_NO_RECURSE,
3876 - head_oid.hash, &head_type);
3887 + head_ref = refs_resolve_refdup(ref_store, "HEAD",
3888 + RESOLVE_REF_NO_RECURSE,
3889 + head_oid.hash, &head_type);
3890
3891 if (head_ref && !(head_type & REF_ISSYMREF)) {
3892 free(head_ref);
@@ -4046,7 +4059,8 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
4059 * so here we really only check that none of the references
4060 * that we are creating already exists.
4061 */
4049 - if (for_each_rawref(ref_present, &affected_refnames))
4062 + if (refs_for_each_rawref(&refs->base, ref_present,
4063 + &affected_refnames))
4064 die("BUG: initial ref transaction called with existing refs");
4065
4066 for (i = 0; i < transaction->nr; i++) {
@@ -4165,7 +4179,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
4179 strbuf_release(&err);
4180 return -1;
4181 }
4168 - if (!reflog_exists(refname)) {
4182 + if (!refs_reflog_exists(ref_store, refname)) {
4183 unlock_ref(lock);
4184 return 0;
4185 }
@@ -4196,7 +4210,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
4210 }
4211
4212 (*prepare_fn)(refname, sha1, cb.policy_cb);
4199 - for_each_reflog_ent(refname, expire_reflog_ent, &cb);
4213 + refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);
4214 (*cleanup_fn)(cb.policy_cb);
4215
4216 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {