Skip to content

Commit 994adf8

Browse files
authored
Merge pull request #16 from cmeister2/md3/branches
fix: Make branches the same as heads
2 parents 0c2914a + ada0a5e commit 994adf8

File tree

3 files changed

+15
-34
lines changed

3 files changed

+15
-34
lines changed

gitpure.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ class Repo:
5555
"""
5656
...
5757

58-
def branches(self) -> list[str]:
59-
"""Return the names of all local branches in the repository."""
58+
@property
59+
def branches(self) -> list[Head]:
60+
"""Return all local branches as Head objects."""
6061
...
6162

6263
@property

src/lib.rs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ struct Repo {
120120
inner: gix::Repository,
121121
}
122122

123-
fn path_to_python(py: Python<'_>, path: &Path) -> PyResult<PyObject> {
123+
fn path_to_python(py: Python<'_>, path: &Path) -> PyResult<Py<PyAny>> {
124124
let pathlib = py.import("pathlib")?;
125125
let path_class = pathlib.getattr("Path")?;
126126
Ok(path_class.call1((path.as_os_str(),))?.into())
@@ -138,13 +138,13 @@ fn shorten_reference_name(reference: &gix::Reference) -> String {
138138
impl Repo {
139139
/// The path to the `.git` directory of the repository.
140140
#[getter]
141-
fn git_dir(&self, py: Python) -> PyResult<PyObject> {
141+
fn git_dir(&self, py: Python) -> PyResult<Py<PyAny>> {
142142
path_to_python(py, self.inner.git_dir())
143143
}
144144

145145
/// The path to the working tree directory, if present.
146146
#[getter]
147-
fn working_tree_dir(&self, py: Python) -> PyResult<Option<PyObject>> {
147+
fn working_tree_dir(&self, py: Python) -> PyResult<Option<Py<PyAny>>> {
148148
match self.inner.workdir() {
149149
Some(path) => Ok(Some(path_to_python(py, path)?)),
150150
None => Ok(None),
@@ -202,30 +202,10 @@ impl Repo {
202202
}
203203
}
204204

205-
/// Return the names of all local branches in the repository.
206-
fn branches(&self) -> PyResult<Vec<String>> {
207-
let platform = self.inner.references().map_err(|err| {
208-
PyRuntimeError::new_err(format!("Failed to access references: {err}"))
209-
})?;
210-
211-
let iter = platform.local_branches().map_err(|err| {
212-
PyRuntimeError::new_err(format!("Failed to list local branches: {err}"))
213-
})?;
214-
215-
let branches: BTreeSet<String> = iter
216-
.map(|reference_result| {
217-
reference_result.map_err(|err| {
218-
PyRuntimeError::new_err(format!(
219-
"Failed to load branch reference: {err}"
220-
))
221-
})
222-
})
223-
.collect::<Result<Vec<_>, _>>()?
224-
.into_iter()
225-
.map(|reference| shorten_reference_name(&reference))
226-
.collect();
227-
228-
Ok(branches.into_iter().collect())
205+
/// Return all local branches in the repository.
206+
#[getter]
207+
fn branches(&self) -> PyResult<Vec<Head>> {
208+
self.heads()
229209
}
230210

231211
/// Return the path to the current active branch, if any.

tests/test_repo.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,15 @@ def test_branch_listing_matches_gitpython():
164164
repo_path = Path(tmpdir) / "gitpure"
165165
repo = Repo.clone_from(repo_url, str(repo_path))
166166

167-
gitpure_branches = repo.branches()
167+
gitpure_branches = repo.branches
168168
assert isinstance(gitpure_branches, list)
169-
assert all(isinstance(branch, str) for branch in gitpure_branches)
169+
assert all(hasattr(branch, "name") and hasattr(branch, "commit") for branch in gitpure_branches)
170170

171171
gitpython_repo = GitPythonRepo(str(repo_path))
172172
gitpython_branches = sorted(head.name for head in gitpython_repo.branches)
173173

174-
assert gitpure_branches == gitpython_branches
174+
assert [branch.name for branch in gitpure_branches] == gitpython_branches
175175

176-
# heads property should mirror branches()
176+
# branches property should mirror heads property
177177
gitpure_heads = repo.heads
178-
assert [head.name for head in gitpure_heads] == gitpure_branches
178+
assert [head.name for head in gitpure_heads] == [branch.name for branch in gitpure_branches]

0 commit comments

Comments
 (0)