Skip to content

Commit 9e7ec63

Browse files
pcloudsgitster
authored andcommitted
files-backend: replace submodule_allowed check in files_downcast()
files-backend.c is unlearning submodules. Instead of having a specific check for submodules to see what operation is allowed, files backend now takes a set of flags at init. Each operation will check if the required flags is present before performing. For now we have four flags: read, write and odb access. Main ref store has all flags, obviously, while submodule stores are read-only and have access to odb (*). The "main" flag stays because many functions in the backend calls frontend ones without a ref store, so these functions always target the main ref store. Ideally the flag should be gone after ref-store-aware api is in place and used by backends. (*) Submodule code needs for_each_ref. Try take REF_STORE_ODB flag out. At least t3404 would fail. The "have access to odb" in submodule is a bit hacky since we don't know from he whether add_submodule_odb() has been called. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 5d0bc90 commit 9e7ec63

3 files changed

Lines changed: 73 additions & 37 deletions

File tree

refs.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,8 @@ static struct ref_store *lookup_submodule_ref_store(const char *submodule)
14161416
* Create, record, and return a ref_store instance for the specified
14171417
* gitdir.
14181418
*/
1419-
static struct ref_store *ref_store_init(const char *gitdir)
1419+
static struct ref_store *ref_store_init(const char *gitdir,
1420+
unsigned int flags)
14201421
{
14211422
const char *be_name = "files";
14221423
struct ref_storage_be *be = find_ref_storage_backend(be_name);
@@ -1425,7 +1426,7 @@ static struct ref_store *ref_store_init(const char *gitdir)
14251426
if (!be)
14261427
die("BUG: reference backend %s is unknown", be_name);
14271428

1428-
refs = be->init(gitdir);
1429+
refs = be->init(gitdir, flags);
14291430
return refs;
14301431
}
14311432

@@ -1434,7 +1435,11 @@ struct ref_store *get_main_ref_store(void)
14341435
if (main_ref_store)
14351436
return main_ref_store;
14361437

1437-
main_ref_store = ref_store_init(get_git_dir());
1438+
main_ref_store = ref_store_init(get_git_dir(),
1439+
(REF_STORE_READ |
1440+
REF_STORE_WRITE |
1441+
REF_STORE_ODB |
1442+
REF_STORE_MAIN));
14381443
return main_ref_store;
14391444
}
14401445

@@ -1481,7 +1486,9 @@ struct ref_store *get_ref_store(const char *submodule)
14811486
return NULL;
14821487
}
14831488

1484-
refs = ref_store_init(submodule_sb.buf);
1489+
/* assume that add_submodule_odb() has been called */
1490+
refs = ref_store_init(submodule_sb.buf,
1491+
REF_STORE_READ | REF_STORE_ODB);
14851492
register_submodule_ref_store(refs, submodule);
14861493

14871494
strbuf_release(&submodule_sb);

refs/files-backend.c

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ struct packed_ref_cache {
916916
*/
917917
struct files_ref_store {
918918
struct ref_store base;
919+
unsigned int store_flags;
919920

920921
char *gitdir;
921922
char *gitcommondir;
@@ -976,13 +977,15 @@ static void clear_loose_ref_cache(struct files_ref_store *refs)
976977
* Create a new submodule ref cache and add it to the internal
977978
* set of caches.
978979
*/
979-
static struct ref_store *files_ref_store_create(const char *gitdir)
980+
static struct ref_store *files_ref_store_create(const char *gitdir,
981+
unsigned int flags)
980982
{
981983
struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
982984
struct ref_store *ref_store = (struct ref_store *)refs;
983985
struct strbuf sb = STRBUF_INIT;
984986

985987
base_ref_store_init(ref_store, &refs_be_files);
988+
refs->store_flags = flags;
986989

987990
refs->gitdir = xstrdup(gitdir);
988991
get_common_dir_noenv(&sb, gitdir);
@@ -994,24 +997,27 @@ static struct ref_store *files_ref_store_create(const char *gitdir)
994997
}
995998

996999
/*
997-
* Die if refs is for a submodule (i.e., not for the main repository).
998-
* caller is used in any necessary error messages.
1000+
* Die if refs is not the main ref store. caller is used in any
1001+
* necessary error messages.
9991002
*/
10001003
static void files_assert_main_repository(struct files_ref_store *refs,
10011004
const char *caller)
10021005
{
1003-
/* This function is to be fixed up in the next patch */
1006+
if (refs->store_flags & REF_STORE_MAIN)
1007+
return;
1008+
1009+
die("BUG: operation %s only allowed for main ref store", caller);
10041010
}
10051011

10061012
/*
10071013
* Downcast ref_store to files_ref_store. Die if ref_store is not a
1008-
* files_ref_store. If submodule_allowed is not true, then also die if
1009-
* files_ref_store is for a submodule (i.e., not for the main
1010-
* repository). caller is used in any necessary error messages.
1014+
* files_ref_store. required_flags is compared with ref_store's
1015+
* store_flags to ensure the ref_store has all required capabilities.
1016+
* "caller" is used in any necessary error messages.
10111017
*/
1012-
static struct files_ref_store *files_downcast(
1013-
struct ref_store *ref_store, int submodule_allowed,
1014-
const char *caller)
1018+
static struct files_ref_store *files_downcast(struct ref_store *ref_store,
1019+
unsigned int required_flags,
1020+
const char *caller)
10151021
{
10161022
struct files_ref_store *refs;
10171023

@@ -1021,8 +1027,9 @@ static struct files_ref_store *files_downcast(
10211027

10221028
refs = (struct files_ref_store *)ref_store;
10231029

1024-
if (!submodule_allowed)
1025-
files_assert_main_repository(refs, caller);
1030+
if ((refs->store_flags & required_flags) != required_flags)
1031+
die("BUG: operation %s requires abilities 0x%x, but only have 0x%x",
1032+
caller, required_flags, refs->store_flags);
10261033

10271034
return refs;
10281035
}
@@ -1398,7 +1405,7 @@ static int files_read_raw_ref(struct ref_store *ref_store,
13981405
struct strbuf *referent, unsigned int *type)
13991406
{
14001407
struct files_ref_store *refs =
1401-
files_downcast(ref_store, 1, "read_raw_ref");
1408+
files_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
14021409
struct strbuf sb_contents = STRBUF_INIT;
14031410
struct strbuf sb_path = STRBUF_INIT;
14041411
const char *path;
@@ -1815,10 +1822,14 @@ static enum peel_status peel_entry(struct ref_entry *entry, int repeel)
18151822
static int files_peel_ref(struct ref_store *ref_store,
18161823
const char *refname, unsigned char *sha1)
18171824
{
1818-
struct files_ref_store *refs = files_downcast(ref_store, 0, "peel_ref");
1825+
struct files_ref_store *refs =
1826+
files_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
1827+
"peel_ref");
18191828
int flag;
18201829
unsigned char base[20];
18211830

1831+
files_assert_main_repository(refs, "peel_ref");
1832+
18221833
if (current_ref_iter && current_ref_iter->refname == refname) {
18231834
struct object_id peeled;
18241835

@@ -1923,8 +1934,7 @@ static struct ref_iterator *files_ref_iterator_begin(
19231934
struct ref_store *ref_store,
19241935
const char *prefix, unsigned int flags)
19251936
{
1926-
struct files_ref_store *refs =
1927-
files_downcast(ref_store, 1, "ref_iterator_begin");
1937+
struct files_ref_store *refs;
19281938
struct ref_dir *loose_dir, *packed_dir;
19291939
struct ref_iterator *loose_iter, *packed_iter;
19301940
struct files_ref_iterator *iter;
@@ -1935,6 +1945,10 @@ static struct ref_iterator *files_ref_iterator_begin(
19351945
if (ref_paranoia)
19361946
flags |= DO_FOR_EACH_INCLUDE_BROKEN;
19371947

1948+
refs = files_downcast(ref_store,
1949+
REF_STORE_READ | (ref_paranoia ? 0 : REF_STORE_ODB),
1950+
"ref_iterator_begin");
1951+
19381952
iter = xcalloc(1, sizeof(*iter));
19391953
ref_iterator = &iter->base;
19401954
base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);
@@ -2415,7 +2429,8 @@ static void prune_refs(struct ref_to_prune *r)
24152429
static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
24162430
{
24172431
struct files_ref_store *refs =
2418-
files_downcast(ref_store, 0, "pack_refs");
2432+
files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
2433+
"pack_refs");
24192434
struct pack_refs_cb_data cbdata;
24202435

24212436
memset(&cbdata, 0, sizeof(cbdata));
@@ -2494,7 +2509,7 @@ static int files_delete_refs(struct ref_store *ref_store,
24942509
struct string_list *refnames, unsigned int flags)
24952510
{
24962511
struct files_ref_store *refs =
2497-
files_downcast(ref_store, 0, "delete_refs");
2512+
files_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
24982513
struct strbuf err = STRBUF_INIT;
24992514
int i, result = 0;
25002515

@@ -2598,7 +2613,7 @@ static int files_verify_refname_available(struct ref_store *ref_store,
25982613
struct strbuf *err)
25992614
{
26002615
struct files_ref_store *refs =
2601-
files_downcast(ref_store, 1, "verify_refname_available");
2616+
files_downcast(ref_store, REF_STORE_READ, "verify_refname_available");
26022617
struct ref_dir *packed_refs = get_packed_refs(refs);
26032618
struct ref_dir *loose_refs = get_loose_refs(refs);
26042619

@@ -2623,7 +2638,7 @@ static int files_rename_ref(struct ref_store *ref_store,
26232638
const char *logmsg)
26242639
{
26252640
struct files_ref_store *refs =
2626-
files_downcast(ref_store, 0, "rename_ref");
2641+
files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
26272642
unsigned char sha1[20], orig_sha1[20];
26282643
int flag = 0, logmoved = 0;
26292644
struct ref_lock *lock;
@@ -2873,7 +2888,7 @@ static int files_create_reflog(struct ref_store *ref_store,
28732888
struct strbuf *err)
28742889
{
28752890
struct files_ref_store *refs =
2876-
files_downcast(ref_store, 0, "create_reflog");
2891+
files_downcast(ref_store, REF_STORE_WRITE, "create_reflog");
28772892
int fd;
28782893

28792894
if (log_ref_setup(refs, refname, force_create, &fd, err))
@@ -3117,7 +3132,7 @@ static int files_create_symref(struct ref_store *ref_store,
31173132
const char *logmsg)
31183133
{
31193134
struct files_ref_store *refs =
3120-
files_downcast(ref_store, 0, "create_symref");
3135+
files_downcast(ref_store, REF_STORE_WRITE, "create_symref");
31213136
struct strbuf err = STRBUF_INIT;
31223137
struct ref_lock *lock;
31233138
int ret;
@@ -3143,7 +3158,9 @@ int set_worktree_head_symref(const char *gitdir, const char *target, const char
31433158
* backends. This function needs to die.
31443159
*/
31453160
struct files_ref_store *refs =
3146-
files_downcast(get_main_ref_store(), 0, "set_head_symref");
3161+
files_downcast(get_main_ref_store(),
3162+
REF_STORE_WRITE,
3163+
"set_head_symref");
31473164

31483165
static struct lock_file head_lock;
31493166
struct ref_lock *lock;
@@ -3182,7 +3199,7 @@ static int files_reflog_exists(struct ref_store *ref_store,
31823199
const char *refname)
31833200
{
31843201
struct files_ref_store *refs =
3185-
files_downcast(ref_store, 0, "reflog_exists");
3202+
files_downcast(ref_store, REF_STORE_READ, "reflog_exists");
31863203
struct strbuf sb = STRBUF_INIT;
31873204
struct stat st;
31883205
int ret;
@@ -3197,7 +3214,7 @@ static int files_delete_reflog(struct ref_store *ref_store,
31973214
const char *refname)
31983215
{
31993216
struct files_ref_store *refs =
3200-
files_downcast(ref_store, 0, "delete_reflog");
3217+
files_downcast(ref_store, REF_STORE_WRITE, "delete_reflog");
32013218
struct strbuf sb = STRBUF_INIT;
32023219
int ret;
32033220

@@ -3253,7 +3270,8 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
32533270
void *cb_data)
32543271
{
32553272
struct files_ref_store *refs =
3256-
files_downcast(ref_store, 0, "for_each_reflog_ent_reverse");
3273+
files_downcast(ref_store, REF_STORE_READ,
3274+
"for_each_reflog_ent_reverse");
32573275
struct strbuf sb = STRBUF_INIT;
32583276
FILE *logfp;
32593277
long pos;
@@ -3361,7 +3379,8 @@ static int files_for_each_reflog_ent(struct ref_store *ref_store,
33613379
each_reflog_ent_fn fn, void *cb_data)
33623380
{
33633381
struct files_ref_store *refs =
3364-
files_downcast(ref_store, 0, "for_each_reflog_ent");
3382+
files_downcast(ref_store, REF_STORE_READ,
3383+
"for_each_reflog_ent");
33653384
FILE *logfp;
33663385
struct strbuf sb = STRBUF_INIT;
33673386
int ret = 0;
@@ -3449,7 +3468,8 @@ static struct ref_iterator_vtable files_reflog_iterator_vtable = {
34493468
static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)
34503469
{
34513470
struct files_ref_store *refs =
3452-
files_downcast(ref_store, 0, "reflog_iterator_begin");
3471+
files_downcast(ref_store, REF_STORE_READ,
3472+
"reflog_iterator_begin");
34533473
struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
34543474
struct ref_iterator *ref_iterator = &iter->base;
34553475
struct strbuf sb = STRBUF_INIT;
@@ -3787,7 +3807,8 @@ static int files_transaction_commit(struct ref_store *ref_store,
37873807
struct strbuf *err)
37883808
{
37893809
struct files_ref_store *refs =
3790-
files_downcast(ref_store, 0, "ref_transaction_commit");
3810+
files_downcast(ref_store, REF_STORE_WRITE,
3811+
"ref_transaction_commit");
37913812
int ret = 0, i;
37923813
struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;
37933814
struct string_list_item *ref_to_delete;
@@ -3992,7 +4013,8 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
39924013
struct strbuf *err)
39934014
{
39944015
struct files_ref_store *refs =
3995-
files_downcast(ref_store, 0, "initial_ref_transaction_commit");
4016+
files_downcast(ref_store, REF_STORE_WRITE,
4017+
"initial_ref_transaction_commit");
39964018
int ret = 0, i;
39974019
struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
39984020

@@ -4114,7 +4136,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
41144136
void *policy_cb_data)
41154137
{
41164138
struct files_ref_store *refs =
4117-
files_downcast(ref_store, 0, "reflog_expire");
4139+
files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire");
41184140
static struct lock_file reflog_lock;
41194141
struct expire_reflog_cb cb;
41204142
struct ref_lock *lock;
@@ -4220,7 +4242,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
42204242
static int files_init_db(struct ref_store *ref_store, struct strbuf *err)
42214243
{
42224244
struct files_ref_store *refs =
4223-
files_downcast(ref_store, 0, "init_db");
4245+
files_downcast(ref_store, REF_STORE_WRITE, "init_db");
42244246
struct strbuf sb = STRBUF_INIT;
42254247

42264248
/*

refs/refs-internal.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,19 @@ struct ref_store;
481481

482482
/* refs backends */
483483

484+
/* ref_store_init flags */
485+
#define REF_STORE_READ (1 << 0)
486+
#define REF_STORE_WRITE (1 << 1) /* can perform update operations */
487+
#define REF_STORE_ODB (1 << 2) /* has access to object database */
488+
#define REF_STORE_MAIN (1 << 3)
489+
484490
/*
485491
* Initialize the ref_store for the specified gitdir. These functions
486492
* should call base_ref_store_init() to initialize the shared part of
487493
* the ref_store and to record the ref_store for later lookup.
488494
*/
489-
typedef struct ref_store *ref_store_init_fn(const char *gitdir);
495+
typedef struct ref_store *ref_store_init_fn(const char *gitdir,
496+
unsigned int flags);
490497

491498
typedef int ref_init_db_fn(struct ref_store *refs, struct strbuf *err);
492499

0 commit comments

Comments
 (0)