Skip to content

Commit

Permalink
feat: Add support to finding previous version from tags if not using …
Browse files Browse the repository at this point in the history
…commit messages (#68)

* feat: Be a bit more forgiving to find previous tags

Now grabs the previous version from tag names if it can't find it in the commit

* quantifiedcode and flake8 fixes

* Update cli.py

* Switch to ImproperConfigurationError
  • Loading branch information
halkeye authored and relekang committed Apr 12, 2018
1 parent a40f56f commit 6786487
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
9 changes: 7 additions & 2 deletions semantic_release/cli.py
Expand Up @@ -87,8 +87,13 @@ def changelog(**kwargs):
Generates the changelog since the last release.
"""
current_version = get_current_version()
log = generate_changelog(
get_previous_version(current_version), current_version)
if current_version is None:
raise ImproperConfigurationError("Unable to get the current version."
" Make sure semantic_release.version_variable "
"is setup correctly")
previous_version = get_previous_version(current_version)

log = generate_changelog(previous_version, current_version)
for section in CHANGELOG_SECTIONS:
if not log[section]:
continue
Expand Down
7 changes: 5 additions & 2 deletions semantic_release/history/__init__.py
Expand Up @@ -69,8 +69,11 @@ def get_previous_version(version):
continue

if found_version:
if re.match(r'v?\d+.\d+.\d+', commit_message):
return commit_message.replace('v', '').strip()
matches = re.match(r'v?(\d+.\d+.\d+)', commit_message)
if matches:
return matches.group(1).strip()

return get_last_version([version, 'v{}'.format(version)])


def set_new_version(new_version):
Expand Down
14 changes: 11 additions & 3 deletions semantic_release/vcs_helpers.py
@@ -1,6 +1,6 @@
import re

from git import GitCommandError, NoSuchPathError, Repo
from git import GitCommandError, TagObject, NoSuchPathError, Repo

from .errors import GitError
from .settings import config
Expand All @@ -23,15 +23,23 @@ def get_commit_log(from_rev=None):
yield (commit.hexsha, commit.message)


def get_last_version():
def get_last_version(skip_tags=None):
"""
return last version from repo tags
:return: a string contains version number
"""
skip_tags = skip_tags or []

for i in sorted(repo.tags, key=lambda x: x.commit.committed_date, reverse=True):
def version_finder(x):
if isinstance(x.commit, TagObject):
return x.tag.tagged_date
return x.commit.committed_date

for i in sorted(repo.tags, reverse=True, key=version_finder):
if re.match('v\d+\.\d+\.\d+', i.name):
if i.name in skip_tags:
continue
return i.name[1:]


Expand Down

0 comments on commit 6786487

Please sign in to comment.