Skip to content

Instantly share code, notes, and snippets.

@willfish
Created August 29, 2019 15:45
Show Gist options
  • Select an option

  • Save willfish/bf7325e8214eb4f5732dda1c938b328b to your computer and use it in GitHub Desktop.

Select an option

Save willfish/bf7325e8214eb4f5732dda1c938b328b to your computer and use it in GitHub Desktop.
#!/bin/bash
contains() {
[[ $1 =~ (^| )$2($| ) ]] && echo 'yes' || echo 'no'
}
# Takes a space separated list of directories that we're interested in seeing have changed
# Returns a space separated list of directories that have changed.
changed() {
local directories="$*"
local changed_files=""
local gems=()
local unique_directories=""
changed_files=$(git diff --name-only origin/master)
for file in $changed_files; do
directory=$(echo -n "$file" | awk -F/ '{print $1}')
gem=$(contains "$directories" "$directory")
if [ "$gem" == "yes" ]; then
gems+=("$directory")
fi
done
unique_directories=$(echo -n "${gems[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')
echo "$unique_directories"
}
# Simple bash function for determining whether a single level list of directories
# in a git repo have changed compared to master.
# How to use
# Given a multi-project repository where we've updated a file in project3.
# source changed.sh
# PROJECT_DIRECTORIES="project1 project2 project3"
# echo changed $PROJECT_DIRECTORIES # project3
# You can store the result in a variable and use it to make decisions about
# whether to run ci, etc.
# Prerequisites
# - Bash
# - Git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment