Skip to content

Commit

Permalink
Add Pre-Commit hook. (elastic#5305)
Browse files Browse the repository at this point in the history
  • Loading branch information
simitt authored and tsg committed Oct 3, 2017
1 parent 6b85d18 commit 6e07d0b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ REVIEWDOG_REPO=github.com/haya14busa/reviewdog/cmd/reviewdog

# Runs complete testsuites (unit, system, integration) for all beats with coverage and race detection.
# Also it builds the docs and the generators

.PHONY: setup-commit-hook
setup-commit-hook:
@cp script/pre_commit.sh .git/hooks/pre-commit
@chmod 751 .git/hooks/pre-commit

.PHONY: testsuite
testsuite:
@$(foreach var,$(PROJECTS),$(MAKE) -C $(var) testsuite || exit 1;)
Expand Down
29 changes: 29 additions & 0 deletions script/pre_commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

set -e

echo "-- pre commit hook running"
#return if no files staged for commit
staged_files=$(git diff --name-only --cached)
[ -z "$staged_files" ] && exit 0

#run make cmds and check whether
#- unstaged files have changed
#- make cmd fails
unstaged_files=$(git diff --name-only)

echo "---- lint"
make lint

echo "---- format"
make fmt

echo "---- misspell"
make misspell

unstaged_files_after=$(git diff --name-only)
if [ "$unstaged_files" == "$unstaged_files_after" ] ; then
exit 0
fi;
echo "Pre-Commit hook has failed, see changed files."
exit 1

0 comments on commit 6e07d0b

Please sign in to comment.