summary refs log tree commit diff
path: root/bin/semver
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2021-07-29 23:45:43 +0200
committerRobert Günzler <r@gnzler.io>2021-07-29 23:48:28 +0200
commite77881a72b735ca12aa055054656afdfbd6a8fc8 (patch)
tree53ed4778acca18f55141e4efbdc0a841b64116a5 /bin/semver
parent3079e935c793a9aa1831fd165e28636eb7a87dae (diff)
bin: format scripts
Diffstat (limited to 'bin/semver')
-rwxr-xr-xbin/semver85
1 files changed, 44 insertions, 41 deletions
diff --git a/bin/semver b/bin/semver
index 876ae65..4072662 100755
--- a/bin/semver
+++ b/bin/semver
@@ -18,7 +18,6 @@ simple_tag = True
 force = False
 changelog = False
 
-
 def usage():
     print(f"usage: {sys.argv[0]} OPTIONS COMMANDS")
     print("""
@@ -28,7 +27,7 @@ OPTIONS:
 \t-h\tshow this help
 \t-a\tannotate tag
 \t-f\toverwrite existing tag
-\t-c\tdon't write changelog
+\t-c\twrite CHANGELOG
 
 COMMANDS:
 \tpatch
@@ -61,40 +60,49 @@ if any(item in ["-h", "--help", "help"] for item in argv):
 
 if subprocess.run(["git", "branch", "--show-current"],
                   stdout=subprocess.PIPE).stdout.decode().strip() != "master":
-    print("WARNING! Not on the master branch.")
+    print("WARNING: not on master branch.")
 
 subprocess.run(["git", "pull", "--rebase"])
 
-p = subprocess.run(["git", "describe", "--abbrev=0", "--tags"],
-                   stdout=subprocess.PIPE)
-describe = p.stdout.decode().strip()
+describe = subprocess.run(["git", "describe", "--abbrev=0", "--tags"],
+                   stdout=subprocess.PIPE).stdout.decode().strip()
 # TODO also ask to re-append the trailing portion.... e.g. -rev1
 
-prepend = ''
-if describe[0] == 'v':
-    describe = describe[1:]
-    prepend = 'v'
+prepend = None
 
-old_version = describe.split("-")[0].split(".")
-if len(old_version) == 2:
-    [major, minor] = old_version
-    [major, minor] = map(int, [major, minor])
-    patch = 0
-elif len(old_version) == 3:
-    [major, minor, patch] = old_version
-    [major, minor, patch] = map(int, [major, minor, patch])
+if describe:
+    if describe[0] == 'v':
+        describe = describe[1:]
+        prepend = 'v'
 
-else:
-    print(
-        f"WARNING: git describe returned invalid semver version: \"{describe}\""
-    )
-    sys.exit(1)
-    # old_version = [0, 0, 0]
+    old_version = describe.split("-")[0].split(".")
+    if len(old_version) == 2:
+        [major, minor] = old_version
+        [major, minor] = map(int, [major, minor])
+        patch = 0
+    elif len(old_version) == 3:
+        [major, minor, patch] = old_version
+        [major, minor, patch] = map(int, [major, minor, patch])
 
-p = subprocess.run(
-    ["git", "shortlog", "--no-merges", f"{prepend}{describe}..HEAD"],
-    stdout=subprocess.PIPE)
-shortlog = p.stdout.decode()
+    else:
+        print(
+            f"WARNING: git describe returned invalid semver version: \"{describe}\""
+        )
+        sys.exit(1)
+
+    old_version_revision = subprocess.run(["git", "log", "--pretty=%H", "-1", describe],
+                                        stdout=subprocess.PIPE).stdout.decode().strip()
+    head_revision = subprocess.run(["git", "log", "--pretty=%H", "-1", "HEAD"],
+                                        stdout=subprocess.PIPE).stdout.decode().strip()
+    if old_version_revision == head_revision:
+        print(f"WARNING: HEAD already tagged as {describe}")
+        if not force:
+            sys.exit(0)
+
+if not simple_tag or changelog:
+    shortlog = subprocess.run(
+        ["git", "shortlog", "--no-merges", f"{prepend}{describe}..HEAD"],
+        stdout=subprocess.PIPE).stdout.decode()
 
 new_version = None
 
@@ -109,28 +117,20 @@ elif argv[0] == "major":
 else:
     new_version = argv[0]
 
-if new_version is None:
+if not new_version:
     if len(old_version) == 2 and patch == 0:
         new_version = f"{major}.{minor}"
     else:
         new_version = f"{major}.{minor}.{patch}"
 
-if prepend != '':
+if prepend and (new_version[0] != 'v'):
     new_version = prepend + new_version
 
-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 p.returncode != 0:
+        print("Error: _incr_version returned nonzero exit code")
+        sys.exit(1)
 
 args = ["git", "tag"]
 if force:
@@ -148,6 +148,8 @@ else:
     args.extend(["-m", new_version, new_version])
     subprocess.run(args)
 
+print(f"Tagged {new_version}")
+
 if changelog:
     import time
     now = time.strftime("%Y-%m-%d", time.localtime())
@@ -158,3 +160,4 @@ if changelog:
         f.write(shortlog)
         f.write(data)
         f.flush()
+    print("Generated CHANGELOG")