diff --git a/builtin/refs.c b/builtin/refs.c index 3064f888b24304ce07dba107be137ad8d420e731..97f1d748acfb6dc18bcd40192a194a8091d1f037 100644 --- a/builtin/refs.c +++ b/builtin/refs.c @@ -1,4 +1,3 @@ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "fsck.h" @@ -23,7 +22,7 @@ N_("git refs optimize " PACK_REFS_OPTS) static int cmd_refs_migrate(int argc, const char **argv, const char *prefix, - struct repository *repo UNUSED) + struct repository *repo) { const char * const migrate_usage[] = { REFS_MIGRATE_USAGE, @@ -59,13 +58,13 @@ static int cmd_refs_migrate(int argc, const char **argv, const char *prefix, goto out; } - if (the_repository->ref_storage_format == format) { + if (repo->ref_storage_format == format) { err = error(_("repository already uses '%s' format"), ref_storage_format_to_name(format)); goto out; } - if (repo_migrate_ref_storage_format(the_repository, format, flags, &errbuf) < 0) { + if (repo_migrate_ref_storage_format(repo, format, flags, &errbuf) < 0) { err = error("%s", errbuf.buf); goto out; } @@ -78,7 +77,7 @@ static int cmd_refs_migrate(int argc, const char **argv, const char *prefix, } static int cmd_refs_verify(int argc, const char **argv, const char *prefix, - struct repository *repo UNUSED) + struct repository *repo) { struct fsck_options fsck_refs_options = FSCK_REFS_OPTIONS_DEFAULT; struct worktree **worktrees; @@ -97,8 +96,8 @@ static int cmd_refs_verify(int argc, const char **argv, const char *prefix, if (argc) usage(_("'git refs verify' takes no arguments")); - repo_config(the_repository, git_fsck_config, &fsck_refs_options); - prepare_repo_settings(the_repository); + repo_config(repo, git_fsck_config, &fsck_refs_options); + prepare_repo_settings(repo); worktrees = get_worktrees_without_reading_head(); for (size_t i = 0; worktrees[i]; i++) @@ -122,7 +121,7 @@ static int cmd_refs_list(int argc, const char **argv, const char *prefix, } static int cmd_refs_exists(int argc, const char **argv, const char *prefix, - struct repository *repo UNUSED) + struct repository *repo) { struct strbuf unused_referent = STRBUF_INIT; struct object_id unused_oid; @@ -143,7 +142,7 @@ static int cmd_refs_exists(int argc, const char **argv, const char *prefix, die(_("'git refs exists' requires a reference")); ref = *argv++; - if (refs_read_raw_ref(get_main_ref_store(the_repository), ref, + if (refs_read_raw_ref(get_main_ref_store(repo), ref, &unused_oid, &unused_referent, &unused_type, &failure_errno)) { if (failure_errno == ENOENT || failure_errno == EISDIR) { diff --git a/environment.h b/environment.h index 51898c99cd1e451a47c1a4aae32869cfbddbce45..ac6a3905df9e8438c54cb9821909b5194e8f9676 100644 --- a/environment.h +++ b/environment.h @@ -42,6 +42,8 @@ #define GIT_OPTIONAL_LOCKS_ENVIRONMENT "GIT_OPTIONAL_LOCKS" #define GIT_TEXT_DOMAIN_DIR_ENVIRONMENT "GIT_TEXTDOMAINDIR" #define GIT_ATTR_SOURCE_ENVIRONMENT "GIT_ATTR_SOURCE" +#define GIT_REF_DIRECTORY_ENVIRONMENT "GIT_REF_DIRECTORY" +#define GIT_REF_FORMAT_ENVIRONMENT "GIT_REF_FORMAT" /* * Environment variable used to propagate the --no-advice global option to the diff --git a/refs.c b/refs.c index 965381367e0e53914040d77bea12975354c8b854..a775c644c4bfc7b72548c47e16a458558085872d 100644 --- a/refs.c +++ b/refs.c @@ -2177,6 +2177,14 @@ void ref_store_release(struct ref_store *ref_store) free(ref_store->gitdir); } +struct ref_store *get_ref_store_for_dir(struct repository *r, char *dir, + enum ref_storage_format format) +{ + struct ref_store *ref_store = ref_store_init(r, format, dir, + REF_STORE_ALL_CAPS); + return maybe_debug_wrap_ref_store(dir, ref_store); +} + struct ref_store *get_main_ref_store(struct repository *r) { if (r->refs_private) @@ -2185,9 +2193,7 @@ struct ref_store *get_main_ref_store(struct repository *r) if (!r->gitdir) BUG("attempting to get main_ref_store outside of repository"); - r->refs_private = ref_store_init(r, r->ref_storage_format, - r->gitdir, REF_STORE_ALL_CAPS); - r->refs_private = maybe_debug_wrap_ref_store(r->gitdir, r->refs_private); + r->refs_private = get_ref_store_for_dir(r, r->gitdir, r->ref_storage_format); return r->refs_private; } diff --git a/refs.h b/refs.h index 4e6bd63aa86c540734e488027e0ff4af7149ad61..f96522fe688ad4daf7da838e8557c44df75dc607 100644 --- a/refs.h +++ b/refs.h @@ -1066,6 +1066,8 @@ int refs_reflog_expire(struct ref_store *refs, reflog_expiry_cleanup_fn cleanup_fn, void *policy_cb_data); +struct ref_store *get_ref_store_for_dir(struct repository *r, char *dir, + enum ref_storage_format format); struct ref_store *get_main_ref_store(struct repository *r); /** diff --git a/repository.c b/repository.c index 6faf5c73981ebf6c520919a5475f0243f08cea87..6a4463238a2b7195ab246e64747096ee53020c0e 100644 --- a/repository.c +++ b/repository.c @@ -177,6 +177,17 @@ void repo_set_gitdir(struct repository *repo, repo->objects->sources->disable_ref_updates = o->disable_ref_updates; + if ((o->ref_dir != NULL) ^ (o->ref_format != NULL)) + BUG("both reference directory and reference format must be set"); + + if (o->ref_dir != NULL && o->ref_format != NULL) { + enum ref_storage_format format = ref_storage_format_by_name(o->ref_format); + if (format == REF_STORAGE_FORMAT_UNKNOWN) + BUG("unknown ref storage format '%s'", o->ref_format); + + repo->refs_private = get_ref_store_for_dir(repo, (char *)o->ref_dir, format); + } + free(repo->objects->alternate_db); repo->objects->alternate_db = xstrdup_or_null(o->alternate_db); expand_base_dir(&repo->graft_file, o->graft_file, diff --git a/repository.h b/repository.h index 5808a5d610846a0e42233f66e56dcbcebbd3ecd0..28c7dcd4c29ca3f38568b219a5bbdbdcdc1f69d1 100644 --- a/repository.h +++ b/repository.h @@ -187,6 +187,8 @@ struct set_gitdir_args { const char *graft_file; const char *index_file; const char *alternate_db; + const char *ref_dir; + const char *ref_format; int disable_ref_updates; }; diff --git a/setup.c b/setup.c index 7086741e6c2d1f5c73e484b76e1a1272f7d0697d..198538b1dd1b7262b7d5dfedf98af7f498ce9b55 100644 --- a/setup.c +++ b/setup.c @@ -1641,6 +1641,8 @@ void setup_git_env(const char *git_dir) args.graft_file = getenv_safe(&to_free, GRAFT_ENVIRONMENT); args.index_file = getenv_safe(&to_free, INDEX_ENVIRONMENT); args.alternate_db = getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT); + args.ref_dir = getenv_safe(&to_free, GIT_REF_DIRECTORY_ENVIRONMENT); + args.ref_format = getenv_safe(&to_free, GIT_REF_FORMAT_ENVIRONMENT); if (getenv(GIT_QUARANTINE_ENVIRONMENT)) { args.disable_ref_updates = 1; }