Skip to content

Commit

Permalink
adding double clickable shell script to switch branches and update
Browse files Browse the repository at this point in the history
  • Loading branch information
mkassner committed Sep 3, 2013
1 parent 8ee9342 commit 46e5577
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 0 deletions.
83 changes: 83 additions & 0 deletions switch_to_dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash
#
# take from https://github.com/jamiew/git-friendly
# Usage: branch [branchname]
#
#
# Switch to an existing branch, or create a new local-only branch.
# Adds remote tracking if a remote branch with the same name exists,
# in any remote, preferring the "origin" master if there are duplicates.
#
# Executing without arguments prints all local & remote branches
#
cd $(dirname $0)

remote="origin"
branch="dev"

if [ -z $branch ]; then
echo -e "\nSwitch to or create a new branch:"
echo -e " $(basename $0) [name]"
echo -e "\nCurrent branches: "
branches="git branch -a -vv"
$branches
echo
exit 0
fi

local_branch_exists=$(git branch --no-color | egrep "$branch\$")
remote_branch_exists=$(git branch -r --no-color | egrep "$remote/$branch\$")
remotes=( $(git remote) )
remotes_with_branch=()
origin_has_branch=

for remote in "${remotes[@]}";
do
remote_branch_exists=$(git branch -r --no-color | egrep "$remote/$branch\$")
if [ "$remote_branch_exists" ] ; then
remotes_with_branch=("${remotes_with_branch[@]}" "$remote")
if [ "$remote" = "origin" ] ; then
origin_has_branch=1
fi
fi
done

if [ ${#remotes_with_branch[@]} -gt 0 ]; then
# if there's an origin remote with the named branch, use it
if [ "$origin_has_branch" = "1" ] ; then
remote=origin
else # track the first matching branch alphabetically
remote=${remotes[0]}
fi
remote_branch_exists="$remote/$branch"
fi

# If local exists already, switch to it
if [ -n "$local_branch_exists" ] && [ ! "$local_branch_exists" == '' ]; then
echo "Switching to existing local branch..."
git checkout $branch

# Track remote branch if not already
if [ -n "$remote_branch_exists" ] && [ ! "$remote_branch_exists" == '' ]; then
tracking=$(git branch -vv | grep "*" | awk '{ print $4 '})
# echo "Remote branch exists. Local branch is tracking: $tracking"
if [[ ! "$tracking" =~ "$remote" ]]; then
echo "Your local branch is not tracking the corresponding remote branch, fixing..."
git branch --set-upstream-to $branch $remote/$branch
fi
# else
# echo "Remote branch does not exist, not doing anything"
fi

# If remote exists, create a local branch that tracks the remote
elif [ -n "$remote_branch_exists" ] && [ ! "$remote_branch_exists" == '' ]; then
echo "Tracking existing remote branch '$remote_branch_exists'..."
git checkout -b $branch --track $remote/$branch

# Otherwise create a new local branch
else
echo "Creating new local branch..."
git checkout -b $branch --no-track
fi

exit 0
83 changes: 83 additions & 0 deletions switch_to_master
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash
#
# take from https://github.com/jamiew/git-friendly
# Usage: branch [branchname]
#
#
# Switch to an existing branch, or create a new local-only branch.
# Adds remote tracking if a remote branch with the same name exists,
# in any remote, preferring the "origin" master if there are duplicates.
#
# Executing without arguments prints all local & remote branches
#
cd $(dirname $0)

remote="origin"
branch="master"

if [ -z $branch ]; then
echo -e "\nSwitch to or create a new branch:"
echo -e " $(basename $0) [name]"
echo -e "\nCurrent branches: "
branches="git branch -a -vv"
$branches
echo
exit 0
fi

local_branch_exists=$(git branch --no-color | egrep "$branch\$")
remote_branch_exists=$(git branch -r --no-color | egrep "$remote/$branch\$")
remotes=( $(git remote) )
remotes_with_branch=()
origin_has_branch=

for remote in "${remotes[@]}";
do
remote_branch_exists=$(git branch -r --no-color | egrep "$remote/$branch\$")
if [ "$remote_branch_exists" ] ; then
remotes_with_branch=("${remotes_with_branch[@]}" "$remote")
if [ "$remote" = "origin" ] ; then
origin_has_branch=1
fi
fi
done

if [ ${#remotes_with_branch[@]} -gt 0 ]; then
# if there's an origin remote with the named branch, use it
if [ "$origin_has_branch" = "1" ] ; then
remote=origin
else # track the first matching branch alphabetically
remote=${remotes[0]}
fi
remote_branch_exists="$remote/$branch"
fi

# If local exists already, switch to it
if [ -n "$local_branch_exists" ] && [ ! "$local_branch_exists" == '' ]; then
echo "Switching to existing local branch..."
git checkout $branch

# Track remote branch if not already
if [ -n "$remote_branch_exists" ] && [ ! "$remote_branch_exists" == '' ]; then
tracking=$(git branch -vv | grep "*" | awk '{ print $4 '})
# echo "Remote branch exists. Local branch is tracking: $tracking"
if [[ ! "$tracking" =~ "$remote" ]]; then
echo "Your local branch is not tracking the corresponding remote branch, fixing..."
git branch --set-upstream-to $branch $remote/$branch
fi
# else
# echo "Remote branch does not exist, not doing anything"
fi

# If remote exists, create a local branch that tracks the remote
elif [ -n "$remote_branch_exists" ] && [ ! "$remote_branch_exists" == '' ]; then
echo "Tracking existing remote branch '$remote_branch_exists'..."
git checkout -b $branch --track $remote/$branch

# Otherwise create a new local branch
else
echo "Creating new local branch..."
git checkout -b $branch --no-track
fi

exit 0
52 changes: 52 additions & 0 deletions update
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash
#
# Usage: pull
#
# Pulls remote changes using rebase & tries to rebundle,
# safely stashing and re-applying your local changes, if any
#


function do_update(){
# Update our remote
echo "Fetching from $remote ..."
git fetch $remote || exit $?

# Pull, using rebase if configured
rebase="--rebase" # TODO disable if env-var is set
git pull $rebase $remote $remote_branch || exit $?

# Update submodules
git submodule update || exit $?

echo "Done"
}


cd $(dirname $0)

branch=$(git branch --no-color 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') || exit $?
default_remote="origin"
remote=$(git config "branch.${branch}.remote" || echo "$default_remote")
remote_branch=$( (git config "branch.${branch}.merge" || echo "refs/heads/$branch") | awk -F '/' '{ print $3 }' )

# Stash any local changes
stash=$(git stash)

if [[ "$stash" =~ "No local changes to save" ]]; then
do_update
exit 0
fi

echo "You have made changes to the source code. Do you want to continue the update and have them saved in a WIP stash?"
read -p "Type: Yes/No" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]] ; then
do_update
exit 0
fi
git stash pop
echo "Aborted the Update, nothing changed"
exit 1


0 comments on commit 46e5577

Please sign in to comment.