Skip to content

Instantly share code, notes, and snippets.

@dbechrd
Last active April 20, 2023 16:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbechrd/3a03d63d15ededa1f0360822b731838a to your computer and use it in GitHub Desktop.
Save dbechrd/3a03d63d15ededa1f0360822b731838a to your computer and use it in GitHub Desktop.
#!/bin/bash
DEBUG=0
VERBOSE=0
CMDS="args|b|br|co|db|nb|pb|pr|rbm|web"
BRANCH_PREFIX="dlb-"
BRANCH_URL_SAFE=""
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
BRANCH_URL_SAFE=`echo $BRANCH | tr -d "\n" | jq -sRr @uri`
echo "Using branch '$BRANCH' (url: $BRANCH_URL_SAFE)"
else
BRANCH=`git rev-parse --abbrev-ref HEAD`
BRANCH_URL_SAFE=`echo $BRANCH | tr -d "\n" | jq -sRr @uri`
echo "No branch specified, using current branch '$BRANCH' (url: $BRANCH_URL_SAFE)"
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])
local have_main=$(git branch --list main)
if [[ -n ${have_main} ]]; then
git switch main 2>/dev/null
else
git switch master 2>/dev/null
fi
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 $@
open "https://github.com/$REPO/compare/$BRANCH_URL_SAFE?quick_pull=1"
echo "[GitHub] Creating pull request for branch '$BRANCH' in '$REPO'"
}
# 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 --ff-only
echo "[Local] Switching back to branch '$BRANCH'"
git checkout $BRANCH
echo "[Local] Rebasing branch '$BRANCH' from master"
git rebase origin/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/tree/$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