"benchmarks/vscode:/vscode.git/clone" did not exist on "1b962e24577dddc0d7441ae0b06392e1f9262a51"
shellcheck.sh 1.88 KB
Newer Older
1
#!/bin/bash
2
set -euo pipefail
3
4

scversion="stable"
5
baseline="tools/pre_commit/shellcheck.baseline"
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

if [ -d "shellcheck-${scversion}" ]; then
    export PATH="$PATH:$(pwd)/shellcheck-${scversion}"
fi

if ! [ -x "$(command -v shellcheck)" ]; then
    if [ "$(uname -s)" != "Linux" ] || [ "$(uname -m)" != "x86_64" ]; then
        echo "Please install shellcheck: https://github.com/koalaman/shellcheck?tab=readme-ov-file#installing"
        exit 1
    fi

    # automatic local install if linux x86_64
    wget -qO- "https://github.com/koalaman/shellcheck/releases/download/${scversion?}/shellcheck-${scversion?}.linux.x86_64.tar.xz" | tar -xJv
    export PATH="$PATH:$(pwd)/shellcheck-${scversion}"
fi

22
# TODO - fix warnings in .buildkite/scripts/hardware_ci/run-amd-test.sh
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# collects warnings as "file:SCcode" pairs for baseline comparison.
collect() {
  find . -path ./.git -prune -o -name "*.sh" \
    -not -path "./.buildkite/scripts/hardware_ci/run-amd-test.sh" -print0 | \
    xargs -0 sh -c 'for f in "$@"; do git check-ignore -q "$f" || shellcheck -s bash -f gcc "$f" || true; done' -- | \
    sed -nE 's|^\./||; s|^([^:]+):[0-9]+:[0-9]+:.*\[(SC[0-9]+)\]$|\1:\2|p' | \
    sort -u
}

if [[ "${1:-}" == "--generate-baseline" ]]; then
  collect > "$baseline"
  echo "Wrote baseline to $baseline"
  exit 0
fi

if [[ ! -f "$baseline" ]]; then
  echo "Baseline not found: $baseline (run: $0 --generate-baseline)"
  exit 1
fi

current="$(mktemp)"
trap 'rm -f "$current"' EXIT
collect > "$current"

# finds new warnings not in baseline
new_errors="$(comm -23 "$current" <(sort -u "$baseline") || true)"
if [ -n "$new_errors" ]; then
  echo "$new_errors" | cut -d: -f1 | sort -u | while IFS= read -r file; do
    if [[ -f "$file" ]]; then
      codes=$(echo "$new_errors" | awk -F: -v f="$file" '$1==f {print $2}' | paste -sd ',' -)
      shellcheck -s bash --include="$codes" "$file" 2>&1 || true
    fi
  done
  exit 1
fi