Skip to content

Commit

Permalink
Generate .pre-commit-config.yaml; add to gitignore!
Browse files Browse the repository at this point in the history
  • Loading branch information
roberth committed Nov 6, 2019
1 parent eedea33 commit 4e7fcba
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 69 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
result*
.pre-commit-hooks

# Generated by nix-pre-commit-hooks
/.pre-commit-config.yaml
19 changes: 0 additions & 19 deletions .pre-commit-config.yaml

This file was deleted.

8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is loosely based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and is a rolling release.

## 2019-11-06

### Changed

- **BREAKING:** Add `/.pre-commit-config.yaml` to `/.gitignore`

- Do not generate `.pre-commit-hooks` fake repo

## 2019-10-02

### Added
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ The goal is to manage these hooks with Nix and solve the following:
}
```

Add `/.pre-commit-config.yaml` to `.gitignore`.

Run `$ nix-shell` to execute `shellHook` which will:
- install git hooks
- symlink hooks to `.pre-commit-hooks/`
- provide `pre-commit` executable that `git commit` will invoke
- build the tools and `.pre-commit-config.yaml` config file symlink which
references the binaries, for speed and safe garbage collection
- provide the `pre-commit` executable that `git commit` will invoke

## Optional

Expand Down
53 changes: 7 additions & 46 deletions modules/pre-commit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ let
processedHooks =
mapAttrsToList ( id: value: value.raw // { inherit id; } ) enabledHooks;

hooksFile =
writeText "pre-commit-hooks.json" ( builtins.toJSON processedHooks );
configFile =
runCommand "pre-commit-config.json" {
buildInputs = [ pkgs.jq ];
Expand All @@ -140,26 +138,12 @@ let
} >$out
'';

hooks =
runCommand "pre-commit-hooks-dir" { buildInputs = [ git ]; } ''
HOME=$PWD
mkdir -p $out
ln -s ${hooksFile} $out/.pre-commit-hooks.yaml
cd $out
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
git init
git add .
git commit -m "init"
'';

run =
runCommand "pre-commit-run" { buildInputs = [ git ]; } ''
set +e
HOME=$PWD
cp --no-preserve=mode -R ${cfg.rootSrc} src
unlink src/.pre-commit-hooks || true
ln -fs ${hooks} src/.pre-commit-hooks
ln -fs ${configFile} src/.pre-commit-config.yaml
cd src
rm -rf src/.git
git init
Expand Down Expand Up @@ -289,10 +273,8 @@ in {
repos =
[
{
repo = ".pre-commit-hooks/";
rev = "master";
hooks =
mapAttrsToList ( id: _value: { inherit id; } ) enabledHooks;
repo = "local";
hooks = processedHooks;
}
];
} // lib.optionalAttrs (cfg.excludes != []) {
Expand All @@ -306,41 +288,20 @@ in {
# This happens in pure shells, including lorri
echo 1>&2 "WARNING: nix-pre-commit-hooks: git command not found; skipping installation."
else
local doInstall=false
# These update procedures compare before they write, to avoid
# filesystem churn. This improves performance with watch tools like lorri
# and prevents installation loops by via lorri.
if readlink .pre-commit-hooks >/dev/null \
&& [[ $(readlink .pre-commit-hooks) == ${hooks} ]]; then
if readlink .pre-commit-config.yaml >/dev/null \
&& [[ $(readlink .pre-commit-config.yaml) == ${configFile} ]]; then
echo 1>&2 "nix-pre-commit-hooks: hooks up to date"
else
echo 1>&2 "nix-pre-commit-hooks: updating $PWD repo"
[ -L .pre-commit-hooks ] && unlink .pre-commit-hooks
ln -s ${hooks} .pre-commit-hooks
doInstall=true
fi
if cmp ${configFile} .pre-commit-config.yaml &>/dev/null; then
echo 1>&2 "nix-pre-commit-hooks: config up to date"
else
echo 1>&2 "nix-pre-commit-hooks: updating $PWD config"
# This can't be a symlink because its path is not constant,
# thus can not be committed and is invisible to pre-commit.
rm -f .pre-commit-config.yaml
cat ${configFile} >.pre-commit-config.yaml
doInstall=true
fi
[ -L .pre-commit-config.yaml ] && unlink .pre-commit-config.yaml
ln -s ${configFile} .pre-commit-config.yaml
if $doInstall; then
pre-commit install
# this is needed as the hook repo configuration is cached
pre-commit clean
fi
fi
'';
Expand Down

0 comments on commit 4e7fcba

Please sign in to comment.