-
-
Notifications
You must be signed in to change notification settings - Fork 966
Fix commit hooks to respect core.hooksPath configuration #2090
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
MirrorDNA-Reflection-Protocol
wants to merge
4
commits into
gitpython-developers:main
Choose a base branch
from
MirrorDNA-Reflection-Protocol:fix-core-hookspath-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+161
−2
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7977004
Fix commit hooks to respect core.hooksPath configuration
MirrorDNA-Reflection-Protocol 1a10226
Add test for core.hooksPath in bare repositories
MirrorDNA-Reflection-Protocol 7b1acdb
Fix ruff lint: remove whitespace from blank lines in docstrings
MirrorDNA-Reflection-Protocol 6e58a69
Fix bare repo test: use custom-hooks dir inside git_dir
MirrorDNA-Reflection-Protocol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1055,6 +1055,128 @@ def test_run_commit_hook(self, rw_repo): | |
| output = Path(rw_repo.git_dir, "output.txt").read_text(encoding="utf-8") | ||
| self.assertEqual(output, "ran fake hook\n") | ||
|
|
||
| @pytest.mark.xfail( | ||
| type(_win_bash_status) is WinBashStatus.Absent, | ||
| reason="Can't run a hook on Windows without bash.exe.", | ||
| raises=HookExecutionError, | ||
| ) | ||
| @pytest.mark.xfail( | ||
| type(_win_bash_status) is WinBashStatus.WslNoDistro, | ||
| reason="Currently uses the bash.exe of WSL, even with no WSL distro installed", | ||
| raises=HookExecutionError, | ||
| ) | ||
| @with_rw_repo("HEAD", bare=False) | ||
| def test_run_commit_hook_respects_core_hookspath(self, rw_repo): | ||
| """Test that run_commit_hook() respects core.hooksPath configuration. | ||
|
|
||
| This addresses issue #2083 where commit hooks were always looked for in | ||
| $GIT_DIR/hooks instead of respecting the core.hooksPath config setting. | ||
| """ | ||
| index = rw_repo.index | ||
|
|
||
| # Create a custom hooks directory outside of .git | ||
| custom_hooks_dir = Path(rw_repo.working_tree_dir) / "custom-hooks" | ||
| custom_hooks_dir.mkdir() | ||
|
|
||
| # Create a hook in the custom location | ||
| custom_hook = custom_hooks_dir / "fake-hook" | ||
| custom_hook.write_text(HOOKS_SHEBANG + "echo 'ran from custom hooks path' >output.txt") | ||
| custom_hook.chmod(0o744) | ||
|
|
||
| # Set core.hooksPath in the repo config | ||
| with rw_repo.config_writer() as config: | ||
| config.set_value("core", "hooksPath", str(custom_hooks_dir)) | ||
|
|
||
| # Run the hook - it should use the custom path | ||
| run_commit_hook("fake-hook", index) | ||
|
|
||
| output_file = Path(rw_repo.working_tree_dir) / "output.txt" | ||
| self.assertTrue(output_file.exists(), "Hook should have created output.txt") | ||
| output = output_file.read_text(encoding="utf-8") | ||
| self.assertEqual(output, "ran from custom hooks path\n") | ||
|
|
||
| @pytest.mark.xfail( | ||
| type(_win_bash_status) is WinBashStatus.Absent, | ||
| reason="Can't run a hook on Windows without bash.exe.", | ||
| raises=HookExecutionError, | ||
| ) | ||
| @pytest.mark.xfail( | ||
| type(_win_bash_status) is WinBashStatus.WslNoDistro, | ||
| reason="Currently uses the bash.exe of WSL, even with no WSL distro installed", | ||
| raises=HookExecutionError, | ||
| ) | ||
| @with_rw_repo("HEAD", bare=False) | ||
| def test_run_commit_hook_respects_relative_core_hookspath(self, rw_repo): | ||
| """Test that run_commit_hook() handles relative core.hooksPath correctly. | ||
|
|
||
| Per git-config docs, relative paths for core.hooksPath are relative to | ||
| the directory where hooks are run (typically the working tree root). | ||
| """ | ||
| index = rw_repo.index | ||
|
|
||
| # Create a custom hooks directory with a relative path | ||
| custom_hooks_dir = Path(rw_repo.working_tree_dir) / "relative-hooks" | ||
| custom_hooks_dir.mkdir() | ||
|
|
||
| # Create a hook in the custom location | ||
| custom_hook = custom_hooks_dir / "fake-hook" | ||
| custom_hook.write_text(HOOKS_SHEBANG + "echo 'ran from relative hooks path' >output.txt") | ||
| custom_hook.chmod(0o744) | ||
|
|
||
| # Set core.hooksPath to a relative path | ||
| with rw_repo.config_writer() as config: | ||
| config.set_value("core", "hooksPath", "relative-hooks") | ||
|
|
||
| # Run the hook - it should resolve the relative path correctly | ||
| run_commit_hook("fake-hook", index) | ||
|
|
||
| output_file = Path(rw_repo.working_tree_dir) / "output.txt" | ||
| self.assertTrue(output_file.exists(), "Hook should have created output.txt") | ||
| output = output_file.read_text(encoding="utf-8") | ||
| self.assertEqual(output, "ran from relative hooks path\n") | ||
|
Comment on lines
1109
to
1136
|
||
|
|
||
| @pytest.mark.xfail( | ||
| type(_win_bash_status) is WinBashStatus.Absent, | ||
| reason="Can't run a hook on Windows without bash.exe.", | ||
| raises=HookExecutionError, | ||
| ) | ||
| @pytest.mark.xfail( | ||
| type(_win_bash_status) is WinBashStatus.WslNoDistro, | ||
| reason="Currently uses the bash.exe of WSL, even with no WSL distro installed", | ||
| raises=HookExecutionError, | ||
| ) | ||
| @with_rw_repo("HEAD", bare=True) | ||
| def test_run_commit_hook_respects_core_hookspath_bare_repo(self, rw_repo): | ||
| """Test that run_commit_hook() respects core.hooksPath in bare repositories. | ||
|
|
||
| For bare repos, relative paths should be resolved relative to git_dir since | ||
| there is no working tree. | ||
| """ | ||
| index = rw_repo.index | ||
|
|
||
| # Create a custom hooks directory inside the git_dir for bare repo | ||
| # This ensures the path is relative to working_dir (which equals git_dir for bare repos) | ||
| custom_hooks_dir = Path(rw_repo.git_dir) / "custom-hooks" | ||
| custom_hooks_dir.mkdir(exist_ok=True) | ||
|
|
||
| # Create a hook in the custom location | ||
| custom_hook = custom_hooks_dir / "fake-hook" | ||
| custom_hook.write_text(HOOKS_SHEBANG + "echo 'ran from custom hooks path in bare repo' >output.txt") | ||
| custom_hook.chmod(0o744) | ||
|
|
||
| # Set core.hooksPath in the repo config (absolute path) | ||
| with rw_repo.config_writer() as config: | ||
| config.set_value("core", "hooksPath", str(custom_hooks_dir)) | ||
|
|
||
| # Run the hook - it should use the custom path | ||
| run_commit_hook("fake-hook", index) | ||
|
|
||
| # Output goes to cwd, which for bare repos during hook execution is working_dir (same as git_dir) | ||
| output_file = Path(rw_repo.git_dir) / "output.txt" | ||
| self.assertTrue(output_file.exists(), "Hook should have created output.txt") | ||
| output = output_file.read_text(encoding="utf-8") | ||
| self.assertEqual(output, "ran from custom hooks path in bare repo\n") | ||
|
|
||
| @ddt.data((False,), (True,)) | ||
| @with_rw_directory | ||
| def test_hook_uses_shell_not_from_cwd(self, rw_dir, case): | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test only covers non-bare repositories (
bare=False). Consider adding test coverage for bare repositories withcore.hooksPathset, since the existingtest_run_commit_hooktest on line 1050 uses a bare repository. This is important because the logic in_get_hooks_dir()falls back togit_dirfor bare repos when resolving relative paths (line 98 ofgit/index/fun.py).