Skip to content

Commit

Permalink
fix(init): resolve hooks path relative to repo root
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ethanwu10 committed Nov 15, 2024
1 parent 7e8853e commit 5c0aa86
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion git-branchless-lib/src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
};
Expand Down
4 changes: 2 additions & 2 deletions git-branchless/tests/test_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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: <repo-path>/my-hooks,
which is not the expected default value of: <repo-path>/.git/hooks
The Git hooks above may have been installed to an unexpected global location.
Successfully installed git-branchless.
Expand Down Expand Up @@ -600,7 +600,7 @@ hooksPath = my-hooks
Auto-detected your main branch as: master
If this is incorrect, run: git branchless init --main-branch <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: <repo-path>/my-hooks,
which is not the expected default value of: <repo-path>/.git/hooks
The Git hooks above may have been installed to an unexpected global location.
Successfully installed git-branchless.
Expand Down

0 comments on commit 5c0aa86

Please sign in to comment.