GitPython
GitPython copied to clipboard
Fix commit hooks to respect core.hooksPath configuration
Summary
Fixes #2083
The run_commit_hook() function was hardcoded to look for hooks in $GIT_DIR/hooks, ignoring the core.hooksPath configuration option that Git has supported since v2.9.
Changes
- Add
_get_hooks_dir()helper function that readscore.hooksPathfrom config - Handle both absolute and relative paths per git-config documentation:
- Absolute paths: used as-is
- Relative paths: resolved relative to the working tree root (or git_dir for bare repos)
-
Not set: defaults to
$GIT_DIR/hooks
- Update
run_commit_hook()to use the new helper - Add comprehensive tests for both absolute and relative
hooksPathconfigurations
Testing
- All existing hook tests pass
- Added 2 new tests:
-
test_run_commit_hook_respects_core_hookspath- tests absolute path -
test_run_commit_hook_respects_relative_core_hookspath- tests relative path
-
Backwards Compatibility
The existing hook_path() function is preserved for backwards compatibility and has been documented to note that it does not respect the config. Code that directly uses hook_path() will behave as before. Only code using run_commit_hook() (including index.commit()) will benefit from the new behavior.
References
- Git documentation on core.hooksPath: https://git-scm.com/docs/git-config#Documentation/git-config.txt-corehooksPath
- Issue: https://github.com/gitpython-developers/GitPython/issues/2083