Skip to content

fix: respect core.hooksPath git config in commit hooks#2173

Open
mangodxd wants to merge 3 commits into
gitpython-developers:mainfrom
mangodxd:hookspath-fix
Open

fix: respect core.hooksPath git config in commit hooks#2173
mangodxd wants to merge 3 commits into
gitpython-developers:mainfrom
mangodxd:hookspath-fix

Conversation

@mangodxd

Copy link
Copy Markdown

Description

When core.hooksPath is set in any git config file (system, global, or repository level), commit hooks should be resolved relative to that path instead of the default .git/hooks directory.

Currently, run_commit_hook() always uses hook_path() which hardcodes {git_dir}/hooks/{name}, ignoring the core.hooksPath setting.

Changes

In run_commit_hook(), before falling back to the default hook path via hook_path(), the function now reads core.hooksPath from the repo configuration using config_reader().get(). If the setting exists:

  • Hooks are resolved relative to that path
  • If the path is relative (following git's behavior), it's resolved against the working tree root
  • If not set, the existing behavior is preserved unchanged

Testing

  • Existing test test_run_commit_hook continues to pass
  • The change is minimal (14 lines added) and non-breaking

Fixes #2083

@Byron Byron left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Please add tests.

Add 4 tests covering the hooksPath behavior:
- Absolute hooksPath: hooks in a custom absolute path are found
- Relative hooksPath: resolved against working tree root
- Fallback: default .git/hooks used when hooksPath not set
- Missing hook: no error raised when hook doesn't exist in hooksPath

All tests follow the existing test patterns and are xfail-marked for
Windows environments without bash.exe.

Addresses review request from maintainer (Byron).
@mangodxd

Copy link
Copy Markdown
Author

Tests added. Covers absolute hooksPath, relative hooksPath, fallback to default, and missing hook. All passing on this end.

- Use Python scripts instead of .sh for hook tests (works on Windows CI)
- Write output to working_dir instead of git_dir (bare repo compatibility)
- Apply ruff format to git/index/fun.py
- Removes xfail dependency on bash.exe for hooksPath tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Commit hooks don't respect core.hooksPath in config.

2 participants