Skip to content

Commit bf8288f

Browse files
authored
fix(ci): share Git ownership across workflow fetch policies (#132428)
* fix(ci): stop Git descendants before base and ref checks Related: #132183. Builds on canonical Git cleanup prerequisite #132169. This covers the shared owner package, base checks, and six preflight ref lookups; remaining workflow consumers are deferred. * fix(ci): keep Git owner budgets fixed and cover native runners * fix(ci): cover shared checkout helper on native runners * test(ci): consolidate checkout fixture clocks * fix(agents): own apply_patch filesystem initialization * test(agents): track apply_patch temp roots
1 parent 2df7c25 commit bf8288f

26 files changed

Lines changed: 1869 additions & 539 deletions

‎.github/actions/ensure-base-commit/action.yml‎

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,65 +13,14 @@ runs:
1313
- name: Ensure base commit is available
1414
shell: bash
1515
env:
16+
BASE_ACTION_PATH: ${{ github.action_path }}
1617
BASE_SHA: ${{ inputs.base-sha }}
1718
FETCH_REF: ${{ inputs.fetch-ref }}
1819
run: |
1920
set -euo pipefail
20-
21-
if [ -z "$BASE_SHA" ] || [[ "$BASE_SHA" =~ ^0+$ ]]; then
22-
echo "No concrete base SHA available; skipping targeted fetch."
23-
exit 0
21+
python_command=python3
22+
if [ "$RUNNER_OS" = "Windows" ]; then
23+
python_command=python
2424
fi
25-
26-
if ! [[ "$BASE_SHA" =~ ^[0-9a-fA-F]{7,40}$ ]]; then
27-
echo "::error title=ensure-base-commit invalid base sha::Refusing invalid base SHA: $BASE_SHA"
28-
exit 2
29-
fi
30-
31-
if ! git check-ref-format --branch "$FETCH_REF" >/dev/null 2>&1; then
32-
echo "::error title=ensure-base-commit invalid fetch ref::Refusing invalid fetch ref: $FETCH_REF"
33-
exit 2
34-
fi
35-
36-
if git rev-parse --verify "$BASE_SHA^{commit}" >/dev/null 2>&1; then
37-
echo "Base commit already present: $BASE_SHA"
38-
exit 0
39-
fi
40-
41-
fetch_base_ref() {
42-
timeout --signal=TERM --kill-after=10s 30s git \
43-
-c protocol.version=2 \
44-
fetch "$@"
45-
}
46-
47-
echo "Base commit missing; fetching exact SHA before branch history."
48-
if ! fetch_base_ref --no-tags --depth=1 origin "$BASE_SHA"; then
49-
echo "::warning title=ensure-base-commit exact fetch failed::Failed to fetch exact base SHA $BASE_SHA"
50-
fi
51-
if git rev-parse --verify "$BASE_SHA^{commit}" >/dev/null 2>&1; then
52-
echo "Resolved base commit after exact fetch: $BASE_SHA"
53-
exit 0
54-
fi
55-
56-
for deepen_by in 25 100 300; do
57-
echo "Base commit missing; deepening $FETCH_REF by $deepen_by."
58-
if ! fetch_base_ref --no-tags --deepen="$deepen_by" origin -- "$FETCH_REF"; then
59-
echo "::warning title=ensure-base-commit fetch failed::Failed to deepen $FETCH_REF by $deepen_by while looking for $BASE_SHA"
60-
fi
61-
if git rev-parse --verify "$BASE_SHA^{commit}" >/dev/null 2>&1; then
62-
echo "Resolved base commit after deepening: $BASE_SHA"
63-
exit 0
64-
fi
65-
done
66-
67-
echo "Base commit still missing; fetching full history for $FETCH_REF."
68-
if ! fetch_base_ref --no-tags origin -- "$FETCH_REF"; then
69-
echo "::warning title=ensure-base-commit fetch failed::Failed to fetch full history for $FETCH_REF while looking for $BASE_SHA"
70-
fi
71-
if git rev-parse --verify "$BASE_SHA^{commit}" >/dev/null 2>&1; then
72-
echo "Resolved base commit after full ref fetch: $BASE_SHA"
73-
exit 0
74-
fi
75-
76-
echo "::error title=ensure-base-commit missing base::Base commit still unavailable after fetch attempts: $BASE_SHA"
77-
exit 1
25+
exec "$python_command" -I -S "$BASE_ACTION_PATH/../git-owner/owner.py" \
26+
--policy "$BASE_ACTION_PATH/policy.py"
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
import re
3+
import subprocess
4+
5+
from ci_git_owner import FetchTimeout, GitFailure, run_git
6+
7+
8+
def quiet_git(*arguments):
9+
try:
10+
run_git(os.getcwd(), *arguments, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
11+
return True
12+
except GitFailure:
13+
return False
14+
15+
16+
def ensure_base():
17+
base = os.environ["BASE_SHA"]
18+
ref = os.environ["FETCH_REF"]
19+
if not base or re.fullmatch("0+", base):
20+
print("No concrete base SHA available; skipping targeted fetch.")
21+
return 0
22+
if not re.fullmatch("[0-9a-fA-F]{7,40}", base):
23+
print(f"::error title=ensure-base-commit invalid base sha::Refusing invalid base SHA: {base}")
24+
return 2
25+
if not quiet_git("check-ref-format", "--branch", ref):
26+
print(f"::error title=ensure-base-commit invalid fetch ref::Refusing invalid fetch ref: {ref}")
27+
return 2
28+
if quiet_git("rev-parse", "--verify", f"{base}^{{commit}}"):
29+
print(f"Base commit already present: {base}")
30+
return 0
31+
32+
attempts = [
33+
(
34+
["--no-tags", "--depth=1", "origin", base],
35+
"Base commit missing; fetching exact SHA before branch history.",
36+
f"::warning title=ensure-base-commit exact fetch failed::Failed to fetch exact base SHA {base}",
37+
"exact fetch",
38+
),
39+
*[(
40+
["--no-tags", f"--deepen={depth}", "origin", "--", ref],
41+
f"Base commit missing; deepening {ref} by {depth}.",
42+
f"::warning title=ensure-base-commit fetch failed::Failed to deepen {ref} by {depth} while looking for {base}",
43+
"deepening",
44+
) for depth in (25, 100, 300)],
45+
(
46+
["--no-tags", "origin", "--", ref],
47+
f"Base commit still missing; fetching ref {ref}.",
48+
f"::warning title=ensure-base-commit fetch failed::Failed to fetch ref {ref} while looking for {base}",
49+
"full ref fetch",
50+
),
51+
]
52+
for arguments, message, warning, resolution in attempts:
53+
print(message, flush=True)
54+
try:
55+
run_git(os.getcwd(), "-c", "protocol.version=2", "fetch", *arguments, timeout=30)
56+
except (GitFailure, FetchTimeout):
57+
# A failed fetch can still supply the base. Lifecycle failures and
58+
# cancellation escape this policy before any availability probe.
59+
print(warning)
60+
if quiet_git("rev-parse", "--verify", f"{base}^{{commit}}"):
61+
print(f"Resolved base commit after {resolution}: {base}")
62+
return 0
63+
print(f"::error title=ensure-base-commit missing base::Base commit still unavailable after fetch attempts: {base}")
64+
return 1
65+
66+
67+
raise SystemExit(ensure_base())
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Prepare Git owner
2+
description: Materialize the action's trusted Git lifecycle owner outside the checkout.
3+
outputs:
4+
owner-path:
5+
description: Absolute path to the job's isolated Git owner copy.
6+
value: ${{ steps.prepare.outputs.owner-path }}
7+
runs:
8+
using: composite
9+
steps:
10+
- id: prepare
11+
shell: bash
12+
env:
13+
OWNER_ACTION_PATH: ${{ github.action_path }}
14+
run: |
15+
set -euo pipefail
16+
python_command=python3
17+
if [ "$RUNNER_OS" = "Windows" ]; then
18+
python_command=python
19+
fi
20+
"$python_command" -I -S - <<'PYTHON'
21+
import os
22+
from pathlib import Path
23+
import shutil
24+
import tempfile
25+
26+
directory = Path(tempfile.mkdtemp(prefix="ci-git-owner-", dir=os.environ["RUNNER_TEMP"]))
27+
owner = directory / "owner.py"
28+
shutil.copyfile(Path(os.environ["OWNER_ACTION_PATH"]) / "owner.py", owner)
29+
with open(os.environ["GITHUB_OUTPUT"], "a") as output:
30+
output.write(f"owner-path={owner}\n")
31+
with open(os.environ["GITHUB_ENV"], "a") as output:
32+
output.write(f"CI_GIT_OWNER={owner}\n")
33+
PYTHON

0 commit comments

Comments
 (0)