Last active
August 29, 2015 14:00
-
-
Save skriticos/11315767 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# 2014-04-22, v1.0, pull, push, initial clone of projects | |
# 2014-04-26, v2.0, speed improvement, use hashtable to determine push/pull | |
# automatically pull new projects based on remote hashtable | |
# setting up new repo (note: we should automate this at some point): | |
# create local git repo in projects (~/projects/xxx_name) | |
# login remote, create git repo $remote/"$target".git | |
# local git remote add "$remote"/ | |
# git push | |
# echo $target `git rev-parse HEAD` >> $HOME/.sync.hashtable.remote | |
# scp $HOME/.sync.hashtable.remote "$remote" | |
echo "setting up variables.." | |
remotehost="example.com" | |
remoteuser="user" | |
remoterepo="/home/$remoteuser/stash" | |
cd $HOME | |
if [ ! -d $HOME/projects ]; then | |
echo "no projects folder found, creating projects folder.." | |
mkdir $HOME/projects | |
fi | |
echo "downloading remote hashtable.." | |
mv -f $HOME/.sync.hashtable.remote $HOME/.sync.hashtable.remote.old | |
scp "$remoteuser"@"$remotehost":"$remoterepo"/.sync.hashtable.remote . || exit | |
echo "retriving project names.." | |
projects=( `cut -d" " -f 1 $HOME/.sync.hashtable.remote` ) | |
echo " ${projects[@]}" | |
if [ "$1" == "pull" ]; then | |
echo "performing initial cloning for new projects.." | |
cd $HOME/projects | |
for target in "${projects[@]}" | |
do | |
if [ ! -d "$target" ]; then | |
echo "directory not found, performing initial cloning.." | |
git clone ssh://"$remoteuser"@"$remotehost""$remoterepo"/"$target".git "$target" | |
fi | |
done | |
fi | |
if [ ! -e $HOME/.sync.hashtable ]; then | |
echo "new install presumed, copying remote hash to local.." | |
cp $HOME/.sync.hashtable.remote $HOME/.sync.hashtable | |
fi | |
echo "verify sync base.." | |
if ! cmp -s "$HOME/.sync.hashtable" "$HOME/.sync.hashtable.remote"; then | |
echo "hashtables don't match, aborting.." | |
exit 1 | |
fi | |
# note: commiting is fast, so we can do it for all even if there is nothing to | |
# commit. this removes the requirement for some complex checks and does not | |
# impede performance much. pushing on the other hand takes long, even if nothing | |
# is pushed. | |
if [ "$1" == "push" ]; then | |
echo "commit local changes.." | |
cd $HOME/projects | |
for target in "${projects[@]}" | |
do | |
cd "$target" | |
git add * | |
git commit -am "sync" | |
cd .. | |
done | |
fi | |
echo "updateing local hashtable.." | |
rm -f $HOME/.sync.hashtable | |
cd $HOME/projects | |
for target in "${projects[@]}" | |
do | |
cd "$target" | |
echo "$target" `git rev-parse HEAD` >> $HOME/.sync.hashtable | |
cd .. | |
done | |
echo "extracting changed project names from hashtable.." | |
cd $HOME | |
diffs=( `diff .sync.hashtable .sync.hashtable.remote | grep "<" | cut -d" " -sf 2` ) | |
echo " ${diffs[@]}" | |
cd $HOME/projects | |
for target in "${diffs[@]}" | |
do | |
if [ "$1" == "push" ]; then | |
echo "puishing $target" | |
cd $target | |
git push | |
cd .. | |
elif [ "$1" == "pull" ]; then | |
cd $target | |
git pull | |
cd .. | |
fi | |
done | |
if [ "$1" == "push" ]; then | |
cp -f $HOME/.sync.hashtable $HOME/.sync.hashtable.remote | |
scp $HOME/.sync.hashtable.remote "$remoteuser"@"$remotehost":"$remoterepo" || exit | |
fi | |
echo "all done, exiting.." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment