File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77import re
88
99from pre_commit .errors import FatalError
10+ from pre_commit .util import CalledProcessError
1011from pre_commit .util import cmd_output
1112from pre_commit .util import memoize_by_cwd
1213
1516
1617
1718def 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
3027def is_in_merge_conflict ():
Original file line number Diff line number Diff line change 1515def 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
2121def 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
3030def test_get_root_not_git_dir (tempdir_factory ):
Original file line number Diff line number Diff 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
3131def 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
4545def test_config_file_path ():
You can’t perform that action at this time.
0 commit comments