Skip to content

Commit 2f40e95

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c0fe4e8 commit 2f40e95

File tree

1 file changed

+49
-35
lines changed

1 file changed

+49
-35
lines changed

refs/files-backend.c

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,8 +1829,6 @@ static int files_peel_ref(struct ref_store *ref_store,
18291829
int flag;
18301830
unsigned char base[20];
18311831

1832-
files_assert_main_repository(refs, "peel_ref");
1833-
18341832
if (current_ref_iter && current_ref_iter->refname == refname) {
18351833
struct object_id peeled;
18361834

@@ -1840,7 +1838,8 @@ static int files_peel_ref(struct ref_store *ref_store,
18401838
return 0;
18411839
}
18421840

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))
18441843
return -1;
18451844

18461845
/*
@@ -2008,15 +2007,15 @@ static struct ref_iterator *files_ref_iterator_begin(
20082007
* on success. On error, write an error message to err, set errno, and
20092008
* return a negative value.
20102009
*/
2011-
static int verify_lock(struct ref_lock *lock,
2010+
static int verify_lock(struct ref_store *ref_store, struct ref_lock *lock,
20122011
const unsigned char *old_sha1, int mustexist,
20132012
struct strbuf *err)
20142013
{
20152014
assert(err);
20162015

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)) {
20202019
if (old_sha1) {
20212020
int save_errno = errno;
20222021
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,
20852084
resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
20862085

20872086
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);
20902090
if (!resolved && errno == EISDIR) {
20912091
/*
20922092
* 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,
21032103
refname);
21042104
goto error_return;
21052105
}
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);
21082109
}
21092110
if (!resolved) {
21102111
last_errno = errno;
@@ -2142,7 +2143,7 @@ static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
21422143
goto error_return;
21432144
}
21442145

2145-
if (verify_lock(lock, old_sha1, mustexist, err)) {
2146+
if (verify_lock(&refs->base, lock, old_sha1, mustexist, err)) {
21462147
last_errno = errno;
21472148
goto error_return;
21482149
}
@@ -2397,15 +2398,15 @@ static void try_remove_empty_parents(struct files_ref_store *refs,
23972398
}
23982399

23992400
/* 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)
24012402
{
24022403
struct ref_transaction *transaction;
24032404
struct strbuf err = STRBUF_INIT;
24042405

24052406
if (check_refname_format(r->name, 0))
24062407
return;
24072408

2408-
transaction = ref_transaction_begin(&err);
2409+
transaction = ref_store_transaction_begin(&refs->base, &err);
24092410
if (!transaction ||
24102411
ref_transaction_delete(transaction, r->name, r->sha1,
24112412
REF_ISPRUNING | REF_NODEREF, NULL, &err) ||
@@ -2419,10 +2420,10 @@ static void prune_ref(struct ref_to_prune *r)
24192420
strbuf_release(&err);
24202421
}
24212422

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)
24232424
{
24242425
while (r) {
2425-
prune_ref(r);
2426+
prune_ref(refs, r);
24262427
r = r->next;
24272428
}
24282429
}
@@ -2446,7 +2447,7 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
24462447
if (commit_packed_refs(refs))
24472448
die_errno("unable to overwrite old ref-pack file");
24482449

2449-
prune_refs(cbdata.ref_to_prune);
2450+
prune_refs(refs, cbdata.ref_to_prune);
24502451
return 0;
24512452
}
24522453

@@ -2538,7 +2539,7 @@ static int files_delete_refs(struct ref_store *ref_store,
25382539
for (i = 0; i < refnames->nr; i++) {
25392540
const char *refname = refnames->items[i].string;
25402541

2541-
if (delete_ref(NULL, refname, NULL, flags))
2542+
if (refs_delete_ref(&refs->base, NULL, refname, NULL, flags))
25422543
result |= error(_("could not remove reference %s"), refname);
25432544
}
25442545

@@ -2660,7 +2661,8 @@ static int files_rename_ref(struct ref_store *ref_store,
26602661
goto out;
26612662
}
26622663

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,
26642666
orig_sha1, &flag)) {
26652667
ret = error("refname %s not found", oldrefname);
26662668
goto out;
@@ -2682,7 +2684,8 @@ static int files_rename_ref(struct ref_store *ref_store,
26822684
goto out;
26832685
}
26842686

2685-
if (delete_ref(logmsg, oldrefname, orig_sha1, REF_NODEREF)) {
2687+
if (refs_delete_ref(&refs->base, logmsg, oldrefname,
2688+
orig_sha1, REF_NODEREF)) {
26862689
error("unable to delete old %s", oldrefname);
26872690
goto rollback;
26882691
}
@@ -2694,9 +2697,11 @@ static int files_rename_ref(struct ref_store *ref_store,
26942697
* the safety anyway; we want to delete the reference whatever
26952698
* its current value.
26962699
*/
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)) {
27002705
if (errno == EISDIR) {
27012706
struct strbuf path = STRBUF_INIT;
27022707
int result;
@@ -3052,8 +3057,9 @@ static int commit_ref_update(struct files_ref_store *refs,
30523057
int head_flag;
30533058
const char *head_ref;
30543059

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);
30573063
if (head_ref && (head_flag & REF_ISSYMREF) &&
30583064
!strcmp(head_ref, lock->ref_name)) {
30593065
struct strbuf log_err = STRBUF_INIT;
@@ -3097,7 +3103,9 @@ static void update_symref_reflog(struct files_ref_store *refs,
30973103
{
30983104
struct strbuf err = STRBUF_INIT;
30993105
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) &&
31013109
files_log_ref_write(refs, refname, lock->old_oid.hash,
31023110
new_sha1, logmsg, 0, &err)) {
31033111
error("%s", err.buf);
@@ -3402,6 +3410,7 @@ static int files_for_each_reflog_ent(struct ref_store *ref_store,
34023410
struct files_reflog_iterator {
34033411
struct ref_iterator base;
34043412

3413+
struct ref_store *ref_store;
34053414
struct dir_iterator *dir_iterator;
34063415
struct object_id oid;
34073416
};
@@ -3423,8 +3432,9 @@ static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
34233432
if (ends_with(diter->basename, ".lock"))
34243433
continue;
34253434

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)) {
34283438
error("bad ref for %s", diter->path.buf);
34293439
continue;
34303440
}
@@ -3478,6 +3488,7 @@ static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_st
34783488
base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
34793489
files_reflog_path(refs, &sb, NULL);
34803490
iter->dir_iterator = dir_iterator_begin(sb.buf);
3491+
iter->ref_store = ref_store;
34813492
strbuf_release(&sb);
34823493
return ref_iterator;
34833494
}
@@ -3717,8 +3728,9 @@ static int lock_ref_for_update(struct files_ref_store *refs,
37173728
* the transaction, so we have to read it here
37183729
* to record and possibly check old_sha1:
37193730
*/
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)) {
37223734
if (update->flags & REF_HAVE_OLD) {
37233735
strbuf_addf(err, "cannot lock ref '%s': "
37243736
"error reading reference",
@@ -3872,8 +3884,9 @@ static int files_transaction_commit(struct ref_store *ref_store,
38723884
* head_ref within the transaction, then split_head_update()
38733885
* arranges for the reflog of HEAD to be updated, too.
38743886
*/
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);
38773890

38783891
if (head_ref && !(head_type & REF_ISSYMREF)) {
38793892
free(head_ref);
@@ -4046,7 +4059,8 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
40464059
* so here we really only check that none of the references
40474060
* that we are creating already exists.
40484061
*/
4049-
if (for_each_rawref(ref_present, &affected_refnames))
4062+
if (refs_for_each_rawref(&refs->base, ref_present,
4063+
&affected_refnames))
40504064
die("BUG: initial ref transaction called with existing refs");
40514065

40524066
for (i = 0; i < transaction->nr; i++) {
@@ -4165,7 +4179,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
41654179
strbuf_release(&err);
41664180
return -1;
41674181
}
4168-
if (!reflog_exists(refname)) {
4182+
if (!refs_reflog_exists(ref_store, refname)) {
41694183
unlock_ref(lock);
41704184
return 0;
41714185
}
@@ -4196,7 +4210,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
41964210
}
41974211

41984212
(*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);
42004214
(*cleanup_fn)(cb.policy_cb);
42014215

42024216
if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {

0 commit comments

Comments
 (0)