[GSoC Patch v8 4/7] repo: add path.hooks with absolute and relative suffixes
- From
K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
- Date
- Nov 7, 2026, 16:00 UTC
- Message-ID
- <20261107160100.122243-5-jayatheerthkulkarni2005@gmail.com>
- In-Reply-To
- <20261107160100.122243-1-jayatheerthkulkarni2005@gmail.com>
Hooks are an integral part of a repository's configuration and are commonly used by tooling to automate repository-specific workflows. Currently, scripts typically retrieve the hooks directory by invoking `git rev-parse --git-path hooks`.
Introduce `path.hooks.absolute` and `path.hooks.relative` keys to `git repo info`. This exposes the hooks directory as a scriptable config-like key using standard format rules, allowing scripts to retrieve it through the same interface as other repository path information.
Mentored-by: Justin Tobler <jltobler@gmail.com> Mentored-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com> Signed-off-by: K Jayatheerth <jayatheerthkulkarni2005@gmail.com> --- Documentation/git-repo.adoc | 10 ++++++++++ builtin/repo.c | 30 ++++++++++++++++++++++++++++++ t/t1900-repo-info.sh | 25 +++++++++++++++++++++++++ 3 files changed, 65 insertions(+)
diff --git a/Documentation/git-repo.adoc b/Documentation/git-repo.adoc index e524a07f53..526a4ccfbd 100644 --- a/Documentation/git-repo.adoc +++ b/Documentation/git-repo.adoc @@ -119,6 +119,16 @@ values that they return: `path.gitdir.relative`:: The path to the Git repository directory relative to the current working directory. +`path.hooks.absolute`:: + The canonical absolute path to the repository's hooks directory. + Respects the `core.hooksPath` configuration. If `core.hooksPath` is + set to `/dev/null`, that value is returned unchanged. + +`path.hooks.relative`:: + The path to the repository's hooks directory relative to the current + working directory. Respects the `core.hooksPath` configuration. If + `core.hooksPath` is set to `/dev/null`, that value is returned unchanged. + `path.superproject-root.absolute`:: The canonical absolute path to the working tree root of the superproject if the current repository is an initialized submodule. Outputs an empty diff --git a/builtin/repo.c b/builtin/repo.c index c17a8a3b35..739ac2974c 100644 --- a/builtin/repo.c +++ b/builtin/repo.c @@ -122,6 +122,34 @@ static int get_path_gitdir_relative(struct repository *repo, struct strbuf *buf) return 0; } +static int get_path_hooks_absolute(struct repository *repo, struct strbuf *buf) +{ + struct strbuf hooks_path = STRBUF_INIT; + + repo_git_path_replace(repo, &hooks_path, "hooks"); + if (!strcmp(hooks_path.buf, "/dev/null")) + strbuf_addstr(buf, "/dev/null"); + else + format_path(buf, hooks_path.buf, "", PATH_FORMAT_CANONICAL); + + strbuf_release(&hooks_path); + return 0; +} + +static int get_path_hooks_relative(struct repository *repo, struct strbuf *buf) +{ + struct strbuf hooks_path = STRBUF_INIT; + + repo_git_path_replace(repo, &hooks_path, "hooks"); + if (!strcmp(hooks_path.buf, "/dev/null")) + strbuf_addstr(buf, "/dev/null"); + else + format_path(buf, hooks_path.buf, repo->prefix, PATH_FORMAT_RELATIVE); + + strbuf_release(&hooks_path); + return 0; +} + static int get_path_superproject_absolute(struct repository *repo, struct strbuf *buf) { struct strbuf superproject = STRBUF_INIT; @@ -182,6 +210,8 @@ static const struct repo_info_field repo_info_field[] = { { "path.commondir.relative", get_path_commondir_relative }, { "path.gitdir.absolute", get_path_gitdir_absolute }, { "path.gitdir.relative", get_path_gitdir_relative }, + { "path.hooks.absolute", get_path_hooks_absolute }, + { "path.hooks.relative", get_path_hooks_relative }, { "path.superproject-root.absolute", get_path_superproject_absolute }, { "path.superproject-root.relative", get_path_superproject_relative }, { "path.toplevel.absolute", get_path_toplevel_absolute }, diff --git a/t/t1900-repo-info.sh b/t/t1900-repo-info.sh index eec576a1d9..5fefb0ca79 100755 --- a/t/t1900-repo-info.sh +++ b/t/t1900-repo-info.sh @@ -213,6 +213,31 @@ test_repo_info_path 'gitdir with explicit GIT_DIR' 'gitdir' \ '.git' \ 'GIT_DIR="../.git" && export GIT_DIR' +test_repo_info_path 'hooks standard' 'hooks' '.git/hooks' + +test_repo_info_path 'hooks with core.hooksPath override' 'hooks' \ + 'custom-hooks' \ + 'git config core.hooksPath "$ROOT/custom-hooks" && mkdir -p "$ROOT/custom-hooks"' + +test_expect_success 'hooks with core.hooksPath=/dev/null' ' + test_when_finished "rm -rf repo" && + git init repo && + ( + mkdir -p repo/sub && + cd repo/sub && + cat >>../.git/config <<-\EOF && + [core] + hooksPath = /dev/null + EOF + echo "path.hooks.absolute=/dev/null" >expect.abs && + echo "path.hooks.relative=/dev/null" >expect.rel && + git repo info path.hooks.absolute >actual.abs && + git repo info path.hooks.relative >actual.rel && + test_cmp expect.abs actual.abs && + test_cmp expect.rel actual.rel + ) +' + test_expect_success 'path.superproject-root absolute and relative' ' test_when_finished "rm -rf sub super" && git init sub &&
-- 2.56.0-rc2