Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.github: add workflow for rejecting PRs that introduce whitespace errors. #171

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .githooks/pre-commit
49 changes: 49 additions & 0 deletions .githooks/pre-commit.d/00-stock-git-checks
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".

if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(git hash-object -t tree /dev/null)
fi

# If you want to allow non-ASCII filenames set this variable to true.
allownonascii=$(git config --type=bool hooks.allownonascii)

# Redirect output to stderr.
exec 1>&2

# Cross platform projects tend to avoid non-ASCII filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
# Note that the use of brackets around a tr range is ok here, (it's
# even required, for portability to Solaris 10's /usr/bin/tr), since
# the square bracket bytes happen to fall in the designated range.
test $(git diff --cached --name-only --diff-filter=A -z $against |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
cat <<\EOF
Error: Attempt to add a non-ASCII file name.

This can cause problems if you want to work with people on other platforms.

To be portable it is advisable to rename the file.

If you know what you are doing you can disable this check using:

git config hooks.allownonascii true
EOF
exit 1
fi

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --
34 changes: 34 additions & 0 deletions .githooks/run-hooks
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

hook="${0##*/}"
dotd="${0}.d"
orig="${0%/*}/../.git/hooks/$hook"

exec 1>&2

for h in "$dotd"/???*; do
case "$h" in
*~|*.swp)
continue
;;
[0-9]*-*)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should document somehow (more clearly, somewhere), that the hooks in the drop-in dir have strict naming requirements (otherwise they are silently ignored). WDYT?

;;
klihub marked this conversation as resolved.
Show resolved Hide resolved
*)
continue
;;
esac
if [ ! -x "$h" ]; then
continue
fi

echo "[running git hook ${h##*/}]"
$h $@
status=$?
if [ $status != 0 ]; then
exit $status
fi
done

if [ -x "$orig" ]; then
exec $orig
fi
klihub marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 8 additions & 0 deletions .github/workflows/project-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ jobs:
with:
go-version: ${{ env.GO_VERSION }}

- name: Git-check PR for conflict markers and trailing whitespace errors.
if: ${{ github.event_name == 'pull_request' }}
run: |
if ! git diff-index --check origin/${{ github.base_ref }} --; then
echo "This PR would introduce the above errors."
echo "Please fix them and update the PR."
exit 1
fi
- name: make verify
run: |
make verify
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,11 @@ site-serve: .$(DOCKER_SITE_BUILDER_IMAGE).image.stamp
touch $@

docs: site-build

#
# rules for enabling project-specific git hooks
#

install-git-hooks:
$(Q)echo "Enabling in-tree git hook..."
git config core.hookspath .githooks