Skip to content

Commit 1de6f78

Browse files
feat(version-cmd): add toggle of --no-verify option to git commit (#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]>
1 parent 170bc80 commit 1de6f78

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

docs/configuration.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,24 @@ When :ref:`config-allow_zero_version` is set to ``false``, this setting is ignor
702702
703703
----
704704
705+
.. _config-no_git_verify:
706+
707+
``no_git_verify``
708+
"""""""""""""""""
709+
710+
**Type:** ``bool``
711+
712+
This flag is passed along to ``git`` upon performing a ``git commit`` during :ref:`cmd-version`.
713+
714+
When true, it will bypass any git hooks that are set for the repository when Python Semantic
715+
Release makes a version commit. When false, the commit is performed as normal. This option
716+
has no effect when there are not any git hooks configured nor when the ``--no-commit`` option
717+
is passed.
718+
719+
**Default:** ``false``
720+
721+
----
722+
705723
.. _config-publish:
706724
707725
``publish``

semantic_release/cli/commands/version.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ def version( # noqa: C901
330330
commit_author = runtime.commit_author
331331
commit_message = runtime.commit_message
332332
major_on_zero = runtime.major_on_zero
333+
no_verify = runtime.no_git_verify
333334
build_command = runtime.build_command
334335
opts = runtime.global_cli_options
335336
gha_output = VersionGitHubActionsOutput()
@@ -613,6 +614,7 @@ def custom_git_environment() -> ContextManager[None]:
613614
)
614615

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

617619
noop_report(
618620
indented(
@@ -628,6 +630,7 @@ def custom_git_environment() -> ContextManager[None]:
628630
repo.git.commit(
629631
m=commit_message.format(version=new_version),
630632
date=int(commit_date.timestamp()),
633+
no_verify=no_verify,
631634
)
632635

633636
# Run the tagging after potentially creating a new HEAD commit.

semantic_release/cli/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ class RawConfig(BaseModel):
225225
major_on_zero: bool = True
226226
allow_zero_version: bool = True
227227
remote: RemoteConfig = RemoteConfig()
228+
no_git_verify: bool = False
228229
tag_format: str = "v{version}"
229230
publish: PublishConfig = PublishConfig()
230231
version_toml: Optional[Tuple[str, ...]] = None
@@ -347,6 +348,7 @@ class RuntimeContext:
347348
major_on_zero: bool
348349
allow_zero_version: bool
349350
prerelease: bool
351+
no_git_verify: bool
350352
assets: List[str]
351353
commit_author: Actor
352354
commit_message: str
@@ -596,6 +598,7 @@ def from_raw_config(
596598
upload_to_vcs_release=raw.publish.upload_to_vcs_release,
597599
global_cli_options=global_cli_options,
598600
masker=masker,
601+
no_git_verify=raw.no_git_verify,
599602
)
600603
# credential masker
601604
self.apply_log_masking(self.masker)

tests/command_line/test_version.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import os
66
import re
77
import shutil
8+
from pathlib import Path
89
from subprocess import CompletedProcess
10+
from textwrap import dedent
911
from typing import TYPE_CHECKING
1012
from unittest import mock
1113

@@ -24,7 +26,6 @@
2426
)
2527

2628
if TYPE_CHECKING:
27-
from pathlib import Path
2829
from unittest.mock import MagicMock
2930

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

549550

551+
def test_version_version_no_verify(
552+
repo_with_single_branch_angular_commits: Repo,
553+
cli_runner: CliRunner,
554+
update_pyproject_toml: UpdatePyprojectTomlFn,
555+
):
556+
# setup: set configuration setting
557+
update_pyproject_toml("tool.semantic_release.no_git_verify", True)
558+
repo_with_single_branch_angular_commits.git.commit(
559+
m="chore: adjust project configuration for --no-verify release commits", a=True
560+
)
561+
# create executable pre-commit script
562+
precommit_hook = Path(
563+
repo_with_single_branch_angular_commits.git_dir, "hooks", "pre-commit"
564+
)
565+
precommit_hook.parent.mkdir(parents=True, exist_ok=True)
566+
precommit_hook.write_text(
567+
dedent(
568+
"""\
569+
#!/bin/sh
570+
echo >&2 "Always fail pre-commit" && exit 1;
571+
"""
572+
)
573+
)
574+
precommit_hook.chmod(0o754)
575+
repo_with_single_branch_angular_commits.git.config(
576+
"core.hookspath",
577+
str(
578+
precommit_hook.parent.relative_to(
579+
repo_with_single_branch_angular_commits.working_dir
580+
)
581+
),
582+
local=True,
583+
)
584+
# Take measurement beforehand
585+
head_before = repo_with_single_branch_angular_commits.head.commit
586+
587+
# Execute
588+
result = cli_runner.invoke(
589+
main, [version_subcmd, "--patch", "--no-tag", "--no-push"]
590+
)
591+
592+
# Evaluate
593+
head_after = repo_with_single_branch_angular_commits.head.commit
594+
595+
assert head_before != head_after # A commit has been made
596+
assert head_before in repo_with_single_branch_angular_commits.head.commit.parents
597+
assert result.exit_code == 0
598+
599+
550600
@pytest.mark.parametrize("shell", ("/usr/bin/bash", "/usr/bin/zsh", "powershell"))
551601
def test_version_runs_build_command(
552602
repo_with_git_flow_angular_commits: Repo,

0 commit comments

Comments
 (0)