From 8a1b2c7aa6f10882866d1c8138c146091aa443ac Mon Sep 17 00:00:00 2001
From: Daira Hopwood <david-sarah@jacaranda.org>
Date: Thu, 25 Apr 2013 02:14:50 +0100
Subject: [PATCH] Show git branch in version output. fixes #1953 Also make sure
 strings in _version.py are correctly escaped, and repair a test.

Signed-off-by: Daira Hopwood <david-sarah@jacaranda.org>
---
 setup.py                          | 21 +++++++++++++++------
 src/allmydata/__init__.py         | 20 ++++++++++++++++----
 src/allmydata/test/test_runner.py |  6 +++++-
 3 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/setup.py b/setup.py
index 0aa5cc66..d2c0b875 100644
--- a/setup.py
+++ b/setup.py
@@ -243,10 +243,11 @@ class MakeExecutable(Command):
 GIT_VERSION_BODY = '''
 # This _version.py is generated from git metadata by the tahoe setup.py.
 
-__pkgname__ = "%(pkgname)s"
-real_version = "%(version)s"
-full_version = "%(full)s"
-verstr = "%(normalized)s"
+__pkgname__ = %(pkgname)r
+real_version = %(version)r
+full_version = %(full)r
+branch = %(branch)r
+verstr = %(normalized)r
 __version__ = verstr
 '''
 
@@ -309,6 +310,7 @@ def versions_from_git(tag_prefix, verbose=False):
         normalized_version = pieces[0]
     else:
         normalized_version = "%s.post%s" % (pieces[0], pieces[1])
+
     stdout = run_command([GIT, "rev-parse", "HEAD"], cwd=source_dir)
     if stdout is None:
         return {}
@@ -316,7 +318,12 @@ def versions_from_git(tag_prefix, verbose=False):
     if version.endswith("-dirty"):
         full += "-dirty"
         normalized_version += ".dev0"
-    return {"version": version, "normalized": normalized_version, "full": full}
+
+    # Thanks to Jistanidiot at <http://stackoverflow.com/questions/6245570/get-current-branch-name>.
+    stdout = run_command([GIT, "rev-parse", "--abbrev-ref", "HEAD"], cwd=source_dir)
+    branch = (stdout or "unknown").strip()
+
+    return {"version": version, "normalized": normalized_version, "full": full, "branch": branch}
 
 # setup.cfg has an [aliases] section which runs "update_version" before many
 # commands (like "build" and "sdist") that need to know our package version
@@ -350,7 +357,9 @@ class UpdateVersion(Command):
                     { "pkgname": self.distribution.get_name(),
                       "version": versions["version"],
                       "normalized": versions["normalized"],
-                      "full": versions["full"] })
+                      "full": versions["full"],
+                      "branch": versions["branch"],
+                    })
             f.close()
             print("git-version: wrote '%s' into '%s'" % (versions["version"], fn))
         return versions.get("normalized", None)
diff --git a/src/allmydata/__init__.py b/src/allmydata/__init__.py
index f6eff199..4bafed47 100644
--- a/src/allmydata/__init__.py
+++ b/src/allmydata/__init__.py
@@ -15,9 +15,19 @@ __version__ = "unknown"
 try:
     from allmydata._version import __version__
 except ImportError:
-    # We're running in a tree that hasn't run "./setup.py darcsver", and didn't
-    # come with a _version.py, so we don't know what our version is. This should
-    # not happen very often.
+    # We're running in a tree that hasn't run update_version, and didn't
+    # come with a _version.py, so we don't know what our version is.
+    # This should not happen very often.
+    pass
+
+full_version = "unknown"
+branch = "unknown"
+try:
+    from allmydata._version import full_version, branch
+except ImportError:
+    # We're running in a tree that hasn't run update_version, and didn't
+    # come with a _version.py, so we don't know what our full version or
+    # branch is. This should not happen very often.
     pass
 
 __appname__ = "unknown"
@@ -191,7 +201,9 @@ def get_package_versions_and_locations():
                 packages.append( (pkgname, (None, None, trace_info)) )
             else:
                 comment = None
-                if pkgname == 'setuptools' and hasattr(module, '_distribute'):
+                if pkgname == __appname__:
+                    comment = "%s: %s" % (branch, full_version)
+                elif pkgname == 'setuptools' and hasattr(module, '_distribute'):
                     # distribute does not report its version in any module variables
                     comment = 'distribute'
                 packages.append( (pkgname, (get_version(module, '__version__'), package_dir(module.__file__), comment)) )
diff --git a/src/allmydata/test/test_runner.py b/src/allmydata/test/test_runner.py
index 33974e2e..b5ccaa56 100644
--- a/src/allmydata/test/test_runner.py
+++ b/src/allmydata/test/test_runner.py
@@ -160,7 +160,9 @@ class BinTahoe(common_util.SignalMixin, unittest.TestCase, RunBinTahoeMixin):
             info = repr((res, allmydata.__appname__, required_verstr, srcdir))
 
             appverpath = out.split(')')[0]
-            (appver, path) = appverpath.split(' (')
+            (appverfull, path) = appverpath.split('] (')
+            (appver, comment) = appverfull.split(' [')
+            (branch, full_version) = comment.split(': ')
             (app, ver) = appver.split(': ')
 
             self.failUnlessEqual(app, allmydata.__appname__, info)
@@ -168,6 +170,8 @@ class BinTahoe(common_util.SignalMixin, unittest.TestCase, RunBinTahoeMixin):
             norm_required = normalized_version(required_verstr)
             self.failUnlessEqual(norm_ver, norm_required, info)
             self.failUnlessEqual(path, srcdir, info)
+            self.failUnlessEqual(branch, allmydata.branch)
+            self.failUnlessEqual(full_version, allmydata.full_version)
         d.addCallback(_cb)
         return d
 
-- 
2.45.2