Skip to content

Commit

Permalink
fix(config): gracefully fail when repo is in a detached HEAD state (#765
Browse files Browse the repository at this point in the history
)

* fix(config): cleanly handle repository in detached HEAD state

* test(cli-main): add detached head cli test
  • Loading branch information
codejedi365 committed Dec 12, 2023
1 parent ea89fa7 commit ac4f9aa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
12 changes: 11 additions & 1 deletion semantic_release/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,18 @@ def from_raw_config(
##
# credentials masking for logging
masker = MaskingFilter(_use_named_masks=raw.logging_use_named_masks)

try:
active_branch = repo.active_branch.name
except TypeError as err:
raise NotAReleaseBranch(
"Detached HEAD state cannot match any release groups; "
"no release will be made"
) from err

# branch-specific configuration
branch_config = cls.select_branch_options(raw.branches, repo.active_branch.name)
branch_config = cls.select_branch_options(raw.branches, active_branch)

# commit_parser
commit_parser_cls = (
_known_commit_parsers[raw.commit_parser]
Expand Down
14 changes: 14 additions & 0 deletions tests/command_line/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ def test_not_a_release_branch_exit_code_with_strict(
assert result.exit_code != 0


def test_not_a_release_branch_detached_head_exit_code(
repo_with_git_flow_angular_commits, cli_runner
):
expected_err_msg = "Detached HEAD state cannot match any release groups; no release will be made"

# cause repo to be in detached head state without file changes
repo_with_git_flow_angular_commits.git.checkout("HEAD", "--detach")
result = cli_runner.invoke(main, ["version", "--no-commit"])

# as non-strict, this will return success exit code
assert result.exit_code == 0
assert expected_err_msg in result.stderr


@pytest.fixture
def toml_file_with_no_configuration_for_psr(tmp_path):
path = tmp_path / "config.toml"
Expand Down

0 comments on commit ac4f9aa

Please sign in to comment.