Skip to content

Commit eff2d0d

Browse files
author
David Arnold
committed
feat: support specifying stages, close #49
1 parent efdbd6d commit eff2d0d

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

modules/pre-commit.nix

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ let
1515
inherit (pkgs) runCommand writeText git;
1616

1717
cfg = config;
18+
install_stages = cfg.default_stages;
1819

1920
hookType =
2021
types.submodule (
@@ -268,6 +269,18 @@ in
268269
default = [ ];
269270
};
270271

272+
default_stages =
273+
mkOption {
274+
type = types.listOf types.str;
275+
description =
276+
''
277+
A configuration wide option for the stages property.
278+
Installs hooks to the defined stages.
279+
Default is empty which falls back to 'commit'.
280+
'';
281+
default = [ ];
282+
};
283+
271284
rawConfig =
272285
mkOption {
273286
type = types.attrs;
@@ -296,6 +309,8 @@ in
296309
];
297310
} // lib.optionalAttrs (cfg.excludes != [ ]) {
298311
exclude = mergeExcludes cfg.excludes;
312+
} // lib.optionalAttrs (cfg.default_stages != [ ]) {
313+
default_stages = cfg.default_stages;
299314
};
300315

301316
installationScript =
@@ -325,7 +340,36 @@ in
325340
echo 1>&2 " 3. add .pre-commit-config.yaml to .gitignore"
326341
else
327342
ln -s ${configFile} .pre-commit-config.yaml
328-
pre-commit install
343+
# Remove any previously installed hooks (since pre-commit itself has no convergent design)
344+
hooks="pre-commit pre-merge-commit pre-push prepare-commit-msg commit-msg post-checkout post-commit"
345+
for hook in $hooks; do
346+
pre-commit uninstall -t $hook
347+
done
348+
# Add hooks for configured stages (only) ...
349+
if [ ! -z "${concatStringsSep " " install_stages}" ]; then
350+
for stage in ${concatStringsSep " " install_stages}; do
351+
if [[ "$stage" == "manual" ]]; then
352+
continue
353+
fi
354+
case $stage in
355+
# if you amend these switches please also review $hooks above
356+
commit | merge-commit | push)
357+
stage="pre-"$stage
358+
pre-commit install -t $stage
359+
;;
360+
prepare-commit-msg | commit-msg | post-checkout | post-commit)
361+
pre-commit install -t $stage
362+
;;
363+
*)
364+
echo 1>&2 "ERROR: nix-pre-commit-hooks: either $stage is not a valid stage or pre-commit-hooks.nix doesn't yet support it."
365+
exit 1
366+
;;
367+
esac
368+
done
369+
# ... or default 'pre-commit' hook
370+
else
371+
pre-commit install
372+
fi
329373
fi
330374
fi
331375
fi

nix/run.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ builtinStuff@{ pkgs, tools, pre-commit, git, runCommand, writeText, writeScript,
55
, excludes ? [ ]
66
, tools ? { }
77
, settings ? { }
8+
, default_stages ? [ ]
89
}:
910
let
1011
sources = import ./sources.nix;
@@ -18,7 +19,7 @@ let
1819
config =
1920
{
2021
_module.args.pkgs = pkgs;
21-
inherit hooks excludes settings src;
22+
inherit hooks excludes settings src default_stages;
2223
tools = builtinStuff.tools // tools;
2324
};
2425
}

0 commit comments

Comments
 (0)