@@ -217,45 +217,49 @@ runs:
217217 echo "'coverage' branch exists."
218218 else
219219 echo "Creating orphan 'coverage' branch..."
220- SAVE_REF="$(git symbolic-ref -q --short HEAD || true)"
221- SAVE_SHA="$(git rev-parse --verify HEAD)"
222- NEED_RESTORE=1
223- restore_ref() {
220+ if [[ -n "${RUNNER_TEMP:-}" ]]; then
221+ BOOTSTRAP_DIR="$(mktemp -d "${RUNNER_TEMP}/gitcoverage-bootstrap.XXXXXX")"
222+ else
223+ BOOTSTRAP_DIR="$(mktemp -d "${PWD}/.gitcoverage-bootstrap.XXXXXX")"
224+ fi
225+
226+ NEED_CLEANUP=1
227+ cleanup_bootstrap() {
224228 local rc=$?
225- if [[ "$NEED_RESTORE" == "1" ]]; then
226- if [[ -n "$SAVE_REF" ]]; then
227- git checkout --force "$SAVE_REF" || git checkout --force --detach "$SAVE_SHA" || true
228- else
229- git checkout --force --detach "$SAVE_SHA" || true
230- fi
231- NEED_RESTORE=0
229+ if [[ "$NEED_CLEANUP" == "1" ]]; then
230+ git worktree remove --force "$BOOTSTRAP_DIR" >/dev/null 2>&1 || true
231+ rm -rf -- "$BOOTSTRAP_DIR" || true
232+ NEED_CLEANUP=0
232233 fi
233234 return "$rc"
234235 }
235- trap restore_ref EXIT
236- git checkout --orphan coverage
237- # Remove index entries (keep working tree) in a filename-safe way.
238- while IFS= read -r -d '' path; do
239- git rm --cached -- "$path"
240- done < <(git ls-files -z)
241- echo '# Coverage branch' > README.md
242- git add README.md
243- git commit -m 'Add README.md'
244- # Push with auth via checkout action's token/remote config.
245- # If another concurrent run creates 'coverage' first, treat that as success.
246- if git push origin coverage; then
247- echo "Created 'coverage' branch."
248- else
249- echo "Initial push failed; checking whether 'coverage' was created concurrently..."
250- git fetch origin +refs/heads/coverage:refs/remotes/origin/coverage >/dev/null 2>&1 || true
251- if git ls-remote --exit-code --heads origin coverage >/dev/null 2>&1; then
252- echo "'coverage' branch was created by another run."
236+ trap cleanup_bootstrap EXIT
237+
238+ # Isolate bootstrap work from the caller's checkout to handle dirty tracked files safely.
239+ git worktree add --detach "$BOOTSTRAP_DIR" HEAD
240+ pushd "$BOOTSTRAP_DIR" >/dev/null
241+ git checkout --orphan coverage
242+ git rm -rf --cached . >/dev/null 2>&1 || true
243+ echo '# Coverage branch' > README.md
244+ git add README.md
245+ git commit -m 'Add README.md'
246+ # Push with auth via checkout action's token/remote config.
247+ # If another concurrent run creates 'coverage' first, treat that as success.
248+ if git push origin coverage; then
249+ echo "Created 'coverage' branch."
253250 else
254- echo "Failed to create 'coverage' branch." >&2
255- exit 1
251+ echo "Initial push failed; checking whether 'coverage' was created concurrently..."
252+ git fetch origin +refs/heads/coverage:refs/remotes/origin/coverage >/dev/null 2>&1 || true
253+ if git ls-remote --exit-code --heads origin coverage >/dev/null 2>&1; then
254+ echo "'coverage' branch was created by another run."
255+ else
256+ echo "Failed to create 'coverage' branch." >&2
257+ exit 1
258+ fi
256259 fi
257- fi
258- restore_ref
260+ popd >/dev/null
261+
262+ cleanup_bootstrap
259263 trap - EXIT
260264 fi
261265
0 commit comments