Skip to content

Commit 4bcc124

Browse files
authored
Merge pull request #610 from cachix/enable/install.enable
add knobs for disabling the whole thing and disabling installation
2 parents 75c8e51 + d8884ae commit 4bcc124

File tree

1 file changed

+106
-82
lines changed

1 file changed

+106
-82
lines changed

modules/pre-commit.nix

Lines changed: 106 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,29 @@ in
139139
options =
140140
{
141141

142+
enable = mkOption {
143+
type = types.bool;
144+
default = true;
145+
description = ''
146+
Whether to enable the pre-commit hooks module.
147+
148+
When set to false, this disables the entire module.
149+
'';
150+
};
151+
152+
install = {
153+
enable = mkOption {
154+
type = types.bool;
155+
default = true;
156+
description = ''
157+
Whether to enable automatic installation of pre-commit hooks.
158+
159+
When set to false, hooks will not be installed into the git repository,
160+
but all other module functionality (like configuration generation) will still work.
161+
'';
162+
};
163+
};
164+
142165
package =
143166
mkOption {
144167
type = types.package;
@@ -399,96 +422,97 @@ in
399422
};
400423
};
401424

402-
config =
403-
{
425+
config = lib.mkIf cfg.enable {
404426

405-
rawConfig =
406-
{
407-
repos =
408-
[
409-
{
410-
repo = "local";
411-
hooks = processedHooks;
412-
}
413-
];
414-
} // lib.optionalAttrs (cfg.excludes != [ ]) {
415-
exclude = mergeExcludes cfg.excludes;
416-
} // lib.optionalAttrs (cfg.default_stages != [ ]) {
417-
default_stages = cfg.default_stages;
418-
};
427+
rawConfig =
428+
{
429+
repos =
430+
[
431+
{
432+
repo = "local";
433+
hooks = processedHooks;
434+
}
435+
];
436+
} // lib.optionalAttrs (cfg.excludes != [ ]) {
437+
exclude = mergeExcludes cfg.excludes;
438+
} // lib.optionalAttrs (cfg.default_stages != [ ]) {
439+
default_stages = cfg.default_stages;
440+
};
419441

420-
installationScript =
421-
''
422-
export PATH=${cfg.package}/bin:$PATH
442+
installationScript =
443+
''
444+
export PATH=${cfg.package}/bin:$PATH
445+
if ${boolToString cfg.install.enable}; then
423446
if ! ${cfg.gitPackage}/bin/git rev-parse --git-dir &> /dev/null; then
424447
echo 1>&2 "WARNING: git-hooks.nix: .git not found; skipping installation."
425448
else
426-
GIT_WC=`${cfg.gitPackage}/bin/git rev-parse --show-toplevel`
427-
428-
# These update procedures compare before they write, to avoid
429-
# filesystem churn. This improves performance with watch tools like lorri
430-
# and prevents installation loops by lorri.
431-
432-
if ! readlink "''${GIT_WC}/${cfg.configPath}" >/dev/null \
433-
|| [[ $(readlink "''${GIT_WC}/${cfg.configPath}") != ${cfg.configFile} ]]; then
434-
echo 1>&2 "git-hooks.nix: updating $PWD repo"
435-
[ -L "''${GIT_WC}/${cfg.configPath}" ] && unlink "''${GIT_WC}/${cfg.configPath}"
436-
437-
if [ -e "''${GIT_WC}/${cfg.configPath}" ]; then
438-
echo 1>&2 "git-hooks.nix: WARNING: Refusing to install because of an existing config at ${cfg.configPath}"
439-
echo 1>&2 ""
440-
echo 1>&2 " To migrate the existing config to a Nix configuration:"
441-
echo 1>&2 " 1. Translate the contents of ${cfg.configPath} into a Nix configuration."
442-
echo 1>&2 " See https://github.com/cachix/git-hooks.nix#getting-started"
443-
echo 1>&2 " 2. Remove ${cfg.configPath}"
444-
echo 1>&2 " 3. Add ${cfg.configPath} to .gitignore"
449+
GIT_WC=`${cfg.gitPackage}/bin/git rev-parse --show-toplevel`
450+
451+
# These update procedures compare before they write, to avoid
452+
# filesystem churn. This improves performance with watch tools like lorri
453+
# and prevents installation loops by lorri.
454+
455+
if ! readlink "''${GIT_WC}/${cfg.configPath}" >/dev/null \
456+
|| [[ $(readlink "''${GIT_WC}/${cfg.configPath}") != ${cfg.configFile} ]]; then
457+
echo 1>&2 "git-hooks.nix: updating $PWD repo"
458+
[ -L "''${GIT_WC}/${cfg.configPath}" ] && unlink "''${GIT_WC}/${cfg.configPath}"
459+
460+
if [ -e "''${GIT_WC}/${cfg.configPath}" ]; then
461+
echo 1>&2 "git-hooks.nix: WARNING: Refusing to install because of an existing config at ${cfg.configPath}"
462+
echo 1>&2 ""
463+
echo 1>&2 " To migrate the existing config to a Nix configuration:"
464+
echo 1>&2 " 1. Translate the contents of ${cfg.configPath} into a Nix configuration."
465+
echo 1>&2 " See https://github.com/cachix/git-hooks.nix#getting-started"
466+
echo 1>&2 " 2. Remove ${cfg.configPath}"
467+
echo 1>&2 " 3. Add ${cfg.configPath} to .gitignore"
468+
else
469+
if ${boolToString cfg.addGcRoot}; then
470+
nix-store --add-root "''${GIT_WC}/${cfg.configPath}" --indirect --realise ${cfg.configFile}
445471
else
446-
if ${boolToString cfg.addGcRoot}; then
447-
nix-store --add-root "''${GIT_WC}/${cfg.configPath}" --indirect --realise ${cfg.configFile}
448-
else
449-
ln -fs ${cfg.configFile} "''${GIT_WC}/${cfg.configPath}"
450-
fi
451-
# Remove any previously installed hooks (since pre-commit itself has no convergent design)
452-
hooks="${concatStringsSep " " (remove "manual" supportedHooksLib.supportedHooks )}"
453-
for hook in $hooks; do
454-
pre-commit uninstall -t $hook
472+
ln -fs ${cfg.configFile} "''${GIT_WC}/${cfg.configPath}"
473+
fi
474+
# Remove any previously installed hooks (since pre-commit itself has no convergent design)
475+
hooks="${concatStringsSep " " (remove "manual" supportedHooksLib.supportedHooks )}"
476+
for hook in $hooks; do
477+
pre-commit uninstall -t $hook
478+
done
479+
${cfg.gitPackage}/bin/git config --local core.hooksPath ""
480+
# Add hooks for configured stages (only) ...
481+
if [ ! -z "${concatStringsSep " " install_stages}" ]; then
482+
for stage in ${concatStringsSep " " install_stages}; do
483+
case $stage in
484+
manual)
485+
;;
486+
# if you amend these switches please also review $hooks above
487+
commit | merge-commit | push)
488+
stage="pre-"$stage
489+
pre-commit install -c ${cfg.configPath} -t $stage
490+
;;
491+
${concatStringsSep "|" supportedHooksLib.supportedHooks})
492+
pre-commit install -c ${cfg.configPath} -t $stage
493+
;;
494+
*)
495+
echo 1>&2 "ERROR: git-hooks.nix: either $stage is not a valid stage or git-hooks.nix doesn't yet support it."
496+
exit 1
497+
;;
498+
esac
455499
done
456-
${cfg.gitPackage}/bin/git config --local core.hooksPath ""
457-
# Add hooks for configured stages (only) ...
458-
if [ ! -z "${concatStringsSep " " install_stages}" ]; then
459-
for stage in ${concatStringsSep " " install_stages}; do
460-
case $stage in
461-
manual)
462-
;;
463-
# if you amend these switches please also review $hooks above
464-
commit | merge-commit | push)
465-
stage="pre-"$stage
466-
pre-commit install -c ${cfg.configPath} -t $stage
467-
;;
468-
${concatStringsSep "|" supportedHooksLib.supportedHooks})
469-
pre-commit install -c ${cfg.configPath} -t $stage
470-
;;
471-
*)
472-
echo 1>&2 "ERROR: git-hooks.nix: either $stage is not a valid stage or git-hooks.nix doesn't yet support it."
473-
exit 1
474-
;;
475-
esac
476-
done
477-
# ... or default 'pre-commit' hook
478-
else
479-
pre-commit install -c ${cfg.configPath}
480-
fi
481-
482-
# Fetch the absolute path to the git common directory. This will normally point to $GIT_WC/.git.
483-
common_dir=''$(${cfg.gitPackage}/bin/git rev-parse --path-format=absolute --git-common-dir)
484-
485-
# Convert the absolute path to a path relative to the toplevel working directory.
486-
common_dir=''${common_dir#''$GIT_WC/}
487-
488-
${cfg.gitPackage}/bin/git config --local core.hooksPath "''$common_dir/hooks"
500+
# ... or default 'pre-commit' hook
501+
else
502+
pre-commit install -c ${cfg.configPath}
489503
fi
504+
505+
# Fetch the absolute path to the git common directory. This will normally point to $GIT_WC/.git.
506+
common_dir=''$(${cfg.gitPackage}/bin/git rev-parse --path-format=absolute --git-common-dir)
507+
508+
# Convert the absolute path to a path relative to the toplevel working directory.
509+
common_dir=''${common_dir#''$GIT_WC/}
510+
511+
${cfg.gitPackage}/bin/git config --local core.hooksPath "''$common_dir/hooks"
490512
fi
491513
fi
492-
'';
493-
};
514+
fi
515+
fi
516+
'';
517+
};
494518
}

0 commit comments

Comments
 (0)