Skip to content

Commit 9befe4f

Browse files
committed
dulwich: use untracked_files="no" in is_dirty
This will speed up calling `is_dirty` with untracked_files=False related to #69
1 parent ca28ae0 commit 9befe4f

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

scmrepo/git/backend/dulwich/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from io import BytesIO, StringIO
88
from typing import (
99
TYPE_CHECKING,
10+
Any,
1011
Callable,
1112
Dict,
1213
Iterable,
@@ -344,8 +345,11 @@ def is_tracked(self, path: str) -> bool:
344345
return False
345346

346347
def is_dirty(self, untracked_files: bool = False) -> bool:
347-
staged, unstaged, untracked = self.status()
348-
return bool(staged or unstaged or (untracked_files and untracked))
348+
kwargs: Dict[str, Any] = (
349+
{} if untracked_files else {"untracked_files": "no"}
350+
)
351+
staged, unstaged, untracked = self.status(**kwargs)
352+
return bool(staged or unstaged or untracked)
349353

350354
def active_branch(self) -> str:
351355
raise NotImplementedError

tests/test_git.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,3 +1041,50 @@ def test_status(
10411041
assert staged["modify"] == ["foo"]
10421042
assert unstaged == ["bar"]
10431043
assert untracked == expected_untracked
1044+
1045+
1046+
@pytest.mark.skip_git_backend("pygit2")
1047+
@pytest.mark.parametrize("untracked_files", [False, True])
1048+
def test_is_dirty(
1049+
tmp_dir: TmpDir,
1050+
scm: Git,
1051+
git: Git,
1052+
tmp_dir_factory: TempDirFactory,
1053+
mocker,
1054+
untracked_files: str,
1055+
):
1056+
tmp_dir.gen(
1057+
{
1058+
"foo": "foo",
1059+
}
1060+
)
1061+
scm.add_commit(["foo"], message="init")
1062+
1063+
assert not git.is_dirty()
1064+
1065+
with (tmp_dir / "foo").open("a") as fobj:
1066+
fobj.write("modified")
1067+
1068+
assert git.is_dirty(untracked_files)
1069+
scm.add_commit(["foo"], message="commit")
1070+
1071+
with (tmp_dir / "untracked").open("w") as fobj:
1072+
fobj.write("untracked")
1073+
1074+
backend = list(git.backends)[0]
1075+
assert backend in ("dulwich", "gitpython")
1076+
if backend == "dulwich":
1077+
spy = mocker.spy(git.dulwich, "status")
1078+
elif backend == "gitpython":
1079+
spy = mocker.spy(git.gitpython.repo, "is_dirty")
1080+
1081+
assert git.is_dirty(untracked_files) == untracked_files
1082+
assert spy.called
1083+
_, kwargs = spy.call_args
1084+
if backend == "dulwich":
1085+
if untracked_files:
1086+
assert not kwargs
1087+
else:
1088+
assert kwargs["untracked_files"] == "no"
1089+
elif backend == "gitpython":
1090+
assert kwargs["untracked_files"] == untracked_files

0 commit comments

Comments
 (0)