Skip to content

Commit

Permalink
Update git-rev script
Browse files Browse the repository at this point in the history
  • Loading branch information
oseiler2 committed Jun 1, 2023
1 parent 29d0d60 commit e831c34
Showing 1 changed file with 42 additions and 32 deletions.
74 changes: 42 additions & 32 deletions git-rev.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,60 @@
import os
import re

# get tag/base version
try:
tag = subprocess.check_output("git describe --tags --abbrev=0", shell=True).decode().strip()
except:
tag = "?"

GITHUB_TAG = os.environ.get('GITHUB_TAG')

if GITHUB_TAG is not None:
tag = GITHUB_TAG
branch = "?"
tag = "?"

# Strip any leading v, and any trailing :name suffix.
TAG_RE = re.compile(r'^v?([^-]+)(-.*)?$')
m = TAG_RE.match(tag)
if m:
tag = m.group(1)
version = tag
GITHUB_REF = os.environ.get('GITHUB_REF')

# get current revision hash
try:
commit = subprocess.check_output("git log --pretty=format:%h -n 1", shell=True).decode().strip()
except:
commit = "?"
if GITHUB_REF is not None:
# Running in Github action
BRANCH_TAG_RE = re.compile(r'^refs\/([^\/]+)\/(.*)?$')
m = BRANCH_TAG_RE.match(GITHUB_REF)
if m.group(1) is not None:
if m.group(1) == "tags":
# tagged
tag = m.group(2)
try:
branch = subprocess.check_output("git branch -r --contains tags/" + tag, shell=True).decode().strip()
if branch.startswith("origin/"):
branch = branch[7:]
except:
branch = "unknown"
elif m.group(1) == "heads":
# branch
branch = m.group(2)
tag = "latest"

GITHUB_SHA = os.environ.get('GITHUB_SHA')
# get current revision hash
GITHUB_SHA = os.environ.get('GITHUB_SHA')
if GITHUB_SHA is not None:
commit = GITHUB_SHA

if GITHUB_SHA is not None:
commit = GITHUB_SHA
else:
# running locally

# get branch name
if GITHUB_TAG is not None:
# When running in Github actions we're in a detached head, so we need to look at the remote branches to find the one that contains the tag
# get tag/base version
try:
branch = subprocess.check_output("git branch -r --contains tags/" + GITHUB_TAG, shell=True).decode().strip()
if branch.startswith("origin/"):
branch = branch[7:]
tag = subprocess.check_output("git describe --tags --abbrev=0", shell=True).decode().strip()
except:
branch = "unknown"
else:
tag = "?"
if tag.startswith('v'):
tag = tag[1:]

# get branch name
try:
branch = subprocess.check_output("git rev-parse --abbrev-ref HEAD", shell=True).decode().strip()
except:
branch = "unknown"

# get current revision hash
try:
commit = subprocess.check_output("git log --pretty=format:%h -n 1", shell=True).decode().strip()
except:
commit = "?"

version = tag

# if not main branch append branch name
if branch != "main" and branch != "HEAD":
version += "-[" + branch + "]"
Expand Down

0 comments on commit e831c34

Please sign in to comment.