|
| 1 | +#!/bin/bash |
| 2 | +# SPDX-License-Identifier: GPL-2.0 |
| 3 | + |
| 4 | +HEAD=$(git rev-parse HEAD) |
| 5 | +rc=0 |
| 6 | + |
| 7 | +pr() { |
| 8 | + echo " ====== $* ======" | tee -a /dev/stderr |
| 9 | +} |
| 10 | + |
| 11 | +# If it doesn't touch .py files, don't bother. Ignore deleted. |
| 12 | +if ! git show --diff-filter=AM --pretty="" --name-only "${HEAD}" | grep -q -E "\.py$" |
| 13 | +then |
| 14 | + echo "No python scripts touched, skip" >&"$DESC_FD" |
| 15 | + exit 0 |
| 16 | +fi |
| 17 | + |
| 18 | +ruff --version || exit 1 |
| 19 | + |
| 20 | +tmpfile_o=$(mktemp) |
| 21 | +tmpfile_n=$(mktemp) |
| 22 | + |
| 23 | +echo "Redirect to $tmpfile_o and $tmpfile_n" |
| 24 | + |
| 25 | +echo "Tree base:" |
| 26 | +git log -1 --pretty='%h ("%s")' HEAD~ |
| 27 | +echo "Now at:" |
| 28 | +git log -1 --pretty='%h ("%s")' HEAD |
| 29 | + |
| 30 | +pr "Checking before the patch" |
| 31 | +git checkout -q HEAD~ |
| 32 | + |
| 33 | +# Also ignore created, as not present in the parent commit |
| 34 | +for f in $(git show --diff-filter=M --pretty="" --name-only "${HEAD}" | grep -E "\.py$"); do |
| 35 | + ruff check --output-format pylint "$f" | tee -a "$tmpfile_o" |
| 36 | +done |
| 37 | + |
| 38 | +incumbent=$(wc -l < "$tmpfile_o") |
| 39 | + |
| 40 | +pr "Checking the tree with the patch" |
| 41 | +git checkout -q "$HEAD" |
| 42 | + |
| 43 | +for f in $(git show --diff-filter=AM --pretty="" --name-only "${HEAD}" | grep -E "\.py$"); do |
| 44 | + ruff check --output-format pylint "$f" | tee -a "$tmpfile_n" |
| 45 | +done |
| 46 | + |
| 47 | +current=$(wc -l < "$tmpfile_n") |
| 48 | + |
| 49 | +echo "Errors before: $incumbent ; this patch: $current" >&"$DESC_FD" |
| 50 | + |
| 51 | +if [ "$current" -gt "$incumbent" ]; then |
| 52 | + echo "New errors added" 1>&2 |
| 53 | + diff -U 0 "$tmpfile_o" "$tmpfile_n" 1>&2 |
| 54 | + |
| 55 | + rc=1 |
| 56 | +fi |
| 57 | + |
| 58 | +rm "$tmpfile_o" "$tmpfile_n" |
| 59 | + |
| 60 | +exit $rc |
0 commit comments