Skip to content

Commit 9bc92ae

Browse files
authored
Merge pull request #79 from blaggacao/da-add-support-for-stages
feat: support specifying stages, close #49
2 parents 117579f + 7cf8633 commit 9bc92ae

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

modules/pre-commit.nix

Lines changed: 46 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,19 @@ 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+
See https://pre-commit.com/#confining-hooks-to-run-at-certain-stages
281+
'';
282+
default = [ ];
283+
};
284+
271285
rawConfig =
272286
mkOption {
273287
type = types.attrs;
@@ -296,6 +310,8 @@ in
296310
];
297311
} // lib.optionalAttrs (cfg.excludes != [ ]) {
298312
exclude = mergeExcludes cfg.excludes;
313+
} // lib.optionalAttrs (cfg.default_stages != [ ]) {
314+
default_stages = cfg.default_stages;
299315
};
300316

301317
installationScript =
@@ -325,7 +341,36 @@ in
325341
echo 1>&2 " 3. add .pre-commit-config.yaml to .gitignore"
326342
else
327343
ln -s ${configFile} .pre-commit-config.yaml
328-
pre-commit install
344+
# Remove any previously installed hooks (since pre-commit itself has no convergent design)
345+
hooks="pre-commit pre-merge-commit pre-push prepare-commit-msg commit-msg post-checkout post-commit"
346+
for hook in $hooks; do
347+
pre-commit uninstall -t $hook
348+
done
349+
# Add hooks for configured stages (only) ...
350+
if [ ! -z "${concatStringsSep " " install_stages}" ]; then
351+
for stage in ${concatStringsSep " " install_stages}; do
352+
if [[ "$stage" == "manual" ]]; then
353+
continue
354+
fi
355+
case $stage in
356+
# if you amend these switches please also review $hooks above
357+
commit | merge-commit | push)
358+
stage="pre-"$stage
359+
pre-commit install -t $stage
360+
;;
361+
prepare-commit-msg | commit-msg | post-checkout | post-commit)
362+
pre-commit install -t $stage
363+
;;
364+
*)
365+
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."
366+
exit 1
367+
;;
368+
esac
369+
done
370+
# ... or default 'pre-commit' hook
371+
else
372+
pre-commit install
373+
fi
329374
fi
330375
fi
331376
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)