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>

K Jayatheerth committed Jun 24, 2026 at 09:07 UTC 3ac28d832aefe06e0fb9390d3e20a9c32350e052
3 files changed +36
Documentation/git-repo.adoc
+6
@@ -113,6 +113,12 @@ values that they return:
113 The path to the Git repository's common directory relative to
114 the current working directory.
115
116 +`path.gitdir.absolute`::
117 + The canonical absolute path to the Git repository directory (the `.git` directory).
118 +
119 +`path.gitdir.relative`::
120 + The path to the Git repository directory relative to the current working directory.
121 +
122 `references.format`::
123 The reference storage format. The valid values are:
124 +
builtin/repo.c
+24
@@ -99,6 +99,28 @@ static int get_path_commondir_relative(struct repository *repo, struct strbuf *b
99 return 0;
100 }
101
102 +static int get_path_gitdir_absolute(struct repository *repo, struct strbuf *buf)
103 +{
104 + const char *git_dir = repo_get_git_dir(repo);
105 +
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);
110 + return 0;
111 +}
112 +
113 +static int get_path_gitdir_relative(struct repository *repo, struct strbuf *buf)
114 +{
115 + const char *git_dir = repo_get_git_dir(repo);
116 +
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);
121 + return 0;
122 +}
123 +
124 static int get_references_format(struct repository *repo, struct strbuf *buf)
125 {
126 strbuf_addstr(buf,
@@ -113,6 +135,8 @@ static const struct repo_info_field repo_info_field[] = {
135 { "object.format", get_object_format },
136 { "path.commondir.absolute", get_path_commondir_absolute },
137 { "path.commondir.relative", get_path_commondir_relative },
138 + { "path.gitdir.absolute", get_path_gitdir_absolute },
139 + { "path.gitdir.relative", get_path_gitdir_relative },
140 { "references.format", get_references_format },
141 };
142
t/t1900-repo-info.sh
+6
@@ -207,4 +207,10 @@ test_repo_info_path 'commondir with only GIT_DIR' 'commondir' \
207 '.git' \
208 'GIT_DIR="../.git" && export GIT_DIR'
209
210 +test_repo_info_path 'gitdir standard' 'gitdir' '.git'
211 +
212 +test_repo_info_path 'gitdir with explicit GIT_DIR' 'gitdir' \
213 + '.git' \
214 + 'GIT_DIR="../.git" && export GIT_DIR'
215 +
216 test_done