Skip to content

Commit

Permalink
feat(version-cmd): add toggle of --no-verify option to git commit (
Browse files Browse the repository at this point in the history
…#927)

* test(version-cmd): add test w/ failing pre-commit hook--preventing version commit

* feat(version-cmd): add toggle of `--no-verify` option to `git commit`

  This commit adds a configuration option that toggles the addition of `--no-verify`
command line switch on git commit operations that are run with the `version` command.

* docs(configuration): add `no_git_verify` description to the configuration page

---------

Co-authored-by: bdorsey <[email protected]>
  • Loading branch information
codejedi365 and BrentDorsey committed May 18, 2024
1 parent 170bc80 commit 1de6f78
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
18 changes: 18 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,24 @@ When :ref:`config-allow_zero_version` is set to ``false``, this setting is ignor
----
.. _config-no_git_verify:
``no_git_verify``
"""""""""""""""""
**Type:** ``bool``
This flag is passed along to ``git`` upon performing a ``git commit`` during :ref:`cmd-version`.
When true, it will bypass any git hooks that are set for the repository when Python Semantic
Release makes a version commit. When false, the commit is performed as normal. This option
has no effect when there are not any git hooks configured nor when the ``--no-commit`` option
is passed.
**Default:** ``false``
----
.. _config-publish:
``publish``
Expand Down
3 changes: 3 additions & 0 deletions semantic_release/cli/commands/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ def version( # noqa: C901
commit_author = runtime.commit_author
commit_message = runtime.commit_message
major_on_zero = runtime.major_on_zero
no_verify = runtime.no_git_verify
build_command = runtime.build_command
opts = runtime.global_cli_options
gha_output = VersionGitHubActionsOutput()
Expand Down Expand Up @@ -613,6 +614,7 @@ def custom_git_environment() -> ContextManager[None]:
)

command += f"git commit -m '{indented_commit_message}'"
command += "--no-verify" if no_verify else ""

noop_report(
indented(
Expand All @@ -628,6 +630,7 @@ def custom_git_environment() -> ContextManager[None]:
repo.git.commit(
m=commit_message.format(version=new_version),
date=int(commit_date.timestamp()),
no_verify=no_verify,
)

# Run the tagging after potentially creating a new HEAD commit.
Expand Down
3 changes: 3 additions & 0 deletions semantic_release/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ class RawConfig(BaseModel):
major_on_zero: bool = True
allow_zero_version: bool = True
remote: RemoteConfig = RemoteConfig()
no_git_verify: bool = False
tag_format: str = "v{version}"
publish: PublishConfig = PublishConfig()
version_toml: Optional[Tuple[str, ...]] = None
Expand Down Expand Up @@ -347,6 +348,7 @@ class RuntimeContext:
major_on_zero: bool
allow_zero_version: bool
prerelease: bool
no_git_verify: bool
assets: List[str]
commit_author: Actor
commit_message: str
Expand Down Expand Up @@ -596,6 +598,7 @@ def from_raw_config(
upload_to_vcs_release=raw.publish.upload_to_vcs_release,
global_cli_options=global_cli_options,
masker=masker,
no_git_verify=raw.no_git_verify,
)
# credential masker
self.apply_log_masking(self.masker)
Expand Down
52 changes: 51 additions & 1 deletion tests/command_line/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import os
import re
import shutil
from pathlib import Path
from subprocess import CompletedProcess
from textwrap import dedent
from typing import TYPE_CHECKING
from unittest import mock

Expand All @@ -24,7 +26,6 @@
)

if TYPE_CHECKING:
from pathlib import Path
from unittest.mock import MagicMock

from click.testing import CliRunner
Expand Down Expand Up @@ -547,6 +548,55 @@ def test_version_prints_current_version_if_no_new_version(
assert result.stdout == "1.2.0-alpha.2\n"


def test_version_version_no_verify(
repo_with_single_branch_angular_commits: Repo,
cli_runner: CliRunner,
update_pyproject_toml: UpdatePyprojectTomlFn,
):
# setup: set configuration setting
update_pyproject_toml("tool.semantic_release.no_git_verify", True)
repo_with_single_branch_angular_commits.git.commit(
m="chore: adjust project configuration for --no-verify release commits", a=True
)
# create executable pre-commit script
precommit_hook = Path(
repo_with_single_branch_angular_commits.git_dir, "hooks", "pre-commit"
)
precommit_hook.parent.mkdir(parents=True, exist_ok=True)
precommit_hook.write_text(
dedent(
"""\
#!/bin/sh
echo >&2 "Always fail pre-commit" && exit 1;
"""
)
)
precommit_hook.chmod(0o754)
repo_with_single_branch_angular_commits.git.config(
"core.hookspath",
str(
precommit_hook.parent.relative_to(
repo_with_single_branch_angular_commits.working_dir
)
),
local=True,
)
# Take measurement beforehand
head_before = repo_with_single_branch_angular_commits.head.commit

# Execute
result = cli_runner.invoke(
main, [version_subcmd, "--patch", "--no-tag", "--no-push"]
)

# Evaluate
head_after = repo_with_single_branch_angular_commits.head.commit

assert head_before != head_after # A commit has been made
assert head_before in repo_with_single_branch_angular_commits.head.commit.parents
assert result.exit_code == 0


@pytest.mark.parametrize("shell", ("/usr/bin/bash", "/usr/bin/zsh", "powershell"))
def test_version_runs_build_command(
repo_with_git_flow_angular_commits: Repo,
Expand Down

0 comments on commit 1de6f78

Please sign in to comment.