Skip to content

Commit 4e7fcba

Browse files
committedNov 6, 2019
Generate .pre-commit-config.yaml; add to gitignore!
1 parent eedea33 commit 4e7fcba

File tree

5 files changed

+23
-69
lines changed

5 files changed

+23
-69
lines changed
 

‎.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
result*
2-
.pre-commit-hooks
2+
3+
# Generated by nix-pre-commit-hooks
4+
/.pre-commit-config.yaml

‎.pre-commit-config.yaml

-19
This file was deleted.

‎CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
55
The format is loosely based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
66
and is a rolling release.
77

8+
## 2019-11-06
9+
10+
### Changed
11+
12+
- **BREAKING:** Add `/.pre-commit-config.yaml` to `/.gitignore`
13+
14+
- Do not generate `.pre-commit-hooks` fake repo
15+
816
## 2019-10-02
917

1018
### Added

‎README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ The goal is to manage these hooks with Nix and solve the following:
4949
}
5050
```
5151

52+
Add `/.pre-commit-config.yaml` to `.gitignore`.
53+
5254
Run `$ nix-shell` to execute `shellHook` which will:
53-
- install git hooks
54-
- symlink hooks to `.pre-commit-hooks/`
55-
- provide `pre-commit` executable that `git commit` will invoke
55+
- build the tools and `.pre-commit-config.yaml` config file symlink which
56+
references the binaries, for speed and safe garbage collection
57+
- provide the `pre-commit` executable that `git commit` will invoke
5658

5759
## Optional
5860

‎modules/pre-commit.nix

+7-46
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ let
125125
processedHooks =
126126
mapAttrsToList ( id: value: value.raw // { inherit id; } ) enabledHooks;
127127

128-
hooksFile =
129-
writeText "pre-commit-hooks.json" ( builtins.toJSON processedHooks );
130128
configFile =
131129
runCommand "pre-commit-config.json" {
132130
buildInputs = [ pkgs.jq ];
@@ -140,26 +138,12 @@ let
140138
} >$out
141139
'';
142140

143-
hooks =
144-
runCommand "pre-commit-hooks-dir" { buildInputs = [ git ]; } ''
145-
HOME=$PWD
146-
mkdir -p $out
147-
ln -s ${hooksFile} $out/.pre-commit-hooks.yaml
148-
cd $out
149-
git config --global user.email "you@example.com"
150-
git config --global user.name "Your Name"
151-
git init
152-
git add .
153-
git commit -m "init"
154-
'';
155-
156141
run =
157142
runCommand "pre-commit-run" { buildInputs = [ git ]; } ''
158143
set +e
159144
HOME=$PWD
160145
cp --no-preserve=mode -R ${cfg.rootSrc} src
161-
unlink src/.pre-commit-hooks || true
162-
ln -fs ${hooks} src/.pre-commit-hooks
146+
ln -fs ${configFile} src/.pre-commit-config.yaml
163147
cd src
164148
rm -rf src/.git
165149
git init
@@ -289,10 +273,8 @@ in {
289273
repos =
290274
[
291275
{
292-
repo = ".pre-commit-hooks/";
293-
rev = "master";
294-
hooks =
295-
mapAttrsToList ( id: _value: { inherit id; } ) enabledHooks;
276+
repo = "local";
277+
hooks = processedHooks;
296278
}
297279
];
298280
} // lib.optionalAttrs (cfg.excludes != []) {
@@ -306,41 +288,20 @@ in {
306288
# This happens in pure shells, including lorri
307289
echo 1>&2 "WARNING: nix-pre-commit-hooks: git command not found; skipping installation."
308290
else
309-
local doInstall=false
310-
311291
# These update procedures compare before they write, to avoid
312292
# filesystem churn. This improves performance with watch tools like lorri
313293
# and prevents installation loops by via lorri.
314294
315-
if readlink .pre-commit-hooks >/dev/null \
316-
&& [[ $(readlink .pre-commit-hooks) == ${hooks} ]]; then
295+
if readlink .pre-commit-config.yaml >/dev/null \
296+
&& [[ $(readlink .pre-commit-config.yaml) == ${configFile} ]]; then
317297
echo 1>&2 "nix-pre-commit-hooks: hooks up to date"
318298
else
319299
echo 1>&2 "nix-pre-commit-hooks: updating $PWD repo"
320300
321-
[ -L .pre-commit-hooks ] && unlink .pre-commit-hooks
322-
ln -s ${hooks} .pre-commit-hooks
323-
324-
doInstall=true
325-
fi
326-
327-
if cmp ${configFile} .pre-commit-config.yaml &>/dev/null; then
328-
echo 1>&2 "nix-pre-commit-hooks: config up to date"
329-
else
330-
echo 1>&2 "nix-pre-commit-hooks: updating $PWD config"
331-
332-
# This can't be a symlink because its path is not constant,
333-
# thus can not be committed and is invisible to pre-commit.
334-
rm -f .pre-commit-config.yaml
335-
cat ${configFile} >.pre-commit-config.yaml
336-
337-
doInstall=true
338-
fi
301+
[ -L .pre-commit-config.yaml ] && unlink .pre-commit-config.yaml
302+
ln -s ${configFile} .pre-commit-config.yaml
339303
340-
if $doInstall; then
341304
pre-commit install
342-
# this is needed as the hook repo configuration is cached
343-
pre-commit clean
344305
fi
345306
fi
346307
'';

0 commit comments

Comments
 (0)
Please sign in to comment.