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 <jltobler@gmail.com>
Mentored-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com>
Signed-off-by: K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
K Jayatheerth
2026-06-12 23:58:47 +05:30
committed by Junio C Hamano
parent b9cf3f646b
commit 5a318de632
3 changed files with 37 additions and 0 deletions

View File

@@ -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:
+

View File

@@ -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 },
};

View File

@@ -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