diff --git a/tests/kernel-pr-check b/tests/kernel-pr-check index c6c6f4a20..bfff310ff 100755 --- a/tests/kernel-pr-check +++ b/tests/kernel-pr-check @@ -108,6 +108,39 @@ rel_build_out=$(cd "$k" && "$here/tools/kernel-pr-check" -C . --build out omamob [[ $rel_build_status == 2 ]] || { echo "$rel_build_out"; fail "a relative --build inside the tree: exit $rel_build_status, expected 2"; } echo "ok - --build is refused inside the kernel tree, relative or not" +# Nor where the builds would delete the tree or its git directory. clang and +# ld.lld stubs let the tool reach the builds, and its rm records what under +# $c (resolved, as the rm compares resolved paths) it is asked to delete and +# deletes none of it: a regression reports the deletion instead of making it. +c=$(cd -- "$t" && pwd -P)/collide real_rm=$(command -v rm) +mkdir -p "$c/bin" "$c/link-out" "$c/deep-out/build" +printf '#!/bin/sh\nexit 0\n' > "$c/bin/clang" +cp "$c/bin/clang" "$c/bin/ld.lld" +cat > "$c/bin/rm" <<'EOF' +#!/bin/bash +for a; do [[ $a == -* ]] || [[ $(realpath -m -- "$a")/ != "$COLLIDE"/* ]] || { echo "$a" >> "$COLLIDE/rm.log"; exit 1; }; done +exec "$REAL_RM" "$@" +EOF +chmod +x "$c/bin/clang" "$c/bin/ld.lld" "$c/bin/rm" +git clone -q "$k" "$c/src" +git clone -q "$k" "$c/main/src" +git -C "$c/main/src" worktree add -q "$c/main/wt" HEAD +ln -s "$c/src" "$c/link-out/build" +ln -s "$c/src/arch" "$c/deep-out/build/arch" +refused() { # name tree out + local out status=0 + out=$(PATH=$c/bin:$PATH COLLIDE=$c REAL_RM=$real_rm "$here/tools/kernel-pr-check" -C "$2" --build "$3" origin/omamobile HEAD 2>&1) || status=$? + [[ ! -e $c/rm.log ]] || { echo "$out"; fail "$1: deleted $(paste -sd' ' "$c/rm.log")"; } + [[ $status == 2 ]] || { echo "$out"; fail "$1: exit $status, expected 2"; } + grep -c 'overlaps the kernel tree' <<<"$out" >/dev/null || { echo "$out"; fail "$1: silent"; } +} +refused "OUT holds a tree named src" "$c/src" "$c" +refused "OUT/build links to the tree" "$c/src" "$c/link-out" +refused "OUT/build/arch links into the tree" "$c/src" "$c/deep-out" +refused "OUT/src holds a worktree's git directory" "$c/main/wt" "$c/main" +[[ -x $c/src/scripts/checkpatch.pl && -d $c/main/src/.git ]] || fail "a collision fixture lost its tree" +echo "ok - --build is refused where its src, src-base, build or build-base would delete the tree or its git directory" + # --merge: the base moves on after the branch is cut. A clean merge passes # and says so; a conflict fails as "rebase needed" while the commit checks # still run. diff --git a/tools/kernel-pr-check b/tools/kernel-pr-check index de4ff728d..6fef60186 100755 --- a/tools/kernel-pr-check +++ b/tools/kernel-pr-check @@ -30,9 +30,10 @@ # tools/kernel-pr-check [-C KERNEL] [--build OUT] [--merge] [--no-checkpatch] BASE [HEAD] # # KERNEL defaults to $KERNEL_BASE/linux (~/omamobile-kernel/linux, or -# ~/android/mainline/linux on older setups), HEAD to HEAD. It reads git and -# builds in OUT (and OUT-base, for the base's device trees); it never -# touches the tree's checkout or anything else. +# ~/android/mainline/linux on older setups), HEAD to HEAD. It reads git, +# exports what it builds to OUT/src and OUT/src-base and builds in OUT/build +# and OUT/build-base; it never touches the tree's checkout or anything else, +# and refuses an OUT that would. # # --merge builds what would land: HEAD merged onto BASE (a test merge, # written as git objects only), compared with BASE itself. The commit checks @@ -67,7 +68,7 @@ while (($#)); do --merge) merge=1; shift ;; --no-checkpatch) checkpatch=0; shift ;; --strict-trailers) shift ;; # no longer stricter; see above - -h|--help) sed -n '3,47p' "$self"; exit 0 ;; + -h|--help) sed -n '3,48p' "$self"; exit 0 ;; -*) echo "kernel-pr-check: unknown option $1" >&2; exit 2 ;; *) break ;; esac @@ -81,14 +82,30 @@ if [[ ! -d $kernel ]]; then echo "kernel-pr-check: no such tree $kernel" >&2 exit 2 fi -kernel=$(CDPATH='' cd -- "$kernel" && pwd) -# --build too: the builds run after the cd, and they delete and re-extract -# directories under OUT, so OUT must never resolve inside the kernel tree. +# Symlinks resolved too, so the --build check below sees the real tree. +kernel=$(CDPATH='' cd -- "$kernel" && pwd -P) +# --build too: the builds run after the cd. They replace OUT/src and +# OUT/src-base, build in OUT/build and OUT/build-base and delete $stale there. +# None of these, symlinks resolved, may be the tree or its git directory (a +# worktree's lies outside it), hold it or lie inside it: an OUT that was the +# parent of a tree named src had the tree deleted. if [[ -n $out ]]; then out=$(realpath -m -- "$out") - case $out/ in - "$kernel"/*) echo "kernel-pr-check: --build $out is inside the kernel tree; use a directory outside it" >&2; exit 2 ;; - esac + src=$out/src srcb=$out/src-base + stale=("$out"/build{,-base}/{arch/arm64/boot/dts,Documentation/devicetree/bindings/processed-schema.json}) + inside() { [[ ${1%/}/ == "${2%/}"/* ]]; } # $1 is $2 or lies inside it + inside "$out" "$kernel" && { echo "kernel-pr-check: --build $out is inside the kernel tree; use a directory outside it" >&2; exit 2; } + gitdir=$(git -C "$kernel" rev-parse --path-format=absolute --git-common-dir 2>/dev/null) && gitdir=$(realpath -m -- "$gitdir") + for d in "$src" "$srcb" "$out"/build{,-base} "${stale[@]}"; do + r=$(realpath -m -- "$d") || { echo "kernel-pr-check: can't resolve $d" >&2; exit 2; } + for p in "$kernel" ${gitdir:+"$gitdir"}; do + inside "$r" "$p" || inside "$p" "$r" || continue + [[ $p == "$kernel" ]] && where="the kernel tree $p" || where="the kernel tree's git directory $p" + [[ $r == "$d" ]] || d+=" (that is, $r)" + echo "kernel-pr-check: --build $out overlaps $where: the builds replace $d; use another directory" >&2 + exit 2 + done + done mkdir -p -- "$out" || { echo "kernel-pr-check: can't create $out" >&2; exit 2; } fi cd -- "$kernel" || exit 2 @@ -228,7 +245,6 @@ if [[ -n $out ]]; then skip "builds: no clang or ld.lld" else # A clean export of what's built and its base, so the tree's own checkout is never touched. - src=$out/src srcb=$out/src-base rm -rf "$src" "$srcb"; mkdir -p "$src" "$srcb" git archive "$build_head" | tar -x -C "$src" k=(make -s -C "$src" O="$out/build" ARCH=arm64 LLVM=1 -j"$(nproc)") @@ -257,8 +273,7 @@ ${bad%$'\n'}"; fi # outputs so make rebuilds, and prints, every warning. The processed # schema goes too, or a reused OUT validates against an earlier # tree's bindings (kernel #32's own binding went unseen that way). - rm -rf "$out/build/arch/arm64/boot/dts" "$out/build-base/arch/arm64/boot/dts" \ - "$out"/build{,-base}/Documentation/devicetree/bindings/processed-schema.json + rm -rf "${stale[@]}" log=$("${k[@]}" W=1 $check $targets 2>&1); st=$? if ((st)); then fail "device trees don't build: