forked from linode/linode-api-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version
executable file
·96 lines (78 loc) · 3.1 KB
/
version
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
95
96
#!/usr/bin/env python3
# Usage:
# ./bin/version
# Prints the current version
# ./bin/version minor
# Tags a minor version bump
# ./bin/version major
# Tags a major version bump
# ./bin/version changelog [-d distribution]
# Prints a Debian changelog
import subprocess
import sys
import os
import re
def get_version(ref="HEAD"):
describe = call('git describe {}'.format(ref))
branch = call('git rev-parse --abbrev-ref HEAD')
describe = describe.replace("-", ".").split(".")
parts = [0, 0, 0, branch]
l = len(describe)
for i in range(3):
if l > i:
parts[i] = int(describe[i])
return tuple(parts)
template = """{} ({}-1) {}; urgency=low
* {}
-- {} <{}> {}
"""
def call(cmd):
_, output = subprocess.getstatusoutput(cmd)
return output
def gen_deb_changelog(distribution="", branchname=None):
project = "apinext"
git_cmd="git log -5 --pretty=format:%{}"
commits = call(git_cmd.format("H")).split("\n")
authors = call(git_cmd.format("aN")).split("\n")
author_emails = call(git_cmd.format("aE")).split("\n")
subjects = call(git_cmd.format("s")).split("\n")
dates = call(git_cmd.format("aD")).split("\n")
for commit, author, email, subject, date in zip(commits, authors, author_emails, subjects, dates):
major, minor, patch, branch = get_version(commit)
if not (major or minor or patch):
version = "{}.{}.{}+git{}".format(major, minor, patch, commit)
else:
version = "{}.{}.{}{}".format(major, minor, patch,
'' if branchname == 'master'
else "~dev" if branchname == 'development'
else "~testing" if re.findall('^release\/\d+\.\d+$', branchname)
else '~PR' if branchname
else '')
if not len(distribution):
distribution = "stable" if major >= 1 else "unstable"
print(template.format(project, version, distribution, subject, author, email, date))
major, minor, patch, branch = get_version()
if len(sys.argv) == 1:
print("{}.{}.{}".format(major, minor, patch))
else:
if sys.argv[1] == "branch":
if len(sys.argv) == 2 or sys.argv[2] == "master":
print("{}.{}.{}".format(major, minor, patch))
elif sys.argv[2] == "development":
print("{}.{}.{}~dev".format(major, minor, patch))
elif re.findall('^release\/\d+\.\d+$', sys.argv[2]):
print("{}.{}.{}~testing".format(major, minor, patch))
elif sys.argv[1] == "minor":
ver = "{}.{}".format(major, minor + 1)
call("git tag -a {} -m {}".format(ver, ver))
print("Created tag {}".format(ver))
elif sys.argv[1] == "major":
ver = "{}.{}".format(major + 1, 0)
call("git tag -a {} -m {}".format(ver, ver))
print("Created tag {}".format(ver))
elif sys.argv[1] == "changelog":
if len(sys.argv)>3 and sys.argv[2] == "-d":
distribution=sys.argv[3]
else:
distribution=""
gen_deb_changelog(distribution, sys.argv[4] if len(sys.argv) > 4 else None)