Created
September 21, 2012 21:19
-
-
Save henrylim/3763970 to your computer and use it in GitHub Desktop.
bash functions for trellis and git
This file contains 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 | |
#-------------------------------------------------------------------- | |
# | |
# can put this in your .bashrc or .zshrc | |
# if [ -f ~/.git_bash_functions ]; then | |
# . ~/.git_bash_functions | |
# fi | |
# | |
#-------------------------------------------------------------------- | |
# Add autocompletion to aliases or functions | |
#-------------------------------------------------------------------- | |
# | |
# to add autocompletion to these you can use, for example: | |
# complete -o default -o nospace -F _git_checkout git_checkout_pull | |
# complete -o default -o nospace -F _git_checkout gcp | |
# | |
#-------------------------------------------------------------------- | |
# Creating a topic/feature branch | |
#-------------------------------------------------------------------- | |
# | |
# Command: gcb 1234_client_bug | |
# Result: pulls latest stable and creates branch based off of it named '1234_client_bug' | |
# | |
# Command: gcbd new_core_feature | |
# Result: pulls latest stable and creates branch based off of it named '2012_09_01_new_core_feature' | |
# | |
#-------------------------------------------------------------------- | |
# Deploying a topic/feature branch (single-deploy version) | |
#-------------------------------------------------------------------- | |
# | |
# Command: gh master | |
# Result: pulls latest master and merges the last-merged branch in shell, or if none, the current branch, then pushes to deployment server or github | |
# | |
# Command: gh -f development | |
# Result: same as first, but the "-f" flag skips the confirmation prompt | |
# | |
# Command: gh -c staging | |
# Result: same as first, but the "-c" flag ensures that it uses the branch you were on | |
# | |
# Command: gh -cf production | |
# Result: same as last two, just combines flags | |
# | |
#-------------------------------------------------------------------- | |
# Deploying a topic/feature branch (multi-deploy version -- always merge-deploys from currently checked out branch) | |
#-------------------------------------------------------------------- | |
# | |
# Command: gha development | |
# Result: same as single-deploy version, but deploys all servers up to specified server. in this case, deploy to master and then development | |
# | |
# Command: gha -f staging | |
# Result: same as first, deploys all servers up to staging. the "-f" flag removes the confirmation prompt | |
# | |
#-------------------------------------------------------------------- | |
# Helper functions that can also be used | |
#-------------------------------------------------------------------- | |
# | |
# Command: gcp staging | |
# Result: pulls latest and then checks out specified branch | |
# | |
# Command: gpt production | |
# Result: deploys specified branch or the current branch if no arg is supplied | |
# | |
# Command: gmb 2012_09_01_new_core_feature | |
# Result: merges specified branch into current branch | |
# | |
#-------------------------------------------------------------------- | |
# Miscellaneous | |
#-------------------------------------------------------------------- | |
# | |
# Command: gls "scorm api" | |
# Result: searches and displays commits containing the search string | |
# | |
# Command: get_current_branch_name | |
# Result: displays current branch name | |
# ------------------------------------------------------------------- | |
# aliases | |
alias gls='git_log_grep' | |
alias gpt='git_push_trellis' | |
alias gdb='git_delete_branch' | |
alias gcb='git_create_branch' | |
alias gcbd='git_create_branch -d' | |
alias gcp='git_checkout_pull' | |
alias gmb='git_merge_branch' | |
alias gh='git_merge_push' | |
alias gha='git_merge_push_all' | |
function get_current_branch_name { | |
echo $(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') | |
} | |
function git_log_grep { | |
if [ -z "$1" ]; then | |
echo 'Please specify a search pattern.' | |
return | |
fi | |
git log --name-status --grep="$1" | |
} | |
function git_push_trellis { | |
branch=$1 | |
if [ -z "$branch" ]; then | |
branch=$(get_current_branch_name) | |
fi | |
case "$branch" in | |
"master" | "stable" ) | |
server='github' ;; | |
"development" ) | |
server='kim' ;; | |
"staging" ) | |
server='staging' ;; | |
"production" ) | |
server='web1' ;; | |
* ) | |
echo "Branch is invalid." | |
return | |
esac | |
git push $server $branch | |
} | |
function git_delete_branch { | |
branch=$1 | |
if [ -z "$branch" ]; then | |
branch=$(get_current_branch_name) | |
fi | |
echo '' | |
read -p "Delete branch '$branch'? [y]:" -n 1 | |
if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
echo -e "\nAbort!\n" | |
return | |
fi | |
echo '' | |
git checkout -f stable | |
echo '' | |
git branch -D $branch | |
} | |
function git_checkout_pull { | |
branch=$1 | |
if [ -z "$branch" ]; then | |
branch=$(get_current_branch_name) | |
else | |
git checkout $branch | |
fi | |
git pull github $branch | |
git status | |
} | |
function git_create_branch { | |
new_branch=$1 | |
if [ "$new_branch" = "-d" ]; then | |
new_branch="$(date +%Y_%m_%d)_$2" | |
fi | |
if [ -z "$new_branch" ]; then | |
echo 'Please specify a branch name.' | |
return | |
fi | |
git checkout stable | |
git pull github stable | |
git checkout -b $new_branch | |
} | |
function git_merge_branch { | |
branch=$1 | |
if [ -z "$branch" ]; then | |
if [ -z "$HENRY_GMB" ]; then | |
echo "Please specify a branch to merge." | |
return | |
fi | |
git merge $HENRY_GMB | |
return | |
fi | |
. <(echo "export HENRY_GMB=$branch") | |
git merge $HENRY_GMB | |
} | |
function git_merge_push { | |
local OPTIND | |
merge_branch=$HENRY_GMB | |
prompt=true | |
use_current_branch='' | |
need_shift='' | |
flag_error='' | |
# parse flags | |
while getopts ":cf" opt; do | |
case $opt in | |
c) | |
use_current_branch=true | |
need_shift=true | |
;; | |
f) | |
prompt='' | |
need_shift=true | |
;; | |
?) | |
echo "Invalid option: -$OPTARG" >&2 | |
flag_error=true | |
;; | |
esac | |
done | |
if [ "$need_shift" ]; then | |
shift | |
fi | |
if [ "$flag_error" ]; then | |
return | |
fi | |
if [ "$use_current_branch" -o "$merge_branch" = "" ]; then | |
merge_branch=$(get_current_branch_name) | |
fi | |
# make sure merge branch is not a main repository branch | |
case "$merge_branch" in | |
"master" | "development" | "staging" | "production" | "stable" ) | |
echo "You cannot merge-deploy from a main repository branch. Attempted to merge: $merge_branch" | |
return | |
;; | |
esac | |
# confirmation | |
if [ "$prompt" ]; then | |
echo '' | |
read -p "merge-push [$merge_branch] to [$1]? [y]:" -n 1 | |
if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
echo -e "\nAbort!" | |
return | |
fi | |
echo '' | |
fi | |
msg='Updating from Github...' | |
echo -e "\n\e[34m$msg\e[0m\n" | |
git_checkout_pull $1 | |
msg='Merging the feature branch...' | |
echo -e "\n\e[34m$msg\e[0m\n" | |
git_merge_branch $merge_branch | |
msg='Pushing to Github/Deploying to server...' | |
echo -e "\n\e[34m$msg\e[0m\n" | |
git_push_trellis $1 | |
git checkout $merge_branch | |
} | |
function git_merge_push_all { | |
local OPTIND | |
force='' | |
need_shift='' | |
flag_error='' | |
# parse flags | |
while getopts ":f" opt; do | |
case $opt in | |
f) | |
force='-f' | |
need_shift=true | |
;; | |
?) | |
echo "Invalid option: -$OPTARG" >&2 | |
flag_error=true | |
;; | |
esac | |
done | |
if [ "$need_shift" ]; then | |
shift | |
fi | |
if [ "$flag_error" ]; then | |
return | |
fi | |
merge_branch=$(get_current_branch_name) | |
branch_to=$1 | |
case "$merge_branch" in | |
"master" | "development" | "staging" | "production" | "stable" ) | |
echo -e "You cannot merge-deploy from a main repository branch. Attempted to merge: $merge_branch" | |
return | |
;; | |
esac | |
if [ -z "$branch_to" ]; then | |
#branch_to='stable' | |
echo -e "Please specify a branch to deploy up to." | |
return | |
fi | |
. <(echo "export HENRY_GMB=$merge_branch") | |
if [ "$branch_to" = "stable" -o "$branch_to" = "production" ]; then | |
git_merge_push $force master | |
git_merge_push $force development | |
git_merge_push $force staging | |
git_merge_push $force production | |
git_merge_push $force stable | |
fi | |
if [ "$branch_to" = "staging" ]; then | |
git_merge_push $force master | |
git_merge_push $force development | |
git_merge_push $force staging | |
fi | |
if [ "$branch_to" = "development" ]; then | |
git_merge_push master $force | |
git_merge_push development $force | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment