Skip to content

feat: support specifying stages #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
47 changes: 46 additions & 1 deletion modules/pre-commit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ let
inherit (pkgs) runCommand writeText git;

cfg = config.pre-commit;
# TODO: add per-hook stages to install here
install_stages = cfg.default_stages;

hookType =
types.submodule (
Expand Down Expand Up @@ -258,6 +260,18 @@ in
default = [];
};

default_stages =
mkOption {
type = types.listOf types.str;
description =
''
A configuration wide option for the stages property.
Installs hooks to the defined stages.
Default is empty which falls back to 'commit'.
'';
default = [];
};

rawConfig =
mkOption {
type = types.attrs;
Expand Down Expand Up @@ -286,6 +300,8 @@ in
];
} // lib.optionalAttrs (cfg.excludes != []) {
exclude = mergeExcludes cfg.excludes;
} // lib.optionalAttrs (cfg.default_stages != []) {
default_stages = cfg.default_stages;
};

pre-commit.installationScript =
Expand Down Expand Up @@ -315,7 +331,36 @@ in
echo 1>&2 " 3. add .pre-commit-config.yaml to .gitignore"
else
ln -s ${configFile} .pre-commit-config.yaml
pre-commit install
# Remove any previously installed hooks (since pre-commit itself has no convergent design)
hooks="pre-commit pre-merge-commit pre-push prepare-commit-msg commit-msg post-checkout post-commit"
for hook in $hooks; do
pre-commit uninstall -t $hook
done
# Add hooks for configured stages (only) ...
if [ ! -z "${concatStringsSep " " install_stages}" ]; then
for stage in ${concatStringsSep " " install_stages}; do
if [[ "$stage" == "manual" ]]; then
continue
fi
case $stage in
# if you amend these switches please also review $hooks above
commit | merge-commit | push)
stage="pre-"$stage
pre-commit install -t $stage
;;
prepare-commit-msg | commit-msg | post-checkout | post-commit)
pre-commit install -t $stage
;;
*)
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."
exit 1
;;
esac
done
# ... or default 'pre-commit' hook
else
pre-commit install
fi
fi
fi
fi
Expand Down
3 changes: 2 additions & 1 deletion nix/run.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ builtinStuff@{ pkgs, tools, pre-commit, git, runCommand, writeText, writeScript,
, excludes ? []
, tools ? {}
, settings ? {}
, default_stages ? []
}:

let
Expand Down Expand Up @@ -32,7 +33,7 @@ let
_module.args.pkgs = pkgs;
pre-commit =
{
inherit hooks excludes settings;
inherit hooks excludes settings default_stages;
tools = builtinStuff.tools // tools;
};
};
Expand Down