Skip to content

Instantly share code, notes, and snippets.

@ervinb
Created May 24, 2018 15:14
Show Gist options
  • Save ervinb/7b3fa2ddabb8ec0fedd99ec3253350d8 to your computer and use it in GitHub Desktop.
Save ervinb/7b3fa2ddabb8ec0fedd99ec3253350d8 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -euo pipefail
changed_files=""
changed_directories=""
if [ "$BRANCH_NAME" = "master" ]; then
echo "On master. Nothing to do."
exit 0;
fi
# Fetch all the commits from the current branch
branch_commits=$(git log --format=%H --all --not $(git rev-list --no-walk --exclude=refs/heads/${BRANCH_NAME} --exclude=refs/remotes/origin/${BRANCH_NAME} --exclude=HEAD --all))
if [ -z "$branch_commits" ]; then
echo "* No commit found on branch '${BRANCH_NAME}'"
exit 0;
fi
# Collect all the files which are modified on the current branch
echo -e "\n* Looking up modified files."
for commit in $(echo $branch_commits); do
echo ">>>>>>>>>"
echo "${commit}"
commit_files=$(git diff-tree --no-commit-id --name-only -r $commit;)
echo -e "\n${commit_files}"
echo -e "<<<<<<<<\n"
changed_files="${changed_files} $(git diff-tree --no-commit-id --name-only -r $commit;)"
done
# Sort the files and filter out duplicates
changed_files="$(echo ${changed_files} | tr ' ' '\n' | sort -u | tr '\n' ' ')"
echo -e "\n* Files modified on branch '${BRANCH_NAME}':\n${changed_files}"
# Collect all the parent directories of the modified files
for file in $(echo $changed_files); do
dir=$(dirname $file)
if [ $dir = '.' ]; then continue; fi
changed_directories="$changed_directories $dir"
done
echo -e "\n* Directories modified on branch '${BRANCH_NAME}':\n${changed_directories}"
# Run conditional actions based on the changed directories
if ( echo $changed_directories | grep 'feature' ); then
echo "* Running feature specs"
echo "bundle exec rake cucumber"
fi
if [[ $changed_directories =~ app\/controllers* ]]; then
echo "* Running unit tests"
echo "bundle exec rspec spec/controllers"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment