Skip to content

Commit 24ce703

Browse files
authored
ES-305 #done
1 parent 9f37bb4 commit 24ce703

6 files changed

Lines changed: 81 additions & 4 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "eed-basic-utils"
3-
version = "1.0.0"
3+
version = "1.1.0"
44
description = "Add your description here"
55
readme = "README.md"
66
authors = [

src/eed_basic_utils/os/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
__all__ = [
22
"file_exists",
3+
"get_git_root",
34
]
45

56
from .file_exists import file_exists
7+
from .get_git_root import get_git_root
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from pathlib import Path
2+
from typing import Optional
3+
4+
5+
def get_git_root(path: Optional[Path] = None) -> Path | None:
6+
"""
7+
Returns the root directory of the git repository containing the given path.
8+
9+
Parameters
10+
----------
11+
path : Optional[Path], optional
12+
The starting path to search for the git root. If None, uses the current working directory.
13+
14+
Returns
15+
-------
16+
Path or None
17+
The root directory of the git repository, or None if not found.
18+
19+
Notes
20+
-----
21+
Searches upwards from the given path until a directory containing a `.git` folder is found.
22+
"""
23+
if path is None:
24+
path = Path.cwd()
25+
26+
if (path / ".git").exists():
27+
return path
28+
29+
git_root = next(iter(path.parents))
30+
while not (git_root / ".git").exists() and git_root != git_root.parent:
31+
git_root = git_root.parent
32+
if not (git_root / ".git").exists():
33+
return None
34+
else:
35+
return git_root

tests/conftest.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
from collections.abc import Iterator
32
from pathlib import Path
43

@@ -8,9 +7,17 @@
87

98
@pytest.fixture(scope="session")
109
def cfg_test() -> Iterator[dict]:
11-
home_dir = os.path.expanduser("~")
10+
home_dir = Path.home()
11+
cwd = Path.cwd()
12+
for item in cwd.iterdir():
13+
if item.is_dir() and item.name[0] != ".":
14+
subfolder = item
15+
break
16+
# Ensure cwd is a valid directory
1217
cfg_test = {
1318
"home_dir": home_dir,
19+
"cwd": cwd,
20+
"cwd_subfolder": cwd / subfolder, # Example subfolder, adjust as needed
1421
}
1522
yield cfg_test
1623

tests/os/test_get_git_root.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from eed_basic_utils.os import get_git_root
2+
import pytest
3+
4+
5+
@pytest.mark.parametrize(
6+
"path, expected",
7+
[
8+
("cwd", "eed_basic_utils"),
9+
("cwd_subfolder", "eed_basic_utils"),
10+
("home_dir", None),
11+
],
12+
ids=[
13+
"cwd = git root",
14+
"subfolder",
15+
"non_git_path",
16+
],
17+
)
18+
def test_get_git_root(cfg_test, path, expected):
19+
"""Test the get_git_root function."""
20+
if path == "cwd":
21+
path = cfg_test["cwd"]
22+
elif path == "cwd_subfolder":
23+
path = cfg_test["cwd_subfolder"]
24+
elif path == "home_dir":
25+
path = cfg_test["home_dir"]
26+
27+
git_root = get_git_root(path)
28+
if git_root is None:
29+
assert expected is git_root
30+
else:
31+
assert git_root.name == expected
32+
assert (git_root / ".git").exists()
33+
assert git_root.is_dir()

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)