]> git.ipfire.org Git - thirdparty/git.git/commitdiff
environment: make `get_git_dir()` accept a repository
authorPatrick Steinhardt <ps@pks.im>
Thu, 12 Sep 2024 11:29:24 +0000 (13:29 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 12 Sep 2024 17:15:39 +0000 (10:15 -0700)
The `get_git_dir()` function retrieves the path to the Git directory for
`the_repository`. Make it accept a `struct repository` such that it can
work on arbitrary repositories and make it part of the repository
subsystem. This reduces our reliance on `the_repository` and clarifies
scope.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
21 files changed:
apply.c
builtin/am.c
builtin/commit.c
builtin/config.c
builtin/difftool.c
builtin/fsmonitor--daemon.c
builtin/merge.c
builtin/stash.c
cache-tree.c
config.c
environment.c
environment.h
pathspec.c
read-cache.c
repository.c
repository.h
setup.c
setup.h
trace.c
transport-helper.c
worktree.c

diff --git a/apply.c b/apply.c
index 6e1060a952c31769eb7294d8b93e225f53278c78..426cec32f8fc7f00b341d50a87ca6490b50d64a5 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -30,6 +30,7 @@
 #include "path.h"
 #include "quote.h"
 #include "read-cache.h"
+#include "repository.h"
 #include "rerere.h"
 #include "apply.h"
 #include "entry.h"
@@ -4111,7 +4112,7 @@ static int read_apply_cache(struct apply_state *state)
 {
        if (state->index_file)
                return read_index_from(state->repo->index, state->index_file,
-                                      get_git_dir());
+                                      repo_get_git_dir(the_repository));
        else
                return repo_read_index(state->repo);
 }
index d8875ad40229c1e33078f26f7e794d86a00ee741..405214e242aeccc7eaa4ef94b80803a502d1b1fc 100644 (file)
@@ -1544,7 +1544,8 @@ static int run_apply(const struct am_state *state, const char *index_file)
        if (index_file) {
                /* Reload index as apply_all_patches() will have modified it. */
                discard_index(the_repository->index);
-               read_index_from(the_repository->index, index_file, get_git_dir());
+               read_index_from(the_repository->index, index_file,
+                               repo_get_git_dir(the_repository));
        }
 
        return 0;
@@ -1587,7 +1588,7 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
                return error("could not build fake ancestor");
 
        discard_index(the_repository->index);
-       read_index_from(the_repository->index, index_path, get_git_dir());
+       read_index_from(the_repository->index, index_path, repo_get_git_dir(the_repository));
 
        if (write_index_as_tree(&bases[0], the_repository->index, index_path, 0, NULL))
                return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
index b2033c488778ff79c990c23a95f28d10898e2874..a1c1d16a099f635fc5797811d7e737122af7bcb5 100644 (file)
@@ -26,6 +26,7 @@
 #include "path.h"
 #include "preload-index.h"
 #include "read-cache.h"
+#include "repository.h"
 #include "string-list.h"
 #include "rerere.h"
 #include "unpack-trees.h"
@@ -407,7 +408,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
 
                discard_index(the_repository->index);
                read_index_from(the_repository->index, get_lock_file_path(&index_lock),
-                               get_git_dir());
+                               repo_get_git_dir(the_repository));
                if (cache_tree_update(the_repository->index, WRITE_TREE_SILENT) == 0) {
                        if (reopen_lock_file(&index_lock) < 0)
                                die(_("unable to write index file"));
@@ -534,7 +535,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
 
        discard_index(the_repository->index);
        ret = get_lock_file_path(&false_lock);
-       read_index_from(the_repository->index, ret, get_git_dir());
+       read_index_from(the_repository->index, ret, repo_get_git_dir(the_repository));
 out:
        string_list_clear(&partial, 0);
        clear_pathspec(&pathspec);
@@ -1072,7 +1073,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
                 */
                discard_index(the_repository->index);
        }
-       read_index_from(the_repository->index, index_file, get_git_dir());
+       read_index_from(the_repository->index, index_file, repo_get_git_dir(the_repository));
 
        if (cache_tree_update(the_repository->index, 0)) {
                error(_("Error building trees"));
index e00d983596b8db413ab7eba457bbbaafb26c5f33..c10697a2efb342bde24d006d7c1f383b4c24e0f2 100644 (file)
@@ -808,7 +808,7 @@ static void location_options_init(struct config_location_options *opts,
                opts->options.respect_includes = opts->respect_includes_opt;
        if (startup_info->have_repository) {
                opts->options.commondir = get_git_common_dir();
-               opts->options.git_dir = get_git_dir();
+               opts->options.git_dir = repo_get_git_dir(the_repository);
        }
 }
 
index dcc68e190c25b47738bb7b59bfc8648339e810cb..8c59411e6e0f8aa97ded3a15c8ce233627648cea 100644 (file)
@@ -22,6 +22,7 @@
 #include "hex.h"
 #include "parse-options.h"
 #include "read-cache-ll.h"
+#include "repository.h"
 #include "sparse-index.h"
 #include "strvec.h"
 #include "strbuf.h"
@@ -214,7 +215,7 @@ static void changed_files(struct hashmap *result, const char *index_path,
        struct child_process update_index = CHILD_PROCESS_INIT;
        struct child_process diff_files = CHILD_PROCESS_INIT;
        struct strbuf buf = STRBUF_INIT;
-       const char *git_dir = absolute_path(get_git_dir());
+       const char *git_dir = absolute_path(repo_get_git_dir(the_repository));
        FILE *fp;
 
        strvec_pushl(&update_index.args,
@@ -737,7 +738,7 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
 
        if (!no_index){
                setup_work_tree();
-               setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
+               setenv(GIT_DIR_ENVIRONMENT, absolute_path(repo_get_git_dir(the_repository)), 1);
                setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
        } else if (dir_diff)
                die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");
index 1593713f4cb29fe8e608f4c3c7502fa5a624d1f9..c54e736716a19036c38b1a20956a075367ed9c78 100644 (file)
@@ -1311,7 +1311,8 @@ static int fsmonitor_run_daemon(void)
        strbuf_addstr(&state.path_gitdir_watch, "/.git");
        if (!is_directory(state.path_gitdir_watch.buf)) {
                strbuf_reset(&state.path_gitdir_watch);
-               strbuf_addstr(&state.path_gitdir_watch, absolute_path(get_git_dir()));
+               strbuf_addstr(&state.path_gitdir_watch,
+                             absolute_path(repo_get_git_dir(the_repository)));
                state.nr_paths_watching = 2;
        }
 
index 662a49a0e8c303f763012180df75cb6150880f60..a2bae0700b4725f28c70fea9cf6e88dc0b569861 100644 (file)
@@ -17,6 +17,7 @@
 #include "object-name.h"
 #include "parse-options.h"
 #include "lockfile.h"
+#include "repository.h"
 #include "run-command.h"
 #include "hook.h"
 #include "diff.h"
@@ -855,7 +856,8 @@ static void prepare_to_commit(struct commit_list *remoteheads)
                if (invoked_hook)
                        discard_index(the_repository->index);
        }
-       read_index_from(the_repository->index, index_file, get_git_dir());
+       read_index_from(the_repository->index, index_file,
+                       repo_get_git_dir(the_repository));
        strbuf_addbuf(&msg, &merge_msg);
        if (squash)
                BUG("the control must not reach here under --squash");
index fcfd97972a487defda3e5279dbb7a7e89f8bcc0f..ad6bcefb77097462dca0d94fd43e4742a15b070d 100644 (file)
@@ -19,6 +19,7 @@
 #include "entry.h"
 #include "preload-index.h"
 #include "read-cache.h"
+#include "repository.h"
 #include "rerere.h"
 #include "revision.h"
 #include "setup.h"
@@ -642,7 +643,7 @@ restore_untracked:
                strvec_pushf(&cp.env, GIT_WORK_TREE_ENVIRONMENT"=%s",
                             absolute_path(get_git_work_tree()));
                strvec_pushf(&cp.env, GIT_DIR_ENVIRONMENT"=%s",
-                            absolute_path(get_git_dir()));
+                            absolute_path(repo_get_git_dir(the_repository)));
                strvec_push(&cp.args, "status");
                run_command(&cp);
        }
index 50610c3f3cb8e7d3c7b84a513286590201d6f99a..b482167a69ae6acaee05a500850a529e6ae425f9 100644 (file)
@@ -1,7 +1,6 @@
 #define USE_THE_REPOSITORY_VARIABLE
 
 #include "git-compat-util.h"
-#include "environment.h"
 #include "hex.h"
 #include "lockfile.h"
 #include "tree.h"
@@ -12,6 +11,7 @@
 #include "object-store-ll.h"
 #include "read-cache-ll.h"
 #include "replace-object.h"
+#include "repository.h"
 #include "promisor-remote.h"
 #include "trace.h"
 #include "trace2.h"
@@ -725,7 +725,8 @@ int write_index_as_tree(struct object_id *oid, struct index_state *index_state,
 
        hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
 
-       entries = read_index_from(index_state, index_path, get_git_dir());
+       entries = read_index_from(index_state, index_path,
+                                 repo_get_git_dir(the_repository));
        if (entries < 0) {
                ret = WRITE_TREE_UNREADABLE_INDEX;
                goto out;
index 56b5862e59d51411935c399c23cf126005b6b103..1733ba85dcd02d5f2a56336a4d0903ad70dad9ef 100644 (file)
--- a/config.c
+++ b/config.c
@@ -6,6 +6,8 @@
  *
  */
 
+#define USE_THE_REPOSITORY_VARIABLE
+
 #include "git-compat-util.h"
 #include "abspath.h"
 #include "advice.h"
@@ -2212,7 +2214,7 @@ void read_early_config(config_fn_t cb, void *data)
 
        if (have_git_dir()) {
                opts.commondir = get_git_common_dir();
-               opts.git_dir = get_git_dir();
+               opts.git_dir = repo_get_git_dir(the_repository);
        /*
         * When setup_git_directory() was not yet asked to discover the
         * GIT_DIR, we ask discover_git_directory() to figure out whether there
index 1d6c48b52dfc05fe10783c9b252ffe138f5173d8..040b1ff1ba803e1f428bc440f066b3807ce892b4 100644 (file)
@@ -228,13 +228,6 @@ int have_git_dir(void)
                || the_repository->gitdir;
 }
 
-const char *get_git_dir(void)
-{
-       if (!the_repository->gitdir)
-               BUG("git environment hasn't been setup");
-       return the_repository->gitdir;
-}
-
 const char *get_git_common_dir(void)
 {
        if (!the_repository->commondir)
@@ -352,7 +345,8 @@ static void update_relative_gitdir(const char *name UNUSED,
                                   const char *new_cwd,
                                   void *data UNUSED)
 {
-       char *path = reparent_relative_path(old_cwd, new_cwd, get_git_dir());
+       char *path = reparent_relative_path(old_cwd, new_cwd,
+                                           repo_get_git_dir(the_repository));
        struct tmp_objdir *tmp_objdir = tmp_objdir_unapply_primary_odb();
 
        trace_printf_key(&trace_setup_key,
index 0148738ed63f987d81747afe9483097ecbf3fc8e..06d37d5c82b3aa9f01e85174b71464fcf0428502 100644 (file)
@@ -106,7 +106,6 @@ int have_git_dir(void);
 extern int is_bare_repository_cfg;
 int is_bare_repository(void);
 extern char *git_work_tree_cfg;
-const char *get_git_dir(void);
 const char *get_git_common_dir(void);
 const char *get_object_directory(void);
 char *get_index_file(void);
index fe1f0f41af081f5e171cff399a561a5bf798168e..416fe1e3dcce6616a272e9ef42ef85cb5cc74a02 100644 (file)
@@ -497,7 +497,7 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
                                    copyfrom);
                        hint_path = get_git_work_tree();
                        if (!hint_path)
-                               hint_path = get_git_dir();
+                               hint_path = repo_get_git_dir(the_repository);
                        die(_("%s: '%s' is outside repository at '%s'"), elt,
                            copyfrom, absolute_path(hint_path));
                }
index 4e67dc182e7467708f3d6195b642523b0f9a5d01..764fdfec46582a44eaf2d95558346c05275558cf 100644 (file)
@@ -31,6 +31,7 @@
 #include "path.h"
 #include "preload-index.h"
 #include "read-cache.h"
+#include "repository.h"
 #include "resolve-undo.h"
 #include "revision.h"
 #include "strbuf.h"
@@ -3238,10 +3239,11 @@ static int should_delete_shared_index(const char *shared_index_path)
 static int clean_shared_index_files(const char *current_hex)
 {
        struct dirent *de;
-       DIR *dir = opendir(get_git_dir());
+       DIR *dir = opendir(repo_get_git_dir(the_repository));
 
        if (!dir)
-               return error_errno(_("unable to open git dir: %s"), get_git_dir());
+               return error_errno(_("unable to open git dir: %s"),
+                                  repo_get_git_dir(the_repository));
 
        while ((de = readdir(dir)) != NULL) {
                const char *sha1_hex;
index 9825a3089932ede3f5dc588874d618768988c283..31afc62551ecc53849f7fc6458f4016a365e9677 100644 (file)
@@ -91,6 +91,13 @@ static void expand_base_dir(char **out, const char *in,
                *out = xstrfmt("%s/%s", base_dir, def_in);
 }
 
+const char *repo_get_git_dir(struct repository *repo)
+{
+       if (!repo->gitdir)
+               BUG("repository hasn't been set up");
+       return repo->gitdir;
+}
+
 static void repo_set_commondir(struct repository *repo,
                               const char *commondir)
 {
index af6ea0a62cdb70a37c938e6109fa8ce4c8d61749..cf2172c0aa57f1bcfa13b3288bdc26bb6e092352 100644 (file)
@@ -206,6 +206,8 @@ struct repository {
 extern struct repository *the_repository;
 #endif
 
+const char *repo_get_git_dir(struct repository *repo);
+
 /*
  * Define a custom repository layout. Any field can be NULL, which
  * will default back to the path according to the default layout.
diff --git a/setup.c b/setup.c
index 29f86739212eb7fdcbd771d4228737dc4748f0d6..4a9c60922e7615ea81945c215fb4f8764e1afced 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -149,7 +149,7 @@ char *prefix_path(const char *prefix, int len, const char *path)
        if (!r) {
                const char *hint_path = get_git_work_tree();
                if (!hint_path)
-                       hint_path = get_git_dir();
+                       hint_path = repo_get_git_dir(the_repository);
                die(_("'%s' is outside repository at '%s'"), path,
                    absolute_path(hint_path));
        }
@@ -468,7 +468,7 @@ int is_nonbare_repository_dir(struct strbuf *path)
 int is_inside_git_dir(void)
 {
        if (inside_git_dir < 0)
-               inside_git_dir = is_inside_dir(get_git_dir());
+               inside_git_dir = is_inside_dir(repo_get_git_dir(the_repository));
        return inside_git_dir;
 }
 
@@ -1836,7 +1836,7 @@ void check_repository_format(struct repository_format *fmt)
        struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
        if (!fmt)
                fmt = &repo_fmt;
-       check_repository_format_gently(get_git_dir(), fmt, NULL);
+       check_repository_format_gently(repo_get_git_dir(the_repository), fmt, NULL);
        startup_info->have_repository = 1;
        repo_set_hash_algo(the_repository, fmt->hash_algo);
        repo_set_compat_hash_algo(the_repository, fmt->compat_hash_algo);
@@ -2224,7 +2224,7 @@ static int create_default_files(const char *template_path,
         * shared-repository settings, we would need to fix them up.
         */
        if (get_shared_repository()) {
-               adjust_shared_perm(get_git_dir());
+               adjust_shared_perm(repo_get_git_dir(the_repository));
        }
 
        initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, 0);
@@ -2434,12 +2434,12 @@ int init_db(const char *git_dir, const char *real_git_dir,
                        die(_("%s already exists"), real_git_dir);
 
                set_git_dir(real_git_dir, 1);
-               git_dir = get_git_dir();
+               git_dir = repo_get_git_dir(the_repository);
                separate_git_dir(git_dir, original_git_dir);
        }
        else {
                set_git_dir(git_dir, 1);
-               git_dir = get_git_dir();
+               git_dir = repo_get_git_dir(the_repository);
        }
        startup_info->have_repository = 1;
 
diff --git a/setup.h b/setup.h
index cd8dbc249762db3e12f21e8eaddbe52bf45573d0..fd2df7cd5251462f4a59b97ed09f8cc9cdf22af8 100644 (file)
--- a/setup.h
+++ b/setup.h
@@ -176,7 +176,7 @@ int verify_repository_format(const struct repository_format *format,
                             struct strbuf *err);
 
 /*
- * Check the repository format version in the path found in get_git_dir(),
+ * Check the repository format version in the path found in repo_get_git_dir(the_repository),
  * and die if it is a version we don't understand. Generally one would
  * set_git_dir() before calling this, and use it only for "are we in a valid
  * repo?".
diff --git a/trace.c b/trace.c
index 8669ddfca25e8fe91b57d96875f63214e5700a9e..32c5cda7afd4c00c244fe71ca9f5487ac857d291 100644 (file)
--- a/trace.c
+++ b/trace.c
  *  along with this program; if not, see <https://www.gnu.org/licenses/>.
  */
 
+#define USE_THE_REPOSITORY_VARIABLE
+
 #include "git-compat-util.h"
 #include "abspath.h"
 #include "environment.h"
+#include "repository.h"
 #include "quote.h"
 #include "setup.h"
 #include "trace.h"
@@ -311,7 +314,7 @@ void trace_repo_setup(void)
        if (!startup_info->prefix)
                prefix = "(null)";
 
-       trace_printf_key(&trace_setup_key, "setup: git_dir: %s\n", quote_crnl(get_git_dir()));
+       trace_printf_key(&trace_setup_key, "setup: git_dir: %s\n", quote_crnl(repo_get_git_dir(the_repository)));
        trace_printf_key(&trace_setup_key, "setup: git_common_dir: %s\n", quote_crnl(get_git_common_dir()));
        trace_printf_key(&trace_setup_key, "setup: worktree: %s\n", quote_crnl(git_work_tree));
        trace_printf_key(&trace_setup_key, "setup: cwd: %s\n", quote_crnl(cwd));
index 09b3560ffdc41e52eff6fdc049222a802ef9ee46..abe16eea651562afb7c5c9a0af0ffdec983fdf10 100644 (file)
@@ -143,7 +143,7 @@ static struct child_process *get_helper(struct transport *transport)
 
        if (have_git_dir())
                strvec_pushf(&helper->env, "%s=%s",
-                            GIT_DIR_ENVIRONMENT, get_git_dir());
+                            GIT_DIR_ENVIRONMENT, repo_get_git_dir(the_repository));
 
        helper->trace2_child_class = helper->args.v[0]; /* "remote-<name>" */
 
index 30a947426ee9d7366f1391344e1d9526e857d65e..11335c5d9a3f48afc46fabb95c7edb204c22cbc6 100644 (file)
@@ -57,7 +57,7 @@ static void add_head_info(struct worktree *wt)
 
 static int is_current_worktree(struct worktree *wt)
 {
-       char *git_dir = absolute_pathdup(get_git_dir());
+       char *git_dir = absolute_pathdup(repo_get_git_dir(the_repository));
        const char *wt_git_dir = get_worktree_git_dir(wt);
        int is_current = !fspathcmp(git_dir, absolute_path(wt_git_dir));
        free(git_dir);
@@ -171,7 +171,7 @@ struct worktree **get_worktrees(void)
 const char *get_worktree_git_dir(const struct worktree *wt)
 {
        if (!wt)
-               return get_git_dir();
+               return repo_get_git_dir(the_repository);
        else if (!wt->id)
                return get_git_common_dir();
        else