19
19
from __future__ import absolute_import , print_function
20
20
21
21
import re
22
+ import os
22
23
from os .path import dirname , isdir , join , exists
23
24
from subprocess import CalledProcessError , check_output
24
25
@@ -43,23 +44,33 @@ def get_version():
43
44
# (eg. "gaip-0.0.0-651-gcf335a9-dirty")
44
45
cmd = [
45
46
'git' ,
46
- '--git-dir' , git_dir ,
47
47
'describe' , '--tags' , '--match' , '[0-9]*' , '--dirty'
48
48
]
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 )
60
63
61
64
return version
62
65
63
66
67
+ def remember_cwd ():
68
+ current_dir = os .getcwd ()
69
+ try :
70
+ yield
71
+ finally :
72
+ os .chdir (current_dir )
73
+
74
+
64
75
if __name__ == '__main__' :
65
76
print (get_version ())
0 commit comments