[GSoC Patch v9 5/5] repo: add path.prefix
- From
K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
- Date
- Nov 14, 2026, 16:37 UTC
- Message-ID
- <20261114163715.257705-6-jayatheerthkulkarni2005@gmail.com>
- In-Reply-To
- <20261114163715.257705-1-jayatheerthkulkarni2005@gmail.com>
Scripts sometimes need the path from the repository's working tree root to the current working directory. While this information can be derived through existing Git commands, `git repo info` does not currently expose it as a scriptable key.
Introduce the `path.prefix` key to `git repo info`. The key returns the path from the working tree root to the current working directory, returning the empty string when invoked from the working tree root.
Mentored-by: Justin Tobler <jltobler@gmail.com> Mentored-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com> Signed-off-by: K Jayatheerth <jayatheerthkulkarni2005@gmail.com> --- Documentation/git-repo.adoc | 5 +++++ builtin/repo.c | 11 +++++++++++ t/t1900-repo-info.sh | 23 +++++++++++++++++++++++ 3 files changed, 39 insertions(+)
diff --git a/Documentation/git-repo.adoc b/Documentation/git-repo.adoc index c515e9372f..17992c7a67 100644 --- a/Documentation/git-repo.adoc +++ b/Documentation/git-repo.adoc @@ -131,6 +131,11 @@ values that they return: Returns the configured index path even if the repository is bare or the file does not exist. +`path.prefix`:: + The path from the root of the working tree to the current working + directory. Returns the empty string when the current working directory + is the root of the working tree. + `path.superprojectworkingtree.absolute`:: The canonical absolute path to the working tree root of the superproject if the current repository is an initialized submodule. Outputs an empty diff --git a/builtin/repo.c b/builtin/repo.c index eeaee98c75..87c5743246 100644 --- a/builtin/repo.c +++ b/builtin/repo.c @@ -100,6 +100,16 @@ static int get_path_commondir_relative(struct repository *repo, struct strbuf *b return 0; } +static int get_path_prefix(struct repository *repo, struct strbuf *buf) +{ + /* + * repo->prefix is NULL when the current working directory is + * the worktree root. + */ + strbuf_addstr(buf, repo->prefix ? repo->prefix : ""); + return 0; +} + static int get_path_gitdir_absolute(struct repository *repo, struct strbuf *buf) { const char *git_dir = repo_get_git_dir(repo); @@ -200,6 +210,7 @@ static const struct repo_info_field repo_info_field[] = { { "path.gitdir.relative", get_path_gitdir_relative }, { "path.index.absolute", get_path_index_absolute }, { "path.index.relative", get_path_index_relative }, + { "path.prefix", get_path_prefix }, { "path.superprojectworkingtree.absolute", get_path_superprojectworkingtree_absolute }, { "path.superprojectworkingtree.relative", get_path_superprojectworkingtree_relative }, { "path.workingtree.absolute", get_path_workingtree_absolute }, diff --git a/t/t1900-repo-info.sh b/t/t1900-repo-info.sh index 46c92d4e43..d0631a8f3b 100755 --- a/t/t1900-repo-info.sh +++ b/t/t1900-repo-info.sh @@ -207,6 +207,29 @@ test_repo_info_path 'commondir with only GIT_DIR' 'commondir' \ '.git' \ 'GIT_DIR="../.git" && export GIT_DIR' +test_expect_success 'path.prefix at repository root' ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + echo "path.prefix=" >expect && + git repo info path.prefix >actual && + test_cmp expect actual + ) +' + +test_expect_success 'path.prefix in subdirectory' ' + test_when_finished "rm -rf repo" && + git init repo && + mkdir -p repo/sub/dir && + ( + cd repo/sub/dir && + echo "path.prefix=sub/dir/" >expect && + git repo info path.prefix >actual && + test_cmp expect actual + ) +' + test_repo_info_path 'gitdir standard' 'gitdir' '.git' test_repo_info_path 'gitdir with explicit GIT_DIR' 'gitdir' \
-- 2.56.0-rc2