in builder/core/env.py [0:0]
def _get_git_branch():
travis_pr_branch = os.environ.get("TRAVIS_PULL_REQUEST_BRANCH")
if travis_pr_branch:
print("Found branch:", travis_pr_branch)
return travis_pr_branch
# NOTE: head_ref only set for pull_request events
# see: https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables
github_head_ref = os.environ.get("GITHUB_HEAD_REF")
github_ref = os.environ.get("GITHUB_REF")
if github_head_ref:
# if we are triggered from a PR then we are in a detached head state (e.g. `refs/pull/:prNumber/merge`)
# and we need to grab the branch being merged from
# see: https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request
branch = github_head_ref
print("Found github ref for PR from: {}".format(branch))
return branch
elif github_ref:
origin_str = "refs/heads/"
if github_ref.startswith(origin_str):
branch = github_ref[len(origin_str):]
print("Found github ref: {}".format(branch))
return branch
try:
branches = subprocess.check_output(
["git", "branch", "-a", "--contains", "HEAD"]).decode("utf-8")
star_branch = None
for branch in branches.split('\n'):
if branch and branch.startswith('*'):
star_branch = branch.strip('*').strip()
break
branches = [branch.strip('*').strip()
for branch in branches.split('\n') if branch]
print("Found branches:", branches)
# if git branch says we're on a branch, that's it
if star_branch:
print('Working in branch: {}'.format(star_branch))
return star_branch
# pick the first one (it should be the only one, if it's a fresh sync)
for branch in branches:
if branch == "(no branch)":
continue
origin_str = "remotes/origin/"
if branch.startswith(origin_str):
branch = branch[len(origin_str):]
print('Working in branch: {}'.format(branch))
return branch
except:
print("Current directory () is not a git repository".format(os.getcwd()))
# git symbolic-ref --short HEAD
return 'main'