Git Branch Helpers (ZSH)
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
# Git Helper: Search branch | |
# usage: qbranch keyword | |
# Example: git checkout $(qbranch branch-keyword) | |
# If keyword not | |
function qbranch() { | |
if [ -z "$1" ]; then | |
branches=`git branch` | |
else | |
branches=`git branch | grep $1` | |
fi | |
matchcount=`echo $branches | wc -l` | |
if [ "$matchcount" -gt "1" ] | |
then | |
echo "which one? ('q' to cancel)" | |
# Set internal field separator to newlines only | |
IFS=$'\n' | |
select d in `echo $branches` | |
do | |
if [ -z "$d" ]; then | |
targetbranch="" | |
return | |
fi | |
targetbranch=$d | |
break | |
done | |
else | |
targetbranch=$branches | |
fi | |
targetbranch=${targetbranch/* /} | |
echo "$targetbranch" | |
} | |
# Git Helper: Run git commands with branch search | |
# usage: qgit command [-args] [branch-keyword] | |
# If keyword not | |
function qgit() { | |
if [ -z "$2" ]; then | |
branch="" | |
# Only one arg | |
args=$1 | |
else | |
branch=${@: -1} | |
# Get args except last (branch) | |
args=${@: 1: -1} | |
fi | |
# Convert array to inline string | |
args=$(echo "$args" | tr '\n' ' ') | |
# Search the branch input | |
qbranch $branch | |
# Apply to command if a matching branch was found | |
if [ -n "$targetbranch" ]; then | |
command="git $args$targetbranch" | |
echo "$command" | |
eval "$command" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment