Move most of the actual hook script into `pre-commit hook-impl` · precommit/pre-commit@95b8d71 · GitHub
Skip to content

Commit 95b8d71

Browse files
committed
Move most of the actual hook script into pre-commit hook-impl
1 parent 9315221 commit 95b8d71

10 files changed

Lines changed: 471 additions & 201 deletions

File tree

pre_commit/commands/hook_impl.py

Lines changed: 180 additions & 0 deletions

pre_commit/commands/install_uninstall.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def shebang() -> str:
6060
f'python{sys.version_info[0]}',
6161
]
6262
for path, exe in itertools.product(path_choices, exe_choices):
63-
if os.path.exists(os.path.join(path, exe)):
63+
if os.access(os.path.join(path, exe), os.X_OK):
6464
py = exe
6565
break
6666
else:
@@ -92,12 +92,10 @@ def _install_hook_script(
9292
f'Use -f to use only pre-commit.',
9393
)
9494

95-
params = {
96-
'CONFIG': config_file,
97-
'HOOK_TYPE': hook_type,
98-
'INSTALL_PYTHON': sys.executable,
99-
'SKIP_ON_MISSING_CONFIG': skip_on_missing_config,
100-
}
95+
args = ['hook-impl', f'--config={config_file}', f'--hook-type={hook_type}']
96+
if skip_on_missing_config:
97+
args.append('--skip-on-missing-config')
98+
params = {'INSTALL_PYTHON': sys.executable, 'ARGS': args}
10199

102100
with open(hook_path, 'w') as hook_file:
103101
contents = resource_text('hook-tmpl')

pre_commit/languages/python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def _find_by_sys_executable() -> Optional[str]:
5757
def _norm(path: str) -> Optional[str]:
5858
_, exe = os.path.split(path.lower())
5959
exe, _, _ = exe.partition('.exe')
60-
if find_executable(exe) and exe not in {'python', 'pythonw'}:
60+
if exe not in {'python', 'pythonw'} and find_executable(exe):
6161
return exe
6262
return None
6363

pre_commit/main.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pre_commit.commands.autoupdate import autoupdate
1414
from pre_commit.commands.clean import clean
1515
from pre_commit.commands.gc import gc
16+
from pre_commit.commands.hook_impl import hook_impl
1617
from pre_commit.commands.init_templatedir import init_templatedir
1718
from pre_commit.commands.install_uninstall import install
1819
from pre_commit.commands.install_uninstall import install_hooks
@@ -197,6 +198,16 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
197198
_add_color_option(clean_parser)
198199
_add_config_option(clean_parser)
199200

201+
hook_impl_parser = subparsers.add_parser('hook-impl')
202+
_add_color_option(hook_impl_parser)
203+
_add_config_option(hook_impl_parser)
204+
hook_impl_parser.add_argument('--hook-type')
205+
hook_impl_parser.add_argument('--hook-dir')
206+
hook_impl_parser.add_argument(
207+
'--skip-on-missing-config', action='store_true',
208+
)
209+
hook_impl_parser.add_argument(dest='rest', nargs=argparse.REMAINDER)
210+
200211
gc_parser = subparsers.add_parser('gc', help='Clean unused cached repos.')
201212
_add_color_option(gc_parser)
202213
_add_config_option(gc_parser)
@@ -329,6 +340,16 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
329340
return clean(store)
330341
elif args.command == 'gc':
331342
return gc(store)
343+
elif args.command == 'hook-impl':
344+
return hook_impl(
345+
store,
346+
config=args.config,
347+
color=args.color,
348+
hook_type=args.hook_type,
349+
hook_dir=args.hook_dir,
350+
skip_on_missing_config=args.skip_on_missing_config,
351+
args=args.rest[1:],
352+
)
332353
elif args.command == 'install':
333354
return install(
334355
args.config, store,

pre_commit/parse_shebang.py

Lines changed: 2 additions & 4 deletions

0 commit comments

Comments
 (0)