From 5b53f3377fe307e5a98910f2cd6d24881ff69401 Mon Sep 17 00:00:00 2001 From: Vincent Dechenaux Date: Mon, 15 May 2017 16:24:39 +0200 Subject: [PATCH] Add option to coke only Git changed files --- README.md | 4 ++++ coke | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/README.md b/README.md index ddd6976..a386b37 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,9 @@ standard=Symfony2 # Verbose mode (optional - default: false) verbose=true +# Only Git changed mode (optional - default: false) +only-git-changed=true + # White list of files and directories (optional) src/ test.php @@ -47,6 +50,7 @@ The order of arguments is not important `src test.php` Files/Directories to include in the check `--standard=Symfony2` Standard to use for check `--ignore=Tests,src/OldFile.php` URL patterns to ignore in the check +`--only-git-changed` Check only changed files `-v` Use verbose mode diff --git a/coke b/coke index 9db046b..094a9f1 100755 --- a/coke +++ b/coke @@ -15,6 +15,7 @@ ignored=0 allowed=0 verbose=0 fix=0 +onlyGitChanged=0 function allow { @@ -104,6 +105,9 @@ do elif [ "${CUR_PARAM:2:5}" = "fix" ] then fix=1 + elif [ "${CUR_PARAM:2:16}" = "only-git-changed" ] + then + onlyGitChanged=1 else params="$params $CUR_PARAM" fi @@ -159,6 +163,9 @@ then elif [ "$verbose" -eq 0 ] && [ "${ligne:0:8}" = "verbose=" ] && [ "${ligne:8}" = "true" ] then verbose=1 + elif [ "$onlyGitChanged" -eq 0 ] && [ "${ligne:0:17}" = "only-git-changed=" ] && [ "${ligne:17}" = "true" ] + then + onlyGitChanged=1 elif [ "$ignored" -eq 0 ] && [ "${ligne:0:1}" = "!" ] then ignore "${ligne:1}" @@ -195,6 +202,37 @@ then $commandPath$command --config-set installed_paths $standardPath fi +# Check Git changes + +if [ $onlyGitChanged -eq 1 ] +then + command -v git >/dev/null 2>&1 || { fail "Cannot find git. Git is required when --only-git-changed is used."; } + + filesAllowedAndChanged="" + for file in $(git --no-pager diff --name-only --diff-filter=MARC) + do + allowed=0 + + for ligne in $filesAllowed + do + if [ "${ligne:0:1}" = '!' ] && [[ "$file" == *"${ligne:1}"* ]] + then + continue 2 + elif [[ "$file" == "$ligne"* ]] + then + allowed=1 + fi + done + + if [ "$allowed" -eq 1 ] + then + filesAllowedAndChanged="$filesAllowedAndChanged $file" + fi + done + + filesAllowed=$filesAllowedAndChanged +fi + # Punch it! if [ -n "$filesAllowed" ]