git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[StGit PATCH 2/2] Better StGit version tracking

From
Karl Hasselström <kha@treskal.com>
Date
May 14, 2008, 01:47 UTC
Message-ID
<20080514014502.7043.44236.stgit@yoghurt>
In-Reply-To
<20080514014309.GA17955@diana.vm.bytemark.co.uk>

Instead of claiming to be the latest released version (really, a hardcoded string that we hope is the latest released version), run git describe to figure out what version we are, just like git does. Fall back to a hardcoded value that is generated at install time, or supplied in a release tarball.

Currently, we have to give git describe the --tags flag, since StGit release tags are lightweight tags. This means we're going to pick up any lightweight tags the user makes, which isn't ideal. The solution is to start making annotated release tags, and then remove that flag.

Signed-off-by: Karl Hasselström <kha@treskal.com>
---

Catalin, you might want to pay extra attention the first time you release something with this in it. Making sure that stgit/builtin_version.py is included in the tarball, for example.

 setup.py         |    2 ++
 stgit/.gitignore |    1 +
 stgit/version.py |   52 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 54 insertions(+), 1 deletions(-)
diff --git a/setup.py b/setup.py
index 04ca821..40022a7 100755
--- a/setup.py
+++ b/setup.py
@@ -48,6 +48,8 @@ if sys.argv[1] in ['install', 'build']:
     __check_python_version()
     __check_git_version()
 
+version.write_builtin_version()
+
 # ensure readable template files
 old_mask = os.umask(0022)
 
diff --git a/stgit/.gitignore b/stgit/.gitignore
index 0d20b64..4f9c8f1 100644
--- a/stgit/.gitignore
+++ b/stgit/.gitignore
@@ -1 +1,2 @@
 *.pyc
+/builtin_version.py
diff --git a/stgit/version.py b/stgit/version.py
index 06ac723..8ee5009 100644
--- a/stgit/version.py
+++ b/stgit/version.py
@@ -1,4 +1,54 @@
-version = '0.14.2'
+from stgit.exception import StgException
+from stgit import run, utils
+import os.path, re, sys
+
+class VersionUnavailable(StgException):
+    pass
+
+def git_describe_version():
+    path = sys.path[0]
+    try:
+        v = run.Run('git', 'describe', '--tags', '--abbrev=4'
+                    ).cwd(path).output_one_line()
+    except run.RunException, e:
+        raise VersionUnavailable(str(e))
+    if not re.match(r'^v[0-9]', v):
+        raise VersionUnavailable('%s: bad version' % v)
+    try:
+        dirty = run.Run('git', 'diff-index', '--name-only', 'HEAD'
+                        ).cwd(path).raw_output()
+    except run.RunException, e:
+        raise VersionUnavailable(str(e))
+    if dirty:
+        v += '-dirty'
+    return re.sub('-', '.', utils.strip_prefix('v', v))
+
+def builtin_version():
+    try:
+        import builtin_version as bv
+    except ImportError:
+        raise VersionUnavailable()
+    else:
+        return bv.version
+
+def write_builtin_version():
+    try:
+        v = git_describe_version()
+    except VersionUnavailable:
+        return
+    f = file(os.path.join(sys.path[0], 'stgit', 'builtin_version.py'), 'w')
+    f.write('# This file was generated automatically. Do not edit by hand.\n'
+            'version = %r\n' % v)
+
+def get_version():
+    for v in [git_describe_version, builtin_version]:
+        try:
+            return v()
+        except VersionUnavailable:
+            pass
+    return 'unknown-version'
+
+version = get_version()
 
 # minimum version requirements
 git_min_ver = '1.5.2'
Previous: Karl HasselströmNext: Karl Hasselström
Message 3 of 16 in “StGit: kha/{safe,experimental} updated”
  1. Karl HasselströmMay 14, 2008
  2. 1/2 Import version to a separate namespaceKarl Hasselström, May 14, 2008
  3. 2/2 Better StGit version trackingKarl Hasselström, May 14, 2008
  4. Emacs mode: automatically cd up to root of worktreeKarl Hasselström, May 14, 2008
  5. David KågedalMay 14, 2008
  6. Karl HasselströmMay 14, 2008
  7. New command: stg redoKarl Hasselström, May 14, 2008
  8. Catalin MarinasMay 19, 2008
  9. Karl HasselströmMay 20, 2008
  10. Catalin MarinasMay 20, 2008
  11. Karl HasselströmMay 20, 2008
  12. Try the built-in version string before git-describeKarl Hasselström, May 20, 2008
  13. Catalin MarinasMay 21, 2008
  14. Karl HasselströmMay 21, 2008
  15. Catalin MarinasMay 21, 2008
  16. Karl HasselströmMay 21, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.