Skip to content

Instantly share code, notes, and snippets.

@Loupax
Last active October 6, 2021 20:33
Show Gist options
  • Save Loupax/1df8d912b4942af19b56b262928f0a3f to your computer and use it in GitHub Desktop.
Save Loupax/1df8d912b4942af19b56b262928f0a3f to your computer and use it in GitHub Desktop.
golang-ci linter for files getting pushed to remote only
#!/bin/sh
# An example hook script to verify what is about to be pushed. Called by "git
# push" after it has checked the remote status, but before anything has been
# pushed. If this script exits with a non-zero status nothing will be pushed.
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
# <local ref> <local oid> <remote ref> <remote oid>
#
remote="$1"
url="$2"
zero=$(git hash-object --stdin </dev/null | tr '[0-9a-f]' '0')
while read local_ref local_oid remote_ref remote_oid
do
if test "$local_oid" = "$zero"
then
# Handle delete
:
else
if test "$remote_oid" = "$zero"
then
# New branch, examine all commits
range="$local_oid"
else
# Update to existing branch, examine new commits
range="$remote_oid..$local_oid"
fi
# Gather all files getting pushed to remote. Bail if nothing is found
files=($(git diff --name-status --diff-filter=ACM $remote_oid | awk '{print $2}'))
if [ -z "$files" ]
then
exit 0
fi
# Create a grep pattern consisting of all the files we have changed
grepFiles=$(printf "\|%s" "${files[@]}")
grepFiles=${grepFiles:2}
# Run golangci-lint run for the entire project but only output the reports that are relevant to our changes
errs=$(golangci-lint run ./... --out-format tab | grep $grepFiles)
printf '%s\n' "${errs[@]}"
# No errors? We are good to go!
if [ -z "$errs" ]
then
exit 0
else
exit 1
fi
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment