git-hooks.nix icon indicating copy to clipboard operation
git-hooks.nix copied to clipboard

`post-checkout` hooks do not run on new worktrees when `.pre-commit-config.yaml` is not committed

Open eval-on-point opened this issue 1 year ago • 0 comments

Given the following flake.nix:

{
  inputs.pre-commit-hooks.url = "github:cachix/git-hooks.nix";

  outputs =
    { self, nixpkgs, ... }@inputs:
    let
      supportedSystems = [
        "x86_64-linux"
        "aarch64-linux"
        "x86_64-darwin"
        "aarch64-darwin"
      ];

      forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
    in
    {
      checks = forAllSystems (system: {
        pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run {
          src = ./.;
          hooks = {
            post-checkout = {
              enable = true;
              entry = "echo success!";
              always_run = true;
              stages = [ "post-checkout" ];
            };
          };
        };
      });

      devShells = forAllSystems (system: {
        default = nixpkgs.legacyPackages.${system}.mkShell {
          inherit (self.checks.${system}.pre-commit-check) shellHook;
          buildInputs = self.checks.${system}.pre-commit-check.enabledPackages;
        };
      });
    };
}

The post-checkout hook does not run if .pre-commit.yaml is not in the repository:

$ nix develop
...
pre-commit installed at .git/hooks/post-checkout

$ git worktree add ../test
Preparing worktree (new branch 'test')
warning: asked to inherit tracking from 'main', but no remote is set
HEAD is now at b67e139 init
No .pre-commit-config.yaml file was found
- To temporarily silence this, run `PRE_COMMIT_ALLOW_NO_CONFIG=1 git ...`
- To permanently silence this, install pre-commit with the --allow-missing-config option
- To uninstall pre-commit run `pre-commit uninstall`

I believe that either #494 or #496 would create configuration options to fix this by using an absolute path for the .pre-commit-config.yaml.

eval-on-point avatar Jan 09 '25 14:01 eval-on-point