From ada0a5e8f86d0941b4fc030e37213b21ac5d4438 Mon Sep 17 00:00:00 2001 From: Max Dymond Date: Wed, 1 Oct 2025 20:08:09 +0100 Subject: [PATCH] Make branches the same as heads --- gitpure.pyi | 5 +++-- src/lib.rs | 34 +++++++--------------------------- tests/test_repo.py | 10 +++++----- 3 files changed, 15 insertions(+), 34 deletions(-) diff --git a/gitpure.pyi b/gitpure.pyi index b21f239..597a150 100644 --- a/gitpure.pyi +++ b/gitpure.pyi @@ -55,8 +55,9 @@ class Repo: """ ... - def branches(self) -> list[str]: - """Return the names of all local branches in the repository.""" + @property + def branches(self) -> list[Head]: + """Return all local branches as Head objects.""" ... @property diff --git a/src/lib.rs b/src/lib.rs index 55b5ef7..ebf149e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -120,7 +120,7 @@ struct Repo { inner: gix::Repository, } -fn path_to_python(py: Python<'_>, path: &Path) -> PyResult { +fn path_to_python(py: Python<'_>, path: &Path) -> PyResult> { let pathlib = py.import("pathlib")?; let path_class = pathlib.getattr("Path")?; Ok(path_class.call1((path.as_os_str(),))?.into()) @@ -138,13 +138,13 @@ fn shorten_reference_name(reference: &gix::Reference) -> String { impl Repo { /// The path to the `.git` directory of the repository. #[getter] - fn git_dir(&self, py: Python) -> PyResult { + fn git_dir(&self, py: Python) -> PyResult> { path_to_python(py, self.inner.git_dir()) } /// The path to the working tree directory, if present. #[getter] - fn working_tree_dir(&self, py: Python) -> PyResult> { + fn working_tree_dir(&self, py: Python) -> PyResult>> { match self.inner.workdir() { Some(path) => Ok(Some(path_to_python(py, path)?)), None => Ok(None), @@ -202,30 +202,10 @@ impl Repo { } } - /// Return the names of all local branches in the repository. - fn branches(&self) -> PyResult> { - let platform = self.inner.references().map_err(|err| { - PyRuntimeError::new_err(format!("Failed to access references: {err}")) - })?; - - let iter = platform.local_branches().map_err(|err| { - PyRuntimeError::new_err(format!("Failed to list local branches: {err}")) - })?; - - let branches: BTreeSet = iter - .map(|reference_result| { - reference_result.map_err(|err| { - PyRuntimeError::new_err(format!( - "Failed to load branch reference: {err}" - )) - }) - }) - .collect::, _>>()? - .into_iter() - .map(|reference| shorten_reference_name(&reference)) - .collect(); - - Ok(branches.into_iter().collect()) + /// Return all local branches in the repository. + #[getter] + fn branches(&self) -> PyResult> { + self.heads() } /// Return the path to the current active branch, if any. diff --git a/tests/test_repo.py b/tests/test_repo.py index 48b9a00..9e3a47a 100644 --- a/tests/test_repo.py +++ b/tests/test_repo.py @@ -164,15 +164,15 @@ def test_branch_listing_matches_gitpython(): repo_path = Path(tmpdir) / "gitpure" repo = Repo.clone_from(repo_url, str(repo_path)) - gitpure_branches = repo.branches() + gitpure_branches = repo.branches assert isinstance(gitpure_branches, list) - assert all(isinstance(branch, str) for branch in gitpure_branches) + assert all(hasattr(branch, "name") and hasattr(branch, "commit") for branch in gitpure_branches) gitpython_repo = GitPythonRepo(str(repo_path)) gitpython_branches = sorted(head.name for head in gitpython_repo.branches) - assert gitpure_branches == gitpython_branches + assert [branch.name for branch in gitpure_branches] == gitpython_branches - # heads property should mirror branches() + # branches property should mirror heads property gitpure_heads = repo.heads - assert [head.name for head in gitpure_heads] == gitpure_branches + assert [head.name for head in gitpure_heads] == [branch.name for branch in gitpure_branches]