Skip to content

Commit 14bf052

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 d4d1917 commit 14bf052

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

scmrepo/git/backend/dulwich/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,9 @@ def is_tracked(self, path: str) -> bool:
344344
return False
345345

346346
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))
347+
kwargs = {} if untracked_files else {"untracked_files": "no"}
348+
staged, unstaged, untracked = self.status(**kwargs) # type: ignore
349+
return bool(staged or unstaged or untracked)
349350

350351
def active_branch(self) -> str:
351352
raise NotImplementedError

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ zip_safe = False
2525
packages = find:
2626
install_requires=
2727
gitpython>3
28-
dulwich>=0.20.38
28+
dulwich<=0.20.40 # set upper bound for dulwich so that we can check that wheels are available before bumping version
2929
pygit2>=1.7.2
3030
pygtrie>=2.3.2
3131
fsspec>=2021.7.0

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)