Browse code

report version 'unknown' with git hash as a label when there are no tags

This gives more useful information with no real disadvantage.
In the future we may consider reporting 'v0.0.0' if there are no tags.

Closes #9.

Joseph Weston authored on 06/08/2018 20:15:36
Showing 1 changed files
... ...
@@ -70,11 +70,12 @@ def get_version_from_git():
70 70
         return
71 71
 
72 72
     # git describe --first-parent does not take into account tags from branches
73
-    # that were merged-in.
73
+    # that were merged-in. The '--long' flag gets us the 'dev' version and
74
+    # git hash, '--always' returns the git hash even if there are no tags.
74 75
     for opts in [['--first-parent'], []]:
75 76
         try:
76 77
             p = subprocess.Popen(
77
-                ['git', 'describe', '--long'] + opts,
78
+                ['git', 'describe', '--long', '--always'] + opts,
78 79
                 cwd=distr_root,
79 80
                 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
80 81
         except OSError:
... ...
@@ -83,9 +84,20 @@ def get_version_from_git():
83 84
             break
84 85
     else:
85 86
         return
86
-    description = p.communicate()[0].decode().strip('v').rstrip('\n')
87 87
 
88
-    release, dev, git = description.rsplit('-', 2)
88
+    description = (p.communicate()[0]
89
+        .decode()
90
+        .strip('v')  # Tags can have a leading 'v', but the version should not
91
+        .rstrip('\n')
92
+        .rsplit('-', 2))  # Split the latest tag, commits since tag, and hash
93
+
94
+    try:
95
+        release, dev, git = description
96
+    except ValueError:  # No tags, only the git hash
97
+        git, = description
98
+        release = 'unknown'
99
+        dev = None
100
+
89 101
     labels = []
90 102
     if dev == "0":
91 103
         dev = None