Returns abbreviated commit hash number as retrieved with "git rev-parse --short HEAD" >>> len(getRevisionNumber() or (' ' * 7)) == 7 True
()
| 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 | _ = os.path.dirname(__file__) |
| 26 | |
| 27 | while True: |
| 28 | filePath = os.path.join(_, ".git", "HEAD") |
| 29 | if os.path.exists(filePath): |
| 30 | break |
| 31 | else: |
| 32 | filePath = None |
| 33 | if _ == os.path.dirname(_): |
| 34 | break |
| 35 | else: |
| 36 | _ = os.path.dirname(_) |
| 37 | |
| 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 |
| 43 | |
| 44 | if content.startswith("ref: "): |
| 45 | try: |
| 46 | filePath = os.path.join(_, ".git", content.replace("ref: ", "")).strip() |
| 47 | except UnicodeError: |
| 48 | pass |
| 49 | |
| 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 |
| 56 | |
| 57 | if not retVal: |
| 58 | try: |
| 59 | process = subprocess.Popen("git rev-parse --verify HEAD", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 60 | stdout, _ = process.communicate() |
| 61 | match = re.search(r"(?i)[0-9a-f]{32}", getText(stdout or "")) |
| 62 | retVal = match.group(0) if match else None |
| 63 | except: |
| 64 | pass |
| 65 | |
| 66 | return retVal[:7] if retVal else None |