Skip to content

Commit 658477d

Browse files
committed
fix(git): Fix update_repo when there are untracked files
1 parent dcf97f8 commit 658477d

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/libvcs/sync/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def update_repo(self, set_remotes: bool = False, *args: Any, **kwargs: Any) -> N
408408
if is_remote_ref:
409409
# Check if stash is needed
410410
try:
411-
process = self.run(["status", "--porcelain"])
411+
process = self.run(["status", "--porcelain", "--untracked-files=no"])
412412
except exc.CommandError:
413413
self.log.error("Failed to get the status")
414414
return

tests/sync/test_git.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import datetime
33
import os
44
import pathlib
5+
import random
56
import shutil
67
import textwrap
78
from typing import Callable, TypedDict
@@ -120,7 +121,7 @@ def test_repo_git_obtain_full(
120121
"url": f"file://{git_remote_repo}",
121122
"dir": tmp_path / "myrepo",
122123
"vcs": "git",
123-
},
124+
}
124125
],
125126
[
126127
create_project,
@@ -141,6 +142,7 @@ def test_repo_update_handle_cases(
141142
) -> None:
142143
git_repo: GitSync = constructor(**lazy_constructor_options(**locals()))
143144
git_repo.obtain() # clone initial repo
145+
144146
mocka = mocker.spy(git_repo, "run")
145147
git_repo.update_repo()
146148

@@ -154,6 +156,60 @@ def test_repo_update_handle_cases(
154156
assert mocker.call(["symbolic-ref", "--short", "HEAD"]) not in mocka.mock_calls
155157

156158

159+
@pytest.mark.parametrize("has_untracked_files,needs_stash,has_remote_changes", [
160+
[True, True, True],
161+
[True, True, False],
162+
[True, False, True],
163+
[True, False, False],
164+
[False, True, True],
165+
[False, True, False],
166+
[False, False, True],
167+
[False, False, False]
168+
])
169+
def test_repo_update_stash_cases(
170+
tmp_path: pathlib.Path,
171+
git_remote_repo: pathlib.Path,
172+
mocker: MockerFixture,
173+
has_untracked_files: bool,
174+
needs_stash: bool,
175+
has_remote_changes: bool,
176+
) -> None:
177+
options = {
178+
"url": f"file://{git_remote_repo}",
179+
"dir": tmp_path / "myrepo",
180+
"vcs": "git",
181+
}
182+
183+
git_repo: GitSync = GitSync(**options)
184+
git_repo.obtain() # clone initial repo
185+
186+
def make_file(filename):
187+
some_file = git_repo.dir.joinpath(filename)
188+
with open(some_file, "w") as file:
189+
file.write("some content: " + str(random.random()))
190+
191+
return some_file
192+
193+
if has_remote_changes:
194+
some_file = make_file("some_file")
195+
git_repo.run(["add", some_file])
196+
git_repo.run(["commit", "-m", "a commit"])
197+
git_repo.run(["push"])
198+
git_repo.run(["reset", "--hard", "HEAD^"])
199+
200+
if has_untracked_files:
201+
make_file("some_file")
202+
203+
if needs_stash:
204+
some_file = make_file("some_stashed_file")
205+
git_repo.run(["add", some_file])
206+
207+
mocka = mocker.spy(git_repo, "run")
208+
git_repo.update_repo()
209+
210+
mocka.assert_any_call(["symbolic-ref", "--short", "HEAD"])
211+
212+
157213
@pytest.mark.parametrize(
158214
# Postpone evaluation of options so fixture variables can interpolate
159215
"constructor,lazy_constructor_options",

0 commit comments

Comments
 (0)