sqlmap/lib/core/revision.py

55 lines
1.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python
"""
2012-07-12 21:38:03 +04:00
Copyright (c) 2006-2012 sqlmap developers (http://sqlmap.org/)
See the file 'doc/COPYING' for copying permission
"""
import os
import re
from subprocess import PIPE
from subprocess import Popen as execute
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"
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):
with open(filePath, "r") as f:
content = f.read()
filePath = None
if content.startswith("ref: "):
filePath = os.path.join(_, ".git", content.replace("ref: ", "")).strip()
else:
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:
process = execute("git rev-parse --verify HEAD", shell=True, stdout=PIPE, stderr=PIPE)
stdout, _ = process.communicate()
match = re.search(r"(?i)[0-9a-f]{32}", stdout or "")
retVal = match.group(0) if match else None
2012-07-03 15:06:52 +04:00
return retVal[:7] if retVal else None