mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-14 13:00:10 -06:00
Merge pull request #1606 from pre-commit/move_cygwin_mismatch
check cygwin mismatch earlier
This commit is contained in:
@@ -13,7 +13,7 @@ from identify.identify import ALL_TAGS
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.color import add_color_option
|
||||
from pre_commit.error_handler import FatalError
|
||||
from pre_commit.errors import FatalError
|
||||
from pre_commit.languages.all import all_languages
|
||||
from pre_commit.logging_handler import logging_handler
|
||||
from pre_commit.util import parse_version
|
||||
|
||||
@@ -7,14 +7,11 @@ from typing import Generator
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit import output
|
||||
from pre_commit.errors import FatalError
|
||||
from pre_commit.store import Store
|
||||
from pre_commit.util import force_bytes
|
||||
|
||||
|
||||
class FatalError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
def _log_and_exit(msg: str, exc: BaseException, formatted: str) -> None:
|
||||
error_msg = f'{msg}: {type(exc).__name__}: '.encode() + force_bytes(exc)
|
||||
output.write_line_b(error_msg)
|
||||
|
||||
2
pre_commit/errors.py
Normal file
2
pre_commit/errors.py
Normal file
@@ -0,0 +1,2 @@
|
||||
class FatalError(RuntimeError):
|
||||
pass
|
||||
@@ -6,6 +6,8 @@ from typing import List
|
||||
from typing import Optional
|
||||
from typing import Set
|
||||
|
||||
from pre_commit.errors import FatalError
|
||||
from pre_commit.util import CalledProcessError
|
||||
from pre_commit.util import cmd_output
|
||||
from pre_commit.util import cmd_output_b
|
||||
from pre_commit.util import EnvironT
|
||||
@@ -43,7 +45,21 @@ def no_git_env(_env: Optional[EnvironT] = None) -> Dict[str, str]:
|
||||
|
||||
|
||||
def get_root() -> str:
|
||||
return cmd_output('git', 'rev-parse', '--show-toplevel')[1].strip()
|
||||
try:
|
||||
root = cmd_output('git', 'rev-parse', '--show-toplevel')[1].strip()
|
||||
except CalledProcessError:
|
||||
raise FatalError(
|
||||
'git failed. Is it installed, and are you in a Git repository '
|
||||
'directory?',
|
||||
)
|
||||
else:
|
||||
if root == '': # pragma: no cover (old git)
|
||||
raise FatalError(
|
||||
'git toplevel unexpectedly empty! make sure you are not '
|
||||
'inside the `.git` directory of your repository.',
|
||||
)
|
||||
else:
|
||||
return root
|
||||
|
||||
|
||||
def get_git_dir(git_root: str = '.') -> str:
|
||||
@@ -181,7 +197,7 @@ def check_for_cygwin_mismatch() -> None:
|
||||
"""See https://github.com/pre-commit/pre-commit/issues/354"""
|
||||
if sys.platform in ('cygwin', 'win32'): # pragma: no cover (windows)
|
||||
is_cygwin_python = sys.platform == 'cygwin'
|
||||
toplevel = cmd_output('git', 'rev-parse', '--show-toplevel')[1]
|
||||
toplevel = get_root()
|
||||
is_cygwin_git = toplevel.startswith('/')
|
||||
|
||||
if is_cygwin_python ^ is_cygwin_git:
|
||||
|
||||
@@ -23,10 +23,8 @@ from pre_commit.commands.run import run
|
||||
from pre_commit.commands.sample_config import sample_config
|
||||
from pre_commit.commands.try_repo import try_repo
|
||||
from pre_commit.error_handler import error_handler
|
||||
from pre_commit.error_handler import FatalError
|
||||
from pre_commit.logging_handler import logging_handler
|
||||
from pre_commit.store import Store
|
||||
from pre_commit.util import CalledProcessError
|
||||
|
||||
|
||||
logger = logging.getLogger('pre_commit')
|
||||
@@ -146,21 +144,8 @@ def _adjust_args_and_chdir(args: argparse.Namespace) -> None:
|
||||
if args.command == 'try-repo' and os.path.exists(args.repo):
|
||||
args.repo = os.path.abspath(args.repo)
|
||||
|
||||
try:
|
||||
toplevel = git.get_root()
|
||||
except CalledProcessError:
|
||||
raise FatalError(
|
||||
'git failed. Is it installed, and are you in a Git repository '
|
||||
'directory?',
|
||||
)
|
||||
else:
|
||||
if toplevel == '': # pragma: no cover (old git)
|
||||
raise FatalError(
|
||||
'git toplevel unexpectedly empty! make sure you are not '
|
||||
'inside the `.git` directory of your repository.',
|
||||
)
|
||||
else:
|
||||
os.chdir(toplevel)
|
||||
toplevel = git.get_root()
|
||||
os.chdir(toplevel)
|
||||
|
||||
args.config = os.path.relpath(args.config)
|
||||
if args.command in {'run', 'try-repo'}:
|
||||
@@ -339,11 +324,11 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||
parser.parse_args(['--help'])
|
||||
|
||||
with error_handler(), logging_handler(args.color):
|
||||
git.check_for_cygwin_mismatch()
|
||||
|
||||
if args.command not in COMMANDS_NO_GIT:
|
||||
_adjust_args_and_chdir(args)
|
||||
|
||||
git.check_for_cygwin_mismatch()
|
||||
|
||||
store = Store()
|
||||
store.mark_config_used(args.config)
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import pytest
|
||||
import re_assert
|
||||
|
||||
from pre_commit import error_handler
|
||||
from pre_commit.errors import FatalError
|
||||
from pre_commit.store import Store
|
||||
from pre_commit.util import CalledProcessError
|
||||
from testing.util import cmd_output_mocked_pre_commit_home
|
||||
@@ -26,7 +27,7 @@ def test_error_handler_no_exception(mocked_log_and_exit):
|
||||
|
||||
|
||||
def test_error_handler_fatal_error(mocked_log_and_exit):
|
||||
exc = error_handler.FatalError('just a test')
|
||||
exc = FatalError('just a test')
|
||||
with error_handler.error_handler():
|
||||
raise exc
|
||||
|
||||
@@ -44,7 +45,7 @@ def test_error_handler_fatal_error(mocked_log_and_exit):
|
||||
r' File ".+tests.error_handler_test.py", line \d+, '
|
||||
r'in test_error_handler_fatal_error\n'
|
||||
r' raise exc\n'
|
||||
r'(pre_commit\.error_handler\.)?FatalError: just a test\n',
|
||||
r'(pre_commit\.errors\.)?FatalError: just a test\n',
|
||||
)
|
||||
pattern.assert_matches(mocked_log_and_exit.call_args[0][2])
|
||||
|
||||
@@ -99,11 +100,11 @@ def test_log_and_exit(cap_out, mock_store_dir):
|
||||
tb = (
|
||||
'Traceback (most recent call last):\n'
|
||||
' File "<stdin>", line 2, in <module>\n'
|
||||
'pre_commit.error_handler.FatalError: hai\n'
|
||||
'pre_commit.errors.FatalError: hai\n'
|
||||
)
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
error_handler._log_and_exit('msg', error_handler.FatalError('hai'), tb)
|
||||
error_handler._log_and_exit('msg', FatalError('hai'), tb)
|
||||
|
||||
printed = cap_out.get()
|
||||
log_file = os.path.join(mock_store_dir, 'pre-commit.log')
|
||||
@@ -133,7 +134,7 @@ def test_log_and_exit(cap_out, mock_store_dir):
|
||||
r'```\n'
|
||||
r'Traceback \(most recent call last\):\n'
|
||||
r' File "<stdin>", line 2, in <module>\n'
|
||||
r'pre_commit\.error_handler\.FatalError: hai\n'
|
||||
r'pre_commit\.errors\.FatalError: hai\n'
|
||||
r'```\n',
|
||||
)
|
||||
pattern.assert_matches(logged)
|
||||
|
||||
@@ -6,7 +6,7 @@ import pytest
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit import main
|
||||
from pre_commit.error_handler import FatalError
|
||||
from pre_commit.errors import FatalError
|
||||
from testing.auto_namedtuple import auto_namedtuple
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user