diff options
author | Matthew Lemon <y@yulqen.org> | 2023-10-17 13:35:51 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2023-10-17 13:35:51 +0100 |
commit | 1e66a163ee04e9fea9e16353f23f52b6bc0098ff (patch) | |
tree | 87d25c848c7ef83e86d3a193ea1f24e4df8c188c | |
parent | 6b2ddee030502a258e2a3f9c1984e3b5b96b2550 (diff) |
Adds Drew DeVault's semver script
I'm sure I can find a use for this.
-rw-r--r-- | semver | 65 |
1 files changed, 65 insertions, 0 deletions
@@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +import os +import subprocess +import sys +import tempfile +if subprocess.run(["git", "branch", "--show-current"], stdout=subprocess.PIPE + ).stdout.decode().strip() != "master": + print("WARNING! Not on the master branch.") + +subprocess.run(["git", "pull", "--rebase"]) +p = subprocess.run(["git", "describe", "--abbrev=0"], stdout=subprocess.PIPE) +describe = p.stdout.decode().strip() +old_version = describe.split("-")[0].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"{describe}..HEAD"], + stdout=subprocess.PIPE) +shortlog = p.stdout.decode() + +new_version = None + +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 +else: + new_version = sys.argv[1] + +if new_version is None: + if len(old_version) == 2 and patch == 0: + new_version = f"{major}.{minor}" + else: + new_version = f"{major}.{minor}.{patch}" + +p = None +if os.path.exists("contrib/_incr_version"): + p = subprocess.run(["contrib/_incr_version", describe, new_version]) +elif os.path.exists(".git/_incr_version"): + print("Warning: _incr_version found at legacy path") + p = subprocess.run([".git/_incr_version", describe, new_version]) +else: + print("Warning: no _incr_version script. " + + "Does this project have any specific release requirements?") + +if p and p.returncode != 0: + print("Error: _incr_version returned nonzero exit code") + sys.exit(1) + +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) |