Skip to content

Commit

Permalink
fix(git): Fix update_repo when there are untracked files
Browse files Browse the repository at this point in the history
  • Loading branch information
jfpedroza committed Sep 25, 2022
1 parent 9fddb55 commit d16c5f9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/libvcs/sync/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def update_repo(self, set_remotes: bool = False, *args: Any, **kwargs: Any) -> N
if is_remote_ref:
# Check if stash is needed
try:
process = self.run(["status", "--porcelain"])
process = self.run(["status", "--porcelain", "--untracked-files=no"])
except exc.CommandError:
self.log.error("Failed to get the status")
return
Expand Down
58 changes: 57 additions & 1 deletion tests/sync/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
import os
import pathlib
import random
import shutil
import textwrap
from typing import Callable, TypedDict
Expand Down Expand Up @@ -120,7 +121,7 @@ def test_repo_git_obtain_full(
"url": f"file://{git_remote_repo}",
"dir": tmp_path / "myrepo",
"vcs": "git",
},
}
],
[
create_project,
Expand All @@ -141,6 +142,7 @@ def test_repo_update_handle_cases(
) -> None:
git_repo: GitSync = constructor(**lazy_constructor_options(**locals()))
git_repo.obtain() # clone initial repo

mocka = mocker.spy(git_repo, "run")
git_repo.update_repo()

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


@pytest.mark.parametrize("has_untracked_files,needs_stash,has_remote_changes", [
[True, True, True],
[True, True, False],
[True, False, True],
[True, False, False],
[False, True, True],
[False, True, False],
[False, False, True],
[False, False, False]
])
def test_repo_update_stash_cases(
tmp_path: pathlib.Path,
git_remote_repo: pathlib.Path,
mocker: MockerFixture,
has_untracked_files: bool,
needs_stash: bool,
has_remote_changes: bool,
) -> None:
options = {
"url": f"file://{git_remote_repo}",
"dir": tmp_path / "myrepo",
"vcs": "git",
}

git_repo: GitSync = GitSync(**options)
git_repo.obtain() # clone initial repo

def make_file(filename):
some_file = git_repo.dir.joinpath(filename)
with open(some_file, "w") as file:
file.write("some content: " + str(random.random()))

return some_file

if has_remote_changes:
some_file = make_file("some_file")
git_repo.run(["add", some_file])
git_repo.run(["commit", "-m", "a commit"])
git_repo.run(["push"])
git_repo.run(["reset", "--hard", "HEAD^"])

if has_untracked_files:
make_file("some_file")

if needs_stash:
some_file = make_file("some_stashed_file")
git_repo.run(["add", some_file])

mocka = mocker.spy(git_repo, "run")
git_repo.update_repo()

mocka.assert_any_call(["symbolic-ref", "--short", "HEAD"])


@pytest.mark.parametrize(
# Postpone evaluation of options so fixture variables can interpolate
"constructor,lazy_constructor_options",
Expand Down

0 comments on commit d16c5f9

Please sign in to comment.