Merge pull request #320 from pre-commit/use_show_toplevel

Use rev-parse --show-toplevel
This commit is contained in:
Anthony Sottile 2015-12-18 11:33:51 -05:00
commit d6cf62532d
3 changed files with 13 additions and 16 deletions

View file

@ -7,6 +7,7 @@ import os.path
import re import re
from pre_commit.errors import FatalError 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
from pre_commit.util import memoize_by_cwd from pre_commit.util import memoize_by_cwd
@ -15,16 +16,12 @@ logger = logging.getLogger('pre_commit')
def get_root(): def get_root():
path = os.getcwd() try:
while path != os.path.normpath(os.path.join(path, '../')): return cmd_output('git', 'rev-parse', '--show-toplevel')[1].strip()
if os.path.exists(os.path.join(path, '.git')): except CalledProcessError:
return path raise FatalError(
else: 'Called from outside of the gits. Please cd to a git repository.'
path = os.path.normpath(os.path.join(path, '../')) )
raise FatalError(
'Called from outside of the gits. '
'Please cd to a git repository.'
)
def is_in_merge_conflict(): def is_in_merge_conflict():

View file

@ -15,7 +15,7 @@ from testing.fixtures import git_dir
def test_get_root_at_root(tempdir_factory): def test_get_root_at_root(tempdir_factory):
path = git_dir(tempdir_factory) path = git_dir(tempdir_factory)
with cwd(path): with cwd(path):
assert git.get_root() == path assert os.path.normcase(git.get_root()) == os.path.normcase(path)
def test_get_root_deeper(tempdir_factory): def test_get_root_deeper(tempdir_factory):
@ -24,7 +24,7 @@ def test_get_root_deeper(tempdir_factory):
foo_path = os.path.join(path, 'foo') foo_path = os.path.join(path, 'foo')
os.mkdir(foo_path) os.mkdir(foo_path)
with cwd(foo_path): with cwd(foo_path):
assert git.get_root() == path assert os.path.normcase(git.get_root()) == os.path.normcase(path)
def test_get_root_not_git_dir(tempdir_factory): def test_get_root_not_git_dir(tempdir_factory):

View file

@ -24,8 +24,8 @@ def test_create_sets_correct_directory(tempdir_factory):
path = git_dir(tempdir_factory) path = git_dir(tempdir_factory)
with cwd(path): with cwd(path):
runner = Runner.create() runner = Runner.create()
assert runner.git_root == path assert os.path.normcase(runner.git_root) == os.path.normcase(path)
assert os.getcwd() == path assert os.path.normcase(os.getcwd()) == os.path.normcase(path)
def test_create_changes_to_git_root(tempdir_factory): def test_create_changes_to_git_root(tempdir_factory):
@ -38,8 +38,8 @@ def test_create_changes_to_git_root(tempdir_factory):
assert os.getcwd() != path assert os.getcwd() != path
runner = Runner.create() runner = Runner.create()
assert runner.git_root == path assert os.path.normcase(runner.git_root) == os.path.normcase(path)
assert os.getcwd() == path assert os.path.normcase(os.getcwd()) == os.path.normcase(path)
def test_config_file_path(): def test_config_file_path():