Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/lib/core/revision.py
3554 views
1
#!/usr/bin/env python
2
3
"""
4
Copyright (c) 2006-2026 sqlmap developers (https://sqlmap.org)
5
See the file 'LICENSE' for copying permission
6
"""
7
8
import os
9
import re
10
import subprocess
11
12
from lib.core.common import openFile
13
from lib.core.convert import getText
14
15
def getRevisionNumber():
16
"""
17
Returns abbreviated commit hash number as retrieved with "git rev-parse --short HEAD"
18
19
>>> len(getRevisionNumber() or (' ' * 7)) == 7
20
True
21
"""
22
23
retVal = None
24
filePath = None
25
directory = os.path.dirname(__file__)
26
27
while True:
28
candidate = os.path.join(directory, ".git", "HEAD")
29
if os.path.exists(candidate):
30
filePath = candidate
31
break
32
33
parent = os.path.dirname(directory)
34
if parent == directory:
35
break
36
directory = parent
37
38
if filePath:
39
with openFile(filePath, "r") as f:
40
content = getText(f.read()).strip()
41
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
52
53
if not retVal:
54
try:
55
process = subprocess.Popen(["git", "rev-parse", "--verify", "HEAD"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
56
stdout, _ = process.communicate()
57
match = re.search(r"(?i)[0-9a-f]{40}", getText(stdout or ""))
58
retVal = match.group(0) if match else None
59
except:
60
pass
61
62
return retVal[:7] if retVal else None
63
64