Skip to content

Commit

Permalink
feat: get repo version from historical tags instead of config file
Browse files Browse the repository at this point in the history
repo version will get from historical tags. init 0.0.0 if fail of find any version tag
  • Loading branch information
KenMercusLai committed Jun 23, 2016
1 parent 378e2eb commit a45a9bf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
16 changes: 7 additions & 9 deletions semantic_release/history/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import semver

from ..settings import config
from ..vcs_helpers import get_commit_log
from ..vcs_helpers import get_commit_log, get_last_version
from .logs import evaluate_version_bump # noqa

from .parser_angular import parse_commit_message as angular_parser # noqa isort:skip
Expand All @@ -13,17 +13,15 @@
def get_current_version():
"""
Finds the current version of the package in the current working directory.
Check tags rather than config file. return 0.0.0 if fails
:return: A string with the version number.
"""
filename, variable = config.get('semantic_release', 'version_variable').split(':')
variable = variable.strip()
with open(filename, 'r') as fd:
return re.search(
r'^{0}\s*=\s*[\'"]([^\'"]*)[\'"]'.format(variable),
fd.read(),
re.MULTILINE
).group(1)
version = get_last_version()
if version:
return version
else:
return '0.0.0'


def get_new_version(current_version, level_bump):
Expand Down
12 changes: 6 additions & 6 deletions semantic_release/history/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from ..errors import UnknownCommitMessageStyleError
from ..settings import config, current_commit_parser
from ..vcs_helpers import get_commit_log
from ..vcs_helpers import get_commit_log, get_version_from_tag

LEVELS = {
1: 'patch',
Expand Down Expand Up @@ -31,11 +31,11 @@ def evaluate_version_bump(current_version, force=None):

changes = []
commit_count = 0

for commit_message in get_commit_log('v{0}'.format(current_version)):
if current_version in commit_message:
break

# we have to first find our the version hash tagged correspondingly
version_hash = get_version_from_tag('v{0}'.format(current_version))
for commit_message in get_commit_log(version_hash):
# since changed the version as tag, it won't show up in commit message
# so we could simply get all commits in this version
try:
message = current_commit_parser()(commit_message)
changes.append(message[0])
Expand Down
20 changes: 19 additions & 1 deletion semantic_release/vcs_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ def get_commit_log(from_rev=None):
yield commit.message


def get_last_version():
'''
return last version from repo tags
:return: a string contains version number
'''
tags = len(repo.tags)
for i in range(tags - 1, -1, -1):
if re.match('v\d+\.\d+\.\d+', repo.tags[i].name):
return repo.tags[i].name[1:]


def get_version_from_tag(tag_name):
for i in repo.tags:
if i.name == tag_name:
return i.commit.hexsha

def get_repository_owner_and_name():
"""
Checks the origin remote to get the owner and name of the remote repository.
Expand Down Expand Up @@ -47,7 +64,8 @@ def commit_new_version(version):
:param version: The version number to be used in the commit message
"""
repo.git.add(config.get('semantic_release', 'version_variable').split(':')[0])
repo.git.add(
config.get('semantic_release', 'version_variable').split(':')[0])
return repo.git.commit(m=version, author="semantic-release <semantic-release>")


Expand Down

0 comments on commit a45a9bf

Please sign in to comment.