diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000000..7b48e065e2 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,29 @@ +#!/bin/sh + +# This scripts checks if the generated file is commited together with files that it is generated from. + +if git diff-index --cached --name-only HEAD | grep -i "^templates/.*$" >/dev/null; then + if ! git diff-index --cached --name-only HEAD | grep -i "^generate/zz_filesystem_generated.go$" >/dev/null; then + echo "WARNING: You are about to commit changes to the templates directory," \ + "but the generated generate/zz_filesystem_generated.go file is not staged." + echo "If this is intentional use '--no-verify' flag." + exit 1 + fi +fi + +if git diff-index --cached --name-only HEAD | grep -i "^generate/zz_filesystem_generated.go$" >/dev/null; then + UNVERSIONED=$(git ls-files --others --exclude-standard --ignored -- templates/ 2>/dev/null) + if [ -n "$UNVERSIONED" ]; then + echo "WARNING: You are about to commit generate/zz_filesystem_generated.go," \ + "but the templates directory contains some unversioned files" \ + "that may be unintentionally included in generate/zz_filesystem_generated.go" + for f in $UNVERSIONED; do + echo " $f" + done + echo "If this is intentional use '--no-verify' flag." + exit 1 + fi +fi + +exit 0 + diff --git a/docs/CONTRIBUTING.md b/CONTRIBUTING.md similarity index 77% rename from docs/CONTRIBUTING.md rename to CONTRIBUTING.md index 13d8fd837f..4b2692fa87 100644 --- a/docs/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,6 +2,13 @@ This document details how to get started contributing to the project. This includes building and testing the core, templates and how to update them, and usage of the optional integration testing tooling. +**Tip**: +Install git hooks that do basic pre-commit checks. + +```sh +make setup-githooks +``` + ## Building To build the core project, run `make` from the repository root. This will result in a `func` binary being generated. Run `make help` for additional targets and `./func help` for usage-centric documentation. @@ -33,39 +40,6 @@ It is also important to run the unit tests of the template modified. For example, to run the unit tests of the Go templates, use `make test-go`. For a list of available make targets, use `make help`. -**Tip**: -Put the following script in your `.git/hooks/pre-commit` file. - -```sh -#!/bin/sh - -if git diff-index --cached --name-only HEAD | grep -i "^templates/.*$" >/dev/null; then - if ! git diff-index --cached --name-only HEAD | grep -i "^generate/zz_filesystem_generated.go$" >/dev/null; then - echo "WARNING: You are about to commit changes to the templates directory," \ - "but the generated generate/zz_filesystem_generated.go file is not staged." - echo "If this is intentional use '--no-verify' flag." - exit 1 - fi -fi - -if git diff-index --cached --name-only HEAD | grep -i "^generate/zz_filesystem_generated.go$" >/dev/null; then - UNVERSIONED=$(git ls-files --others --exclude-standard --ignored -- templates/ 2>/dev/null) - if [ -n "$UNVERSIONED" ]; then - echo "WARNING: You are about to commit generate/zz_filesystem_generated.go," \ - "but the templates directory contains some unversioned files" \ - "that may be unintentionally included in generate/zz_filesystem_generated.go" - for f in $UNVERSIONED; do - echo " $f" - done - echo "If this is intentional use '--no-verify' flag." - exit 1 - fi -fi - -exit 0 - -``` - ## Integration Testing *Integration tests are run automatically* for all pull requests from within the GitHub Pull Request. This process includes creating and configuring a lightweight cluster. diff --git a/Makefile b/Makefile index 5da91274e7..06e0670013 100644 --- a/Makefile +++ b/Makefile @@ -409,3 +409,7 @@ test-hack: .PHONY: update-builder __update-builder: # Used in automation cd hack && go run ./cmd/update-builder + +.PHONY: setup-githooks +setup-githooks: + git config --local core.hooksPath .githooks/