]> git.ipfire.org Git - thirdparty/git.git/commitdiff
setup: move prefix into repository
authorPatrick Steinhardt <ps@pks.im>
Tue, 30 Jun 2026 11:47:46 +0000 (13:47 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 30 Jun 2026 18:29:41 +0000 (11:29 -0700)
The repository prefix is currently stored in the startup info. This
feels somewhat awkward though, as it is inherently a property of a given
repository.

Move the prefix into the repository accordingly.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/repo.c
builtin/rev-parse.c
builtin/update-index.c
object-name.c
repository.c
repository.h
setup.c
setup.h
trace.c

index 042d6de558e93078614bf1cb8b72ddf62f73c23e..84e012f83f65ae6d9e51aa97eaa2da93578dc73e 100644 (file)
@@ -84,7 +84,7 @@ static int get_path_commondir_absolute(struct repository *repo, struct strbuf *b
        if (!common_dir)
                return error(_("unable to get common directory"));
 
-       format_path(buf, common_dir, startup_info->prefix, PATH_FORMAT_CANONICAL);
+       format_path(buf, common_dir, repo->prefix, PATH_FORMAT_CANONICAL);
        return 0;
 }
 
@@ -95,7 +95,7 @@ static int get_path_commondir_relative(struct repository *repo, struct strbuf *b
        if (!common_dir)
                return error(_("unable to get common directory"));
 
-       format_path(buf, common_dir, startup_info->prefix, PATH_FORMAT_RELATIVE);
+       format_path(buf, common_dir, repo->prefix, PATH_FORMAT_RELATIVE);
        return 0;
 }
 
@@ -106,7 +106,7 @@ static int get_path_gitdir_absolute(struct repository *repo, struct strbuf *buf)
        if (!git_dir)
                return error(_("unable to get git directory"));
 
-       format_path(buf, git_dir, startup_info->prefix, PATH_FORMAT_CANONICAL);
+       format_path(buf, git_dir, repo->prefix, PATH_FORMAT_CANONICAL);
        return 0;
 }
 
@@ -117,7 +117,7 @@ static int get_path_gitdir_relative(struct repository *repo, struct strbuf *buf)
        if (!git_dir)
                return error(_("unable to get git directory"));
 
-       format_path(buf, git_dir, startup_info->prefix, PATH_FORMAT_RELATIVE);
+       format_path(buf, git_dir, repo->prefix, PATH_FORMAT_RELATIVE);
        return 0;
 }
 
index 5e04b0e2bd6416b8e35fe9fc1a733da6bd5aad35..43693454d53f0cfa9dd2018bbf04cbc0d4e3299c 100644 (file)
@@ -255,7 +255,7 @@ static int show_file(const char *arg, int output_prefix)
        show_default();
        if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
                if (output_prefix) {
-                       const char *prefix = startup_info->prefix;
+                       const char *prefix = the_repository->prefix;
                        char *fname = prefix_filename(prefix, arg);
                        show(fname);
                        free(fname);
@@ -832,7 +832,8 @@ int cmd_rev_parse(int argc,
                                prefix = argv[++i];
                                if (!prefix)
                                        die(_("--prefix requires an argument"));
-                               startup_info->prefix = prefix;
+                               FREE_AND_NULL(the_repository->prefix);
+                               the_repository->prefix = xstrdup(prefix);
                                output_prefix = 1;
                                continue;
                        }
index 3d6646c318b98e806a0aba09c3f034b133dc582c..f43d150eb351cd68873bfa1db145e2721df77be2 100644 (file)
@@ -875,7 +875,7 @@ static enum parse_opt_result unresolve_callback(
        const char *arg, int unset)
 {
        int *has_errors = opt->value;
-       const char *prefix = startup_info->prefix;
+       const char *prefix = the_repository->prefix;
 
        BUG_ON_OPT_NEG(unset);
        BUG_ON_OPT_ARG(arg);
@@ -896,7 +896,7 @@ static enum parse_opt_result reupdate_callback(
        const char *arg, int unset)
 {
        int *has_errors = opt->value;
-       const char *prefix = startup_info->prefix;
+       const char *prefix = the_repository->prefix;
 
        BUG_ON_OPT_NEG(unset);
        BUG_ON_OPT_ARG(arg);
index 46159466ac543ad72df963ebd24b2b12cc8ee7be..fc70acc9e032932d2a8111653f1fa3290b02ab83 100644 (file)
@@ -1708,8 +1708,8 @@ static char *resolve_relative_path(struct repository *r, const char *rel)
                die(_("relative path syntax can't be used outside working tree"));
 
        /* die() inside prefix_path() if resolved path is outside worktree */
-       return prefix_path(the_repository, startup_info->prefix,
-                          startup_info->prefix ? strlen(startup_info->prefix) : 0,
+       return prefix_path(the_repository, the_repository->prefix,
+                          the_repository->prefix ? strlen(the_repository->prefix) : 0,
                           rel);
 }
 
index 73d80bcffdf5c2ff83b66ea2c2b2a89bedc19e3b..2ef0778846bcf193abc5b4deaaa31ca5cebc9ef3 100644 (file)
@@ -376,6 +376,7 @@ void repo_clear(struct repository *repo)
 
        FREE_AND_NULL(repo->gitdir);
        FREE_AND_NULL(repo->commondir);
+       FREE_AND_NULL(repo->prefix);
        FREE_AND_NULL(repo->graft_file);
        FREE_AND_NULL(repo->index_file);
        FREE_AND_NULL(repo->worktree);
index 7d649e32e7fad3781befb430c5045c2a12357c8a..b7673079119e6f67909a0abb9a70a6ce8877df0d 100644 (file)
@@ -52,6 +52,14 @@ struct repository {
         */
        char *commondir;
 
+       /*
+        * The "prefix", a path to the current working directory relative to
+        * the work tree root, or NULL, if the current working directory is not
+        * a strict subdirectory of the work tree root. The prefix always ends
+        * with a '/' character.
+        */
+       char *prefix;
+
        /*
         * Holds any information related to accessing the raw object content.
         */
diff --git a/setup.c b/setup.c
index 6b914659517ba637aca84d5eef93edae5c6ca6c3..58c97c13df1f6e05f122fa5a2feedbd658053ca3 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -2030,7 +2030,7 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
         * repository and that the caller expects startup_info to reflect
         * this.
         *
-        * Regardless of the state of nongit_ok, startup_info->prefix and
+        * Regardless of the state of nongit_ok, the_repository->prefix and
         * the GIT_PREFIX environment variable must always match. For details
         * see Documentation/config/alias.adoc.
         */
@@ -2105,10 +2105,10 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
         */
        if (prefix) {
                prefix = precompose_string_if_needed(prefix);
-               startup_info->prefix = prefix;
+               repo->prefix = xstrdup(prefix);
                setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1);
        } else {
-               startup_info->prefix = NULL;
+               FREE_AND_NULL(repo->prefix);
                setenv(GIT_PREFIX_ENVIRONMENT, "", 1);
        }
 
diff --git a/setup.h b/setup.h
index b9fd96bea6e639d207a39ce09cf7d0a745425cba..c01a244fe93a6a14c2ab0bcf2a432cb85ff1f3e4 100644 (file)
--- a/setup.h
+++ b/setup.h
@@ -299,7 +299,6 @@ struct startup_info {
        bool force_bare_repository;
 
        int have_repository;
-       const char *prefix;
        const char *original_cwd;
 };
 extern struct startup_info *startup_info;
diff --git a/trace.c b/trace.c
index 9b99460db82a28634c09260b42f311f7c69f4d59..515b99e7f5a10585170226e480824fdaa113f9ca 100644 (file)
--- a/trace.c
+++ b/trace.c
@@ -299,7 +299,7 @@ static const char *quote_crnl(const char *path)
 
 void trace_repo_setup(struct repository *r)
 {
-       const char *git_work_tree, *prefix = startup_info->prefix;
+       const char *git_work_tree, *prefix = r->prefix;
        char *cwd;
 
        if (!trace_want(&trace_setup_key))
@@ -310,7 +310,7 @@ void trace_repo_setup(struct repository *r)
        if (!(git_work_tree = repo_get_work_tree(r)))
                git_work_tree = "(null)";
 
-       if (!startup_info->prefix)
+       if (!r->prefix)
                prefix = "(null)";
 
        trace_printf_key(&trace_setup_key, "setup: git_dir: %s\n", quote_crnl(repo_get_git_dir(r)));