Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(git): Fix update_repo when there are untracked files (Take 2) #425

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ URL renamings (#417):
- `URLProtocol`: Fix `is_valid` to use `classmethod`
- All: Fix `is_valid` to use default of `None` to avoid implicitly filtering
- Reduce duplicated code in methods by using `super()`
- `git`: Fix `update_repo` when there are only untracked files

### Packaging

Expand Down
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],
tony marked this conversation as resolved.
Show resolved Hide resolved
],
)
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