Skip to content

Instantly share code, notes, and snippets.

@kadamwhite
Created March 4, 2021 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadamwhite/4d6a2ac5f602afc988d6242a80c8bc3b to your computer and use it in GitHub Desktop.
Save kadamwhite/4d6a2ac5f602afc988d6242a80c8bc3b to your computer and use it in GitHub Desktop.
Bash script to run PHPCS against only files which have changed since branching off a specific base branch.
#/usr/bin/env bash
# Usage:
#
# Run this script (via an npm run alias) with no arguments to compute the
# issues in files changed since you branched from "development."
#
# npm run lint:php:changed
#
# Pass a specific branch name as the first argument to determine which files
# have changed since branching off of a different base branch.
#
# npm run lint:php:changed -- other-base-branch
#
# PHPCS flags can also be provided when calling this script, and will be
# passed through to the linter. This can be done in addition to or instead
# of providing a separate base branch.
#
# npm run lint:php:changed -- -s --report=summary
# npm run lint:php:changed -- main -s
if [[ $1 =~ ^- ]]; then
# A git branch is unlikely to start with a -, so if the first argument
# starts with - we assume we're only passing PHPCS flags.
TARGET_BRANCH="development"
PHPCS_ARGS=$@
else
TARGET_BRANCH=${1:-development}
PHPCS_ARGS=${@:2}
fi
# Run PHPCS (path is relative to package.json/project root) with the normal
# ruleset, and pass in a list of all PHP files which have changed since the
# current branch diverged from TARGET_BRANCH.
# The --diff-filter here verbosely lists all types of git modification except
# deletions (D) to make sure modified files get picked up even if renamed.
vendor/bin/phpcs --standard=phpcs.ruleset.xml $(
git diff --name-only --diff-filter=ACMRTUXB $(git merge-base HEAD "$TARGET_BRANCH") | grep '.php$'
) $PHPCS_ARGS
@prykon
Copy link

prykon commented Mar 7, 2022

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment