Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions scripts/check-affected.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
# scripts/check-affected.sh --tests ... and scripts/affected-tests.sh --run
# scripts/check-affected.sh --jobs N run up to N selected shell suites at
# once (default: $CHECK_AFFECTED_JOBS,
# else one less than the core count)
# else one less than the core count,
# at most 8)
# scripts/check-affected.sh --base <ref> diff against <ref> (default: origin/main)
# scripts/check-affected.sh --no-fetch skip refreshing <ref> before the
# stale-base check
# run resolves it to a commit
# scripts/check-affected.sh --uncommitted check the work tree as the commit
# it would make (default base: HEAD)
#
Expand Down Expand Up @@ -101,9 +102,13 @@ run_tests=0
# The selected suites run through run-plugin-tests.sh's job pool, which keeps
# the suites in scripts/run-plugin-tests-serial.txt off the parallel group.
# One less than the core count: on a 4-vCPU runner, 3 is the proven ceiling
# (scripts/affected-tests.sh, #3694).
# (scripts/affected-tests.sh, #3694). At most 8: past 8 the wall time is the
# longest suite's (#10534), and on a host where several sessions run suites at
# once, 23 jobs failed suites that passed at 4 and 6 (#10719).
cores="$(nproc 2>/dev/null || echo 2)"
jobs="${CHECK_AFFECTED_JOBS:-$((cores > 1 ? cores - 1 : 1))}"
jobs=$((cores > 1 ? cores - 1 : 1))
((jobs <= 8)) || jobs=8
jobs="${CHECK_AFFECTED_JOBS:-$jobs}"
[[ "$jobs" =~ ^[1-9][0-9]*$ ]] || jobs=1
fetch=1
uncommitted=0
Expand Down Expand Up @@ -257,6 +262,14 @@ fetched=1
if ((fetch)) && [[ "$base" == */* ]]; then
git fetch --quiet "${base%%/*}" "${base#*/}" 2>/dev/null || fetched=0
fi
# Then resolved to a commit once, so every check judges the base the run
# started from: every worktree shares the remote-tracking refs, and another
# session's fetch can move one mid-run. Later drift is CI's and the merge queue's.
base_name="$base"
base="$(git rev-parse --verify --quiet "$base^{commit}")" || {
echo "check-affected: cannot compute the merge-base of HEAD and $base_name: it names no commit" >&2
exit 2
}

merge_base="$(git merge-base HEAD "$base" 2>/dev/null)" || {
echo "check-affected: cannot compute the merge-base of HEAD and $base" >&2
Expand All @@ -274,7 +287,7 @@ mapfile -t changed <<<"$changed_raw"
# Nothing changed is a pass only when HEAD is the base; otherwise the run read
# the wrong tree or range, and passing would check nothing.
if [[ -z "$all_raw" && "$(git rev-list --count "$base...HEAD")" != 0 ]]; then
echo "check-affected: no changed files in $top since $merge_base, yet HEAD is not $base, so nothing would be checked. Run the check-affected.sh inside the tree you mean to check, or name that tree in CHECK_DATA_ROOT." >&2
echo "check-affected: no changed files in $top since $merge_base, yet HEAD is not $base_name, so nothing would be checked. Run the check-affected.sh inside the tree you mean to check, or name that tree in CHECK_DATA_ROOT." >&2
exit 2
fi

Expand Down Expand Up @@ -366,7 +379,7 @@ if ((fetched)); then
run_check stale-base-overlap "scripts/check-stale-base-overlap.sh --check $base" \
"$tools/scripts/check-stale-base-overlap.sh" --check "$base"
else
skip stale-base-overlap "git fetch of $base failed"
skip stale-base-overlap "git fetch of $base_name failed"
fi

if ((${#plugins[@]} == 0)); then
Expand Down
43 changes: 41 additions & 2 deletions scripts/check-affected.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ done
cat >"$repo/scripts/affected-tests.sh" <<'STUB'
#!/usr/bin/env bash
echo "affected-tests ran" >&2
[[ -z "${STUB_ARGS_LOG:-}" ]] || printf '%s\n' "$*" >"$STUB_ARGS_LOG"
[[ -z "${STUB_ENV_LOG:-}" ]] || printf '%s' "${CHECK_DATA_ROOT-unset}" >"$STUB_ENV_LOG"
# The real selector diffs its own tree's work tree, so the stub logs that.
[[ -z "${STUB_STATUS_LOG:-}" ]] || git -C "$(dirname "$0")/.." status --porcelain --untracked-files=all >"$STUB_STATUS_LOG"
Expand Down Expand Up @@ -358,8 +359,10 @@ printf 'x\n' >"$repo/plugins/demo/skills/s/notes.txt"
git_test_config "$repo" add -A
git_test_config "$repo" commit -q -m skill
run STUB_CHECK_CHANGED_SKILLS=1 -- --base plugin-base
check "a changed skill file runs changed-skills against the base" \
'[[ $RC == 1 && $ERR == *"FAIL changed-skills"* && $ERR == *"Reproduce: scripts/check-changed-skills.sh plugin-base"* ]]'
# shellcheck disable=SC2034 # read by the eval in check
plugin_base_sha="$(git_test_config "$repo" rev-parse plugin-base)"
check "a changed skill file runs changed-skills against the base, resolved to its commit" \
'[[ $RC == 1 && $ERR == *"FAIL changed-skills"* && $ERR == *"Reproduce: scripts/check-changed-skills.sh $plugin_base_sha"* ]]'
git_test_config "$repo" reset -q --hard "$pre_plugin"

git_test_config "$repo" remote add origin "$tmp/missing-remote"
Expand Down Expand Up @@ -547,6 +550,42 @@ check "--uncommitted leaves HEAD, the index and the worktree list as they were"
run -- --uncommitted
check "--uncommitted with no edits passes with nothing to check" '[[ $RC == 0 && $OUT == *"over 0 changed files"* ]]'

# The default job count is one less than the core count, at most 8, on a host
# whatever nproc reports; CHECK_AFFECTED_JOBS and --jobs still set it outright.
unset CHECK_AFFECTED_JOBS
mkdir -p "$tmp/nprocbin"
printf '#!/usr/bin/env bash\necho "$STUB_NPROC"\n' >"$tmp/nprocbin/nproc"
chmod +x "$tmp/nprocbin/nproc"
jobs_env=(PATH="$tmp/nprocbin:$PATH" STUB_ARGS_LOG="$tmp/args.log")
run "${jobs_env[@]}" STUB_NPROC=24 -- --base base --tests
check "a 24-core host defaults to 8 jobs" '[[ $RC == 0 && $(cat "$tmp/args.log") == *"--jobs 8 "* ]]'
run "${jobs_env[@]}" STUB_NPROC=4 -- --base base --tests
check "a 4-core host defaults to 3 jobs" '[[ $RC == 0 && $(cat "$tmp/args.log") == *"--jobs 3 "* ]]'
run "${jobs_env[@]}" STUB_NPROC=24 CHECK_AFFECTED_JOBS=16 -- --base base --tests
check "CHECK_AFFECTED_JOBS sets the job count past the default cap" '[[ $RC == 0 && $(cat "$tmp/args.log") == *"--jobs 16 "* ]]'
run "${jobs_env[@]}" STUB_NPROC=24 CHECK_AFFECTED_JOBS=16 -- --base base --tests --jobs 12
check "--jobs sets the job count over CHECK_AFFECTED_JOBS and the cap" '[[ $RC == 0 && $(cat "$tmp/args.log") == *"--jobs 12 "* ]]'

# The stale-base check judges the base as resolved at the start of the run,
# not a remote-tracking ref another worktree's fetch moves while it runs. The
# real check runs; the shell-portability stub, which runs before it, moves
# origin/main to a commit that touches the branch's notes.txt.
cp "$SELF_DIR/check-stale-base-overlap.sh" "$repo/scripts/"
cp "$SELF_DIR/lib/changed-files.sh" "$SELF_DIR/lib/gate-entry.sh" "$repo/scripts/lib/"
moved="$(git_test_config "$repo" commit-tree "HEAD^{tree}" -p base -m moved)"
printf '#!/usr/bin/env bash\n[[ -z "${STUB_MOVE_TO:-}" ]] || git update-ref refs/remotes/origin/main "$STUB_MOVE_TO"\n' \
>"$repo/scripts/check-shell-portability.sh"
git_test_config "$repo" update-ref refs/remotes/origin/main "$(git_test_config "$repo" rev-parse base)"
run STUB_MOVE_TO="$moved" -- --base origin/main --no-fetch
check "a base that moves after the run starts does not fail stale-base-overlap" \
'[[ $RC == 0 && $ERR != *"stale-base-overlap"* ]]'
git_test_config "$repo" update-ref refs/remotes/origin/main "$moved"
run -- --base origin/main --no-fetch
check "a branch behind its base at the start on an overlapping path fails stale-base-overlap" \
'[[ $RC == 1 && $ERR == *"FAIL stale-base-overlap"* && $ERR == *"notes.txt"* ]]'
git_test_config "$repo" checkout -q -- scripts/check-stale-base-overlap.sh scripts/check-shell-portability.sh
rm -f "$repo/scripts/lib/changed-files.sh" "$repo/scripts/lib/gate-entry.sh"

run -- --bogus
check "an unknown flag is a usage error" '[[ $RC == 2 ]]'
run -- --jobs 0
Expand Down
Loading