git-lfs/script/cibuild
brian m. carlson 78ea047174
script/cibuild: check for code formatting
Sometimes we get contributions from folks who aren't very familiar with
Go.  This is great, but since they aren't familiar with Go, they don't
know to use go fmt or goimports to tidy files.  This leads to files
that are misformatted, which then causes problems when users who do have
these tools are doing development.

Let's help people check formatting automatically by running it as part
of the CI job.  We already check for trailing whitespace, which is
complementary (since it looks at non-Go files as well), and that check
has been effective, so likely this one will be as well.

There is one potential downside: sometimes output changes between
versions of Go.  This is unfortunate, but we'll need to just pin to
whatever version GitHub Actions uses.
2019-10-03 14:18:38 +00:00

53 lines
1.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
# Strip out CI environment variables which cause tests to fail.
unset $(env | grep -E '^GIT(HUB)?_' | sed -e 's/=.*$//')
UNAME=$(uname -s)
X=""
if [[ $UNAME == MINGW* || $UNAME == MSYS* || $UNAME == CYGWIN* ]]; then
X=".exe"
WINDOWS=1
export GIT_LFS_NO_TEST_COUNT=1
export GIT_LFS_LOCK_ACQUIRE_DISABLED=1
fi
# Set GOPATH if it isn't already set.
eval "$(go env | grep GOPATH)"
go get golang.org/x/tools/cmd/goimports
GOIMPORTS="$GOPATH/bin/goimports"
make GOIMPORTS="$GOIMPORTS" && make GOIMPORTS="$GOIMPORTS" test
# re-run test to ensure GIT_TRACE output doesn't leak into the git package
GIT_TRACE=1 make GOIMPORTS="$GOIMPORTS" PKGS=git test
pushd t >/dev/null
PROVE="prove"
PROVE_EXTRA_ARGS="-j9"
if [ "$WINDOWS" ]; then
export PATH="/c/Strawberry/perl/bin:.:$PATH"
PROVE="prove.bat"
PROVE_EXTRA_ARGS="$PROVE_EXTRA_ARGS --exec bash"
fi
VERBOSE_LOGS=1 make X="$X" clean
VERBOSE_LOGS=1 make X="$X" PROVE="$PROVE" PROVE_EXTRA_ARGS="$PROVE_EXTRA_ARGS"
popd >/dev/null
echo "Looking for trailing whitespace..."
! git grep -lE '[[:space:]]+$' | \
grep -vE '(^vendor/|\.git/(objects/|index)|\.bat$)'
echo "Formatting files..."
# Building and installing goimports and goversioninfo will have modified go.mod
# and go.sum, so reset the branch before formatting.
git reset --hard
make GOIMPORTS="$GOIMPORTS" fmt
echo "Looking for files that are not formatted correctly..."
git status -s
[ -z "$(git status --porcelain)" ]