Skip to content
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

fix: add configFile option #496

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions modules/pre-commit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@ in
display a warning message when a renamed option is used.
'';
};

configFile = lib.mkOption {
type = types.str;
default = ".pre-commit-config.yaml";
description = ''The name of the config file.'';
};
};

config =
Expand Down Expand Up @@ -397,22 +403,22 @@ in
# filesystem churn. This improves performance with watch tools like lorri
# and prevents installation loops by lorri.

if ! readlink "''${GIT_WC}/.pre-commit-config.yaml" >/dev/null \
|| [[ $(readlink "''${GIT_WC}/.pre-commit-config.yaml") != ${configFile} ]]; then
if ! readlink "''${GIT_WC}/${cfg.configFile}" >/dev/null \
|| [[ $(readlink "''${GIT_WC}/${cfg.configFile}") != ${configFile} ]]; then
echo 1>&2 "git-hooks.nix: updating $PWD repo"
[ -L .pre-commit-config.yaml ] && unlink .pre-commit-config.yaml
[ -L ${cfg.configFile} ] && unlink ${cfg.configFile}

if [ -e "''${GIT_WC}/.pre-commit-config.yaml" ]; then
echo 1>&2 "git-hooks.nix: WARNING: Refusing to install because of pre-existing .pre-commit-config.yaml"
echo 1>&2 " 1. Translate .pre-commit-config.yaml contents to the new syntax in your Nix file"
if [ -e "''${GIT_WC}/${cfg.configFile}" ]; then
echo 1>&2 "git-hooks.nix: WARNING: Refusing to install because of pre-existing ${cfg.configFile}"
echo 1>&2 " 1. Translate ${cfg.configFile} contents to the new syntax in your Nix file"
echo 1>&2 " see https://github.com/cachix/git-hooks.nix#getting-started"
echo 1>&2 " 2. remove .pre-commit-config.yaml"
echo 1>&2 " 3. add .pre-commit-config.yaml to .gitignore"
echo 1>&2 " 2. remove ${cfg.configFile}"
echo 1>&2 " 3. add ${cfg.configFile} to .gitignore"
else
if ${boolToString cfg.addGcRoot}; then
nix-store --add-root "''${GIT_WC}/.pre-commit-config.yaml" --indirect --realise ${configFile}
nix-store --add-root "''${GIT_WC}/${cfg.configFile}" --indirect --realise ${configFile}
else
ln -fs ${configFile} "''${GIT_WC}/.pre-commit-config.yaml"
ln -fs ${configFile} "''${GIT_WC}/${cfg.configFile}"
fi
# Remove any previously installed hooks (since pre-commit itself has no convergent design)
hooks="${concatStringsSep " " (remove "manual" supportedHooksLib.supportedHooks )}"
Expand All @@ -429,10 +435,10 @@ in
# if you amend these switches please also review $hooks above
commit | merge-commit | push)
stage="pre-"$stage
pre-commit install -t $stage
pre-commit install -c ${cfg.configFile} -t $stage
;;
${concatStringsSep "|" supportedHooksLib.supportedHooks})
pre-commit install -t $stage
pre-commit install -c ${cfg.configFile} -t $stage
;;
*)
echo 1>&2 "ERROR: git-hooks.nix: either $stage is not a valid stage or git-hooks.nix doesn't yet support it."
Expand All @@ -442,7 +448,7 @@ in
done
# ... or default 'pre-commit' hook
else
pre-commit install
pre-commit install -c ${cfg.configFile}
fi

# Fetch the absolute path to the git common directory. This will normally point to $GIT_WC/.git.
Expand Down