-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.py
94 lines (81 loc) · 3.01 KB
/
version.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
"""
This is a script to output the new version tag on stdout. It assumes
it is run in the git repo, and the latest commit message text is provided to
the program's STDIN. We search the stdin for a pattern saying whether
to increment version and if so, which one (major, minor, bug).
"""
import subprocess
import re
import sys
tag_reg = r'^v([0-9]+)\.([0-9]+)\.([0-9]+)$'
patterns = {
'major': (r'^version: major\s*$', 0),
'minor': (r'^version: minor\s*$', 1),
'bug': (r'^version: bug\s*$', 2),
}
def tag2version(tag: str):
"""Convert a tag string to a version tuple,
returns a three-tuple of integers or None if the tag string wasn't
a proper tag."""
output = re.match(tag_reg, tag)
if output is not None:
return (int(output.group(1)),
int(output.group(2)),
int(output.group(3)))
return None
def find_latest_tag(fetch: bool, branch: str = None):
"""
find the most recent/highest numbered tag. If fetch is True, run
git fetch origin --tags first so we know what the tags are.
If branch is provided, it will get the most recent tag number available
on the specified branch.
"""
if fetch:
subprocess.run(['git', 'fetch', 'origin', '--tags'], check=True)
cmd = ['git', 'tag']
if branch != None:
cmd.append(f"--merged=origin/{branch}")
process = subprocess.run(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True)
if process.returncode != 0:
print(
f'An error occured while getting tags. Command: {cmd}',
process.stderr)
raise SystemExit(1)
tags = [tag2version(i) for i in
(process.stdout.splitlines())]
latest = sorted([i for i in tags if i])[-1]
return latest
def get_branch_input() -> str:
if '--branch' in sys.argv[1:]:
branch_index = sys.argv.index('--branch') + 1
if branch_index < len(sys.argv):
return sys.argv[branch_index]
return None
do_fetch = '--fetch' in sys.argv[1:]
msg = sys.stdin.read().splitlines()
branch = get_branch_input()
latest = find_latest_tag(do_fetch, branch)
# Now, loop through each line of the commit message, looking for what
# version.
for type, (pattern, index) in patterns.items():
# if any line matches the version increment pattern, increment the
# version and exit.
if any((re.match(pattern, i) for i in msg)):
new = list(latest)
new[index] += 1
for i in range(index+1, len(new)):
new[i] = 0
print(f'bumping with a {type} version to: {new}', file=sys.stderr)
print('v' + '.'.join([str(i) for i in new]))
break
else:
print(
f'Could not find a version message in commit msg: {msg}!\n'
'Please make sure your commit has version: (bug|minor|major)\n'
'on a line by itself somewhere. See the README.md Versioning section.',
file=sys.stderr)
raise SystemExit(1)