Created
September 25, 2023 22:14
-
-
Save fabian57fabian/38f5f345cdc764fdeac92d9eb447bda7 to your computer and use it in GitHub Desktop.
Retrieve git rev, short and tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
from typing import Optional | |
def get_git_revision_hash() -> str: | |
return subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode('ascii').strip() | |
def get_git_revision_short_hash() -> str: | |
return subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('ascii').strip() | |
def get_git_tag() -> Optional[str]: | |
res = subprocess.check_output(['git', 'describe', '--tags']).decode('ascii').strip() | |
if res is None: return None | |
if len(res) == 0: return res | |
if '-' in res: return res.split('-')[0] | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment