From 67f33a0c6983091d05977a73e0c1aa3804910e6e Mon Sep 17 00:00:00 2001 From: Vassilii Khachaturov Date: Fri, 13 Sep 2013 21:43:58 +0000 Subject: [PATCH] add get_git_svn_revision and fall back to it reapply from trunk svn: r23119 --- gramps/gen/svn_revision.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/gramps/gen/svn_revision.py b/gramps/gen/svn_revision.py index 9415b6f93..52c24ed81 100644 --- a/gramps/gen/svn_revision.py +++ b/gramps/gen/svn_revision.py @@ -22,6 +22,7 @@ import sys import subprocess +import re if sys.version_info[0] < 3: cuni = unicode @@ -30,21 +31,32 @@ else: return s.decode("utf-8", errors = 'replace') cuni = to_utf8 -def get_svn_revision(path=""): +def _get_svn_revision(path, command, stdout_to_rev): stdout = "" try: - p = subprocess.Popen("svnversion -n \"%s\"" % path, shell=True, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) + p = subprocess.Popen( + "{} \"{}\"".format(command, path), + shell=True, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = p.communicate() except: return "" # subprocess failed # subprocess worked if stdout and len(stdout) > 0: # has output stdout = cuni(stdout) # get a proper string - if stdout[0].isdigit(): - return "-r" + stdout - else: - return "" + rev = stdout_to_rev(stdout) + return "-r" + rev if rev else "" else: # no output from svnversion return "" +def get_svn_revision(path=""): + return _get_svn_revision(path, "svnversion -n", + lambda stdout: stdout if stdout[0].isdigit() else "" + ) or get_git_svn_revision(path) + +def get_git_svn_revision(path=""): + def stdout_to_rev(stdout): + m = re.search("Revision:\s+(\d+)", stdout, re.MULTILINE) + return m.group(1) if m else "" + + return _get_svn_revision(path, "git svn info", stdout_to_rev)