1919from __future__ import absolute_import , print_function
2020
2121import re
22+ import os
2223from os .path import dirname , isdir , join , exists
2324from subprocess import CalledProcessError , check_output
2425
@@ -43,23 +44,33 @@ def get_version():
4344 # (eg. "gaip-0.0.0-651-gcf335a9-dirty")
4445 cmd = [
4546 'git' ,
46- '--git-dir' , git_dir ,
4747 'describe' , '--tags' , '--match' , '[0-9]*' , '--dirty'
4848 ]
49- try :
50- git_version = check_output (cmd ).decode ().strip ()
51- except CalledProcessError :
52- raise RuntimeError ('Unable to get version number from git tags' )
53- components = git_version .split ('-' )
54- version = components .pop (0 )
55-
56- # Any other suffixes imply this is not a release: Append an internal build number
57- if components :
58- # <commit count>.<git hash>.<whether the working tree is dirty>
59- version += '+' + '.' .join (components )
49+ with remember_cwd ():
50+ os .chdir (package_dir )
51+ try :
52+ git_version = check_output (cmd ).decode ().strip ()
53+ except CalledProcessError :
54+ raise RuntimeError ('Unable to get version number from git tags' )
55+
56+ components = git_version .split ('-' )
57+ version = components .pop (0 )
58+
59+ # Any other suffixes imply this is not a release: Append an internal build number
60+ if components :
61+ # <commit count>.<git hash>.<whether the working tree is dirty>
62+ version += '+' + '.' .join (components )
6063
6164 return version
6265
6366
67+ def remember_cwd ():
68+ current_dir = os .getcwd ()
69+ try :
70+ yield
71+ finally :
72+ os .chdir (current_dir )
73+
74+
6475if __name__ == '__main__' :
6576 print (get_version ())
0 commit comments