-
Notifications
You must be signed in to change notification settings - Fork 174
feat: support specifying stages, close #49 #79
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ let | |
inherit (pkgs) runCommand writeText git; | ||
|
||
cfg = config; | ||
install_stages = cfg.default_stages; | ||
|
||
hookType = | ||
types.submodule ( | ||
|
@@ -268,6 +269,19 @@ 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'. | ||
See https://pre-commit.com/#confining-hooks-to-run-at-certain-stages | ||
''; | ||
default = [ ]; | ||
}; | ||
|
||
rawConfig = | ||
mkOption { | ||
type = types.attrs; | ||
|
@@ -296,6 +310,8 @@ in | |
]; | ||
} // lib.optionalAttrs (cfg.excludes != [ ]) { | ||
exclude = mergeExcludes cfg.excludes; | ||
} // lib.optionalAttrs (cfg.default_stages != [ ]) { | ||
default_stages = cfg.default_stages; | ||
}; | ||
|
||
installationScript = | ||
|
@@ -325,7 +341,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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we diverge from pre-commit settings by not just specifying pre-push instead of push? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Precommit has two identifiers "push" (the stage) and "pre-push" (the hook) in an attempt to be extra "correct" about things, while achieving only plain confusion. Here it's "push" (the stage). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ps: I also wondered and got bitten. More than once. |
||
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 | ||
|
Uh oh!
There was an error while loading. Please reload this page.