Skip to content
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

Resolve core.hooksPath relative to GIT_WORK_TREE #2571

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

naseschwarz
Copy link
Contributor

@naseschwarz naseschwarz commented Mar 18, 2025

git supports relative values in core.hooksPath.

man git-config:

A relative path is taken as relative to the directory where the hooks are
run (see the "DESCRIPTION" section of githooks[5]).

man githooks:

Before Git invokes a hook, it changes its working directory to either
$GIT_DIR in a bare repository or the root of the working tree in a >
non-bare repository.

I.e. relative paths in core.hooksPath in non-bare repositories are always relative to GIT_WORK_TREE.

There is a further exception; I believe this is not considered for path resolution:

An exception are hooks triggered during a push (pre-receive, update,
post-receive, post-update, push-to-checkout) which are always executed
in $GIT_DIR.

This Pull Request fixes/closes #2281.

It changes the following:

  • Synchronizes core.hooksPath resolution with the git command line client
  • Moves git2_hooks::create_hook_in_path to git2-testing
  • Moves git2_hooks::create_hook to git2_hooks::test_utils::create_hook, which is only pub for feature = "test-utils".

I followed the checklist:

  • I added unittests
  • I ran make check without errors
  • I tested the overall application
  • I added an appropriate item to the changelog

@naseschwarz naseschwarz force-pushed the issue-2281-fix branch 3 times, most recently from 22ad6a9 to d66d148 Compare March 18, 2025 23:02
///
/// # Panics
/// Panics if hook could not be created
pub fn create_hook_in_path(path: &Path, hook_script: &[u8]) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we just want this to be for testing, we might want to consider feature gating it so it does not become public api which would make it semver breaking to change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, git2_hooks::create_hook already has the same problem.

I've moved git2_hooks::create_hook_in_path into git2-testing. I've also moved git2_hooks::create_hook into a private mod and publish it via git2_hooks::test_utils::crate_hook only if the test-utils feature flag is set. (I can't move create_hook to git2-testing, as it has a dependency on "HookPaths".)

I'm open to as simpler and cleaner solution. Having this feature flag and the function in test_utils makes this a bit more explicit, but this is of course never tested or even built if all tests on this are run in this workspace with the flag enabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you want to deal with this discussion separately, I can easily move this into a separate PR by the way.

Naseschwarz added 3 commits March 19, 2025 12:11
This is a function that's solely used in testing and has no
dependencies into git2_hooks and panics on common errors.
It's a much better fit for git2-testing.
git2_hooks::create_hook is a function used in tests, which panics on common
errors. It can't really be moved into git2-testing, as it depends on
git2_hooks::hookspath::HookPaths.

In order to avoid accidental use, move the function into
git2_hooks::test_utils[_priv] and only publish it as git2_hooks::test_utils
if feature = "test-utils" is enabled.
git supports relative values in core.hooksPath.

`man git-config`:

> A relative path is taken as relative to the directory where the hooks are
> run (see the "DESCRIPTION" section of githooks[5]).

`man githooks`:

> Before Git invokes a hook, it changes its working directory to either
> $GIT_DIR in a bare repository or the root of the working tree in a >
> non-bare repository.

I.e. relative paths in core.hooksPath in non-bare repositories are always
relative to GIT_WORK_TREE.

There is a further exception; I believe this is not considered for path
resolution:

> An exception are hooks triggered during a push (pre-receive, update,
> post-receive, post-update, push-to-checkout) which are always executed
> in $GIT_DIR.
@naseschwarz naseschwarz marked this pull request as ready for review March 19, 2025 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pre-commit hooks are executed only when launched from the git root
2 participants