Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 22 additions & 3 deletions gitgud/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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:
Expand Down
56 changes: 56 additions & 0 deletions gitgud/test_operations.py
Original file line number Diff line number Diff line change
@@ -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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick, I'd change the testing to use fixtures instead, but that's not the point of this PR. So, feel free to keep it as it is, if you want.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We got a pytest

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()