Skip to content

Commit 2f94cff

Browse files
committed
Extract prek builtin hooks
1 parent 61ab0e8 commit 2f94cff

1 file changed

Lines changed: 42 additions & 10 deletions

File tree

modules/pre-commit.nix

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ let
3636
(builtins.attrValues enabledHooks);
3737
in
3838
if sortedHooks ? result then
39-
builtins.map (value: value.raw) sortedHooks.result
39+
sortedHooks.result
4040
else
4141
let
4242
getIds = builtins.map (value: value.id);
@@ -61,6 +61,33 @@ let
6161
6262
${prettyPrintCycle { indent = " "; } sortedHooks.cycle}
6363
'';
64+
_prekBuiltins =
65+
if usingPrek then
66+
builtins.filter (s: s != "") (builtins.split "\n" (
67+
builtins.readFile (
68+
pkgs.runCommand "prek-builtins.txt"
69+
{
70+
buildInputs = [ cfg.package ];
71+
PREK_HOME = "/tmp/"; # This is to avoid prek trying to create ~/.cache/prek/
72+
}
73+
''${lib.getExe cfg.package} util list-builtins > $out''
74+
)
75+
))
76+
else
77+
[ ];
78+
79+
_is_builtin =
80+
{
81+
package,
82+
name,
83+
...
84+
}:
85+
usingPrek && (package.pname == "pre-commit-hooks") && (builtins.elem name _prekBuiltins);
86+
87+
_partitioned = builtins.partition _is_builtin processedHooks;
88+
89+
builtinHooks = builtins.map (value: builtins.removeAttrs value.raw [ "entry" ]) _partitioned.right;
90+
localHooks = builtins.map (value: value.raw) _partitioned.wrong;
6491

6592
configFile =
6693
performAssertions (
@@ -439,10 +466,10 @@ in
439466
message = "The `purty` hook has been removed because the project is unmaintained. Consider using `purs-tidy` instead.";
440467
}
441468
{
442-
assertion = usingPrek || (lib.all (hook: !(hook ? priority)) processedHooks);
469+
assertion = usingPrek || (lib.all (hook: !(hook ? priority)) localHooks);
443470
message =
444471
let
445-
hooksWithPriority = lib.filter (hook: hook ? priority) processedHooks;
472+
hooksWithPriority = lib.filter (hook: hook ? priority) localHooks;
446473
hookNames = lib.concatMapStringsSep ", " (hook: hook.id) hooksWithPriority;
447474
in
448475
''
@@ -455,13 +482,18 @@ in
455482

456483
rawConfig =
457484
{
458-
repos =
459-
[
460-
{
461-
repo = "local";
462-
hooks = processedHooks;
463-
}
464-
];
485+
repos = [
486+
{
487+
repo = "local";
488+
hooks = localHooks;
489+
}
490+
]
491+
++ lib.optionals (builtinHooks != [ ]) [
492+
{
493+
repo = "builtin";
494+
hooks = builtinHooks;
495+
}
496+
];
465497
} // lib.optionalAttrs (cfg.excludes != [ ]) {
466498
exclude = mergeExcludes cfg.excludes;
467499
} // lib.optionalAttrs (cfg.default_stages != [ ]) {

0 commit comments

Comments
 (0)