Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pre_commit/clientlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

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
Expand Down
5 changes: 1 addition & 4 deletions pre_commit/error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@

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)
Expand Down
2 changes: 2 additions & 0 deletions pre_commit/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class FatalError(RuntimeError):
pass
20 changes: 18 additions & 2 deletions pre_commit/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
23 changes: 4 additions & 19 deletions pre_commit/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
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')
Expand Down Expand Up @@ -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'}:
Expand Down Expand Up @@ -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)

Expand Down
11 changes: 6 additions & 5 deletions tests/error_handler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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
Expand All @@ -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

Expand All @@ -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])

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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


Expand Down