Skip to content

Commit e5484ce

Browse files
committed
describe: make active branch always be the first choice of describe(#4)
fix: #4 and treeverse/dvc#6051 In dvc we get a random toggling branch name in exp show. It is because when we iter branches the dulwich.refs.keys gives an unordered Set of refs. Here we choose the active branch if we are `describe` its commit rev. 1. Make active branch always in the first place. 2. Add a new test to it.
1 parent 8843079 commit e5484ce

File tree

6 files changed

+34
-5
lines changed

6 files changed

+34
-5
lines changed

scmrepo/git/__init__.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def add_commit(
314314
_stash_push = partialmethod(_backend_func, "_stash_push")
315315
_stash_apply = partialmethod(_backend_func, "_stash_apply")
316316
_stash_drop = partialmethod(_backend_func, "_stash_drop")
317-
describe = partialmethod(_backend_func, "describe")
317+
_describe = partialmethod(_backend_func, "_describe")
318318
diff = partialmethod(_backend_func, "diff")
319319
reset = partialmethod(_backend_func, "reset")
320320
checkout_index = partialmethod(_backend_func, "checkout_index")
@@ -397,3 +397,17 @@ def stash_workspace(self, **kwargs):
397397

398398
def _reset(self) -> None:
399399
self.backends.reset_all()
400+
401+
def describe(
402+
self,
403+
rev: str,
404+
base: Optional[str] = None,
405+
match: Optional[str] = None,
406+
exclude: Optional[str] = None,
407+
) -> Optional[str]:
408+
if base == "refs/heads" and self.get_rev() == rev:
409+
try:
410+
return base + "/" + self.active_branch()
411+
except TypeError:
412+
pass
413+
return self._describe(rev, base, match, exclude)

scmrepo/git/backend/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def _stash_drop(self, ref: str, index: int):
278278
"""Drop the specified stash revision."""
279279

280280
@abstractmethod
281-
def describe(
281+
def _describe(
282282
self,
283283
rev: str,
284284
base: Optional[str] = None,

scmrepo/git/backend/dulwich/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ def _stash_drop(self, ref: str, index: int):
590590
except ValueError as exc:
591591
raise SCMError("Failed to drop stash entry") from exc
592592

593-
def describe(
593+
def _describe(
594594
self,
595595
rev: str,
596596
base: Optional[str] = None,

scmrepo/git/backend/gitpython.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ def _stash_drop(self, ref: str, index: int):
552552
except GitCommandError:
553553
self.remove_ref(ref)
554554

555-
def describe(
555+
def _describe(
556556
self,
557557
rev: str,
558558
base: Optional[str] = None,

scmrepo/git/backend/pygit2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ def _stash_drop(self, ref: str, index: int):
459459

460460
self.repo.stash_drop(index)
461461

462-
def describe(
462+
def _describe(
463463
self,
464464
rev: str,
465465
base: Optional[str] = None,

tests/test_dulwich.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import pytest
22
from pytest_mock import MockerFixture
3+
from pytest_test_utils import TmpDir
4+
5+
from scmrepo.git import Git
36

47

58
@pytest.mark.parametrize(
@@ -26,3 +29,15 @@ def test_dulwich_github_compat(mocker: MockerFixture, algorithm: bytes):
2629
strings = iter((b"ssh-rsa", key_data))
2730
packet.get_string = lambda: next(strings)
2831
_process_public_key_ok_gh(auth, None, None, packet)
32+
33+
34+
def test_dulwich_describe(tmp_dir: TmpDir, scm: Git):
35+
36+
tmp_dir.gen({"foo": "foo"})
37+
scm.add_commit("foo", message="foo")
38+
rev = scm.get_rev()
39+
40+
scm.checkout("branch", create_new=True)
41+
assert scm.describe(rev, "refs/heads") == "refs/heads/branch"
42+
scm.checkout("master")
43+
assert scm.describe(rev, "refs/heads") == "refs/heads/master"

0 commit comments

Comments
 (0)