Skip to content

Instantly share code, notes, and snippets.

@HarlemSquirrel
Created December 5, 2020 19:36
Show Gist options
  • Save HarlemSquirrel/dae7923024c456cb68b929037dd4bdd6 to your computer and use it in GitHub Desktop.
Save HarlemSquirrel/dae7923024c456cb68b929037dd4bdd6 to your computer and use it in GitHub Desktop.
Filter a command like rubocop to look for issues only on modified lines.
#!/bin/bash
##
# Filter a command like rubocop to look for issues only on modified lines.
#
# Inspired by http://takemikami.com/2018/01/30/RubocopPullRequestCI.html
#
BASE_REMOTE=origin
BASE_BRANCH=master
diff_list=()
commit_list=$(git --no-pager log --no-merges $BASE_REMOTE/$BASE_BRANCH...HEAD | grep -e '^commit' | sed -e "s/^commit \(.\{8\}\).*/\1/")
for f in $(git --no-pager diff $BASE_REMOTE/$BASE_BRANCH...HEAD --name-only --diff-filter=AM); do
for c in $commit_list; do
diffs=$(git --no-pager blame --show-name -s $f | grep $c | sed -e "s/^[^ ]* *\([^ ]*\) *\([0-9]*\)*).*$/\1:\2/")
for ln in $diffs; do
diff_list+=( $ln )
done
done
done
err_count=0
while read -r ln; do
for m in ${diff_list[@]}; do
if [[ ${ln} =~ ^$m ]]; then
echo $ln
err_count=$((err_count+1))
break
fi
done
done < /dev/stdin
if [ $err_count -ne 0 ]; then
echo -e "\033[0;31mERROR FOUND, check above messages.\033[0;39m"
exit 1
fi
echo "OK"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment