Skip to content

Commit

Permalink
Allow creating annotated tags
Browse files Browse the repository at this point in the history
When creating annotated tags (by passing a message into them), you can
use "git describe".

This is based on work by @gvangool.
  • Loading branch information
ekohl committed Oct 9, 2014
1 parent b27cd51 commit 716d117
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
1 change: 1 addition & 0 deletions .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[bumpversion]
commit = True
tag = True
tag_message = "bumpversion {new_version}"
current_version = 0.5.1-dev
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+))?
serialize =
Expand Down
20 changes: 14 additions & 6 deletions bumpversion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,11 @@ def add_path(cls, path):
subprocess.check_output(["git", "add", "--update", path])

@classmethod
def tag(cls, name):
subprocess.check_output(["git", "tag", name])
def tag(cls, name, message):
command = ["git", "tag", name]
if message:
command += ['-m', message]
subprocess.check_output(command)


class Mercurial(BaseVCS):
Expand Down Expand Up @@ -181,7 +184,8 @@ def add_path(cls, path):
pass

@classmethod
def tag(cls, name):
def tag(cls, name, message):
# TODO: use message?
subprocess.check_output(["hg", "tag", name])

VCS = [Git, Mercurial]
Expand Down Expand Up @@ -571,6 +575,7 @@ def serialize(self, version, context):
'--search',
'--replace',
'--tag-name',
'--tag-message',
]


Expand Down Expand Up @@ -843,11 +848,13 @@ def main(original_args=None):
help='Tag name (only works with --tag)',
default=defaults.get('tag_name', 'v{new_version}'))

parser3.add_argument('--tag-message', metavar='TAG_MESSAGE', dest='tag_message',
help='Tag message', default=defaults.get('tag_message', ''))

parser3.add_argument('--message', '-m', metavar='COMMIT_MSG',
help='Commit message',
default=defaults.get('message', 'Bump version: {current_version} → {new_version}'))


file_names = []
if 'files' in defaults:
assert defaults['files'] != None
Expand Down Expand Up @@ -977,12 +984,13 @@ def main(original_args=None):
vcs.commit(message=commit_message)

tag_name = args.tag_name.format(**vcs_context)
tag_message = args.tag_message.format(**vcs_context)
# TODO: log tag_message
logger.info("{} '{}' in {}".format(
"Would tag" if not do_tag else "Tagging",
tag_name,
vcs.__name__
))

if do_tag:
vcs.tag(tag_name)

vcs.tag(tag_name, tag_message)
35 changes: 34 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def _mock_calls_to_string(called_mock):
[--parse REGEX] [--serialize FORMAT] [--search SEARCH]
[--replace REPLACE] [--current-version VERSION] [--dry-run]
--new-version VERSION [--commit | --no-commit]
[--tag | --no-tag] [--tag-name TAG_NAME] [--message COMMIT_MSG]
[--tag | --no-tag] [--tag-name TAG_NAME]
[--tag-message TAG_MESSAGE] [--message COMMIT_MSG]
part [file [file ...]]
%s
Expand Down Expand Up @@ -98,6 +99,8 @@ def _mock_calls_to_string(called_mock):
--no-tag Do not create a tag in version control
--tag-name TAG_NAME Tag name (only works with --tag) (default:
v{new_version})
--tag-message TAG_MESSAGE
Tag message (default: )
--message COMMIT_MSG, -m COMMIT_MSG
Commit message (default: Bump version:
{current_version} → {new_version})
Expand Down Expand Up @@ -686,6 +689,36 @@ def test_message_from_config_file(tmpdir, capsys, vcs):

assert b'from-400.0.0-to-401.0.0' in tag_out

# TODO: hg
@pytest.mark.parametrize(("vcs"), [xfail_if_no_git("git")])
def test_tag_message(tmpdir, vcs):
tmpdir.chdir()
check_call([vcs, "init"])
tmpdir.join("VERSION").write("42.3.1")
check_call([vcs, "add", "VERSION"])
check_call([vcs, "commit", "-m", "initial commit"])

main(['patch', '--current-version', '42.3.1', '--commit', '--tag', 'VERSION', '--tag-name', 'ReleasedVersion-{new_version}'])

tag_out = check_output([vcs, {"git": "tag", "hg": "tags"}[vcs]])

assert b'ReleasedVersion-42.3.2' in tag_out

describe_out = subprocess.call([vcs, "describe"])
assert 128 == describe_out

main(['patch', '--current-version', '42.3.2', '--commit', '--tag', 'VERSION', '--tag-message', 'test {new_version}-tag'])

assert '42.3.3' == tmpdir.join("VERSION").read()

tag_out = subprocess.check_output(["git", "tag"])

assert b'v42.3.3' in tag_out

describe_out = subprocess.check_output(["git", "describe"])

assert b'v42.3.3' in describe_out

config_parser_handles_utf8 = True
try:
import configparser
Expand Down

0 comments on commit 716d117

Please sign in to comment.