|
# return whether directory is a git repository |
|
function _dir_is_git { |
|
if [ -d .git ] || git rev-parse --is-inside-work-tree > /dev/null 2>&1; then |
|
return 0 |
|
fi |
|
|
|
return 1 |
|
} |
|
|
|
# print current branch |
|
function _git_current_branch { |
|
if _dir_is_git; then |
|
echo $(git symbolic-ref HEAD | sed -e 's,refs/heads/\(.*\),\1,') |
|
|
|
return 0 |
|
fi |
|
|
|
return 1 |
|
} |
|
|
|
# merge current branch into $1 branch |
|
function gmto { |
|
# require branches |
|
if ! _dir_is_git || [ -z "$@" ]; then |
|
echo "Usage: gmto [branch]" |
|
|
|
return 1 |
|
fi |
|
|
|
# set branch to merge from as current |
|
from=$(_git_current_branch) |
|
|
|
# set branch to merge into |
|
to=$1 |
|
|
|
# merge branch |
|
echo "Merging '$from' into '$to'" |
|
{ |
|
# checkout and pull $1 branch |
|
git checkout $to |
|
git pull origin $to |
|
|
|
# merge current branch into $1 branch |
|
git merge --no-edit --no-ff $from |
|
} &> /dev/null |
|
|
|
# print status |
|
git status |
|
|
|
# confirm push |
|
read "R?Would you like to push any changes to origin? [y/n] " |
|
echo |
|
|
|
if [[ "$R" =~ ^[Yy] ]]; then |
|
# push changes |
|
git push origin $to |
|
fi |
|
|
|
return 0 |
|
} |
|
compdef _git gmto=git-merge |
|
|
|
# merge $1 branch into current branch |
|
function gmfr { |
|
# require branches |
|
if ! _dir_is_git || [ -z "$@" ]; then |
|
echo "Usage: gmfr [branch]" |
|
|
|
return 1 |
|
fi |
|
|
|
# set branch to merge to as current |
|
to=$(_git_current_branch) |
|
|
|
# set branch to merge from |
|
from=$1 |
|
|
|
echo "Merging '$from' into '$to'" |
|
{ |
|
# pull current branch |
|
git pull origin $to |
|
|
|
# merge $1 branch into current branch |
|
git merge --no-edit --no-ff $from |
|
} &> /dev/null |
|
|
|
# print status |
|
git status |
|
|
|
# confirm push |
|
read "R?Would you like to push any changes to origin? [y/n] " |
|
echo |
|
|
|
if [[ "$R" =~ ^[Yy] ]]; then |
|
# push changes |
|
git push origin $to |
|
fi |
|
} |
|
compdef _git gmto=git-merge |
|
|
|
# archive current branch |
|
function gar { |
|
# require branch |
|
if ! _dir_is_git; then |
|
echo "Usage: gar" |
|
|
|
return 1 |
|
fi |
|
|
|
# set branch as current |
|
branch=$(_git_current_branch) |
|
|
|
# confirm archival |
|
read "R?Would you like to archive '$branch'? [y/n] " |
|
echo |
|
|
|
if [[ "$R" =~ ^[Yy] ]]; then |
|
# checkout and pull master |
|
git checkout master |
|
git pull origin master |
|
|
|
# tag create archive branch from original branch |
|
git tag archive/$branch $branch |
|
|
|
# delete original branch |
|
git branch -d $branch |
|
|
|
# print status |
|
git status |
|
|
|
# confirm push |
|
read "R?Would you like to push any changes to origin? [y/n] " |
|
echo |
|
|
|
if [[ "$R" =~ ^[Yy] ]]; then |
|
# push changes |
|
git push origin archive/$branch |
|
git push origin --delete $branch |
|
fi |
|
fi |
|
} |
|
|
|
# rebase svn branch |
|
function gsre { |
|
# require branch |
|
if ! _dir_is_git; then |
|
echo "Usage: gsre" |
|
|
|
return 1 |
|
fi |
|
|
|
# confirm rebase |
|
read "R?Would you like to rebase 'svn'? [y/n] " |
|
echo |
|
|
|
if [[ "$R" =~ ^[Yy] ]]; then |
|
echo "Rebasing 'svn'..." |
|
{ |
|
# checkout and rebase svn |
|
git checkout svn |
|
git svn rebase |
|
} &> /dev/null |
|
|
|
# print status |
|
git status |
|
|
|
# confirm merge |
|
read -q "R?Would you like to merge 'svn' to 'master'? [y/n] " |
|
echo |
|
|
|
if [[ $R =~ ^[Yy]$ ]]; then |
|
echo "Merging 'svn' to 'master'..." |
|
{ |
|
# checkout and pull master |
|
git checkout master |
|
git pull origin master |
|
|
|
# merge svn |
|
git merge svn |
|
} &> /dev/null |
|
|
|
# print status |
|
git status |
|
|
|
# confirm push |
|
read -q "R?Would you like to push any changes to origin? [y/n] " |
|
echo |
|
|
|
if [[ $R =~ ^[Yy]$ ]]; then |
|
# push changes |
|
git push origin master |
|
fi |
|
fi |
|
fi |
|
} |