From 7ff0bf6057be36917ec0a92222a2b152cfd71901 Mon Sep 17 00:00:00 2001 From: Ben Thayer Date: Sun, 13 Sep 2020 14:49:35 -0500 Subject: [PATCH 1/2] Fix errors setting up levels during rebases and bisects --- gitgud/operations.py | 25 ++++++++++++++--- gitgud/test_operations.py | 56 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 gitgud/test_operations.py diff --git a/gitgud/operations.py b/gitgud/operations.py index 032aa5d0..c9372469 100644 --- a/gitgud/operations.py +++ b/gitgud/operations.py @@ -6,6 +6,7 @@ from pathlib import Path from git import Repo, Git +from git.exc import GitCommandError from git.exc import InvalidGitRepositoryError from gitgud import actor @@ -151,9 +152,16 @@ def commit(self, commit_message, parents, time_offset): skip_hooks=True) return commit_obj - 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() @@ -167,13 +175,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_operations.py b/gitgud/test_operations.py new file mode 100644 index 00000000..f47658c5 --- /dev/null +++ b/gitgud/test_operations.py @@ -0,0 +1,56 @@ +from git.exc import GitCommandError + +from gitgud.skills import all_skills +from gitgud.operations import get_operator + + +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): + all_skills["1"]["1"]._setup() + setup_rebase_conflict() + all_skills["1"]["1"]._setup() + + +def test_reset_during_bisect(gg): + all_skills["1"]["1"]._setup() + setup_bisect() + all_skills["1"]["1"]._setup() + + +def test_reset_during_bisect_then_rebase_conflict(gg): + all_skills["1"]["1"]._setup() + setup_bisect() + setup_rebase_conflict() + all_skills["1"]["1"]._setup() + + +def test_reset_during_rebase_conflict_then_bisect(gg): + all_skills["1"]["1"]._setup() + setup_rebase_conflict() + setup_bisect() + all_skills["1"]["1"]._setup() From 3fa176f0c31a5af0917669de67ace55061dc4109 Mon Sep 17 00:00:00 2001 From: Ben Thayer Date: Sat, 10 Oct 2020 08:09:36 -0500 Subject: [PATCH 2/2] Added fixture to automatically set up a level The level loaded is used for the other levels to base their rebase conflicts off of --- gitgud/{test_operations.py => test_setup.py} | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) rename gitgud/{test_operations.py => test_setup.py} (92%) diff --git a/gitgud/test_operations.py b/gitgud/test_setup.py similarity index 92% rename from gitgud/test_operations.py rename to gitgud/test_setup.py index f47658c5..2f00fd78 100644 --- a/gitgud/test_operations.py +++ b/gitgud/test_setup.py @@ -1,9 +1,17 @@ +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() @@ -31,26 +39,22 @@ def setup_bisect(): def test_reset_during_rebase_conflict(gg): - all_skills["1"]["1"]._setup() setup_rebase_conflict() all_skills["1"]["1"]._setup() def test_reset_during_bisect(gg): - all_skills["1"]["1"]._setup() setup_bisect() all_skills["1"]["1"]._setup() def test_reset_during_bisect_then_rebase_conflict(gg): - all_skills["1"]["1"]._setup() setup_bisect() setup_rebase_conflict() all_skills["1"]["1"]._setup() def test_reset_during_rebase_conflict_then_bisect(gg): - all_skills["1"]["1"]._setup() setup_rebase_conflict() setup_bisect() all_skills["1"]["1"]._setup()