Skip to content

Commit 5a318de

Browse files
jayatheerthkulkarnigitster
authored andcommitted
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>
1 parent b9cf3f6 commit 5a318de

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

Documentation/git-repo.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ values that they return:
113113
The path to the Git repository's common directory relative to
114114
the current working directory.
115115

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+
116122
`references.format`::
117123
The reference storage format. The valid values are:
118124
+

builtin/repo.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,28 @@ static int get_path_commondir_relative(struct repository *repo, struct strbuf *b
9999
return 0;
100100
}
101101

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+
append_formatted_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+
append_formatted_path(buf, git_dir, startup_info->prefix, PATH_FORMAT_RELATIVE);
121+
return 0;
122+
}
123+
102124
static int get_references_format(struct repository *repo, struct strbuf *buf)
103125
{
104126
strbuf_addstr(buf,
@@ -113,6 +135,8 @@ static const struct repo_info_field repo_info_field[] = {
113135
{ "object.format", get_object_format },
114136
{ "path.commondir.absolute", get_path_commondir_absolute },
115137
{ "path.commondir.relative", get_path_commondir_relative },
138+
{ "path.gitdir.absolute", get_path_gitdir_absolute },
139+
{ "path.gitdir.relative", get_path_gitdir_relative },
116140
{ "references.format", get_references_format },
117141
};
118142

t/t1900-repo-info.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,11 @@ test_repo_info_path 'commondir with only GIT_DIR' 'commondir' \
216216
'commondir-only-gitdir' '.git' '../.git' \
217217
'GIT_DIR="../.git" && export GIT_DIR'
218218

219+
test_repo_info_path 'gitdir standard' 'gitdir' 'gitdir-std' \
220+
'.git' '../.git'
221+
222+
test_repo_info_path 'gitdir with explicit GIT_DIR' 'gitdir' \
223+
'gitdir-env' '.git' '../.git' \
224+
'GIT_DIR="../.git" && export GIT_DIR'
225+
219226
test_done

0 commit comments

Comments
 (0)