From 5c0aa862df7b15bab9bc7b93d07674aa3d50ef90 Mon Sep 17 00:00:00 2001 From: Ethan Wu Date: Thu, 14 Nov 2024 18:50:28 -0800 Subject: [PATCH] fix(init): resolve hooks path relative to repo root Ensure that `core.hooksPath` is resolved relative to the repository root of the current worktree, or the git dir if it is bare. This should be the same logic as git uses for resolution. --- git-branchless-lib/src/core/config.rs | 8 +++++++- git-branchless/tests/test_init.rs | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/git-branchless-lib/src/core/config.rs b/git-branchless-lib/src/core/config.rs index 248525a36..82eac41b1 100644 --- a/git-branchless-lib/src/core/config.rs +++ b/git-branchless-lib/src/core/config.rs @@ -54,7 +54,13 @@ pub fn get_main_worktree_hooks_dir( let hooks_path = if result.exit_code.is_success() { let path = String::from_utf8(result.stdout) .context("Decoding git config output for hooks path")?; - PathBuf::from(path.strip_suffix('\n').unwrap_or(&path)) + + let path = PathBuf::from(path.strip_suffix('\n').unwrap_or(&path)); + + repo.get_working_copy_path() + .as_deref() + .unwrap_or_else(|| repo.get_path()) + .join(path) } else { get_default_hooks_dir(repo)? }; diff --git a/git-branchless/tests/test_init.rs b/git-branchless/tests/test_init.rs index 82be1b2d1..d5a4ce8b2 100644 --- a/git-branchless/tests/test_init.rs +++ b/git-branchless/tests/test_init.rs @@ -547,7 +547,7 @@ fn test_init_core_hooks_path_warning() -> eyre::Result<()> { Auto-detected your main branch as: master If this is incorrect, run: git branchless init --main-branch Installing hooks: post-applypatch, post-checkout, post-commit, post-merge, post-rewrite, pre-auto-gc, reference-transaction - Warning: the configuration value core.hooksPath was set to: my-hooks, + Warning: the configuration value core.hooksPath was set to: /my-hooks, which is not the expected default value of: /.git/hooks The Git hooks above may have been installed to an unexpected global location. Successfully installed git-branchless. @@ -600,7 +600,7 @@ hooksPath = my-hooks Auto-detected your main branch as: master If this is incorrect, run: git branchless init --main-branch Installing hooks: post-applypatch, post-checkout, post-commit, post-merge, post-rewrite, pre-auto-gc, reference-transaction - Warning: the configuration value core.hooksPath was set to: my-hooks, + Warning: the configuration value core.hooksPath was set to: /my-hooks, which is not the expected default value of: /.git/hooks The Git hooks above may have been installed to an unexpected global location. Successfully installed git-branchless.