The benefit engineers can get from the Unix (-like) OS is you can exploit its bash commands and automate tedious and complicated operations.
The vim /emacs user customize their editor and extend it so as to make those operations pretty quicker, and today I would like to introduce you how to navigate to/fro between git branches of your repository.
On IDE, or any other GUI based systems probably you can navigate objects with right arrow key / left arrow key or mouses, but command line user, it is nasty shit.
#!/usr/bin/env bash#
# git walk
#
brs=`git branch`
cur=`git branch --show-current`
np=0
cache=''option=$1if [[ $option != '-p' && $option != '-n' ]]; then
cat << EOFgw -p ... go to previous branch
gw -n ... go to next branchEOF
exit
fii=0
first=''
last=''
for b in $brs; do
if [[ $b == */* || $b == 'develop' || $b == 'master' ]];then
if [ $i == 0 ]; then
first=$b
fi
if [ $np == 1 ]; then
git checkout $b
exit
fi
if [ $cur == $b ]; then
if [ $option == '-p' ]; then
if [ $cur != $first ]; then
git checkout $cache
exit
else
git checkout `git branch | tail -n 1`
exit
fi
else # next
np=1
fi
fi
cache=$b
i=$((i+1))
fi
doneif [ $option == '-n' ]; then
git checkout $first
fi
Pretty procedural and you see nested if else clauses but I don’t care. It’s merely a script and seems not the volume of the code increases. You can call this via macro of Vim via vimscript or emacs lisp.
also, since adding `-n -p` is not that fancy job such that you make alias:
echo 'gw -p' >> /usr/local/bin/gwp; chmod +x /usr/local/bin/gwp
echo 'gw -n' >> /usr/local/bin/gwn; chmod +x /usr/local/bin/gwn
This makes you easily swap between your branches in the repository, once it reached the last brach since it’s circular that it is going to be first branch or vice versa.
Yeah, that’s it.
No comments:
Post a Comment