├── LICENSE ├── README └── phpcs-pre-commit ├── README ├── config └── pre-commit /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2010-2016 Soenke Ruempler 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | git-hooks collection (ABANDONED) 2 | 3 | Currently just a pre-commit hook for PHP Codesniffer 4 | 5 | -------------------------------------------------------------------------------- /phpcs-pre-commit/README: -------------------------------------------------------------------------------- 1 | PHP Codesniffer Pre-Commit Hook for GIT 2 | 3 | Author: Soenke Ruempler 4 | Website: http://github.com/s0enke/git-hooks 5 | 6 | REQUIREMENTS 7 | 8 | * Bash 9 | * PHP CodeSniffer: http://pear.php.net/package/PHP_CodeSniffer/redirected 10 | 11 | 12 | FEATURES 13 | 14 | * Check Coding style and forbid the commit if violations are found 15 | * Configuration file for Coding Standard, Path to PHPCS, Ignore List 16 | * Shows output in a 'less' pipe following the smart git principles 17 | 18 | 19 | USAGE 20 | 21 | * Put the script "pre-commit" into your .git/hooks directory 22 | * OR: add the script to your pre-commit "chain" (you probably know what to do then) 23 | * Put the Config file "config" into the same dir as the "pre-commit" script and 24 | edit it to your requirements 25 | * Ensure that the script is executable. 26 | 27 | -------------------------------------------------------------------------------- /phpcs-pre-commit/config: -------------------------------------------------------------------------------- 1 | # path to phpcs "binary" 2 | PHPCS_BIN=/usr/bin/phpcs 3 | 4 | # the coding standard, you can also specify a path to your own standard here 5 | # e. g. /path/to/my/standard/dir/ 6 | PHPCS_CODING_STANDARD=PEAR 7 | 8 | # comma-separated list of file patterns being ignored 9 | PHPCS_IGNORE= 10 | 11 | # comma-seperated list of sniffs from the standard that should be used 12 | # use `phpcs --standard=PSR1 -e` to list sniffs for your standard 13 | PHPCS_SNIFFS=Generic.Files.ByteOrderMark,Generic.PHP.DisallowShortOpenTag 14 | 15 | # egrep compatible pattern of files to be checked 16 | PHPCS_FILE_PATTERN="\.(php|phtml)$" 17 | 18 | # ignore warnings 19 | PHPCS_IGNORE_WARNINGS=1 20 | 21 | # encoding 22 | PHPCS_ENCODING=utf-8 23 | -------------------------------------------------------------------------------- /phpcs-pre-commit/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # PHP CodeSniffer pre-commit hook for git 3 | # 4 | # @author Soenke Ruempler 5 | # @author Sebastian Kaspari 6 | # 7 | # see the README 8 | 9 | PHPCS_BIN=/usr/bin/phpcs 10 | PHPCS_CODING_STANDARD=PEAR 11 | PHPCS_IGNORE= 12 | TMP_STAGING=".tmp_staging" 13 | 14 | # parse config 15 | CONFIG_FILE=$(dirname $0)/config 16 | if [ -e $CONFIG_FILE ]; then 17 | . $CONFIG_FILE 18 | fi 19 | 20 | # simple check if code sniffer is set up correctly 21 | if [ ! -x $PHPCS_BIN ]; then 22 | echo "PHP CodeSniffer bin not found or executable -> $PHPCS_BIN" 23 | exit 1 24 | fi 25 | 26 | # stolen from template file 27 | if git rev-parse --verify HEAD 28 | then 29 | against=HEAD 30 | else 31 | # Initial commit: diff against an empty tree object 32 | against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 33 | fi 34 | 35 | # this is the magic: 36 | # retrieve all files in staging area that are added, modified or renamed 37 | # but no deletions etc 38 | FILES=$(git diff-index --name-only --cached --diff-filter=ACMR $against -- ) 39 | 40 | if [ "$FILES" == "" ]; then 41 | exit 0 42 | fi 43 | 44 | # create temporary copy of staging area 45 | if [ -e $TMP_STAGING ]; then 46 | rm -rf $TMP_STAGING 47 | fi 48 | mkdir $TMP_STAGING 49 | 50 | # match files against whitelist 51 | FILES_TO_CHECK="" 52 | for FILE in $FILES 53 | do 54 | echo "$FILE" | egrep -q "$PHPCS_FILE_PATTERN" 55 | RETVAL=$? 56 | if [ "$RETVAL" -eq "0" ] 57 | then 58 | FILES_TO_CHECK="$FILES_TO_CHECK $FILE" 59 | fi 60 | done 61 | 62 | if [ "$FILES_TO_CHECK" == "" ]; then 63 | exit 0 64 | fi 65 | 66 | # execute the code sniffer 67 | if [ "$PHPCS_IGNORE" != "" ]; then 68 | IGNORE="--ignore=$PHPCS_IGNORE" 69 | else 70 | IGNORE="" 71 | fi 72 | 73 | if [ "$PHPCS_SNIFFS" != "" ]; then 74 | SNIFFS="--sniffs=$PHPCS_SNIFFS" 75 | else 76 | SNIFFS="" 77 | fi 78 | 79 | if [ "$PHPCS_ENCODING" != "" ]; then 80 | ENCODING="--encoding=$PHPCS_ENCODING" 81 | else 82 | ENCODING="" 83 | fi 84 | 85 | if [ "$PHPCS_IGNORE_WARNINGS" == "1" ]; then 86 | IGNORE_WARNINGS="-n" 87 | else 88 | IGNORE_WARNINGS="" 89 | fi 90 | 91 | # Copy contents of staged version of files to temporary staging area 92 | # because we only want the staged version that will be commited and not 93 | # the version in the working directory 94 | STAGED_FILES="" 95 | for FILE in $FILES_TO_CHECK 96 | do 97 | ID=$(git diff-index --cached $against $FILE | cut -d " " -f4) 98 | 99 | # create staged version of file in temporary staging area with the same 100 | # path as the original file so that the phpcs ignore filters can be applied 101 | mkdir -p "$TMP_STAGING/$(dirname $FILE)" 102 | git cat-file blob $ID > "$TMP_STAGING/$FILE" 103 | STAGED_FILES="$STAGED_FILES $TMP_STAGING/$FILE" 104 | done 105 | 106 | OUTPUT=$($PHPCS_BIN -s $IGNORE_WARNINGS --standard=$PHPCS_CODING_STANDARD $ENCODING $IGNORE $SNIFFS $STAGED_FILES) 107 | RETVAL=$? 108 | 109 | # delete temporary copy of staging area 110 | rm -rf $TMP_STAGING 111 | 112 | if [ $RETVAL -ne 0 ]; then 113 | echo "$OUTPUT" | less 114 | fi 115 | 116 | exit $RETVAL 117 | --------------------------------------------------------------------------------