Repository: s0enke/git-hooks Branch: master Commit: 7e6266cafc89 Files: 5 Total size: 5.1 KB Directory structure: gitextract_wcbn21q_/ ├── LICENSE ├── README └── phpcs-pre-commit/ ├── README ├── config └── pre-commit ================================================ FILE CONTENTS ================================================ ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2010-2016 Soenke Ruempler Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README ================================================ git-hooks collection (ABANDONED) Currently just a pre-commit hook for PHP Codesniffer ================================================ FILE: phpcs-pre-commit/README ================================================ PHP Codesniffer Pre-Commit Hook for GIT Author: Soenke Ruempler Website: http://github.com/s0enke/git-hooks REQUIREMENTS * Bash * PHP CodeSniffer: http://pear.php.net/package/PHP_CodeSniffer/redirected FEATURES * Check Coding style and forbid the commit if violations are found * Configuration file for Coding Standard, Path to PHPCS, Ignore List * Shows output in a 'less' pipe following the smart git principles USAGE * Put the script "pre-commit" into your .git/hooks directory * OR: add the script to your pre-commit "chain" (you probably know what to do then) * Put the Config file "config" into the same dir as the "pre-commit" script and edit it to your requirements * Ensure that the script is executable. ================================================ FILE: phpcs-pre-commit/config ================================================ # path to phpcs "binary" PHPCS_BIN=/usr/bin/phpcs # the coding standard, you can also specify a path to your own standard here # e. g. /path/to/my/standard/dir/ PHPCS_CODING_STANDARD=PEAR # comma-separated list of file patterns being ignored PHPCS_IGNORE= # comma-seperated list of sniffs from the standard that should be used # use `phpcs --standard=PSR1 -e` to list sniffs for your standard PHPCS_SNIFFS=Generic.Files.ByteOrderMark,Generic.PHP.DisallowShortOpenTag # egrep compatible pattern of files to be checked PHPCS_FILE_PATTERN="\.(php|phtml)$" # ignore warnings PHPCS_IGNORE_WARNINGS=1 # encoding PHPCS_ENCODING=utf-8 ================================================ FILE: phpcs-pre-commit/pre-commit ================================================ #!/bin/bash # PHP CodeSniffer pre-commit hook for git # # @author Soenke Ruempler # @author Sebastian Kaspari # # see the README PHPCS_BIN=/usr/bin/phpcs PHPCS_CODING_STANDARD=PEAR PHPCS_IGNORE= TMP_STAGING=".tmp_staging" # parse config CONFIG_FILE=$(dirname $0)/config if [ -e $CONFIG_FILE ]; then . $CONFIG_FILE fi # simple check if code sniffer is set up correctly if [ ! -x $PHPCS_BIN ]; then echo "PHP CodeSniffer bin not found or executable -> $PHPCS_BIN" exit 1 fi # stolen from template file if git rev-parse --verify HEAD then against=HEAD else # Initial commit: diff against an empty tree object against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 fi # this is the magic: # retrieve all files in staging area that are added, modified or renamed # but no deletions etc FILES=$(git diff-index --name-only --cached --diff-filter=ACMR $against -- ) if [ "$FILES" == "" ]; then exit 0 fi # create temporary copy of staging area if [ -e $TMP_STAGING ]; then rm -rf $TMP_STAGING fi mkdir $TMP_STAGING # match files against whitelist FILES_TO_CHECK="" for FILE in $FILES do echo "$FILE" | egrep -q "$PHPCS_FILE_PATTERN" RETVAL=$? if [ "$RETVAL" -eq "0" ] then FILES_TO_CHECK="$FILES_TO_CHECK $FILE" fi done if [ "$FILES_TO_CHECK" == "" ]; then exit 0 fi # execute the code sniffer if [ "$PHPCS_IGNORE" != "" ]; then IGNORE="--ignore=$PHPCS_IGNORE" else IGNORE="" fi if [ "$PHPCS_SNIFFS" != "" ]; then SNIFFS="--sniffs=$PHPCS_SNIFFS" else SNIFFS="" fi if [ "$PHPCS_ENCODING" != "" ]; then ENCODING="--encoding=$PHPCS_ENCODING" else ENCODING="" fi if [ "$PHPCS_IGNORE_WARNINGS" == "1" ]; then IGNORE_WARNINGS="-n" else IGNORE_WARNINGS="" fi # Copy contents of staged version of files to temporary staging area # because we only want the staged version that will be commited and not # the version in the working directory STAGED_FILES="" for FILE in $FILES_TO_CHECK do ID=$(git diff-index --cached $against $FILE | cut -d " " -f4) # create staged version of file in temporary staging area with the same # path as the original file so that the phpcs ignore filters can be applied mkdir -p "$TMP_STAGING/$(dirname $FILE)" git cat-file blob $ID > "$TMP_STAGING/$FILE" STAGED_FILES="$STAGED_FILES $TMP_STAGING/$FILE" done OUTPUT=$($PHPCS_BIN -s $IGNORE_WARNINGS --standard=$PHPCS_CODING_STANDARD $ENCODING $IGNORE $SNIFFS $STAGED_FILES) RETVAL=$? # delete temporary copy of staging area rm -rf $TMP_STAGING if [ $RETVAL -ne 0 ]; then echo "$OUTPUT" | less fi exit $RETVAL