sqlmap/lib/core/revision.py

67 lines
1.8 KiB
Python
Raw Normal View History

2019-05-08 13:47:52 +03:00
#!/usr/bin/env python
"""
2019-01-05 23:38:52 +03:00
Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
"""
import os
import re
2016-12-20 01:47:39 +03:00
import subprocess
2019-05-13 12:51:47 +03:00
from lib.core.common import openFile
2019-05-27 14:09:13 +03:00
from lib.core.convert import getText
2019-05-13 12:08:25 +03:00
def getRevisionNumber():
2012-07-03 02:50:23 +04:00
"""
2012-07-03 15:06:52 +04:00
Returns abbreviated commit hash number as retrieved with "git rev-parse --short HEAD"
2019-05-06 15:41:35 +03:00
>>> len(getRevisionNumber() or (' ' * 7)) == 7
True
2012-07-03 02:50:23 +04:00
"""
retVal = None
filePath = None
_ = os.path.dirname(__file__)
2012-07-26 02:08:49 +04:00
while True:
2012-07-26 02:02:38 +04:00
filePath = os.path.join(_, ".git", "HEAD")
if os.path.exists(filePath):
break
else:
filePath = None
if _ == os.path.dirname(_):
break
else:
_ = os.path.dirname(_)
2012-07-26 02:08:49 +04:00
2012-07-26 02:02:38 +04:00
while True:
if filePath and os.path.isfile(filePath):
2019-05-13 12:51:47 +03:00
with openFile(filePath, "r") as f:
2019-07-07 16:56:54 +03:00
content = getText(f.read())
2012-07-26 02:02:38 +04:00
filePath = None
2019-10-28 14:30:54 +03:00
2012-07-26 02:02:38 +04:00
if content.startswith("ref: "):
2019-10-28 14:30:54 +03:00
try:
filePath = os.path.join(_, ".git", content.replace("ref: ", "")).strip()
except UnicodeError:
pass
if filePath is None:
2012-07-26 02:02:38 +04:00
match = re.match(r"(?i)[0-9a-f]{32}", content)
retVal = match.group(0) if match else None
break
2012-07-30 14:09:20 +04:00
else:
break
if not retVal:
2019-03-26 14:52:19 +03:00
try:
process = subprocess.Popen("git rev-parse --verify HEAD", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, _ = process.communicate()
2019-05-13 12:08:25 +03:00
match = re.search(r"(?i)[0-9a-f]{32}", getText(stdout or ""))
2019-03-26 14:52:19 +03:00
retVal = match.group(0) if match else None
except:
pass
2012-07-03 15:06:52 +04:00
return retVal[:7] if retVal else None