Skip to content

Instantly share code, notes, and snippets.

@fabian57fabian
Created September 25, 2023 22:14
Show Gist options
  • Save fabian57fabian/38f5f345cdc764fdeac92d9eb447bda7 to your computer and use it in GitHub Desktop.
Save fabian57fabian/38f5f345cdc764fdeac92d9eb447bda7 to your computer and use it in GitHub Desktop.
Retrieve git rev, short and tag
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