Skip to content

Instantly share code, notes, and snippets.

@makuchaku
Created March 19, 2012 07:24
Show Gist options
  • Save makuchaku/2100701 to your computer and use it in GitHub Desktop.
Save makuchaku/2100701 to your computer and use it in GitHub Desktop.
Git workflow in bash scripts
############ WORK IN PROCESS ############
# -- Copy (append) the following script to your ~/.bashrc file and restart console --
## Workflow ##
# Suppose the current deploy branch is deploy_x_y
# Suppose you have to start work on Bug #100
# Run "bbcreate 100" => a new branch 100_bug will be created out of deploy_x_y and will be checked out (ready to start work)
# You work and finish the changes in your 100_bug branch
# Run "bbpush" to add + commit + push your changes into 100_bug branch. Commit log will be "Fixes for #100"
# You can do bbpush multiple times you like
# Once ready to merge 100_bug into deploy_x_y, run "bbmerge deploy_x_y" => Will checkout deploy_x_y, pull its latest contents, merge your branch & will try to push it!
## TODO ##
# Detect merge conflicts & prevent any further actions
###########################################################################################
# Gets current local branch from current GIT repo
# call as $(getGitBranch)
# Returns => branch_name
function getGitBranch() {
local currentBranch=`git branch | grep "*" | cut -d " " -f 2`
echo $currentBranch
}
# Tries to get the bug ID from the current GIT branch
# Returns => bugId
function getBugIdFromGitBranch() {
echo $(getGitBranch) | cut -d "_" -f 1
}
# Generates a general purpose log...
# $1 => Log scope
# $2 => Log message
function log() {
echo "==> [$1] $2"
}
# Asset minification => Works only in deploy_x_y branches!
function minifyAssets() {
if [ $(getBugIdFromGitBranch) == "deploy" ]
then
# Minify if minifier exists
if [ -a "./deploy/minify/minify.sh" ]
then
log "bbpush" "Minifying assets"
./deploy/minify/minify.sh
git add .
git commit -am "Asset minification"
fi
fi
}
# Tests if required arg is missing. If yes, print a help message and exit
# $1 => function name for generating help message
# $2 => The argument to be tested (tested for zero string length) - http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
function testArgs() {
if [ -z $2 ]
then
echo "Required arg missing in $1! Please check ~/.bashrc for more details!"
exit 1
fi
}
# Closes the given unfuddle ticket. Also adds the given description
# $1 => bugId
function closeUnfuddleTicket() {
local lastCommit=`git log | head -n 1 | cut -d " " -f 2`
local repositoryUrl="https://github.com/RevoContent/cricket_expert/commit/"
curl -i -u user:pass -X PUT \
-H 'Accept: application/xml' \
-d "<ticket><resolution>fixed</resolution><resolution-description>Fixed in $repositoryUrl$lastCommit</resolution-description></ticket>" \
"http://playupindia.unfuddle.com/api/v1/projects/11/tickets/$1.xml"
}
# Create a bug branch from current branch
# $1 => Bug ID (numeric)
function bbcreate() {
testArgs "bbcreate" $1
log "bbcreate" "Pulling $(getGitBranch)"
git pull origin $(getGitBranch)
log "bbcreate" "Creating a new branch called $1_bug"
git branch "$1_bug"
git checkout "$1_bug"
}
# Commit and push to current bug branch
function bbpush() {
log "bbpush" "Going to push changes into $(getGitBranch)"
minifyAssets
git add .
git commit -am "Fixes for #$(getBugIdFromGitBranch)"
git push origin $(getGitBranch)
}
# merges the bug branch into $1 and pushes $1. $1 will generally be one of the deploy_x_y branches
# $1 => deploy_v_1_1
function bbmerge() {
testArgs "bbmerge" $1
bugBranch=$(getGitBranch)
log "bbmerge" "Going to merge changes from $bugBranch into $1"
git checkout $1
git pull origin $1
log "bbmerge" "Merging now!"
git merge $bugBranch
minifyAssets
git push origin $1
}
# Deploys the current branch onto staging server
function bbdeploy() {
bugBranch=$(getGitBranch)
log "Deploying on staging now..."
echo $bugBranch | cap staging deploy
}
###########################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment