Compare commits

...
This repository has been archived on 2022-08-18. You can view files and clone it, but cannot push or open issues or pull requests.

7 Commits

Author SHA1 Message Date
Richard van der Hoff cb93741050 random 2017-04-05 14:20:08 +01:00
Richard van der Hoff 5a7b9bb323 -u dance 2017-04-05 14:07:30 +01:00
Richard van der Hoff 2b59bb94b0 index_file 2017-04-05 13:58:43 +01:00
Richard van der Hoff ea2aca161c run hook with -x 2017-04-05 13:31:22 +01:00
Richard van der Hoff 22ce003d81 Add comment 2017-04-05 13:24:18 +01:00
Richard van der Hoff 804edaad62 Use readlink -f instead of realpath
... because readlink has a better chance of being installed.
2017-04-05 13:23:12 +01:00
Richard van der Hoff f02c685d9a fix up the pre-commit hook
* Run the checks against the git index (ie, what you're about to commit),
  rather than the index

* Add the changes made by `go fmt` back to the index, so that they are included
  in the commit, as well as the working copy.
2017-04-05 13:18:06 +01:00
2 changed files with 37 additions and 2 deletions

View File

@ -5,4 +5,4 @@ install:
- go get github.com/constabulary/gb/...
- go get github.com/golang/lint/golint
- go get github.com/fzipp/gocyclo
script: ./hooks/pre-commit
script: bash -x ./hooks/pre-commit

View File

@ -1,9 +1,44 @@
#! /bin/bash
#!/bin/bash
set -eu
# make the GIT_DIR and GIT_INDEX_FILE absolute, before we change dir
export GIT_DIR=$(readlink -f `git rev-parse --git-dir`)
if [ -n "${GIT_INDEX_FILE:+x}" ]; then
export GIT_INDEX_FILE=$(readlink -f "$GIT_INDEX_FILE")
fi
wd=`pwd`
# create a temp dir. The `trap` incantaion will ensure that it is removed again
# when this script completes.
tmpdir=`mktemp -d`
trap 'rm -rf "$tmpdir"' EXIT
cd "$tmpdir"
# get a copy of the index
git checkout-index -a
# run our checks
golint src/...
go fmt ./src/...
go tool vet --shadow ./src
gocyclo -over 12 src/
gb test
# if there are no changes from the index, we are done
git diff --quiet && exit 0
# we now need to apply any changes made to both the index and the working copy.
# so, first get a patch
git diff > "$GIT_DIR/pre-commit.patch"
# add the changes to the index
git add -u
# attempt to apply the changes to the wc (but don't fail the commit for it if
# there are conflicts).
cd "$wd"
git apply "$GIT_DIR/pre-commit.patch" 2>/dev/null &&
rm "$GIT_DIR/pre-commit.patch" ||
echo "warning: unable to apply changes from commit hook to working copy; patch is in $GIT_DIR/pre-commit.patch" >&2