Skip to content

Commit 5a83561

Browse files
committed
limit branch names on windows, check required git feature early
1 parent 10a1caf commit 5a83561

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
lines changed

.github/workflows/self-test.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ jobs:
5757
git tag v-test
5858
git push origin v-test
5959
60+
# Branch used to verify Windows-only branch-name filtering.
61+
# '=' is Git-valid but rejected by our Windows compatibility regex.
62+
git branch 'feat=compat'
63+
git push origin 'feat=compat'
64+
6065
# Detached/tag-only commit (not contained by any remote branch)
6166
git checkout --detach
6267
echo "detached-only" > detached-only.txt
@@ -79,6 +84,24 @@ jobs:
7984
branch: main
8085
report: reports/report.html
8186

87+
- name: Run action with Windows-restricted branch charset
88+
id: windows_branch
89+
continue-on-error: true
90+
uses: ./action-src
91+
with:
92+
coverage: "80%"
93+
branch: "feat=compat"
94+
95+
- name: Verify Windows-only branch filter
96+
shell: bash
97+
run: |
98+
set -euo pipefail
99+
if [[ "${RUNNER_OS}" == "Windows" ]]; then
100+
test "${{ steps.windows_branch.outcome }}" = "failure"
101+
else
102+
test "${{ steps.windows_branch.outcome }}" = "success"
103+
fi
104+
82105
- name: Verify explicit branch run
83106
shell: bash
84107
run: |

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This action has no dependencies except for `git`, a `bash` shell and common *nix
1010
It supports Linux/macOS runners and Windows runners with Bash tooling (Git Bash/WSL-enabled images such as
1111
`windows-2025`).
1212

13-
Requires **Git 2.15.0 or newer**.
13+
Requires **Git 2.15.0 or newer** (the action fails fast on older versions).
1414

1515
## Usage
1616

@@ -34,7 +34,7 @@ If you submitted a detailed HTML report of the coverage to the action, replace t
3434
- `report` (optional): Path to an HTML report file to publish as `report.html`.
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.
37-
A Git-valid branch name may still be an invalid Windows path component (for example names containing `<`, `>`, `|`, or `"`), which can fail on Windows runners because this action writes files under `<branch>/...`.
37+
On Windows runners, the action applies a strict compatibility filter and requires branch names to match `[A-Za-z0-9._/+-]+`.
3838

3939
## Examples
4040

action.yml

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ runs:
4343
printf '%s\n' "${p//\\//}"
4444
fi
4545
}
46+
detect_and_store_repo_depth_mode() {
47+
local shallow
48+
if ! shallow="$(git rev-parse --is-shallow-repository 2>/dev/null)"; then
49+
echo "gitcoverage requires Git 2.15.0 or newer (missing 'git rev-parse --is-shallow-repository')." >&2
50+
exit 1
51+
fi
52+
if [[ "$shallow" != "true" && "$shallow" != "false" ]]; then
53+
echo "Unexpected result from 'git rev-parse --is-shallow-repository': '$shallow'" >&2
54+
exit 1
55+
fi
56+
echo "GITCOVERAGE_IS_SHALLOW_REPOSITORY=$shallow" >> "$GITHUB_ENV"
57+
}
58+
59+
detect_and_store_repo_depth_mode
4660
4761
WORKSPACE_PATH="${GITHUB_WORKSPACE:-$PWD}"
4862
WORKSPACE_PATH="$(to_posix_path "$WORKSPACE_PATH")"
@@ -125,9 +139,20 @@ runs:
125139
local b="$1"
126140
git check-ref-format --branch "$b" >/dev/null 2>&1
127141
}
142+
validate_windows_safe_branch_path_or_fail() {
143+
local b="$1"
144+
# Only enforce this compatibility check on Windows runners.
145+
if [[ "${RUNNER_OS:-}" == "Windows" ]]; then
146+
# Keep this intentionally strict and simple for filesystem safety.
147+
if ! [[ "$b" =~ ^[A-Za-z0-9._/+-]+$ ]]; then
148+
echo "On Windows runners, branch '$b' must match: [A-Za-z0-9._/+-]+" >&2
149+
return 1
150+
fi
151+
fi
152+
}
128153
ensure_tag_resolution_refs() {
129154
# Only tag-triggered auto-mapping needs full remote-branch refs.
130-
if git rev-parse --is-shallow-repository >/dev/null 2>&1 && [[ "$(git rev-parse --is-shallow-repository)" == "true" ]]; then
155+
if [[ "$GITCOVERAGE_IS_SHALLOW_REPOSITORY" == "true" ]]; then
131156
# Fully unshallow if possible so --contains works reliably.
132157
git fetch --prune --tags --unshallow origin || git fetch --prune --tags --depth=1000 origin
133158
else
@@ -161,19 +186,19 @@ runs:
161186
exit 1
162187
fi
163188
echo "Using explicit branch input: $BRANCH"
164-
echo "branch=$BRANCH" >> "$GITHUB_OUTPUT"
165-
exit 0
166189
fi
167190
168191
# --- Fast paths: branch refs; never let PR head-ref override tag context ---
169-
if [[ "$REF_TYPE" == "tag" || "$REF_FULL" == refs/tags/* ]]; then
170-
BRANCH=""
171-
elif [[ -n "$HEAD_REF" ]]; then
172-
BRANCH="${HEAD_REF}"
173-
elif [[ "$REF_TYPE" == "branch" && -n "$REF_NAME" ]]; then
174-
BRANCH="${REF_NAME}"
175-
else
176-
BRANCH=""
192+
if [[ -z "$BRANCH" ]]; then
193+
if [[ "$REF_TYPE" == "tag" || "$REF_FULL" == refs/tags/* ]]; then
194+
BRANCH=""
195+
elif [[ -n "$HEAD_REF" ]]; then
196+
BRANCH="${HEAD_REF}"
197+
elif [[ "$REF_TYPE" == "branch" && -n "$REF_NAME" ]]; then
198+
BRANCH="${REF_NAME}"
199+
else
200+
BRANCH=""
201+
fi
177202
fi
178203
179204
# --- Tag path: map tag -> containing branch ---
@@ -254,6 +279,9 @@ runs:
254279
echo "Could not determine branch (still detached at a tag). Consider actions/checkout with 'fetch-depth: 0'." >&2
255280
exit 1
256281
fi
282+
if ! validate_windows_safe_branch_path_or_fail "$BRANCH"; then
283+
exit 1
284+
fi
257285
258286
echo "Resolved branch: $BRANCH"
259287
echo "branch=$BRANCH" >> "$GITHUB_OUTPUT"

0 commit comments

Comments
 (0)