setup: move prefix into repository
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>
Patrick Steinhardt committed
Jul 7, 2026 at 09:21 UTC
b964ad1f019d66604d7577e2db49481461bcab5a
9 files changed
+25
-16
builtin/repo.c
+4
-4
@@ -84,7 +84,7 @@ static int get_path_commondir_absolute(struct repository *repo, struct strbuf *b
84
if (!common_dir)
85
return error(_("unable to get common directory"));
86
87
- format_path(buf, common_dir, startup_info->prefix, PATH_FORMAT_CANONICAL);
87
+ format_path(buf, common_dir, repo->prefix, PATH_FORMAT_CANONICAL);
88
return 0;
89
}
90
@@ -95,7 +95,7 @@ static int get_path_commondir_relative(struct repository *repo, struct strbuf *b
95
if (!common_dir)
96
return error(_("unable to get common directory"));
97
98
- format_path(buf, common_dir, startup_info->prefix, PATH_FORMAT_RELATIVE);
98
+ format_path(buf, common_dir, repo->prefix, PATH_FORMAT_RELATIVE);
99
return 0;
100
}
101
@@ -106,7 +106,7 @@ static int get_path_gitdir_absolute(struct repository *repo, struct strbuf *buf)
106
if (!git_dir)
107
return error(_("unable to get git directory"));
108
109
- format_path(buf, git_dir, startup_info->prefix, PATH_FORMAT_CANONICAL);
109
+ format_path(buf, git_dir, repo->prefix, PATH_FORMAT_CANONICAL);
110
return 0;
111
}
112
@@ -117,7 +117,7 @@ static int get_path_gitdir_relative(struct repository *repo, struct strbuf *buf)
117
if (!git_dir)
118
return error(_("unable to get git directory"));
119
120
- format_path(buf, git_dir, startup_info->prefix, PATH_FORMAT_RELATIVE);
120
+ format_path(buf, git_dir, repo->prefix, PATH_FORMAT_RELATIVE);
121
return 0;
122
}
123
builtin/rev-parse.c
+3
-2
@@ -255,7 +255,7 @@ static int show_file(const char *arg, int output_prefix)
255
show_default();
256
if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
257
if (output_prefix) {
258
- const char *prefix = startup_info->prefix;
258
+ const char *prefix = the_repository->prefix;
259
char *fname = prefix_filename(prefix, arg);
260
show(fname);
261
free(fname);
@@ -832,7 +832,8 @@ int cmd_rev_parse(int argc,
832
prefix = argv[++i];
833
if (!prefix)
834
die(_("--prefix requires an argument"));
835
- startup_info->prefix = prefix;
835
+ FREE_AND_NULL(the_repository->prefix);
836
+ the_repository->prefix = xstrdup(prefix);
837
output_prefix = 1;
838
continue;
839
}
builtin/update-index.c
+2
-2
@@ -875,7 +875,7 @@ static enum parse_opt_result unresolve_callback(
875
const char *arg, int unset)
876
{
877
int *has_errors = opt->value;
878
- const char *prefix = startup_info->prefix;
878
+ const char *prefix = the_repository->prefix;
879
880
BUG_ON_OPT_NEG(unset);
881
BUG_ON_OPT_ARG(arg);
@@ -896,7 +896,7 @@ static enum parse_opt_result reupdate_callback(
896
const char *arg, int unset)
897
{
898
int *has_errors = opt->value;
899
- const char *prefix = startup_info->prefix;
899
+ const char *prefix = the_repository->prefix;
900
901
BUG_ON_OPT_NEG(unset);
902
BUG_ON_OPT_ARG(arg);
object-name.c
+2
-2
@@ -1708,8 +1708,8 @@ static char *resolve_relative_path(struct repository *r, const char *rel)
1708
die(_("relative path syntax can't be used outside working tree"));
1709
1710
/* die() inside prefix_path() if resolved path is outside worktree */
1711
- return prefix_path(the_repository, startup_info->prefix,
1712
- startup_info->prefix ? strlen(startup_info->prefix) : 0,
1711
+ return prefix_path(the_repository, the_repository->prefix,
1712
+ the_repository->prefix ? strlen(the_repository->prefix) : 0,
1713
rel);
1714
}
1715
repository.c
+1
@@ -376,6 +376,7 @@ void repo_clear(struct repository *repo)
376
377
FREE_AND_NULL(repo->gitdir);
378
FREE_AND_NULL(repo->commondir);
379
+ FREE_AND_NULL(repo->prefix);
380
FREE_AND_NULL(repo->graft_file);
381
FREE_AND_NULL(repo->index_file);
382
FREE_AND_NULL(repo->worktree);
repository.h
+8
@@ -52,6 +52,14 @@ struct repository {
52
*/
53
char *commondir;
54
55
+ /*
56
+ * The "prefix", a path to the current working directory relative to
57
+ * the work tree root, or NULL, if the current working directory is not
58
+ * a strict subdirectory of the work tree root. The prefix always ends
59
+ * with a '/' character.
60
+ */
61
+ char *prefix;
62
+
63
/*
64
* Holds any information related to accessing the raw object content.
65
*/
setup.c
+3
-3
@@ -2030,7 +2030,7 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
2030
* repository and that the caller expects startup_info to reflect
2031
* this.
2032
*
2033
- * Regardless of the state of nongit_ok, startup_info->prefix and
2033
+ * Regardless of the state of nongit_ok, the_repository->prefix and
2034
* the GIT_PREFIX environment variable must always match. For details
2035
* see Documentation/config/alias.adoc.
2036
*/
@@ -2105,10 +2105,10 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
2105
*/
2106
if (prefix) {
2107
prefix = precompose_string_if_needed(prefix);
2108
- startup_info->prefix = prefix;
2108
+ repo->prefix = xstrdup(prefix);
2109
setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1);
2110
} else {
2111
- startup_info->prefix = NULL;
2111
+ FREE_AND_NULL(repo->prefix);
2112
setenv(GIT_PREFIX_ENVIRONMENT, "", 1);
2113
}
2114
setup.h
-1
@@ -299,7 +299,6 @@ struct startup_info {
299
bool force_bare_repository;
300
301
int have_repository;
302
- const char *prefix;
302
const char *original_cwd;
303
};
304
extern struct startup_info *startup_info;
trace.c
+2
-2
@@ -299,7 +299,7 @@ static const char *quote_crnl(const char *path)
299
300
void trace_repo_setup(struct repository *r)
301
{
302
- const char *git_work_tree, *prefix = startup_info->prefix;
302
+ const char *git_work_tree, *prefix = r->prefix;
303
char *cwd;
304
305
if (!trace_want(&trace_setup_key))
@@ -310,7 +310,7 @@ void trace_repo_setup(struct repository *r)
310
if (!(git_work_tree = repo_get_work_tree(r)))
311
git_work_tree = "(null)";
312
313
- if (!startup_info->prefix)
313
+ if (!r->prefix)
314
prefix = "(null)";
315
316
trace_printf_key(&trace_setup_key, "setup: git_dir: %s\n", quote_crnl(repo_get_git_dir(r)));