From ae53fb0516a9c2a1256141a8e12b266b1df91ad8 Mon Sep 17 00:00:00 2001 From: Trevor Currie Date: Mon, 5 Apr 2021 09:24:08 -0700 Subject: [PATCH 1/2] Adding --show option for CI/CD processes to consume --- bumpversion/cli.py | 15 ++++++++++++++- tests/test_cli.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/bumpversion/cli.py b/bumpversion/cli.py index b107e10d..1575b15b 100644 --- a/bumpversion/cli.py +++ b/bumpversion/cli.py @@ -55,6 +55,7 @@ ) logger_list = logging.getLogger("bumpversion.list") +logger_show = logging.getLogger("bumpversion.show") logger = logging.getLogger(__name__) time_context = {"now": datetime.now(), "utcnow": datetime.utcnow()} special_char_context = {c: c for c in ("#", ";")} @@ -79,7 +80,7 @@ def main(original_args=None): # determine configuration based on command-line arguments # and on-disk configuration files args, known_args, root_parser, positionals = _parse_arguments_phase_1(original_args) - _setup_logging(known_args.list, known_args.verbose) + _setup_logging(known_args.list or known_args.show, known_args.verbose) vcs_info = _determine_vcs_usability() defaults = _determine_current_version(vcs_info) explicit_config = None @@ -94,6 +95,10 @@ def main(original_args=None): ) version_config = _setup_versionconfig(known_args, part_configs) current_version = version_config.parse(known_args.current_version) + if known_args.show: + logger_show.info("{}".format(current_version.original)) + return + context = dict( itertools.chain( time_context.items(), @@ -192,6 +197,13 @@ def _parse_arguments_phase_1(original_args): help="List machine readable information", required=False, ) + root_parser.add_argument( + "--show", + action="store_true", + default=False, + help="Show machine readable current version", + required=False, + ) root_parser.add_argument( "--allow-dirty", action="store_true", @@ -215,6 +227,7 @@ def _setup_logging(show_list, verbose): logger_list.addHandler(ch2) if show_list: logger_list.setLevel(logging.DEBUG) + logger_show.setLevel(logging.DEBUG) try: log_level = [logging.WARNING, logging.INFO, logging.DEBUG][verbose] except IndexError: diff --git a/tests/test_cli.py b/tests/test_cli.py index 1589da01..0ba0d39c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -101,6 +101,7 @@ def _mock_calls_to_string(called_mock): [--config-file FILE] [--verbose] [--list] +[--show] [--allow-dirty] [--parse REGEX] [--serialize FORMAT] @@ -134,6 +135,7 @@ def _mock_calls_to_string(called_mock): (default: .bumpversion.cfg) --verbose Print verbose logging to stderr (default: 0) --list List machine readable information (default: False) + --show Show machine readable current version (default: False) --allow-dirty Don't abort if working directory is dirty (default: False) --parse REGEX Regex parsing the version string (default: @@ -1548,6 +1550,32 @@ def test_no_list_no_stdout(tmpdir, vcs): assert out == "" +def test_show(tmpdir, vcs): + tmpdir.join("show_me_what_i_want_to_see.txt").write("0.7.9") + tmpdir.chdir() + + tmpdir.join(".bumpversion.cfg").write(dedent(""" + [bumpversion] + current_version = 0.7.9 + commit = False + tag = False + [bumpversion:file:show_me_what_i_want_to_see.txt] + """).strip()) + + check_call([vcs, "init"]) + check_call([vcs, "add", "show_me_what_i_want_to_see.txt"]) + check_call([vcs, "commit", "-m", "initial commit"]) + + with LogCapture() as log_capture: + main(['--show']) + + log_capture.check( + ('bumpversion.show', 'INFO', '0.7.9'), + ) + + assert '0.7.9' == tmpdir.join("show_me_what_i_want_to_see.txt").read() + + def test_bump_non_numeric_parts(tmpdir): tmpdir.join("with_pre_releases.txt").write("1.5.dev") tmpdir.chdir() From c7737534ce20dd17cc3cd0df4671e0788ecb2862 Mon Sep 17 00:00:00 2001 From: Trevor Currie Date: Wed, 7 Apr 2021 09:22:08 -0700 Subject: [PATCH 2/2] Adding readme content. --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index bc09b32c..2c959422 100644 --- a/README.md +++ b/README.md @@ -464,6 +464,11 @@ Additionally, the following options are available: ## Using bumpversion in a script +If you need to use the current version in a script you can make use of +the `--show` option. This will return the current version of the service. + + bump2version --show + If you need to use the version generated by bumpversion in a script you can make use of the `--list` option, combined with `grep` and `sed`.