Skip to content

Commit 4260926

Browse files
committed
feat: add short option to git commit hash retrieval
Signed-off-by: Alex Jones <alex.jones@lowrisc.org>
1 parent 88097a4 commit 4260926

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/dvsim/utils/git.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def repo_root(path: Path) -> Path | None:
2828
return None
2929

3030

31-
def git_commit_hash(path: Path | None = None) -> str:
31+
def git_commit_hash(path: Path | None = None, *, short: bool = False) -> str:
3232
"""Hash of the current git commit."""
3333
root = repo_root(path=path or Path.cwd())
3434

@@ -38,6 +38,9 @@ def git_commit_hash(path: Path | None = None) -> str:
3838

3939
r = Repo(root)
4040

41+
if short:
42+
return r.git.rev_parse(r.head, short=True)
43+
4144
return r.head.commit.hexsha
4245

4346

tests/utils/test_git.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ def test_git_commit_hash(tmp_path: Path) -> None:
5858
equal_to(r.head.commit.hexsha),
5959
)
6060

61+
@staticmethod
62+
def test_git_short_commit_hash(tmp_path: "Path") -> None:
63+
"""Test that the expected shortened git commit sha is returned."""
64+
r = Repo.init(path=tmp_path)
65+
66+
file = tmp_path / "a"
67+
file.write_text("file to commit")
68+
r.index.add([file])
69+
r.index.commit("initial commit")
70+
71+
assert_that(
72+
git_commit_hash(tmp_path, short=True), equal_to(r.git.rev_parse(r.head, short=True))
73+
)
74+
6175
@staticmethod
6276
def test_git_origin_url(tmp_path: Path) -> None:
6377
"""Test that the expected git remote origin url is returned."""

0 commit comments

Comments
 (0)