Skip to content

Commit a42c1c7

Browse files
committed
fix pre_commit_cache_dir when grep.extendedRegexp=true
1 parent cfd7561 commit a42c1c7

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

all_repos/autofix/pre_commit_cache_dir.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,26 @@
88
from all_repos.config import Config
99
from all_repos.grep import repos_matching
1010

11-
12-
APPVEYOR = 'appveyor.yml'
13-
TRAVIS = '.travis.yml'
11+
REPLACES = (
12+
(
13+
'appveyor.yml',
14+
r'%USERPROFILE%\.pre-commit',
15+
r'%USERPROFILE%\.cache\pre-commit',
16+
),
17+
(
18+
'.travis.yml',
19+
'$HOME/.pre-commit',
20+
'$HOME/.cache/pre-commit',
21+
),
22+
)
1423

1524

1625
def find_repos(config: Config) -> set[str]:
17-
return (
18-
repos_matching(config, ('$HOME/.pre-commit', '--', TRAVIS)) |
19-
repos_matching(config, (r'%USERPROFILE%\\.pre-commit', '--', APPVEYOR))
20-
)
26+
return {
27+
repo
28+
for fname, pattern, _ in REPLACES
29+
for repo in repos_matching(config, ('-F', pattern, '--', fname))
30+
}
2131

2232

2333
def _replace_if_exists(filename: str, s1: str, s2: str) -> None:
@@ -30,11 +40,8 @@ def _replace_if_exists(filename: str, s1: str, s2: str) -> None:
3040

3141

3242
def apply_fix() -> None:
33-
_replace_if_exists(TRAVIS, '$HOME/.pre-commit', '$HOME/.cache/pre-commit')
34-
_replace_if_exists(
35-
APPVEYOR,
36-
r'%USERPROFILE%\.pre-commit', r'%USERPROFILE%\.cache\pre-commit',
37-
)
43+
for fname, find, replace in REPLACES:
44+
_replace_if_exists(fname, find, replace)
3845

3946

4047
def main(argv: Sequence[str] | None = None) -> int:

tests/autofix/pre_commit_cache_dir_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from __future__ import annotations
22

3+
import os
4+
from unittest import mock
5+
36
from all_repos import clone
47
from all_repos.autofix.pre_commit_cache_dir import main
58
from testing.git import write_file_commit
@@ -66,3 +69,13 @@ def test_main(file_config_files):
6669
r" - '%LOCALAPPDATA%\pip\cache'\n"
6770
r" - '%USERPROFILE%\.cache\pre-commit'\n"
6871
)
72+
73+
74+
def test_with_extended_regexp(file_config_files):
75+
git_env = {
76+
'GIT_CONFIG_COUNT': '1',
77+
'GIT_CONFIG_KEY_0': 'grep.extendedRegexp',
78+
'GIT_CONFIG_VALUE_0': 'true',
79+
}
80+
with mock.patch.dict(os.environ, git_env):
81+
test_main(file_config_files)

0 commit comments

Comments
 (0)