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

Add option to overwrite any existing config #572

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
34 changes: 25 additions & 9 deletions modules/pre-commit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,18 @@ in
'';
};

overwriteConfig =
mkOption {
type = types.bool;
default = false;
description =
''
Whether to overwrite any existing config at `configPath`.

When enabled, any existing config will be overwritten without prompting.
'';
};

src =
lib.mkOption {
description = ''
Expand Down Expand Up @@ -397,7 +409,6 @@ in

config =
{

rawConfig =
{
repos =
Expand Down Expand Up @@ -428,14 +439,19 @@ in
if ! readlink "''${GIT_WC}/${cfg.configPath}" >/dev/null \
|| [[ $(readlink "''${GIT_WC}/${cfg.configPath}") != ${cfg.configFile} ]]; then
echo 1>&2 "git-hooks.nix: updating $PWD repo"
[ -L ${cfg.configPath} ] && unlink ${cfg.configPath}

if [ -e "''${GIT_WC}/${cfg.configPath}" ]; then
echo 1>&2 "git-hooks.nix: WARNING: Refusing to install because of pre-existing ${cfg.configPath}"
echo 1>&2 " 1. Translate ${cfg.configPath} 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 ${cfg.configPath}"
echo 1>&2 " 3. add ${cfg.configPath} to .gitignore"
[ -L "''${GIT_WC}/${cfg.configPath}" ] && unlink "''${GIT_WC}/${cfg.configPath}"

if [[ -e "''${GIT_WC}/${cfg.configPath}" && ! ${boolToString cfg.overwriteConfig} ]]; then
echo 1>&2 "git-hooks.nix: WARNING: Refusing to install because of an existing config at ${cfg.configPath}"
echo 1>&2 ""
echo 1>&2 " To migrate the existing config to a Nix configuration:"
echo 1>&2 " 1. Translate the contents of ${cfg.configPath} into a Nix configuration."
echo 1>&2 " See https://github.com/cachix/git-hooks.nix#getting-started"
echo 1>&2 " 2. Remove ${cfg.configPath}"
echo 1>&2 " 3. Add ${cfg.configPath} to .gitignore"
echo 1>&2 ""
echo 1>&2 " To always overwrite the existing config, enable `overwriteConfig` in Nix."

else
if ${boolToString cfg.addGcRoot}; then
nix-store --add-root "''${GIT_WC}/${cfg.configPath}" --indirect --realise ${cfg.configFile}
Expand Down
36 changes: 16 additions & 20 deletions nix/run.nix
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
builtinStuff@{ pkgs, tools, isFlakes, pre-commit, git, runCommand, writeText, writeScript, lib, gitignore-nix-src }:

{ src
, settings ? { }
, hooks ? { }
, excludes ? [ ]
, tools ? { }
, default_stages ? [ "pre-commit" ]
, addGcRoot ? true
options@{ src
, imports ? [ ]
, configPath ? ".pre-commit-config.yaml"
, tools ? { }
, ...
}:
let
moduleOptions = builtins.removeAttrs options [ "imports" "tools" ];

project =
lib.evalModules {
modules =
[
../modules/all-modules.nix
{
config =
{
_module.args.pkgs = pkgs;
_module.args.gitignore-nix-src = gitignore-nix-src;
inherit hooks excludes default_stages settings addGcRoot configPath;
tools = builtinStuff.tools // tools;
package = pre-commit;
} // (if isFlakes
then { rootSrc = src; }
else {
rootSrc = gitignore-nix-src.lib.gitignoreSource src;
});
config = moduleOptions //
{
_module.args.pkgs = pkgs;
_module.args.gitignore-nix-src = gitignore-nix-src;
package = lib.mkDefault pre-commit;
tools = lib.mkDefault (builtinStuff.tools // tools);
} // (if isFlakes
then { rootSrc = src; }
else {
rootSrc = gitignore-nix-src.lib.gitignoreSource src;
});
}
] ++ imports;
};
Expand Down