summary refs log tree commit diff
path: root/bin/semver
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2020-01-26 17:16:07 +0100
committerRobert Günzler <r@gnzler.io>2020-01-26 18:00:59 +0100
commitf5d56602df70950de6b7d40966b00c9939c81763 (patch)
treeb50ffeb5496fe3525cc1bab2629389ced95435b7 /bin/semver
Intial commit of v2
See https://drewdevault.com/2019/12/30/dotfiles.html
Diffstat (limited to 'bin/semver')
-rwxr-xr-xbin/semver88
1 files changed, 88 insertions, 0 deletions
diff --git a/bin/semver b/bin/semver
new file mode 100755
index 0000000..e2b51e7
--- /dev/null
+++ b/bin/semver
@@ -0,0 +1,88 @@
+#!/usr/bin/env python3
+
+# src:
+# https://git.sr.ht/~sircmpwn/dotfiles/blob/f9e7e88a72c2b87e5ab823205cb3a3aa8f3babd0/bin/semver
+# usage: semver [patch|minor|major|<new_version>]
+# desc: will tag a new version either based on last tag or <new_version>
+#
+# patch(robertgzr)
+# allow simply tagging a new non-annotated release
+
+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"])
+
+if sys.argv[1] == "-t":
+    sys.argv = [sys.argv[0]] + sys.argv[2:]
+    simple_tag = True
+    describe_args = "--tag"
+else:
+    describe_args = "--abbrev=0"
+
+p = subprocess.run(["git", "describe", describe_args], 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)
+
+if simple_tag:
+    subprocess.run(["git", "tag", new_version])
+
+else:
+    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)