From ed6abe6832682119ad2b3ba04a17e9592cf4c985 Mon Sep 17 00:00:00 2001 From: Chris Evich Date: Fri, 28 Jun 2024 11:24:43 -0400 Subject: [PATCH] Add pre-commit (app) hook to check IMGSFX Intended for use by [the pre-commit app](https://pre-commit.com/#intro), this hook keeps track of all IMG_SFX values pushed, failing when any duplicate is found. In the case of pushing to PRs that don't build CI VM images, the hook failure must be manually bypassed. Example `.pre-commit-config.yaml`: ```yaml --- repos: - repo: https://github.com/containers/automation_images.git rev: hooks: - id: check-imgsfx ``` Signed-off-by: Chris Evich --- .pre-commit-hooks.yaml | 20 ++++++++++++++++++++ check-imgsfx.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 .pre-commit-hooks.yaml create mode 100755 check-imgsfx.sh diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 00000000..f61c65cd --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,20 @@ +--- + +# Ref: https://pre-commit.com/#creating-new-hooks +- id: check-imgsfx + name: Check IMG_SFX for accidental reuse. + description: | + Every PR intended to produce CI VM or container images must update + the `IMG_SFX` file via `make IMG_SFX`. The exact value will be + validated against global suffix usage (encoded as tags on the + `imgts` container image). This pre-commit hook verifies on every + push, the IMG_SFX file's value has not been pushed previously. + It's intended as a simple/imperfect way to save developers time + by avoiding force-pushes that will most certainly fail validation. + entry: ./check-imgsfx.sh + language: system + exclude: '.*' # Not examining any specific file/dir/link + always_run: true # ignore no matching files + fail_fast: true + pass_filenames: false + stages: ["pre-push"] diff --git a/check-imgsfx.sh b/check-imgsfx.sh new file mode 100755 index 00000000..9f89cc91 --- /dev/null +++ b/check-imgsfx.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# +# 2024-01-25 esm +# 2024-06-28 cevich + +set -eo pipefail + +# Ensure CWD is the repo root +cd $(dirname "${BASH_SOURCE[0]}") +imgsfx=$(&2 + exit 1 +fi + +# Pre-commit runs in a temp. dir, history must persist via absolute path. +imgsfx_history="$HOME/.cache/pre-commit/imgsfx.history" + +if [[ -e $imgsfx_history ]]; then + if grep -q "$imgsfx" $imgsfx_history; then + echo "FATAL: $imgsfx has already been used" >&2 + echo "Please rerun 'make IMG_SFX'" >&2 + exit 1 + fi +fi + +mkdir -p $(dirname "$imgsfx_history") +echo $imgsfx >>$imgsfx_history