diff --git a/gitgud/operations.py b/gitgud/operations.py index 7f0d4f5b..3971f2a3 100644 --- a/gitgud/operations.py +++ b/gitgud/operations.py @@ -8,6 +8,7 @@ import json from git import Repo, Git +from git.exc import GitCommandError from git.exc import InvalidGitRepositoryError from gitgud import actor @@ -210,9 +211,16 @@ def get_working_directory_content(self): content[path] = data return DirectoryContent(content) - def create_tree(self, commits, head, details, level_dir): - if not details: - details = {} + def normalize_state(self): + # Make sure we're in a normal state + try: + self.repo.git.rebase('--abort') + except GitCommandError: + pass + try: + self.repo.git.bisect('reset') + except GitCommandError: + pass self.clear_tree_and_index() @@ -226,13 +234,24 @@ def create_tree(self, commits, head, details, level_dir): # Detach HEAD so we can delete branches self.repo.git.checkout(self.repo.head.commit) + def reset_repo(self): + self.normalize_state() + branches = self.repo.branches for branch in branches: self.repo.delete_head(branch, force=True) + self.repo.delete_tag(*self.repo.tags) + for remote in self.repo.remotes: self.repo.delete_remote(remote) + def create_tree(self, commits, head, details, level_dir): + if not details: + details = {} + + self.reset_repo() + commit_objects = {} counter = len(commits) for name, parents, branches, tags in commits: diff --git a/gitgud/test_setup.py b/gitgud/test_setup.py new file mode 100644 index 00000000..2f00fd78 --- /dev/null +++ b/gitgud/test_setup.py @@ -0,0 +1,60 @@ +import pytest + +from git.exc import GitCommandError + +from gitgud.skills import all_skills +from gitgud.operations import get_operator + + +@pytest.fixture(autouse=True) +def setup_level(): + all_skills["1"]["1"]._setup() + yield + + +def setup_rebase_conflict(): + file_operator = get_operator() + + with open("foo", "w") as f: + f.write("branch 1") + file_operator.repo.index.add("foo") + file_operator.repo.index.commit("Add a file") + + file_operator.repo.git.checkout('-b', 'branch', 'HEAD~') + with open("foo", "w") as f: + f.write("branch 2") + file_operator.repo.index.add("foo") + file_operator.repo.index.commit("Add a file") + + try: + file_operator.repo.git.rebase('master') + except GitCommandError: + # This will happen every time + pass + + +def setup_bisect(): + file_operator = get_operator() + file_operator.repo.git.bisect('start') + + +def test_reset_during_rebase_conflict(gg): + setup_rebase_conflict() + all_skills["1"]["1"]._setup() + + +def test_reset_during_bisect(gg): + setup_bisect() + all_skills["1"]["1"]._setup() + + +def test_reset_during_bisect_then_rebase_conflict(gg): + setup_bisect() + setup_rebase_conflict() + all_skills["1"]["1"]._setup() + + +def test_reset_during_rebase_conflict_then_bisect(gg): + setup_rebase_conflict() + setup_bisect() + all_skills["1"]["1"]._setup()