#!/usr/bin/env bash # Which branch should we compare HEAD with for changed files? (defaults: 'development') BRANCH='development' DIRS='app lib' while getopts 'b:d:' optname do case "$optname" in "b") BRANCH=$OPTARG ;; "d") DIRS=$OPTARG ;; esac done # Get a list of all files that have changed on this branch in app and lib directories. CHANGED_FILES=$(git diff --name-only $BRANCH -- $DIRS) # A string with the name of each changed file EXISTING_FILES="" # Iterate over each changed file for FILEPATH in $CHANGED_FILES do # Append this filename if the file still exists (in case the file has been deleted) if [ -f $FILEPATH ]; then EXISTING_FILES+=" $FILEPATH" fi done # If there are no files that have been changed... if [ -z "$EXISTING_FILES" ] then # Print a message and exit echo "Rubocop changed: No matching files have changed." exit 0 else # Run Rubocop against the files that have chagned rubocop -p $EXISTING_FILES fi