Skip to content

Commit f86ef95

Browse files
committed
fix(action): use explicit refs/heads for git ls-remote and fetch
Replace implicit branch name arguments with explicit `refs/heads/` prefixes in `git ls-remote` and `git fetch` commands to ensure consistent behavior across different Git versions and remote configurations. Also move `detect_and_store_repo_depth_mode` call to after workspace initialization and add test cases for partial branch name matching.
1 parent 7889a38 commit f86ef95

3 files changed

Lines changed: 28 additions & 6 deletions

File tree

.github/workflows/self-test.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ jobs:
6262
git branch 'feat=compat'
6363
git push origin 'feat=compat'
6464
65+
# Branches used to verify exact remote branch checks.
66+
git branch 'foo/coverage'
67+
git push origin 'foo/coverage'
68+
git branch 'only/partial'
69+
git push origin 'only/partial'
70+
6571
# Detached/tag-only commit (not contained by any remote branch)
6672
git checkout --detach
6773
echo "detached-only" > detached-only.txt
@@ -102,6 +108,20 @@ jobs:
102108
test "${{ steps.windows_branch.outcome }}" = "success"
103109
fi
104110
111+
- name: Run action with partial branch-name match (should fail)
112+
id: partial_branch
113+
continue-on-error: true
114+
uses: ./action-src
115+
with:
116+
coverage: "80%"
117+
branch: "partial"
118+
119+
- name: Verify partial branch-name match failed
120+
shell: bash
121+
run: |
122+
set -euo pipefail
123+
test "${{ steps.partial_branch.outcome }}" = "failure"
124+
105125
- name: Verify explicit branch run
106126
shell: bash
107127
run: |

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ If you submitted a detailed HTML report of the coverage to the action, replace t
3535
- `branch` (optional): Source branch override. Recommended for tag-triggered workflows where multiple branches may contain the same tag commit.
3636
Also recommended for very large or restricted repos to avoid scanning all remote branches during tag-triggered branch resolution.
3737
On Windows runners, the action applies a strict compatibility filter and requires branch names to match `[A-Za-z0-9._/+-]+`.
38+
This filter does not reject Windows-reserved path components such as `CON`, `NUL`, `AUX`, `COM1`, or `LPT9`; avoid those names on Windows runners.
3839

3940
## Examples
4041

action.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ runs:
5656
echo "GITCOVERAGE_IS_SHALLOW_REPOSITORY=$shallow" >> "$GITHUB_ENV"
5757
}
5858
59-
detect_and_store_repo_depth_mode
60-
6159
WORKSPACE_PATH="${GITHUB_WORKSPACE:-$PWD}"
6260
WORKSPACE_PATH="$(to_posix_path "$WORKSPACE_PATH")"
6361
# Trust only this workspace. If the default global config is not writable,
@@ -77,6 +75,9 @@ runs:
7775
exit 1
7876
fi
7977
fi
78+
79+
detect_and_store_repo_depth_mode
80+
8081
git config --local user.email "action@github.com"
8182
git config --local user.name "GitHub Action"
8283
@@ -158,7 +159,7 @@ runs:
158159
else
159160
git fetch --prune --tags origin
160161
fi
161-
if ! git fetch origin +refs/heads/*:refs/remotes/origin/* --prune; then
162+
if ! git fetch origin '+refs/heads/*:refs/remotes/origin/*' --prune; then
162163
echo "Failed to fetch remote branches needed for tag-triggered branch resolution." >&2
163164
echo "Set input 'branch' to the intended source branch." >&2
164165
exit 1
@@ -181,7 +182,7 @@ runs:
181182
echo "Invalid explicit branch input: '$BRANCH'" >&2
182183
exit 1
183184
fi
184-
if ! git ls-remote --exit-code --heads origin "$BRANCH" >/dev/null 2>&1; then
185+
if ! git ls-remote --exit-code origin "refs/heads/${BRANCH}" >/dev/null 2>&1; then
185186
echo "Explicit branch '$BRANCH' was not found on origin." >&2
186187
exit 1
187188
fi
@@ -299,7 +300,7 @@ runs:
299300
fi
300301
}
301302
# Does 'coverage' branch exist on origin?
302-
if git ls-remote --exit-code --heads origin coverage >/dev/null 2>&1; then
303+
if git ls-remote --exit-code origin refs/heads/coverage >/dev/null 2>&1; then
303304
echo "'coverage' branch exists."
304305
else
305306
echo "Creating orphan 'coverage' branch..."
@@ -339,7 +340,7 @@ runs:
339340
else
340341
echo "Initial push failed; checking whether 'coverage' was created concurrently..."
341342
git -C "$GITCOVERAGE_REPO_ROOT" fetch origin +refs/heads/coverage:refs/remotes/origin/coverage >/dev/null 2>&1 || true
342-
if git -C "$GITCOVERAGE_REPO_ROOT" ls-remote --exit-code --heads origin coverage >/dev/null 2>&1; then
343+
if git -C "$GITCOVERAGE_REPO_ROOT" ls-remote --exit-code origin refs/heads/coverage >/dev/null 2>&1; then
343344
echo "'coverage' branch was created by another run."
344345
else
345346
echo "Failed to create 'coverage' branch." >&2

0 commit comments

Comments
 (0)