dotfiles/bin/updaterepos.sh

63 lines
2.0 KiB
Bash
Raw Permalink Normal View History

2017-03-24 03:14:06 +01:00
#!/usr/bin/env bash
#
# Usage:
2017-05-21 00:59:35 +02:00
# ./updaterepos.sh [parent_directory]
2017-03-24 03:14:06 +01:00
#
# example usage:
2017-05-21 00:59:35 +02:00
# ./updaterepos.sh projects/ [MAKE SURE YOU USE / SLASHES]
2017-03-24 03:14:06 +01:00
#
# stolen from:
# http://stackoverflow.com/a/36800741/733677
#
updateRepo() {
local dir="$1"
local original_dir="$2"
2017-06-12 01:21:12 +02:00
cd "$dir" || return # switch to the git repo
2017-03-24 03:14:06 +01:00
repo_url=$(git config --get remote.origin.url)
echo "$(tput setaf 136)Updating Repo: $dir with url: $repo_url$(tput sgr0)"
main_branch="master"
if [ "$repo_url" == "git@someserver:repo/repo.git" ]; then # if you have a repo where the primary branch isnt master
2017-06-12 01:21:12 +02:00
main_branch="trunk"
2017-03-24 03:14:06 +01:00
fi
# update the repo, then stash any local changes
2017-06-12 01:21:12 +02:00
echo "$(tput setaf 240)calling: git fetch --all && git stash$(tput sgr0)"
2017-03-24 03:14:06 +01:00
(git fetch --all && git stash)
current_branch=$(git rev-parse --abbrev-ref HEAD)
# switch to master/trunk branch and rebase it, then switch back to original branch
2017-06-12 01:21:12 +02:00
if [ "$current_branch" != $main_branch ]; then
echo "$(tput setaf 240)calling: git checkout $main_branch && git rebase && git checkout $current_branch $(tput sgr0)"
(git checkout $main_branch && git rebase && git checkout "$current_branch")
2017-03-24 03:14:06 +01:00
fi
# rebase the original branch and then stash pop back to original state
2017-06-12 01:21:12 +02:00
echo "$(tput setaf 240)calling: git rebase && git stash pop on branch: $current_branch $(tput sgr0)"
2017-03-24 03:14:06 +01:00
(git rebase && git stash pop )
# switch back to the starting directory
2017-06-12 01:21:12 +02:00
cd "$original_dir" || return
2017-03-24 03:14:06 +01:00
echo ""
}
directory_to_update=${1}
if [ -z "$directory_to_update" ] ; then
echo "$(tput setaf 240)no directory passed in, using current directory$(tput sgr0)"
directory_to_update=$PWD
fi
2017-06-12 01:21:12 +02:00
echo "$(tput setaf 240)Updating git repos in directory: $directory_to_update $(tput sgr0)"
2017-03-24 03:14:06 +01:00
count=0
2017-09-10 14:46:58 +02:00
for dir in $(find "$directory_to_update" -maxdepth 4 -type d -name .git -print0 | xargs -n -0 1 dirname); do
2017-06-12 01:21:12 +02:00
updateRepo "$dir" "$directory_to_update"
2017-03-24 03:14:06 +01:00
((count+=1))
done
2017-06-12 01:21:12 +02:00
echo "$(tput setaf 76)$count local git repos have been updated!$(tput sgr0)"