From bf030d4fa0e5b2e7cda64346af85c043f8290868 Mon Sep 17 00:00:00 2001 From: Manuel Barkhau Date: Thu, 13 May 2021 17:07:31 +0000 Subject: [PATCH] implement -c/--commit-message --- src/bumpver/cli.py | 48 ++++++++++++++++++++++++++++++---------------- test/test_cli.py | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 16 deletions(-) diff --git a/src/bumpver/cli.py b/src/bumpver/cli.py index f9811db..3646d88 100755 --- a/src/bumpver/cli.py +++ b/src/bumpver/cli.py @@ -6,6 +6,7 @@ # SPDX-License-Identifier: MIT """cli module for BumpVer.""" import io +import re import sys import typing as typ import logging @@ -687,19 +688,27 @@ def _update_cfg_from_vcs(cfg: config.Config, fetch: bool) -> config.Config: ), ) @version_options +@click.option( + "-c", + "--commit-message", + default=None, + metavar="", + help="Set commit message.", +) def update( - dry : bool = False, - allow_dirty: bool = False, - fetch : bool = True, - verbose : int = 0, - major : bool = False, - minor : bool = False, - patch : bool = False, - tag : typ.Optional[str] = None, - tag_num : bool = False, - pin_date : bool = False, - date : typ.Optional[str] = None, - set_version: typ.Optional[str] = None, + dry : bool = False, + allow_dirty : bool = False, + fetch : bool = True, + verbose : int = 0, + major : bool = False, + minor : bool = False, + patch : bool = False, + tag : typ.Optional[str] = None, + tag_num : bool = False, + pin_date : bool = False, + date : typ.Optional[str] = None, + set_version : typ.Optional[str] = None, + commit_message: typ.Optional[str] = None, ) -> None: """Update project files with the incremented version string.""" verbose = max(_VERBOSE, verbose) @@ -747,18 +756,25 @@ def update( if dry or verbose >= 2: _print_diff(cfg, new_version) - if dry: - return + if commit_message is None: + commit_msg_template = cfg.commit_message + else: + commit_msg_template, _ = re.subn(r"\b(OLD|NEW)\b", r"{\1_VERSION}", commit_message) commit_message_kwargs = { 'new_version' : new_version, 'old_version' : old_version, + 'NEW_VERSION' : new_version, + 'OLD_VERSION' : old_version, 'new_version_pep440': version.to_pep440(new_version), 'old_version_pep440': version.to_pep440(old_version), } - commit_message = cfg.commit_message.format(**commit_message_kwargs) + try_commit_message = commit_msg_template.format(**commit_message_kwargs) - _try_update(cfg, new_version, commit_message, allow_dirty) + if dry: + return + + _try_update(cfg, new_version, try_commit_message, allow_dirty) if __name__ == '__main__': diff --git a/test/test_cli.py b/test/test_cli.py index 5145772..8187ec6 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -890,6 +890,42 @@ def test_git_commit_message(runner, caplog): assert expected in commits[1] +def test_cli_commit_message(runner, caplog): + _add_project_files("README.md", "setup.cfg") + result = runner.invoke(cli.cli, ['init', "-vv"]) + assert result.exit_code == 0 + + _update_config_val( + "setup.cfg", + current_version='"v2019.1001-alpha"', + version_pattern="vYYYY.BUILD[-TAG]", + ) + + _vcs_init("git", ["README.md", "setup.cfg"]) + assert len(caplog.records) > 0 + + cmd = [ + 'update', + "-vv", + "--pin-date", + "--tag", + "beta", + "--commit-message", + "my custom message (OLD -> NEW)", + ] + + result = runner.invoke(cli.cli, cmd) + assert result.exit_code == 0 + + tags = shell("git", "tag", "--list").decode("utf-8") + assert "v2019.1002-beta" in tags + + commits = shell(*shlex.split("git log -l 2")).decode("utf-8").split("\n\n") + + expected = "my custom message (v2019.1001-alpha -> v2019.1002-beta)" + assert expected in commits[1] + + def test_grep(runner): _add_project_files("README.md")