Git development
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: Justin Tobler <jltobler@gmail.com>, Junio C Hamano <gitster@pobox.com>
Subject: [PATCH v2 07/13] setup: move prefix into repository
Date: Tue, 07 Jul 2026 09:21:26 +0200	[thread overview]
Message-ID: <20260707-pks-setup-split-discovery-and-setup-v2-7-aab372cd227c@pks.im> (raw)
In-Reply-To: <20260707-pks-setup-split-discovery-and-setup-v2-0-aab372cd227c@pks.im>

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>
---
 builtin/repo.c         | 8 ++++----
 builtin/rev-parse.c    | 5 +++--
 builtin/update-index.c | 4 ++--
 object-name.c          | 4 ++--
 repository.c           | 1 +
 repository.h           | 8 ++++++++
 setup.c                | 6 +++---
 setup.h                | 1 -
 trace.c                | 4 ++--
 9 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/builtin/repo.c b/builtin/repo.c
index 042d6de558..84e012f83f 100644
--- a/builtin/repo.c
+++ b/builtin/repo.c
@@ -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;
 }
 
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index 5e04b0e2bd..43693454d5 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -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;
 			}
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 3d6646c318..f43d150eb3 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -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);
diff --git a/object-name.c b/object-name.c
index 46159466ac..fc70acc9e0 100644
--- a/object-name.c
+++ b/object-name.c
@@ -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);
 }
 
diff --git a/repository.c b/repository.c
index 73d80bcffd..2ef0778846 100644
--- a/repository.c
+++ b/repository.c
@@ -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);
diff --git a/repository.h b/repository.h
index 7d649e32e7..b767307911 100644
--- a/repository.h
+++ b/repository.h
@@ -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 b755693572..6cc9fa2de8 100644
--- 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 b9fd96bea6..c01a244fe9 100644
--- 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 9b99460db8..515b99e7f5 100644
--- 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)));

-- 
2.55.0.141.g00534a21ce.dirty


  parent reply	other threads:[~2026-07-07  7:21 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-30 11:47 [PATCH 00/13] setup: split up repository discovery and setup Patrick Steinhardt
2026-06-30 11:47 ` [PATCH 01/13] setup: rename `check_repository_format_gently()` Patrick Steinhardt
2026-07-06 21:27   ` Justin Tobler
2026-06-30 11:47 ` [PATCH 02/13] setup: mark bogus worktree in `apply_repository_format()` Patrick Steinhardt
2026-06-30 18:26   ` Junio C Hamano
2026-07-01  6:23     ` Patrick Steinhardt
2026-07-06 21:49   ` Justin Tobler
2026-07-07  6:25     ` Patrick Steinhardt
2026-06-30 11:47 ` [PATCH 03/13] setup: unify setup of shallow file Patrick Steinhardt
2026-07-06 22:02   ` Justin Tobler
2026-07-07  6:25     ` Patrick Steinhardt
2026-06-30 11:47 ` [PATCH 04/13] setup: split up concerns of `setup_git_env_internal()` Patrick Steinhardt
2026-06-30 11:47 ` [PATCH 05/13] setup: introduce explicit repository discovery Patrick Steinhardt
2026-07-06 22:19   ` Justin Tobler
2026-07-07  6:25     ` Patrick Steinhardt
2026-06-30 11:47 ` [PATCH 06/13] setup: embed repository format in discovery Patrick Steinhardt
2026-06-30 11:47 ` [PATCH 07/13] setup: move prefix into repository Patrick Steinhardt
2026-07-06 22:33   ` Justin Tobler
2026-06-30 11:47 ` [PATCH 08/13] setup: drop static `cwd` variable Patrick Steinhardt
2026-06-30 11:47 ` [PATCH 09/13] setup: propagate prefix via repository discovery Patrick Steinhardt
2026-06-30 11:47 ` [PATCH 10/13] setup: make repository discovery self-contained Patrick Steinhardt
2026-06-30 11:47 ` [PATCH 11/13] setup: drop redundant configuration of `startup_info->have_repository` Patrick Steinhardt
2026-06-30 11:47 ` [PATCH 12/13] setup: pass worktree to `init_db()` Patrick Steinhardt
2026-06-30 11:47 ` [PATCH 13/13] setup: mark `set_git_work_tree()` as file-local Patrick Steinhardt
2026-07-07  7:21 ` [PATCH v2 00/13] setup: split up repository discovery and setup Patrick Steinhardt
2026-07-07  7:21   ` [PATCH v2 01/13] setup: rename `check_repository_format_gently()` Patrick Steinhardt
2026-07-07  7:21   ` [PATCH v2 02/13] setup: mark bogus worktree in `apply_repository_format()` Patrick Steinhardt
2026-07-07  7:21   ` [PATCH v2 03/13] setup: unify setup of shallow file Patrick Steinhardt
2026-07-07  7:21   ` [PATCH v2 04/13] setup: split up concerns of `setup_git_env_internal()` Patrick Steinhardt
2026-07-07  7:21   ` [PATCH v2 05/13] setup: introduce explicit repository discovery Patrick Steinhardt
2026-07-07  7:21   ` [PATCH v2 06/13] setup: embed repository format in discovery Patrick Steinhardt
2026-07-07  7:21   ` Patrick Steinhardt [this message]
2026-07-07  7:21   ` [PATCH v2 08/13] setup: drop static `cwd` variable Patrick Steinhardt
2026-07-07  7:21   ` [PATCH v2 09/13] setup: propagate prefix via repository discovery Patrick Steinhardt
2026-07-07  7:21   ` [PATCH v2 10/13] setup: make repository discovery self-contained Patrick Steinhardt
2026-07-07  7:21   ` [PATCH v2 11/13] setup: drop redundant configuration of `startup_info->have_repository` Patrick Steinhardt
2026-07-07  7:21   ` [PATCH v2 12/13] setup: pass worktree to `init_db()` Patrick Steinhardt
2026-07-07  7:21   ` [PATCH v2 13/13] setup: mark `set_git_work_tree()` as file-local Patrick Steinhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260707-pks-setup-split-discovery-and-setup-v2-7-aab372cd227c@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jltobler@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox