Skip to content

Commit

Permalink
Merge pull request #43 from RightBrain-Networks/bugfix/currentVersion
Browse files Browse the repository at this point in the history
Use `current_version` as an input
  • Loading branch information
Derek DeJonghe authored Feb 28, 2020
2 parents dc8fdc1 + cb05f46 commit fa143a3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ tag_name = v{new_version}
message = Bump version: {current_version} -> {new_version}
```

The `current_version` exists to tell bumpversion what the current version is. To have auto-semver manage this value, set it to `0.0.0`. The `commit` and `tag` options determine whether to create a new Git commit and a new Git tag, respectively. The `tag_name` represents what the name of the Git tag will be, and by default is set to `{new_version}`, which will be substitued with the new version during runtime. This can be changed as desired - for example, `v{new_version}` could resolve to `v1.15.5`. The `message` option is what the message used if there is a git commit.
The `current_version` exists to tell bumpversion what the current version is. Auto-semver uses this value as the default version if not version if found in the tags. The `commit` and `tag` options determine whether to create a new Git commit and a new Git tag, respectively. The `tag_name` represents what the name of the Git tag will be, and by default is set to `{new_version}`, which will be substitued with the new version during runtime. This can be changed as desired - for example, `v{new_version}` could resolve to `v1.15.5`. The `message` option is what the message used if there is a git commit.

### File updates

Expand Down
8 changes: 5 additions & 3 deletions semver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
NOT_MAIN_BRANCH = Exception('Not merging into a main branch')
NO_GIT_FLOW = Exception('No git flow branch found')

# Important regex
GET_COMMIT_MESSAGE = re.compile(r"Merge (branch|pull request) '?([^']+)'? (into|from) (?:'(.+)'|[^\/]+\/(.+))")

class SemVer(object):

GET_COMMIT_MESSAGE = re.compile(r"Merge (branch|pull request) '?(.+)'? (into|from) '?([\w\-]+)'?")
# Merge pull request #1 from RightBrain-Networks/feature/PLAT-185-versioning

def __init__(self,global_user=False):
Expand Down Expand Up @@ -52,12 +54,12 @@ def get_branches(self):
message = str(p.stdout.read())
branch = b.stdout.read().decode('utf-8').rstrip()
logger.info('Main branch is ' + branch)
matches = self.GET_COMMIT_MESSAGE.search(message)
matches = GET_COMMIT_MESSAGE.search(message)
if matches:
if str(matches.group(4)) == branch:
self.merged_branch = matches.group(2)
else:
self.merged_branch = matches.group(4)
self.merged_branch = matches.group(5)
self.main_branch = branch
return bool(matches)

Expand Down
12 changes: 11 additions & 1 deletion semver/get_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,25 @@ def get_tag_version():

logger.debug("Tag expression: " + str(tag_expression))

version = "0.0.0"
# Default version is `0.0.0` or what is found in
version = get_file_version(config)

# If a version is found in tags, use that the lastest tagged version
tagged_versions = subprocess.Popen(['git','tag','--sort=taggerdate', '-l',tag_expression],
stdout=subprocess.PIPE, stderr=DEVNULL, cwd=".").stdout.read().decode('utf-8').rstrip().split('\n')
if len(tagged_versions) > 0 and tagged_versions[-1] != "":
version = tagged_versions[-1]

logger.debug("Tag Version: " + str(version))
return version

def get_file_version(config):
version = config.get('bumpversion','current_version')
if not version:
config.set('bumpversion', 'current_version', '0.0.0')
version = '0.0.0'
return version

def get_version(dot=False):
version = get_tag_version()

Expand Down
24 changes: 22 additions & 2 deletions semver/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest, os, subprocess, semver
import unittest, os, subprocess, re, semver
from semver.logger import logging, logger, console_logger

from semver import get_version, NO_MERGE_FOUND
from semver import get_version, NO_MERGE_FOUND, GET_COMMIT_MESSAGE

config_data = """
[bumpversion]
Expand Down Expand Up @@ -78,6 +78,26 @@ def test_default_get_version_tag(self):
create_git_environment()
tag = get_version.get_tag_version()
self.assertEqual(tag, "0.0.0")

class TestGetCommitMessageRegex(unittest.TestCase):
def test_github_message(self):
matches = GET_COMMIT_MESSAGE.search("Merge pull request #1 from user/branch")
if matches:
self.assertEqual(matches.group(4), None)
self.assertEqual(matches.group(5), "branch")
else:
self.assertTrue(False)
pass
def test_gitlab_message(self):
matches = GET_COMMIT_MESSAGE.search("Merge branch 'branch' into 'master'")
if matches:
self.assertEqual(matches.group(4), "master")
self.assertEqual(matches.group(2), "branch")
else:
self.assertTrue(False)
def test_non_merge_message(self):
matches = GET_COMMIT_MESSAGE.search("Example unrelated commit message that should get 0 matches")
self.assertEqual(matches, None)

def create_git_environment():
subprocess.call(['rm', '-rf', './.git'])
Expand Down

0 comments on commit fa143a3

Please sign in to comment.