Skip to content

Commit f1d3d40

Browse files
bors[bot]roberth
andauthored
Merge #25
25: Fix caching r=roberth a=roberth I hope this is acceptable because it already took too many iterations imo. :rabbit: :hole: Co-authored-by: Robert Hensing <[email protected]>
2 parents e62f240 + 10806e4 commit f1d3d40

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"id": "shellcheck"
1313
}
1414
],
15-
"repo": ".pre-commit-hooks/",
15+
"repo": ".pre-commit-hooks",
1616
"rev": "master"
1717
}
1818
]

modules/pre-commit.nix

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,20 @@ let
179179
inherit (import (import ../nix/sources.nix).gitignore { inherit lib; })
180180
gitignoreSource
181181
;
182+
183+
patch =
184+
pre-commit:
185+
pre-commit.overrideAttrs (
186+
drv:
187+
{
188+
patches =
189+
(drv.patches or []) ++ [
190+
../nix/pre-commit-resolve-symlink.patch
191+
];
192+
name = "${drv.name}-nix";
193+
}
194+
);
195+
182196
in {
183197
options.pre-commit =
184198
{
@@ -190,10 +204,10 @@ in {
190204
''
191205
The pre-commit package to use.
192206
'';
193-
default = pkgs.pre-commit;
207+
default = patch pkgs.pre-commit;
194208
defaultText =
195209
literalExample ''
196-
pkgs.pre-commit
210+
patch pkgs.pre-commit
197211
'';
198212
};
199213

@@ -289,7 +303,7 @@ in {
289303
repos =
290304
[
291305
{
292-
repo = ".pre-commit-hooks/";
306+
repo = ".pre-commit-hooks";
293307
rev = "master";
294308
hooks =
295309
mapAttrsToList ( id: _value: { inherit id; } ) enabledHooks;
@@ -338,9 +352,10 @@ in {
338352
fi
339353
340354
if $doInstall; then
341-
pre-commit install
342-
# this is needed as the hook repo configuration is cached
355+
# This because we're still using a clone. The clean invocation can
356+
# be rid of when the patch uses a *working* non-clone code path.
343357
pre-commit clean
358+
pre-commit install
344359
fi
345360
fi
346361
'';

nix/pre-commit-resolve-symlink.patch

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
diff --git a/pre_commit/repository.py b/pre_commit/repository.py
2+
index 3042f12..16ad603 100644
3+
--- a/pre_commit/repository.py
4+
+++ b/pre_commit/repository.py
5+
@@ -195,11 +195,27 @@ def _cloned_repository_hooks(repo_config, store, root_config):
6+
)
7+
8+
9+
+def _adapt_for_external_cache(repo_config):
10+
+ if os.path.exists(repo_config['repo']) and os.path.islink(repo_config['repo']):
11+
+ target = os.readlink(repo_config['repo'])
12+
+ if target.startswith("/nix/store/"):
13+
+ repo_config['repo'] = target
14+
+ return True
15+
+ else:
16+
+ return False
17+
+ else:
18+
+ return False
19+
+
20+
+
21+
def _repository_hooks(repo_config, store, root_config):
22+
if repo_config['repo'] in {LOCAL, META}:
23+
return _non_cloned_repository_hooks(repo_config, store, root_config)
24+
else:
25+
- return _cloned_repository_hooks(repo_config, store, root_config)
26+
+ if _adapt_for_external_cache(repo_config):
27+
+ # No real need to clone, but _non_cloned seems to be broken...
28+
+ return _cloned_repository_hooks(repo_config, store, root_config)
29+
+ else:
30+
+ return _cloned_repository_hooks(repo_config, store, root_config)
31+
32+
33+
def install_hook_envs(hooks, store):

0 commit comments

Comments
 (0)