mirror of
https://github.com/anchore/grype
synced 2024-11-10 14:44:12 +00:00
Modify tidy check to compare against git HEAD
Signed-off-by: Dan Luhring <dan.luhring@anchore.com>
This commit is contained in:
parent
6f0b1995d0
commit
81f68a1562
1 changed files with 21 additions and 11 deletions
32
.github/scripts/go-mod-tidy-check.sh
vendored
32
.github/scripts/go-mod-tidy-check.sh
vendored
|
@ -1,27 +1,37 @@
|
|||
#!/usr/bin/env bash
|
||||
set -u
|
||||
set -eu
|
||||
|
||||
TMP_GOMOD=$(mktemp)
|
||||
TMP_GOSUM=$(mktemp)
|
||||
BACKUPS_DIR=$(mktemp -d "TEMP-backups-XXXXXXXXX")
|
||||
GIT_HEAD_STATE_DIR=$(mktemp -d "TEMP-git-head-state-XXXXXXXXX")
|
||||
TIDY_STATE_DIR=$(mktemp -d "TEMP-tidy-state-XXXXXXXXX")
|
||||
|
||||
trap "rm -f ${TMP_GOSUM} ${TMP_GOMOD}" EXIT
|
||||
trap "cp -v ${BACKUPS_DIR}/* ./ && rm -fR ${BACKUPS_DIR} ${GIT_HEAD_STATE_DIR} ${TIDY_STATE_DIR}" EXIT
|
||||
|
||||
cp go.mod "${TMP_GOMOD}"
|
||||
cp go.sum "${TMP_GOSUM}"
|
||||
echo "Backing up files from working tree..."
|
||||
cp -v go.mod go.sum "${BACKUPS_DIR}"
|
||||
|
||||
echo "Capturing state of go.mod and go.sum from git HEAD..."
|
||||
git checkout go.mod go.sum
|
||||
cp -v go.mod go.sum "${GIT_HEAD_STATE_DIR}"
|
||||
echo ""
|
||||
|
||||
echo "Capturing state of go.mod and go.sum after running go mod tidy..."
|
||||
go mod tidy
|
||||
cp -v go.mod go.sum "${TIDY_STATE_DIR}"
|
||||
echo ""
|
||||
|
||||
DIFF_MOD=$(diff -u "${TMP_GOMOD}" go.mod)
|
||||
DIFF_SUM=$(diff -u "${TMP_GOSUM}" go.sum)
|
||||
set +e
|
||||
|
||||
cp "${TMP_GOMOD}" go.mod
|
||||
cp "${TMP_GOSUM}" go.sum
|
||||
# Detect difference between the git HEAD state and the go mod tidy state
|
||||
DIFF_MOD=$(diff -u "${GIT_HEAD_STATE_DIR}/go.mod" "${TIDY_STATE_DIR}/go.mod")
|
||||
DIFF_SUM=$(diff -u "${GIT_HEAD_STATE_DIR}/go.sum" "${TIDY_STATE_DIR}/go.sum")
|
||||
|
||||
if [[ -n "${DIFF_MOD}" || -n "${DIFF_SUM}" ]]; then
|
||||
echo "go.mod and/or go.sum are not tidy; please run go mod tidy"
|
||||
echo "go.mod diff:"
|
||||
echo "${DIFF_MOD}"
|
||||
echo "go.sum diff:"
|
||||
echo "${DIFF_SUM}"
|
||||
echo ""
|
||||
printf "FAILED! go.mod and/or go.sum are NOT tidy on current git head; please run 'go mod tidy' and commit the change.\n\n"
|
||||
exit 1
|
||||
fi
|
||||
|
|
Loading…
Reference in a new issue