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 26, 2022
1 parent dcf97f8 commit a23b1a6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
4 changes: 2 additions & 2 deletions 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 All @@ -435,7 +435,7 @@ def update_repo(self, set_remotes: bool = False, *args: Any, **kwargs: Any) -> N
try:
process = self.run(["rebase", git_remote_name + "/" + git_tag])
except exc.CommandError as e:
if "invalid_upstream" in str(e):
if any(msg in str(e) for msg in ["invalid_upstream", "Aborting"]):
self.log.error(e)
else:
# Rebase failed: Restore previous state.
Expand Down
65 changes: 65 additions & 0 deletions 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 @@ -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,69 @@ 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,
create_git_remote_repo: CreateProjectCallbackFixtureProtocol,
mocker: MockerFixture,
has_untracked_files: bool,
needs_stash: bool,
has_remote_changes: bool,
) -> None:
git_remote_repo = create_git_remote_repo()

git_repo: GitSync = GitSync(
url=f"file://{git_remote_repo}",
dir=tmp_path / "myrepo",
vcs="git",
)
git_repo.obtain() # clone initial repo

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

return some_file

# Make an initial commit so we can reset
some_file = make_file("initial_file")
git_repo.run(["add", some_file])
git_repo.run(["commit", "-m", "a commit"])
git_repo.run(["push"])

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 a23b1a6

Please sign in to comment.