dotfiles/bin/semver -rwxr-xr-x 1.3 KiB View raw
                                                                                
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
#!/usr/bin/env python3
import os
import subprocess
import sys
import tempfile
p = subprocess.run(["git", "describe"], stdout=subprocess.PIPE)
describe = p.stdout.decode()
describe = describe.split("-")[0]
old_version = describe.split(".")
if len(old_version) == 2:
    [major, minor] = old_version
    [major, minor] = map(int, [major, minor])
    patch = 0
else:
    [major, minor, patch] = old_version
    [major, minor, patch] = map(int, [major, minor, patch])

p = subprocess.run(["git", "shortlog", "--no-merges",
    f"{'.'.join(old_version)}..HEAD"],
        stdout=subprocess.PIPE)
shortlog = p.stdout.decode()

if sys.argv[1] == "patch":
    patch += 1
elif sys.argv[1] == "minor":
    minor += 1
    patch = 0
elif sys.argv[1] == "major":
    major += 1
    minor = patch = 0

if len(old_version) == 2 and patch == 0:
    new_version = f"{major}.{minor}"
else:
    new_version = f"{major}.{minor}.{patch}"

if os.path.exists(".git/_incr_version"):
    subprocess.run([".git/_incr_version", describe, new_version])

with tempfile.NamedTemporaryFile() as f:
    basename = os.path.basename(os.getcwd())
    f.write(f"{basename} {new_version}\n\n".encode())
    f.write(shortlog.encode())
    f.flush()
    subprocess.run(["git", "tag", "-e", "-F", f.name, "-a", new_version])
    print(new_version)