#!/bin/bash ## hack ## A simple script to automate a Git workflow ## Credits # Variation on ReinH's script for managing the git process # as documented here: http://reinh.com/blog/2008/08/27/hack-and-and-ship.html ## Configuration # Set your preferred default branch to hack in #DEFAULT_BRANCH="development" DEFAULT_BRANCH="mybranch" # "rake" is for folks using Ruby #TEST_CMD="rake" # For example merb uses "rake spec" instead of just "rake" # for web2py, we need something different TEST_CMD="echo NO TEST" ## For basic usage you should not need to edit anything below this line ####### usage() { echo " " echo "Usage: hack {on, sync, push}" echo " " echo "hack on " echo "If no branch is supplied '$DEFAULT_BRANCH' is used." echo " " echo "hack sync" echo "Rebase to develop branch" echo " " echo "hack push" echo "Merges and pushes to origin develop" echo " " echo "hack ssp (simple software process)" echo "hack sync && hack push" echo "Use -t flag (hack ssp -t) to include testing." echo "Testing is currently set to $TEST_CMD but can be configured as desired" exit 1 } set_current_branch() { if [ ! $CURRENT ]; then CURRENT=`git branch | grep "*" | awk '{print $2}'`; fi } set_target_branch() { if [ $1 ]; then BRANCH=$1 else BRANCH=$DEFAULT_BRANCH fi } create_branch_unless_exists() { # sed command removes * if present and any leading or trailing white space if [ $BRANCH == "`git branch | grep -w "$BRANCH" | sed 's/*//;s/^[ \t]*//;s/[ \t]*$//'`" ]; then echo "Branch $BRANCH already exists. Switching to existing branch." else git branch $BRANCH echo "New branch $BRANCH created." fi } hack_on() { set_current_branch set_target_branch $1 if [ $CURRENT != "develop" ]; then git checkout develop; fi git pull origin develop create_branch_unless_exists git checkout $BRANCH } hack_sync() { set_current_branch if [ $CURRENT == "develop" ]; then echo "Can't do. Development branch is currently checked out."; exit 1; fi git fetch origin develop git rebase develop } hack_push() { set_current_branch git checkout develop git merge ${CURRENT} git push origin develop git checkout ${CURRENT} } case $1 in on*) hack_on $2 ;; sync*) hack_sync ;; push*) hack_push ;; *) echo 2>&1 "Unrecognized command: $1" usage ;; esac