Skip to content

Commit 8da6323

Browse files
Merge pull request #171 from dscorbett/expand-get_git_root_path
Make `get_git_root_path` search up to 5 levels up
2 parents 1879962 + 97164b0 commit 8da6323

File tree

3 files changed

+12
-27
lines changed

3 files changed

+12
-27
lines changed

lib/fontv/utilities.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,24 @@ def file_exists(filepath):
3333

3434
def get_git_root_path(filepath):
3535
"""
36-
Recursively searches for git root path over 4 directory levels above working directory
36+
Recursively searches for git root path over 5 directory levels above working directory
3737
:param filepath: (string) - path to the font file that is under git version control
3838
:return: validated git root path as string
3939
:raises: IOError if unable to detect the root of the git repository through this path traversal
4040
"""
4141

4242
# begin by defining directory that contains font as the git root needle
43-
unverified_gitroot_path = os.path.dirname(os.path.abspath(filepath))
43+
gitroot_path = os.path.dirname(os.path.abspath(filepath))
4444

45-
# check to see if this assumption is correct
46-
if dir_exists(os.path.join(unverified_gitroot_path, ".git")):
47-
verified_gitroot_path = unverified_gitroot_path
48-
else: # if not, recursive search up to three directories above for the git repo root
49-
one_level_up = os.path.abspath(os.path.join(unverified_gitroot_path, os.pardir))
50-
two_levels_up = os.path.dirname(one_level_up)
51-
three_levels_up = os.path.dirname(two_levels_up)
45+
# search up to five directories above for the git repo root
46+
for _ in range(6):
47+
if dir_exists(os.path.join(gitroot_path, ".git")):
48+
return gitroot_path
49+
gitroot_path = os.path.dirname(gitroot_path)
5250

53-
one_level_up_path = os.path.join(one_level_up, ".git")
54-
two_levels_up_path = os.path.join(two_levels_up, ".git")
55-
three_levels_up_path = os.path.join(three_levels_up, ".git")
56-
57-
if dir_exists(one_level_up_path): # check one directory level up
58-
verified_gitroot_path = os.path.dirname(one_level_up_path)
59-
elif dir_exists(two_levels_up_path): # check two directory levels up
60-
verified_gitroot_path = os.path.dirname(two_levels_up_path)
61-
elif dir_exists(three_levels_up_path): # check three directory levels up
62-
verified_gitroot_path = os.path.dirname(three_levels_up_path)
63-
else:
64-
raise IOError(
65-
"Unable to determine git repository root for font file " + filepath
66-
)
67-
68-
return verified_gitroot_path
51+
raise IOError(
52+
"Unable to determine git repository root for font file " + filepath
53+
)
6954

7055

7156
def is_font(filepath):

tests/test_utilities.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ def test_utilities_get_gitrootpath_function_returns_proper_path_three_levels_up(
5656
assert os.path.isdir(gitdir_path) is True
5757

5858

59-
def test_utilities_get_gitrootpath_function_raises_ioerror_four_levels_up():
59+
def test_utilities_get_gitrootpath_function_raises_ioerror_six_levels_up():
6060
with pytest.raises(IOError):
61-
filepath = "tests/testfiles/deepdir/deepdir2/test.txt"
61+
filepath = "tests/testfiles/deepdir/deepdir2/deepdir3/deepdir4/test.txt"
6262
get_git_root_path(filepath)
6363

6464

0 commit comments

Comments
 (0)