]> git.ipfire.org Git - thirdparty/git.git/commitdiff
repo: add path.gitdir with absolute and relative suffix formatting
authorK Jayatheerth <jayatheerthkulkarni2005@gmail.com>
Wed, 24 Jun 2026 03:37:48 +0000 (09:07 +0530)
committerJunio C Hamano <gitster@pobox.com>
Wed, 24 Jun 2026 04:15:52 +0000 (21:15 -0700)
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>
Documentation/git-repo.adoc
builtin/repo.c
t/t1900-repo-info.sh

index 890c34051da8de5723a1ba7648bca2d5a0b18b18..ed7d80c690c7204638b9c8b3d2380a0148320398 100644 (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:
 +
index 4c3fbc26b967cf060f32d200a7ef4678e3bc14d6..27c8caff38c6a833117668a6bfe9dda901847c6b 100644 (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"));
+
+       format_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"));
+
+       format_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 },
 };
 
index 09158d29f927a4e8e79a17399aabe8e8899eca8a..ae8c22c81749bad153a7965393be3b4b1a77d4ed 100755 (executable)
@@ -207,4 +207,10 @@ test_repo_info_path 'commondir with only GIT_DIR' 'commondir' \
        '.git' \
        'GIT_DIR="../.git" && export GIT_DIR'
 
+test_repo_info_path 'gitdir standard' 'gitdir' '.git'
+
+test_repo_info_path 'gitdir with explicit GIT_DIR' 'gitdir' \
+       '.git' \
+       'GIT_DIR="../.git" && export GIT_DIR'
+
 test_done