Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save StabbyMcDuck/bc093338f68b2cb9f0ae340abe0b26de to your computer and use it in GitHub Desktop.
Save StabbyMcDuck/bc093338f68b2cb9f0ae340abe0b26de to your computer and use it in GitHub Desktop.
#!/bin/bash
# Bash Script for Charles Schwab DevOps challenge
# Regina Imhoff
# regina.imhoff@gmail.com
#############################
# set for exit immediately! #
#############################
set -e
#############
# Variables #
#############
errorMsg="Wrong number of arguments - need 2"
repo_clone_path=$1
destination_path=$2
############################################################
# check the number of arguments #
# print off error message if the wrong number of arguments #
############################################################
if [ "$#" -gt 2 ] || [ "$#" -lt 2 ]
then
echo "$errorMsg" >&2
exit 1
fi
###
# git branches without asterisk
###
cwd=$PWD
pushd "$repo_clone_path"
git branch | tr -d " *" | sort > "$cwd"/git_branches.$$.tmp
popd
###
# list all directories
###
pushd "$destination_path"
find . -maxdepth 1 | sort > "$cwd"/all_directories.$$.tmp
popd
# set complement for new git branches
comm -23 < git_branches.$$.tmp < all_directories.$$.tmp > new_git_branches.$$.tmp
# intersection of 1 and 2 for directories to git pull
comm -12 < git_branches.$$.tmp < all_directories.$$.tmp > directories_to_refresh.$$.tmp
# set complement for directories to delete
comm -23 < all_directories.$$.tmp < git_branches.$$.tmp > directories_to_delete.$$.tmp
# new directories
pushd "$destination_path"
for new_branch in $(cat "$cwd"/new_git_branches.$$.tmp); do
pushd "$new_branch"
git clone "$repo_clone_path"
git checkout "$new_branch"
popd
done
popd
# delete directories
pushd "$destination_path"
rm -rf $(cat $cwd/directories_to_delete.$$.tmp)
popd
# refresh directories
pushd "$destination_path"
for branch in $(cat $cwd/directories_to_refresh.$$.tmp); do
pushd "$branch"
git pull "$repo_clone_path"
popd
done
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment