Skip to content

Commit f7d3948

Browse files
committed
improve branch detection
1 parent 6158b1d commit f7d3948

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

action.yml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,42 @@ runs:
4444
shell: bash
4545
env:
4646
INPUT_BRANCH: ${{ inputs.branch }}
47+
GITHUB_DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
4748
run: |
4849
set -euo pipefail
4950
5051
resolve_default_branch() {
51-
git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null | sed 's@^origin/@@' || true
52+
local d=""
53+
d="$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null | sed 's@^origin/@@' || true)"
54+
if [[ -n "$d" ]] && is_valid_branch_name "$d"; then
55+
echo "$d"
56+
return 0
57+
fi
58+
59+
# If local origin/HEAD is absent or stale, query remote HEAD directly.
60+
d="$(git ls-remote --symref origin HEAD 2>/dev/null | awk '/^ref:/ { sub("^refs/heads/", "", $2); print $2; exit }' || true)"
61+
if [[ -n "$d" ]] && is_valid_branch_name "$d"; then
62+
echo "$d"
63+
return 0
64+
fi
65+
66+
# Use GitHub event metadata when available.
67+
d="${GITHUB_DEFAULT_BRANCH:-}"
68+
if [[ -n "$d" ]] && is_valid_branch_name "$d"; then
69+
echo "$d"
70+
return 0
71+
fi
72+
73+
# Last-resort heuristics for common repo defaults.
74+
if git show-ref --verify --quiet refs/remotes/origin/main; then
75+
echo "main"
76+
return 0
77+
fi
78+
if git show-ref --verify --quiet refs/remotes/origin/master; then
79+
echo "master"
80+
return 0
81+
fi
82+
echo ""
5283
}
5384
is_detached_marker() {
5485
local b="$1"

0 commit comments

Comments
 (0)