Skip to content

Commit

Permalink
#24 Clean up the code and comments in the hook
Browse files Browse the repository at this point in the history
  • Loading branch information
mverleg committed Feb 21, 2018
1 parent a0053a5 commit 8b1f37b
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions other/hooks/post-commit
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

"""
This git hooks makes sure a tag is created each time the version is increased.
This version only works with version from `setup.py`, but can easily be adapted.
This version only works with version from `setup.py` and `settings.gradle`, but can be adapted.
To enable it on git 2.9+, use::
chmod u+x dev/hooks/*
git config core.hooksPath dev/hooks
On older versions, copy or symlink the hooks to `.git/hooks`
https://gist.github.com/mverleg/9988b44d57b63b0eef40dd9ce7b48e45
"""

from re import findall
Expand All @@ -33,27 +34,35 @@ def get_existing_versions():
git('tag --list --format "%(refname:short)"').splitlines())


def get_current_version():
"""
Get the current version of the project, according to package file.
PWD is the root of the git project.
"""
def get_setup_py_version():
if exists('setup.py'):
with open('setup.py', 'r') as fh:
res = findall(r'\n[^#\n]*version\s*=[\'"]([\w.-_]+)[\'"]', fh.read())
assert len(res) != 0, 'Did not find version inside setup.py'
assert len(res) < 2, 'Found multiple versions inside setup.py'
return res[0].strip().lstrip('v')
return None


def get_settings_gradle_version():
if exists('settings.gradle'):
with open('settings.gradle', 'r') as fh:
""" Only single-quotes supported, because double quotes may contain variables. """
res = findall(r'\n\s*version\s*=\'([\w.-_]+)\'', fh.read())
res = findall(r'\n\s*version\s*=\s*\'([\w.-_]+)\'', fh.read())
assert len(res) != 0, 'Did not find version inside settings.gradle'
assert len(res) < 2, 'Found multiple versions inside settings.gradle'
return res[0].strip().lstrip('v')
return None


def get_current_version():
"""
Get the current version of the project, according to package file.
PWD is the root of the git project.
"""
return get_setup_py_version() or get_settings_gradle_version() or None


def version_tag():
"""
Create a tag for the current version if there isn't one yet.
Expand Down

0 comments on commit 8b1f37b

Please sign in to comment.