Skip to content

Commit

Permalink
[fix] Fix crash when no diff between target-branch and sanitized content
Browse files Browse the repository at this point in the history
  • Loading branch information
tohanss authored Aug 19, 2020
1 parent 1fd49b4 commit 2110de7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
10 changes: 9 additions & 1 deletion repobee_sanitizer/_sanitize_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
LOGGER = daiquiri.getLogger(__file__)


class EmptyCommitError(plug.PlugError):
pass


def check_repo_state(repo_root) -> Optional[str]:
try:
repo = git.Repo(repo_root)
Expand Down Expand Up @@ -136,7 +140,11 @@ def _git_commit_on_branch(repo_root: pathlib.Path, target_branch: str):
repo = git.Repo(str(repo_root))
repo.git.symbolic_ref("HEAD", f"refs/heads/{target_branch}")
repo.git.add(".", "--force")
repo.git.commit("-m", "'Sanitize files'")
try:
repo.git.commit("-m", "'Sanitize files'")
except git.GitCommandError as exc:
assert "nothing to commit, working tree clean" in str(exc)
raise EmptyCommitError() from exc


def _git_fetch(
Expand Down
14 changes: 11 additions & 3 deletions repobee_sanitizer/sanitizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,17 @@ def command(self, api) -> Optional[plug.Result]:
errors = _sanitize_repo.sanitize_files(repo_root, file_relpaths)
else:
LOGGER.info(f"Sanitizing repo and updating {self.target_branch}")
errors = _sanitize_repo.sanitize_to_target_branch(
repo_root, self.target_branch
)
try:
errors = _sanitize_repo.sanitize_to_target_branch(
repo_root, self.target_branch
)
except _sanitize_repo.EmptyCommitError:
return plug.Result(
name="sanitize-repo",
msg="No diff between target branch and sanitized output. "
f"No changes will be made to branch: {self.target_branch}",
status=plug.Status.WARNING,
)

if errors:
return plug.Result(
Expand Down
19 changes: 19 additions & 0 deletions tests/test_ext_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,25 @@ def test_target_branch_with_iso8859_file(self, fake_repo):
fake_repo.repo.git.reset("--hard")
assert_expected_text_in_files(fake_repo.file_infos)

def test_throws_error_when_zero_diff_on_target_branch(
self, sanitizer_config, fake_repo
):
"""Test that repobee raises a GitCommandError when we try to sanitize
to a branch when there are going to be no changes. To do this, all we
have to do is run sanitize twice on the same branch.
"""
run_repobee(
f"sanitize repo --target-branch master "
f"--repo-root {fake_repo.path}".split()
)

error = run_repobee(
f"sanitize repo --target-branch master "
f"--repo-root {fake_repo.path}".split()
)

assert "No changes will be made to branch: master" in error.msg

def test_removes_file_with_shred_marker(self, sanitizer_config, fake_repo):
"""Test that sanitize-repo does not send any files that contain a shred
marker to target-branch
Expand Down

0 comments on commit 2110de7

Please sign in to comment.