Merge pull request #43 from RightBrain-Networks/bugfix/currentVersion
Use `current_version` as an input
This commit is contained in:
		@ -49,7 +49,7 @@ tag_name = v{new_version}
 | 
				
			|||||||
message = Bump version: {current_version} -> {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
 | 
					### File updates
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -18,9 +18,11 @@ NO_MERGE_FOUND = Exception('No merge found')
 | 
				
			|||||||
NOT_MAIN_BRANCH = Exception('Not merging into a main branch')
 | 
					NOT_MAIN_BRANCH = Exception('Not merging into a main branch')
 | 
				
			||||||
NO_GIT_FLOW = Exception('No git flow branch found')
 | 
					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):
 | 
					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
 | 
					    # Merge pull request #1 from RightBrain-Networks/feature/PLAT-185-versioning
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self,global_user=False):
 | 
					    def __init__(self,global_user=False):
 | 
				
			||||||
@ -52,12 +54,12 @@ class SemVer(object):
 | 
				
			|||||||
        message = str(p.stdout.read())
 | 
					        message = str(p.stdout.read())
 | 
				
			||||||
        branch = b.stdout.read().decode('utf-8').rstrip()
 | 
					        branch = b.stdout.read().decode('utf-8').rstrip()
 | 
				
			||||||
        logger.info('Main branch is ' + branch)
 | 
					        logger.info('Main branch is ' + branch)
 | 
				
			||||||
        matches = self.GET_COMMIT_MESSAGE.search(message)
 | 
					        matches = GET_COMMIT_MESSAGE.search(message)
 | 
				
			||||||
        if matches:
 | 
					        if matches:
 | 
				
			||||||
            if str(matches.group(4)) == branch:
 | 
					            if str(matches.group(4)) == branch:
 | 
				
			||||||
                self.merged_branch = matches.group(2)
 | 
					                self.merged_branch = matches.group(2)
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                self.merged_branch = matches.group(4)
 | 
					                self.merged_branch = matches.group(5)
 | 
				
			||||||
            self.main_branch = branch
 | 
					            self.main_branch = branch
 | 
				
			||||||
        return bool(matches)
 | 
					        return bool(matches)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -19,15 +19,25 @@ def get_tag_version():
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    logger.debug("Tag expression: " + str(tag_expression))
 | 
					    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],
 | 
					    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')
 | 
					        stdout=subprocess.PIPE, stderr=DEVNULL, cwd=".").stdout.read().decode('utf-8').rstrip().split('\n')
 | 
				
			||||||
    if len(tagged_versions) > 0 and tagged_versions[-1] != "":
 | 
					    if len(tagged_versions) > 0 and tagged_versions[-1] != "":
 | 
				
			||||||
        version = tagged_versions[-1]
 | 
					        version = tagged_versions[-1]
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
    logger.debug("Tag Version: " + str(version))
 | 
					    logger.debug("Tag Version: " + str(version))
 | 
				
			||||||
    return 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):
 | 
					def get_version(dot=False):
 | 
				
			||||||
    version = get_tag_version()
 | 
					    version = get_tag_version()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -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.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 = """
 | 
					config_data = """
 | 
				
			||||||
[bumpversion]
 | 
					[bumpversion]
 | 
				
			||||||
@ -79,6 +79,26 @@ class TestGetTagVersion(unittest.TestCase):
 | 
				
			|||||||
        tag = get_version.get_tag_version()
 | 
					        tag = get_version.get_tag_version()
 | 
				
			||||||
        self.assertEqual(tag, "0.0.0")
 | 
					        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():
 | 
					def create_git_environment():
 | 
				
			||||||
    subprocess.call(['rm', '-rf', './.git'])
 | 
					    subprocess.call(['rm', '-rf', './.git'])
 | 
				
			||||||
    subprocess.call(['git', 'init'])
 | 
					    subprocess.call(['git', 'init'])
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user