Skip to content

Instantly share code, notes, and snippets.

@alecGraves
Created February 11, 2020 20:57
Show Gist options
  • Save alecGraves/03b205385103052779d0d6493502cdbf to your computer and use it in GitHub Desktop.
Save alecGraves/03b205385103052779d0d6493502cdbf to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -eo pipefail
SRC_PATH=src/robotic-lawnmower/
# latest common ancestor commit with master
#LATEST_MASTER_COMMIT=$(git -C $SRC_PATH rev-parse origin/master)
LATEST_MASTER_COMMIT=$(git -C $SRC_PATH merge-base origin/master HEAD)
PR_COMMITS=()
BACK=0
COMMIT=$(git -C $SRC_PATH rev-parse HEAD~$BACK)
while [ "$COMMIT" != "$LATEST_MASTER_COMMIT" ] ;do
PR_COMMITS+=( $COMMIT )
BACK=$(($BACK + 1))
COMMIT=$(git -C $SRC_PATH rev-parse HEAD~$BACK)
done
echo "Commits in this Branch:"
for i in ${PR_COMMITS[@]}; do echo $i; done
function arr_remove() {
to_remove="${1}"
shift
array=("${@}")
new_arr=()
for element in "${array[@]}"; do
if [ "$element" != "$to_remove" ]; then
echo $element
fi
done
}
function arr_contains(){
to_check="${1}"
shift
array=("${@}")
contains="false"
for element in "${array[@]}"; do
if [ "$element" == "$to_check" ]; then
contains="true"
fi
done
echo $contains
}
REBUILD_FILES=$(ls -a $SRC_PATH)
IGNORE=(".git" "docs" "." ".." "README.md")
for i in "${IGNORE[@]}"; do
REBUILD_FILES=$(arr_remove $i $REBUILD_FILES)
done
REBUILD="false"
for entry in ${REBUILD_FILES[@]}; do
COMMIT_NUM=$(git -C $SRC_PATH log -1 --format=format:%H --full-diff $entry)
if [ "$(arr_contains $COMMIT_NUM $PR_COMMITS)" == "true" ]; then
echo "Change to $entry detected"
REBUILD="true"
fi
done
if [ "$REBUILD" == "true" ]; then
eval "$@"
else
echo "All changes were to docs. I am not running the command."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment