@@ -22,43 +22,39 @@ def getRevisionNumber():
2222
2323 retVal = None
2424 filePath = None
25- _ = os .path .dirname (__file__ )
25+ directory = os .path .dirname (__file__ )
2626
2727 while True :
28- filePath = os .path .join (_ , ".git" , "HEAD" )
29- if os .path .exists (filePath ):
28+ candidate = os .path .join (directory , ".git" , "HEAD" )
29+ if os .path .exists (candidate ):
30+ filePath = candidate
3031 break
31- else :
32- filePath = None
33- if _ == os .path .dirname (_ ):
34- break
35- else :
36- _ = os .path .dirname (_ )
3732
38- while True :
39- if filePath and os .path .isfile (filePath ):
40- with openFile (filePath , "r" ) as f :
41- content = getText (f .read ())
42- filePath = None
33+ parent = os .path .dirname (directory )
34+ if parent == directory :
35+ break
36+ directory = parent
4337
44- if content .startswith ("ref: " ):
45- try :
46- filePath = os .path .join (_ , ".git" , content .replace ("ref: " , "" )).strip ()
47- except UnicodeError :
48- pass
38+ if filePath :
39+ with openFile (filePath , "r" ) as f :
40+ content = getText (f .read ()).strip ()
4941
50- if filePath is None :
51- match = re .match (r"(?i)[0-9a-f]{32}" , content )
52- retVal = match .group (0 ) if match else None
53- break
54- else :
55- break
42+ if content .startswith ("ref: " ):
43+ ref_path = content .replace ("ref: " , "" ).strip ()
44+ filePath = os .path .join (directory , ".git" , ref_path )
45+
46+ if os .path .exists (filePath ):
47+ with openFile (filePath , "r" ) as f_ref :
48+ content = getText (f_ref .read ()).strip ()
49+
50+ match = re .match (r"(?i)[0-9a-f]{40}" , content )
51+ retVal = match .group (0 ) if match else None
5652
5753 if not retVal :
5854 try :
59- process = subprocess .Popen ("git rev-parse --verify HEAD " , shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
55+ process = subprocess .Popen ([ "git" , " rev-parse" , " --verify" , "HEAD" ] , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
6056 stdout , _ = process .communicate ()
61- match = re .search (r"(?i)[0-9a-f]{32 }" , getText (stdout or "" ))
57+ match = re .search (r"(?i)[0-9a-f]{40 }" , getText (stdout or "" ))
6258 retVal = match .group (0 ) if match else None
6359 except :
6460 pass
0 commit comments