|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Decide whether local pre-commit should run the Docker stage check. |
| 5 | +# Exit 0 when Docker-risk files changed, otherwise exit 1. |
| 6 | + |
| 7 | +if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then |
| 8 | + echo "Not inside a git worktree; running Docker check." |
| 9 | + exit 0 |
| 10 | +fi |
| 11 | + |
| 12 | +changed_paths="$( |
| 13 | + { |
| 14 | + git diff --name-only |
| 15 | + git diff --name-only --cached |
| 16 | + git ls-files --others --exclude-standard |
| 17 | + } | sed '/^$/d' | sort -u |
| 18 | +)" |
| 19 | + |
| 20 | +if [ -z "${changed_paths}" ]; then |
| 21 | + echo "No file changes detected." |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +is_docker_risk_path() { |
| 26 | + case "$1" in |
| 27 | + .dockerignore) return 0 ;; |
| 28 | + packages/core/src/docker/*) return 0 ;; |
| 29 | + .github/workflows/ci.yml) return 0 ;; |
| 30 | + .github/workflows/docker-publish.yml) return 0 ;; |
| 31 | + .github/workflows/dockerfile-updates.yml) return 0 ;; |
| 32 | + .github/workflows/version-bump.yml) return 0 ;; |
| 33 | + scripts/check-dockerfile-updates.sh) return 0 ;; |
| 34 | + scripts/extract-oci-description.py) return 0 ;; |
| 35 | + scripts/should-run-docker-check.sh) return 0 ;; |
| 36 | + esac |
| 37 | + return 1 |
| 38 | +} |
| 39 | + |
| 40 | +matched_paths=() |
| 41 | +while IFS= read -r path; do |
| 42 | + [ -n "${path}" ] || continue |
| 43 | + if is_docker_risk_path "${path}"; then |
| 44 | + matched_paths+=("${path}") |
| 45 | + fi |
| 46 | +done <<< "${changed_paths}" |
| 47 | + |
| 48 | +if [ "${#matched_paths[@]}" -eq 0 ]; then |
| 49 | + echo "No Docker-risk file changes detected." |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +printf 'Docker-risk file changes detected:\n' |
| 54 | +printf ' - %s\n' "${matched_paths[@]}" |
| 55 | +exit 0 |
0 commit comments