Created
March 27, 2017 21:22
-
-
Save StabbyMcDuck/bc093338f68b2cb9f0ae340abe0b26de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Bash Script for Charles Schwab DevOps challenge | |
# Regina Imhoff | |
# [email protected] | |
############################# | |
# 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