Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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.
# 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$'
Copy link

prykon commented Mar 7, 2022


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