diff --git a/README.md b/README.md index 6f58749..109d78b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/semver/__init__.py b/semver/__init__.py index ea9c9c1..987763a 100644 --- a/semver/__init__.py +++ b/semver/__init__.py @@ -18,9 +18,11 @@ NO_MERGE_FOUND = Exception('No merge found') 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): @@ -52,12 +54,12 @@ class SemVer(object): 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) diff --git a/semver/get_version.py b/semver/get_version.py index 35f208d..6714991 100644 --- a/semver/get_version.py +++ b/semver/get_version.py @@ -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() diff --git a/semver/tests.py b/semver/tests.py index 8e18ea2..ac6555c 100644 --- a/semver/tests.py +++ b/semver/tests.py @@ -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] @@ -78,6 +78,26 @@ class TestGetTagVersion(unittest.TestCase): 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'])