diff --git a/repobee_sanitizer/_sanitize_repo.py b/repobee_sanitizer/_sanitize_repo.py index b036b76..d688af7 100644 --- a/repobee_sanitizer/_sanitize_repo.py +++ b/repobee_sanitizer/_sanitize_repo.py @@ -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) @@ -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( diff --git a/repobee_sanitizer/sanitizer.py b/repobee_sanitizer/sanitizer.py index 7dff38c..2660bff 100644 --- a/repobee_sanitizer/sanitizer.py +++ b/repobee_sanitizer/sanitizer.py @@ -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( diff --git a/tests/test_ext_commands.py b/tests/test_ext_commands.py index f766ac8..50da8a8 100644 --- a/tests/test_ext_commands.py +++ b/tests/test_ext_commands.py @@ -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