Skip to content

Instantly share code, notes, and snippets.

@dbechrd dbechrd/mg
Created May 10, 2018

Embed
What would you like to do?
#!/bin/bash
DEBUG=0
VERBOSE=0
CMDS="args|b|br|co|db|nb|pb|pr|rbm|web"
BRANCH_PREFIX="dlb/"
REPO=`git remote -v | grep -m 1 "(push)" | sed -e "s/.*github.com[:/]\(.*\)\.git.*/\1/"`
_arg_branch_or_current() {
shift
if [ "$DEBUG" -eq 1 ]; then
echo "[Debug][_arg_branch_or_current]: \$0=$0,\$1=$1,\$2=$2,\$3=$3"
fi
if [ -n "$1" ]; then
BRANCH=$1
echo "Using branch '$BRANCH'"
else
BRANCH=`git name-rev --name-only HEAD`
echo "No branch specified, using current branch '$BRANCH'"
fi
}
_arg_branch_or_empty() {
shift
if [ "$DEBUG" -eq 1 ]; then
echo "[Debug][_arg_branch_or_empty]: \$0=$0,\$1=$1,\$2=$2,\$3=$3"
fi
if [ -n "$1" ]; then
BRANCH=$1
echo "Using branch '$BRANCH'"
else
BRANCH=
fi
}
_usage() {
echo "Usage: mg [-hv]
b
list local branches
br
list my remote branches
co [branch]
checkout existing branch
db [branch]
delete specified (or current) branch from local and remote
nb <branch>
create and checkout new branch
pb [branch]
push specified (or current) branch to remote
pr [branch]
initiate a pull request on specified (or current) branch
rbm [branch]
rebase master into specified (or current) branch
web
open current branch in GitHub" 1>&2;
exit 1
}
_version() {
echo "MagicGit v0.1 (dbechrd)"
exit 0
}
# DEBUG: This is for debugging argument passing, not useful in production
args() {
printf "%s" options:
while getopts a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:v:w:x:y:z: OPTION "$@"; do
printf "\n -%s '%s'" $OPTION $OPTARG
done
shift $((OPTIND - 1))
printf "\narg: '%s'" "$@"
echo
}
# List branches
b() {
git branch
}
# List branches
br() {
git branch --remote | grep $BRANCH_PREFIX
}
# Checkout existing branch
co() {
_arg_branch_or_current $@
echo "[Local] Checking out branch '$BRANCH'"
git checkout $BRANCH
}
# Delete branch from local and remote
db() {
_arg_branch_or_current $@
read -p "[Remote] Deleting branch '$BRANCH'. Proceed [y/N]: " yn
case $yn in
[Yy])
git push -d origin $BRANCH
;;
[Nn]*)
;;
esac
# Note: This will only delete the branch if detected as fully merged into
# master.
read -p "[Local] FORCE deleting branch '$BRANCH'. Proceed [y/N]: " yn
case $yn in
[Yy])
git branch -D $BRANCH
;;
[Nn]*)
;;
esac
}
# Create and checkout branch
nb() {
_arg_branch_or_empty $@
if [ "$BRANCH" == "" ]; then
echo "No branch name specified"
return 1
fi
echo "[Local] Creating branch '$BRANCH_PREFIX$BRANCH'"
git checkout -b $BRANCH_PREFIX$BRANCH
}
# Push the branch to remote
pb() {
_arg_branch_or_current $@
echo "[Remote] Pushing branch '$BRANCH' to [Remote] '$REPO'"
git push origin -u $BRANCH
}
# Initiate a pull request for the current branch
pr() {
_arg_branch_or_current $@
echo "[GitHub] Creating pull request for branch '$BRANCH' in '$REPO'"
open https://github.com/$REPO/pull/new/$BRANCH
}
# Rebase master from remote
rbm() {
# Alternatively, define an alias in your bashrc:
# alias rbm='git co master && git pull && git co - && git rebase master'
#local repo=`git remote -v | grep -m 1 "(push)" | sed -e "s/.*github.com[:/]\(.*\)\.git.*/\1/"`
#local branch=`git name-rev --name-only HEAD`
_arg_branch_or_current $@
echo "[Local] Checking out master"
git checkout master
echo "[Local] Pulling changes from [Remote] '$REPO'/master"
git pull
echo "[Local] Switching back to branch '$BRANCH'"
git checkout $BRANCH
echo "[Local] Rebasing branch '$BRANCH' from master"
git rebase master
}
# Open current branch in GitHub
web() {
_arg_branch_or_current $@
local giturl=$(git config --get remote.origin.url)
echo $giturl
if [ "$giturl" = "" ]; then
echo "Not a git repository or no remote.origin.url set"
exit 1
fi
giturl=${giturl/git\@github\.com\:/https://github.com/}
giturl=${giturl/\.git/\/tree/}
#local branch="$(git symbolic-ref HEAD 2>/dev/null)" || branch="(unnamed branch)"
#branch=${branch##refs/heads/}
giturl=$giturl$BRANCH
echo "[GitHub] Opening repo in web browser: '$giturl'."
open $giturl
}
default() {
if [ -z "${v}" ]; then
_version
elif [ -n "${v}" ]; then
_usage
fi
}
# Note: Use "x:" to require option; "x" to not require option; and
# leading ":" to silence option errors.
while getopts "dhv" opt; do
case "${opt}" in
d)
DEBUG=1
;;
h)
_help
;;
v)
#v=${OPTARG}
_version
;;
*)
_usage
;;
esac
done
# DEBUG: Print command line args
if [ "$DEBUG" -eq 1 ]; then
echo "[Debug][ cmd_line_args] \$1=$1, \$2=$2, \$3=$3"
fi
shift $((OPTIND-1))
# DEBUG: Print command line args after switches have been eaten
if [ "$DEBUG" -eq 1 ]; then
echo "[Debug][post_switch_args] \$1=$1, \$2=$2, \$3=$3"
fi
if [[ "$1" == "help" ]]; then
_usage
elif [[ $1 =~ ^($CMDS)$ ]]; then
if [[ $VERBOSE -eq 1 ]]; then
echo "Executing [$1]"
fi
"$1" "$@"
else
echo "Invalid command '$1'" >&2
echo "'`basename $0` help' for help"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.