Skip to content

Commit ec3d091

Browse files
remove .pre-commit-config.yaml if all hooks are disabled
1 parent 9e4003b commit ec3d091

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

src/modules/integrations/git-hooks.nix

+30-15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ let
1717
mkdir -p $out/bin
1818
ln -s ${cfg.package}/bin/pre-commit $out/bin/pre-commit
1919
'';
20+
21+
anyEnabled = ((lib.filterAttrs (id: value: value.enable) cfg.hooks) != { });
2022
in
2123
{
2224
imports = [
@@ -40,20 +42,33 @@ in
4042
description = "Integration with https://github.com/cachix/git-hooks.nix";
4143
};
4244

43-
config = lib.mkIf ((lib.filterAttrs (id: value: value.enable) cfg.hooks) != { }) {
44-
ci = [ cfg.run ];
45-
# Add the packages for any enabled hooks at the end to avoid overriding the language-defined packages.
46-
packages = lib.mkAfter ([ packageBin ] ++ (cfg.enabledPackages or [ ]));
47-
tasks = {
48-
# TODO: split installation script into status + exec
49-
"devenv:git-hooks:install" = {
50-
exec = cfg.installationScript;
51-
before = [ "devenv:enterShell" ];
52-
};
53-
"devenv:git-hooks:run" = {
54-
exec = "pre-commit run -a";
55-
before = [ "devenv:enterTest" ];
45+
config = lib.mkMerge [
46+
(lib.mkIf (!anyEnabled) {
47+
# Remove .pre-commit-config.yaml if it exists and is in the nix store
48+
enterShell = ''
49+
preCommitConfig="$DEVENV_ROOT/.pre-commit-config.yaml"
50+
if $(nix-store --quiet --verify-path "$preCommitConfig" > /dev/null 2>&1); then
51+
echo Removing "$preCommitConfig"
52+
rm -rf "$preCommitConfig"
53+
fi
54+
'';
55+
})
56+
57+
(lib.mkIf anyEnabled {
58+
ci = [ cfg.run ];
59+
# Add the packages for any enabled hooks at the end to avoid overriding the language-defined packages.
60+
packages = lib.mkAfter ([ packageBin ] ++ (cfg.enabledPackages or [ ]));
61+
tasks = {
62+
# TODO: split installation script into status + exec
63+
"devenv:git-hooks:install" = {
64+
exec = cfg.installationScript;
65+
before = [ "devenv:enterShell" ];
66+
};
67+
"devenv:git-hooks:run" = {
68+
exec = "pre-commit run -a";
69+
before = [ "devenv:enterTest" ];
70+
};
5671
};
57-
};
58-
};
72+
})
73+
];
5974
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if ! test -f "$DEVENV_ROOT/.pre-commit-config.yaml"; then
2+
echo "Test not setup correctly: .pre-commit-config.yaml not found"
3+
exit 1
4+
fi
5+
6+
echo "{ lib, ... }: { git-hooks.hooks.no-op.enable = lib.mkForce false; }" > devenv.local.nix
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Assert that .pre-commit-config.yaml is removed when all hooks are disabled
2+
{
3+
git-hooks.hooks = {
4+
no-op = {
5+
enable = true;
6+
name = "No Op";
7+
pass_filenames = false;
8+
raw.always_run = true;
9+
entry = "true";
10+
};
11+
};
12+
13+
enterTest = ''
14+
if test -f "$DEVENV_ROOT/.pre-commit-config.yaml"; then
15+
echo ".pre-commit-config.yaml not removed"
16+
exit 1
17+
fi
18+
'';
19+
}

0 commit comments

Comments
 (0)