From 5a318de632ef300292eb204ad3b2dfd5e9b8d89d Mon Sep 17 00:00:00 2001 From: K Jayatheerth Date: Fri, 12 Jun 2026 23:58:47 +0530 Subject: [PATCH] repo: add path.gitdir with absolute and relative suffix formatting Scripts need a stable way to locate the git directory without parsing rev-parse output or relying on its flag-driven path format selection. There is no way to retrieve this path from git repo info today. Introduce path.gitdir.absolute and path.gitdir.relative keys, consistent with the path.commondir keys added in the previous patch. Reuse the test_repo_info_path helper introduced there to validate both variants. Mentored-by: Justin Tobler Mentored-by: Lucas Seiki Oshiro Signed-off-by: K Jayatheerth Signed-off-by: Junio C Hamano --- Documentation/git-repo.adoc | 6 ++++++ builtin/repo.c | 24 ++++++++++++++++++++++++ t/t1900-repo-info.sh | 7 +++++++ 3 files changed, 37 insertions(+) diff --git a/Documentation/git-repo.adoc b/Documentation/git-repo.adoc index 890c34051d..ed7d80c690 100644 --- a/Documentation/git-repo.adoc +++ b/Documentation/git-repo.adoc @@ -113,6 +113,12 @@ values that they return: The path to the Git repository's common directory relative to the current working directory. +`path.gitdir.absolute`:: + The canonical absolute path to the Git repository directory (the `.git` directory). + +`path.gitdir.relative`:: + The path to the Git repository directory relative to the current working directory. + `references.format`:: The reference storage format. The valid values are: + diff --git a/builtin/repo.c b/builtin/repo.c index c4cc3bf3fc..9a312d127a 100644 --- a/builtin/repo.c +++ b/builtin/repo.c @@ -99,6 +99,28 @@ static int get_path_commondir_relative(struct repository *repo, struct strbuf *b return 0; } +static int get_path_gitdir_absolute(struct repository *repo, struct strbuf *buf) +{ + const char *git_dir = repo_get_git_dir(repo); + + if (!git_dir) + return error(_("unable to get git directory")); + + append_formatted_path(buf, git_dir, startup_info->prefix, PATH_FORMAT_CANONICAL); + return 0; +} + +static int get_path_gitdir_relative(struct repository *repo, struct strbuf *buf) +{ + const char *git_dir = repo_get_git_dir(repo); + + if (!git_dir) + return error(_("unable to get git directory")); + + append_formatted_path(buf, git_dir, startup_info->prefix, PATH_FORMAT_RELATIVE); + return 0; +} + static int get_references_format(struct repository *repo, struct strbuf *buf) { strbuf_addstr(buf, @@ -113,6 +135,8 @@ static const struct repo_info_field repo_info_field[] = { { "object.format", get_object_format }, { "path.commondir.absolute", get_path_commondir_absolute }, { "path.commondir.relative", get_path_commondir_relative }, + { "path.gitdir.absolute", get_path_gitdir_absolute }, + { "path.gitdir.relative", get_path_gitdir_relative }, { "references.format", get_references_format }, }; diff --git a/t/t1900-repo-info.sh b/t/t1900-repo-info.sh index 28fe76e25b..26acb5fe82 100755 --- a/t/t1900-repo-info.sh +++ b/t/t1900-repo-info.sh @@ -216,4 +216,11 @@ test_repo_info_path 'commondir with only GIT_DIR' 'commondir' \ 'commondir-only-gitdir' '.git' '../.git' \ 'GIT_DIR="../.git" && export GIT_DIR' +test_repo_info_path 'gitdir standard' 'gitdir' 'gitdir-std' \ + '.git' '../.git' + +test_repo_info_path 'gitdir with explicit GIT_DIR' 'gitdir' \ + 'gitdir-env' '.git' '../.git' \ + 'GIT_DIR="../.git" && export GIT_DIR' + test_done