-
Notifications
You must be signed in to change notification settings - Fork 7
/
git-bumptip
executable file
·58 lines (45 loc) · 1.7 KB
/
git-bumptip
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
#!/usr/bin/env python
import argparse
import os
import re
import subprocess
import sys
parser = argparse.ArgumentParser(description='Check out the next version available of a given branch',
epilog="Also consider used version numbers that are not in local branches but only in a remote branch. It refuses to bump versions of the 'master' branch since this is usually a mistake.")
parser.add_argument('-n', '--dry-run',
action='store_true', default=False,
help="dry-run, no action is actually taken")
args = parser.parse_args()
def sh(cmd):
try:
return subprocess.check_output(cmd, stderr=subprocess.STDOUT).decode('utf-8')
except subprocess.CalledProcessError as err:
sys.stderr.write(err.output.decode('utf-8'))
sys.exit(1)
def split_version(branch):
m = re.match("^(.*)-v(\d+)$", branch)
if m is None:
return branch, 1
return m.group(1), int(m.group(2))
branch = sh(["git", "rev-parse", "--abbrev-ref", "HEAD"]).rstrip()
name, current = split_version(branch)
if name == "master":
sys.exit("ERROR: won't version the 'master' branch")
latest = current
latest_branch = branch
branches = sh(["git", "for-each-ref", "--format=%(refname:short)"]).rstrip().split("\n")
for b in branches:
m = re.match("(|.*/)%s(-v(\d+))?$" % name, b)
if m is None:
continue
_, v = split_version(b)
if v > latest:
latest = v
latest_branch = b
print("Latest branch is '%s'" % latest_branch)
next_version = latest + 1
checkout = ["git", "checkout", "-b", "%s-v%d" % (name, next_version)]
if args.dry_run:
print("# " + " ".join(checkout))
else:
sys.stdout.write(sh(checkout))