Skip to content

Commit 1870eee

Browse files
authored
Merge pull request #196 from fmi-faim/git-root-fixes
Avoid endless loop in `get_git_root`
2 parents 02a43e9 + 3edb054 commit 1870eee

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/faim_ipa/utils.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,20 @@ def rgb_to_hex(r, g, b):
7272

7373
def create_logger(name: str, *, include_timestamp: bool = True) -> logging.Logger:
7474
"""
75-
Create logger which logs to <timestamp>-<name>.log inside the current
75+
Create logger which logs to `<timestamp>-<name>.log` inside the current
7676
working directory.
7777
7878
Parameters
7979
----------
8080
name
8181
Name of the logger instance.
82+
include_timestamp
83+
Whether to include the timestamp in the log file name.
84+
85+
Returns
86+
-------
87+
logging.Logger
88+
Logger instance
8289
"""
8390
logger = logging.getLogger(name=name)
8491
now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
@@ -97,15 +104,17 @@ def create_logger(name: str, *, include_timestamp: bool = True) -> logging.Logge
97104

98105
def get_git_root() -> Path:
99106
"""
100-
Recursively search for the directory containing the .git folder.
107+
Recursively search for the directory containing the `.git` folder.
101108
102109
Returns
103110
-------
104111
Path
105-
Path to the root of the git repository.
112+
Path to the root of the git repository. None if not found.
106113
"""
107-
parent_dir = Path(__file__).parent
114+
parent_dir = Path.cwd()
108115
while not (parent_dir / ".git").exists():
116+
if parent_dir == parent_dir.parent:
117+
return None # reached root directory
109118
parent_dir = parent_dir.parent
110119

111120
return parent_dir

0 commit comments

Comments
 (0)