diff options
author | Nguyễn Thái Ngọc Duy <pclouds@gmail.com> | 2017-03-26 09:42:32 +0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-04-14 03:53:18 -0700 |
commit | 9e7ec634a130535982e9bc63d65c2fe8c076a662 (patch) | |
tree | ba46cac1a5efa78d3c0fb809f4d3396e25830ea9 /refs | |
parent | 5d0bc90e5de37c708f0856aef2c7b353a9d5030b (diff) | |
download | git-9e7ec634a130535982e9bc63d65c2fe8c076a662.tar.gz |
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>
Diffstat (limited to 'refs')
-rw-r--r-- | refs/files-backend.c | 86 | ||||
-rw-r--r-- | refs/refs-internal.h | 9 |
2 files changed, 62 insertions, 33 deletions
diff --git a/refs/files-backend.c b/refs/files-backend.c index 490f05a6f4..c90b47232e 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -916,6 +916,7 @@ struct packed_ref_cache { */ struct files_ref_store { struct ref_store base; + unsigned int store_flags; char *gitdir; char *gitcommondir; @@ -976,13 +977,15 @@ static void clear_loose_ref_cache(struct files_ref_store *refs) * Create a new submodule ref cache and add it to the internal * set of caches. */ -static struct ref_store *files_ref_store_create(const char *gitdir) +static struct ref_store *files_ref_store_create(const char *gitdir, + unsigned int flags) { struct files_ref_store *refs = xcalloc(1, sizeof(*refs)); struct ref_store *ref_store = (struct ref_store *)refs; struct strbuf sb = STRBUF_INIT; base_ref_store_init(ref_store, &refs_be_files); + refs->store_flags = flags; refs->gitdir = xstrdup(gitdir); get_common_dir_noenv(&sb, gitdir); @@ -994,24 +997,27 @@ static struct ref_store *files_ref_store_create(const char *gitdir) } /* - * Die if refs is for a submodule (i.e., not for the main repository). - * caller is used in any necessary error messages. + * Die if refs is not the main ref store. caller is used in any + * necessary error messages. */ static void files_assert_main_repository(struct files_ref_store *refs, const char *caller) { - /* This function is to be fixed up in the next patch */ + if (refs->store_flags & REF_STORE_MAIN) + return; + + die("BUG: operation %s only allowed for main ref store", caller); } /* * Downcast ref_store to files_ref_store. Die if ref_store is not a - * files_ref_store. If submodule_allowed is not true, then also die if - * files_ref_store is for a submodule (i.e., not for the main - * repository). caller is used in any necessary error messages. + * files_ref_store. required_flags is compared with ref_store's + * store_flags to ensure the ref_store has all required capabilities. + * "caller" is used in any necessary error messages. */ -static struct files_ref_store *files_downcast( - struct ref_store *ref_store, int submodule_allowed, - const char *caller) +static struct files_ref_store *files_downcast(struct ref_store *ref_store, + unsigned int required_flags, + const char *caller) { struct files_ref_store *refs; @@ -1021,8 +1027,9 @@ static struct files_ref_store *files_downcast( refs = (struct files_ref_store *)ref_store; - if (!submodule_allowed) - files_assert_main_repository(refs, caller); + if ((refs->store_flags & required_flags) != required_flags) + die("BUG: operation %s requires abilities 0x%x, but only have 0x%x", + caller, required_flags, refs->store_flags); return refs; } @@ -1398,7 +1405,7 @@ static int files_read_raw_ref(struct ref_store *ref_store, struct strbuf *referent, unsigned int *type) { struct files_ref_store *refs = - files_downcast(ref_store, 1, "read_raw_ref"); + files_downcast(ref_store, REF_STORE_READ, "read_raw_ref"); struct strbuf sb_contents = STRBUF_INIT; struct strbuf sb_path = STRBUF_INIT; const char *path; @@ -1815,10 +1822,14 @@ static enum peel_status peel_entry(struct ref_entry *entry, int repeel) static int files_peel_ref(struct ref_store *ref_store, const char *refname, unsigned char *sha1) { - struct files_ref_store *refs = files_downcast(ref_store, 0, "peel_ref"); + struct files_ref_store *refs = + files_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB, + "peel_ref"); int flag; unsigned char base[20]; + files_assert_main_repository(refs, "peel_ref"); + if (current_ref_iter && current_ref_iter->refname == refname) { struct object_id peeled; @@ -1923,8 +1934,7 @@ static struct ref_iterator *files_ref_iterator_begin( struct ref_store *ref_store, const char *prefix, unsigned int flags) { - struct files_ref_store *refs = - files_downcast(ref_store, 1, "ref_iterator_begin"); + struct files_ref_store *refs; struct ref_dir *loose_dir, *packed_dir; struct ref_iterator *loose_iter, *packed_iter; struct files_ref_iterator *iter; @@ -1935,6 +1945,10 @@ static struct ref_iterator *files_ref_iterator_begin( if (ref_paranoia) flags |= DO_FOR_EACH_INCLUDE_BROKEN; + refs = files_downcast(ref_store, + REF_STORE_READ | (ref_paranoia ? 0 : REF_STORE_ODB), + "ref_iterator_begin"); + iter = xcalloc(1, sizeof(*iter)); ref_iterator = &iter->base; base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable); @@ -2415,7 +2429,8 @@ static void prune_refs(struct ref_to_prune *r) static int files_pack_refs(struct ref_store *ref_store, unsigned int flags) { struct files_ref_store *refs = - files_downcast(ref_store, 0, "pack_refs"); + files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB, + "pack_refs"); struct pack_refs_cb_data cbdata; memset(&cbdata, 0, sizeof(cbdata)); @@ -2494,7 +2509,7 @@ static int files_delete_refs(struct ref_store *ref_store, struct string_list *refnames, unsigned int flags) { struct files_ref_store *refs = - files_downcast(ref_store, 0, "delete_refs"); + files_downcast(ref_store, REF_STORE_WRITE, "delete_refs"); struct strbuf err = STRBUF_INIT; int i, result = 0; @@ -2598,7 +2613,7 @@ static int files_verify_refname_available(struct ref_store *ref_store, struct strbuf *err) { struct files_ref_store *refs = - files_downcast(ref_store, 1, "verify_refname_available"); + files_downcast(ref_store, REF_STORE_READ, "verify_refname_available"); struct ref_dir *packed_refs = get_packed_refs(refs); struct ref_dir *loose_refs = get_loose_refs(refs); @@ -2623,7 +2638,7 @@ static int files_rename_ref(struct ref_store *ref_store, const char *logmsg) { struct files_ref_store *refs = - files_downcast(ref_store, 0, "rename_ref"); + files_downcast(ref_store, REF_STORE_WRITE, "rename_ref"); unsigned char sha1[20], orig_sha1[20]; int flag = 0, logmoved = 0; struct ref_lock *lock; @@ -2873,7 +2888,7 @@ static int files_create_reflog(struct ref_store *ref_store, struct strbuf *err) { struct files_ref_store *refs = - files_downcast(ref_store, 0, "create_reflog"); + files_downcast(ref_store, REF_STORE_WRITE, "create_reflog"); int fd; if (log_ref_setup(refs, refname, force_create, &fd, err)) @@ -3117,7 +3132,7 @@ static int files_create_symref(struct ref_store *ref_store, const char *logmsg) { struct files_ref_store *refs = - files_downcast(ref_store, 0, "create_symref"); + files_downcast(ref_store, REF_STORE_WRITE, "create_symref"); struct strbuf err = STRBUF_INIT; struct ref_lock *lock; int ret; @@ -3143,7 +3158,9 @@ int set_worktree_head_symref(const char *gitdir, const char *target, const char * backends. This function needs to die. */ struct files_ref_store *refs = - files_downcast(get_main_ref_store(), 0, "set_head_symref"); + files_downcast(get_main_ref_store(), + REF_STORE_WRITE, + "set_head_symref"); static struct lock_file head_lock; struct ref_lock *lock; @@ -3182,7 +3199,7 @@ static int files_reflog_exists(struct ref_store *ref_store, const char *refname) { struct files_ref_store *refs = - files_downcast(ref_store, 0, "reflog_exists"); + files_downcast(ref_store, REF_STORE_READ, "reflog_exists"); struct strbuf sb = STRBUF_INIT; struct stat st; int ret; @@ -3197,7 +3214,7 @@ static int files_delete_reflog(struct ref_store *ref_store, const char *refname) { struct files_ref_store *refs = - files_downcast(ref_store, 0, "delete_reflog"); + files_downcast(ref_store, REF_STORE_WRITE, "delete_reflog"); struct strbuf sb = STRBUF_INIT; int ret; @@ -3253,7 +3270,8 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store, void *cb_data) { struct files_ref_store *refs = - files_downcast(ref_store, 0, "for_each_reflog_ent_reverse"); + files_downcast(ref_store, REF_STORE_READ, + "for_each_reflog_ent_reverse"); struct strbuf sb = STRBUF_INIT; FILE *logfp; long pos; @@ -3361,7 +3379,8 @@ static int files_for_each_reflog_ent(struct ref_store *ref_store, each_reflog_ent_fn fn, void *cb_data) { struct files_ref_store *refs = - files_downcast(ref_store, 0, "for_each_reflog_ent"); + files_downcast(ref_store, REF_STORE_READ, + "for_each_reflog_ent"); FILE *logfp; struct strbuf sb = STRBUF_INIT; int ret = 0; @@ -3449,7 +3468,8 @@ static struct ref_iterator_vtable files_reflog_iterator_vtable = { static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store) { struct files_ref_store *refs = - files_downcast(ref_store, 0, "reflog_iterator_begin"); + files_downcast(ref_store, REF_STORE_READ, + "reflog_iterator_begin"); struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter)); struct ref_iterator *ref_iterator = &iter->base; struct strbuf sb = STRBUF_INIT; @@ -3787,7 +3807,8 @@ static int files_transaction_commit(struct ref_store *ref_store, struct strbuf *err) { struct files_ref_store *refs = - files_downcast(ref_store, 0, "ref_transaction_commit"); + files_downcast(ref_store, REF_STORE_WRITE, + "ref_transaction_commit"); int ret = 0, i; struct string_list refs_to_delete = STRING_LIST_INIT_NODUP; struct string_list_item *ref_to_delete; @@ -3992,7 +4013,8 @@ static int files_initial_transaction_commit(struct ref_store *ref_store, struct strbuf *err) { struct files_ref_store *refs = - files_downcast(ref_store, 0, "initial_ref_transaction_commit"); + files_downcast(ref_store, REF_STORE_WRITE, + "initial_ref_transaction_commit"); int ret = 0, i; struct string_list affected_refnames = STRING_LIST_INIT_NODUP; @@ -4114,7 +4136,7 @@ static int files_reflog_expire(struct ref_store *ref_store, void *policy_cb_data) { struct files_ref_store *refs = - files_downcast(ref_store, 0, "reflog_expire"); + files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire"); static struct lock_file reflog_lock; struct expire_reflog_cb cb; struct ref_lock *lock; @@ -4220,7 +4242,7 @@ static int files_reflog_expire(struct ref_store *ref_store, static int files_init_db(struct ref_store *ref_store, struct strbuf *err) { struct files_ref_store *refs = - files_downcast(ref_store, 0, "init_db"); + files_downcast(ref_store, REF_STORE_WRITE, "init_db"); struct strbuf sb = STRBUF_INIT; /* diff --git a/refs/refs-internal.h b/refs/refs-internal.h index dfa1817929..0cca280b5c 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -481,12 +481,19 @@ struct ref_store; /* refs backends */ +/* ref_store_init flags */ +#define REF_STORE_READ (1 << 0) +#define REF_STORE_WRITE (1 << 1) /* can perform update operations */ +#define REF_STORE_ODB (1 << 2) /* has access to object database */ +#define REF_STORE_MAIN (1 << 3) + /* * Initialize the ref_store for the specified gitdir. These functions * should call base_ref_store_init() to initialize the shared part of * the ref_store and to record the ref_store for later lookup. */ -typedef struct ref_store *ref_store_init_fn(const char *gitdir); +typedef struct ref_store *ref_store_init_fn(const char *gitdir, + unsigned int flags); typedef int ref_init_db_fn(struct ref_store *refs, struct strbuf *err); |