1
0
mirror of https://github.com/balkian/pre-commit-hooks.git synced 2024-09-20 20:21:43 +00:00

Version 0.0.2

* Restricts mandatory version bumping to the master branch
This commit is contained in:
J. Fernando Sánchez 2017-02-13 12:26:27 +01:00
parent 2c30892457
commit e1bc6aa98a
2 changed files with 33 additions and 21 deletions

View File

@ -1 +1 @@
0.0.1 0.0.2

View File

@ -1,31 +1,43 @@
import argparse import argparse
import semver import semver
import os
from subprocess import check_output from subprocess import check_output
def main(argv=None): def main(argv=None):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*') parser.add_argument('filenames', nargs='*')
args = parser.parse_args(argv) args = parser.parse_args(argv)
branch_name = check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip().decode() gitout = check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
error = 1 branch_name = gitout.strip().decode()
for fn in args.filenames:
with open(fn) as f: if len(args.filenames) > 1:
currentcontent = f.read().strip() print('There are more than one VERSION files: '
prevcontent = check_output(['git', 'show', 'HEAD:{}'.format(fn)]).strip().decode() '{}'.format(args.filenames))
current = semver.parse_version_info(currentcontent) return 1
print('Checking versions: {} -> {}'.format(prevcontent, currentcontent)) elif not args.filenames:
if branch_name == 'master' and current.prerelease: if branch_name == 'master':
print('Trying to push a pre-release version to a master branch') print('VERSION files should be bumped in every commit to master')
return 1 return 2
elif semver.compare(currentcontent, prevcontent)<1: else:
print('The new version should be newer than the old one') return 0
return 1
else: fn = args.filenames[0]
error = 0 with open(fn) as f:
if error: currentcontent = f.read().strip()
print('VERSION files should be bumped in every commit') gitout = check_output(['git', 'show', 'HEAD:{}'.format(fn)])
return error prevcontent = gitout.strip().decode()
current = semver.parse_version_info(currentcontent)
print('Checking versions: {} -> {}'.format(prevcontent,
currentcontent))
if branch_name == 'master' and current.prerelease:
print('Trying to push a pre-release version to a master branch')
return 1
elif semver.compare(currentcontent, prevcontent) < 1:
print('The new version should be newer than the old one')
return 1
else:
return 0
if __name__ == '__main__': if __name__ == '__main__':
exit(main()) exit(main())