├── check-composer.sh ├── README.md └── check-coke.sh /check-composer.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # check-composer.sh 4 | # 5 | # Check if there a composer installation difference between past and future branch, and launch a composer update command 6 | # 7 | # Install into: 8 | # - .git/hooks/post-merge 9 | # - .git/hooks/post-rewrite 10 | # - .git/hooks/post-checkout 11 | # 12 | # And make sure all are executable. 13 | # 14 | 15 | file="composer.lock" 16 | 17 | if [[ $(git diff HEAD@{1}..HEAD@{0} -- "${file}" | wc -l) -gt 0 ]]; then 18 | echo 19 | echo "NB : The file '${file}' changed, executing 'composer install' command" 20 | echo 21 | composer install 22 | echo 23 | fi 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # m6web/git-hooks 2 | 3 | Collection of hooks git used by M6Web tech team 4 | 5 | ## Check-coke.sh 6 | 7 | [coke](https://github.com/M6Web/Coke) is designed to validate entire project. 8 | For large project, with hight amont of files, coke execution could take several minutes. 9 | 10 | In an other way, we want to check there is no code standard violation before put code update in git, and a way is to have a git pre-commit hook to execute coke. 11 | 12 | To keep commit fast, but execute coke, the check-coke.sh will look for files to be commited and execute code standard check only on it. 13 | 14 | #### Installation 15 | 16 | Go in your project .git/hooks folder and edit (or create if not exists) a `pre-commit` file, made it executable (`chmod u+x pre-commit`), and add this line in the file: 17 | 18 | ```shell 19 | #!/bin/bash 20 | 21 | /check-coke.sh 22 | ``` 23 | 24 | ## Check-composer.sh 25 | 26 | This hooks check if composer.lock file change on git operation, and launch `composer install` command. 27 | 28 | This allow to always work on last vendors versions defined in your project composer.lock, even if one of your teammate update the dependencies. 29 | 30 | ### Installation 31 | 32 | Go in your project .git/hooks folder and edit (or create if not exists) theses files: 33 | * `post-merge` 34 | * `post-rewrite` 35 | * `post-checkout` 36 | 37 | Made it executable (`chmod u+x pre-commit`), and add this line in each file: 38 | 39 | ```shell 40 | #!/bin/bash 41 | 42 | /check-composer.sh 43 | ``` 44 | -------------------------------------------------------------------------------- /check-coke.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # check-coke.sh 4 | # 5 | # Execute a coke check only on file to be commited. 6 | # Allow a very fast check, especially for project with large amount of files 7 | # 8 | # Install into: 9 | # - .git/hooks/pre-commit 10 | # 11 | # And make sure all are executable. 12 | # 13 | 14 | file=".coke" 15 | files="" 16 | stash=0 17 | 18 | function success 19 | { 20 | echo "[$(tput bold)$(tput setaf 2)SUCCESS$(tput sgr0)] $(tput setaf 6)$1$(tput sgr0)" 21 | exit 0 22 | } 23 | 24 | function fail 25 | { 26 | echo "[$(tput bold)$(tput setaf 1)FAIL$(tput sgr0)] $(tput setaf 6)$1$(tput sgr0)" 27 | exit 1 28 | } 29 | 30 | function warning 31 | { 32 | echo "[$(tput bold)$(tput setaf 3)WARNING$(tput sgr0)] $(tput setaf 6)$1$(tput sgr0)" 33 | } 34 | 35 | function info 36 | { 37 | echo "[$(tput bold)$(tput setaf 4)INFO$(tput sgr0)] $(tput setaf 6)$1$(tput sgr0)" 38 | } 39 | 40 | # Execution preparation 41 | STATUS_OUTPUT=$(git status --porcelain) 42 | 43 | if echo "$STATUS_OUTPUT"|grep '^[MARCDU][MARCDU]' > /dev/null 44 | then 45 | fail "Some files appears both in your staging area and your unadded files." 46 | elif echo "$STATUS_OUTPUT"|grep '^ [MARCD]' > /dev/null 47 | then 48 | if git stash save -u -k -q 49 | then 50 | stash=1 51 | else 52 | fail "Unable to stash changes" 53 | fi 54 | fi 55 | 56 | if [ ! -e $file ] 57 | then 58 | warning "Can't find \".coke\" file, aborting check" 59 | exit 0 60 | fi 61 | 62 | cokePaths=$( grep "^[^\#][^\=]*$" $file ) 63 | 64 | for file in $(git status --porcelain | grep '^[MARC]' | colrm 1 3 | cut -f2 -d">") 65 | do 66 | allowed=0 67 | 68 | for ligne in $cokePaths 69 | do 70 | if [ "${ligne:0:1}" = '!' ] && [[ "$file" == *"${ligne:1}"* ]] 71 | then 72 | continue 2 73 | elif [[ "$file" == "$ligne"* ]] 74 | then 75 | allowed=1 76 | fi 77 | done 78 | 79 | if [ "$allowed" -eq 1 ] 80 | then 81 | files="$files $file" 82 | fi 83 | done 84 | 85 | # Command execution 86 | if [ -n "$files" ] 87 | then 88 | coke $files 89 | CS_RESULT=$? 90 | else 91 | CS_RESULT=0 92 | fi 93 | 94 | if [ "$stash" -eq 1 ] 95 | then 96 | if git stash pop -q 97 | then 98 | warning "Unable to revert stash command" 99 | fi 100 | fi 101 | 102 | if [ $CS_RESULT -eq 1 ] 103 | then 104 | fail "You must fix coding standards before commit" 105 | fi 106 | 107 | success "Coding standards" 108 | --------------------------------------------------------------------------------