22
33import argparse
44import logging
5+ import os
56import shutil
67import subprocess
78from pathlib import Path
8- from typing import TYPE_CHECKING , Final
9+ from typing import TYPE_CHECKING , Final , cast
910
1011from pre_commit_hooks import util
1112
1516logger = logging .getLogger (__name__ )
1617
1718SHELLCHECK_PATH : Final [str | None ] = shutil .which ("shellcheck" )
19+ UV_SHELLCHECK_PATH : Final [str | None ] = shutil .which ("uv" )
1820
1921
2022def _get_files (filenames : Sequence [str ]) -> set [Path ]:
@@ -30,18 +32,31 @@ def main() -> int:
3032 nargs = "*" ,
3133 help = "Filenames pre-commit believes are changed." ,
3234 )
35+ parser .add_argument (
36+ "--cwd" ,
37+ type = str ,
38+ default = str (Path .cwd ()),
39+ help = "The current working directory." ,
40+ )
3341
3442 args , extra_args = parser .parse_known_args ()
3543
36- if SHELLCHECK_PATH is None :
44+ if SHELLCHECK_PATH is None and UV_SHELLCHECK_PATH is None :
3745 logger .error ("shellcheck not found." )
3846 return 1
3947
48+ cwd = Path (args .cwd ).resolve ()
49+ if not cwd .is_dir ():
50+ logger .error ("%s is not a directory." , cwd )
51+ return 1
52+
53+ os .chdir (str (cwd ))
4054 files = _get_files (args .filenames )
41- cwd = Path .cwd ().resolve ()
4255
4356 exitcode = 0
4457 cmds = ("shellcheck" , * extra_args )
58+ if SHELLCHECK_PATH is None :
59+ cmds = (cast (str , UV_SHELLCHECK_PATH ), "run" , * cmds )
4560 for file in files :
4661 if not util .is_shell_script (file ):
4762 logger .debug ("%s is not a shell script." , file .relative_to (cwd ))
0 commit comments