Skip to content

Commit d6cf625

Browse files
committed
Merge pull request pre-commit#320 from pre-commit/use_show_toplevel
Use rev-parse --show-toplevel
2 parents 0443ca2 + 3f02a66 commit d6cf625

3 files changed

Lines changed: 13 additions & 16 deletions

File tree

pre_commit/git.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88

99
from pre_commit.errors import FatalError
10+
from pre_commit.util import CalledProcessError
1011
from pre_commit.util import cmd_output
1112
from pre_commit.util import memoize_by_cwd
1213

@@ -15,16 +16,12 @@
1516

1617

1718
def get_root():
18-
path = os.getcwd()
19-
while path != os.path.normpath(os.path.join(path, '../')):
20-
if os.path.exists(os.path.join(path, '.git')):
21-
return path
22-
else:
23-
path = os.path.normpath(os.path.join(path, '../'))
24-
raise FatalError(
25-
'Called from outside of the gits. '
26-
'Please cd to a git repository.'
27-
)
19+
try:
20+
return cmd_output('git', 'rev-parse', '--show-toplevel')[1].strip()
21+
except CalledProcessError:
22+
raise FatalError(
23+
'Called from outside of the gits. Please cd to a git repository.'
24+
)
2825

2926

3027
def is_in_merge_conflict():

tests/git_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
def test_get_root_at_root(tempdir_factory):
1616
path = git_dir(tempdir_factory)
1717
with cwd(path):
18-
assert git.get_root() == path
18+
assert os.path.normcase(git.get_root()) == os.path.normcase(path)
1919

2020

2121
def test_get_root_deeper(tempdir_factory):
@@ -24,7 +24,7 @@ def test_get_root_deeper(tempdir_factory):
2424
foo_path = os.path.join(path, 'foo')
2525
os.mkdir(foo_path)
2626
with cwd(foo_path):
27-
assert git.get_root() == path
27+
assert os.path.normcase(git.get_root()) == os.path.normcase(path)
2828

2929

3030
def test_get_root_not_git_dir(tempdir_factory):

tests/runner_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def test_create_sets_correct_directory(tempdir_factory):
2424
path = git_dir(tempdir_factory)
2525
with cwd(path):
2626
runner = Runner.create()
27-
assert runner.git_root == path
28-
assert os.getcwd() == path
27+
assert os.path.normcase(runner.git_root) == os.path.normcase(path)
28+
assert os.path.normcase(os.getcwd()) == os.path.normcase(path)
2929

3030

3131
def test_create_changes_to_git_root(tempdir_factory):
@@ -38,8 +38,8 @@ def test_create_changes_to_git_root(tempdir_factory):
3838
assert os.getcwd() != path
3939

4040
runner = Runner.create()
41-
assert runner.git_root == path
42-
assert os.getcwd() == path
41+
assert os.path.normcase(runner.git_root) == os.path.normcase(path)
42+
assert os.path.normcase(os.getcwd()) == os.path.normcase(path)
4343

4444

4545
def test_config_file_path():

0 commit comments

Comments
 (0)