mirror of https://git.sr.ht/~skiqqy/bin
parent
ca4793dc82
commit
3cec787b46
@ -0,0 +1,71 @@ |
||||
#!/usr/bin/env bash |
||||
# Branch Stack, think pushd + popd, but for git branches |
||||
# Author: Skiqqy |
||||
|
||||
# Recommended Usage: |
||||
# Add the below aliases, behavior is identical to pushd and popd |
||||
# alias pushb='bs push' # pushd |
||||
# alias popb='bs pop' popd |
||||
# alias birs='bs' # dirs |
||||
|
||||
# Print top of stack, and remove |
||||
# Usage: pop |
||||
pop() |
||||
{ |
||||
tail -n 1 "$stack" |
||||
head -n $(($(wc -l "$stack" | cut -d " " -f 1)-1)) "$stack" > "$stack.tmp" |
||||
mv "$stack.tmp" "$stack" |
||||
} |
||||
|
||||
# Push NAME to top of stack |
||||
# Usage: push NAME |
||||
push() |
||||
{ |
||||
[ -z "$1" ] && return 1 |
||||
echo "$1" >> "$stack" |
||||
} |
||||
|
||||
main() |
||||
{ |
||||
stack=/tmp/bs"${PWD//\//.}".stack # Each repository gets its own stack |
||||
cbranch=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') |
||||
[ -z "$cbranch" ] && echo 'ERR: Not inside a repository' && return 1 |
||||
|
||||
case "$1" in |
||||
--help|help|-h) |
||||
cat << EOF |
||||
|
||||
$(basename "$0") ~ pushd/popd for git branches |
||||
|
||||
Usage: $ $(basename "$0") [--help] push|pop [NAME] |
||||
|
||||
EOF |
||||
;; |
||||
push) |
||||
[ -z "$2" ] && echo "Usage: $ $(basename "$0") push NAME" && return 1 |
||||
git checkout "$2" || return 1 # pushd |
||||
push "$cbranch" |
||||
;; |
||||
pop) |
||||
cbranch=$(pop "$cbranch") |
||||
|
||||
if [ -z "$cbranch" ] |
||||
then |
||||
echo "ERR: The stack is empty" |
||||
return 1 |
||||
fi |
||||
|
||||
git checkout "$cbranch" |
||||
;; |
||||
*) |
||||
if [ "$(wc -l /tmp/bs.home.skippy.bin.stack | cut -d " " -f 1)" -eq 0 ] |
||||
then |
||||
echo "Stack is empty..." |
||||
else |
||||
tac "$stack" # print the stack |
||||
fi |
||||
;; |
||||
esac |
||||
} |
||||
|
||||
main "$@" |
Loading…
Reference in new issue