Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding --show option for CI/CD processes to consume #209

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
15 changes: 14 additions & 1 deletion bumpversion/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ("#", ";")}
Expand All @@ -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
Expand All @@ -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(),
Expand Down Expand Up @@ -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",
Expand All @@ -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:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def _mock_calls_to_string(called_mock):
[--config-file FILE]
[--verbose]
[--list]
[--show]
[--allow-dirty]
[--parse REGEX]
[--serialize FORMAT]
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down