diff --git a/pre_commit/git.py b/pre_commit/git.py index 75e2662c..4d03c26a 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -7,13 +7,13 @@ import os.path import re from plumbum import local +from pre_commit.errors import FatalError from pre_commit.util import memoize_by_cwd logger = logging.getLogger('pre_commit') -@memoize_by_cwd def get_root(): path = os.getcwd() while len(path) > 1: @@ -21,7 +21,10 @@ def get_root(): return path else: path = os.path.normpath(os.path.join(path, '../')) - raise AssertionError('called from outside of the gits') + raise FatalError( + 'Called from outside of the gits. ' + 'Please cd to a git repository.' + ) def is_in_merge_conflict(): diff --git a/tests/git_test.py b/tests/git_test.py index 9b22359d..c3b728a6 100644 --- a/tests/git_test.py +++ b/tests/git_test.py @@ -6,6 +6,7 @@ import pytest from plumbum import local from pre_commit import git +from pre_commit.errors import FatalError from testing.fixtures import git_dir @@ -24,6 +25,12 @@ def test_get_root_deeper(tmpdir_factory): assert git.get_root() == path +def test_get_root_not_git_dir(tmpdir_factory): + with local.cwd(tmpdir_factory.get()): + with pytest.raises(FatalError): + git.get_root() + + def test_is_not_in_merge_conflict(tmpdir_factory): path = git_dir(tmpdir_factory) with local.cwd(path):