Last active
November 21, 2022 20:58
-
-
Save NerdyDeedsLLC/0d7411bade07ed6059a01881573f60e1 to your computer and use it in GitHub Desktop.
BASH CLI utility that presents your branch history, paginated, in descending order, along with a menu interface for auto selecting the one you're after.
This file contains hidden or 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
#!/bin/bash | |
LOADTEXT=" | |
╔═══════════════════════════════════════════════════════════════════════════════╗ | |
║ ║ | |
║ Loading BackInGIT.sh v.0.2... ✓ DONE ║ | |
║ Can't remember which branch you were on 20 minutes ago? Having to jump from ║ | |
║ one to the next like a coked-up kangaroo wearing moonshoes in a trampoline ║ | |
║ factory? Just type bk for an interactive, date-stamped guide to your history ║ | |
║ SYNTAX: bk <optionalPageSize> then follow the on-screen instructions ║ | |
║ ║ | |
╚═══════════════《 𝙃𝙚𝙡𝙥/𝙈𝙤𝙧𝙚 𝙄𝙣𝙛𝙤: unavailable at this time 》═════════════════════𝕁 | |
" && printf "$LOADTEXT" | |
function BackInGIT(){ | |
START=1 | |
[[ "$1" != "" ]] && END=$(awk "BEGIN {print $1 * -1}") || END=25 | |
export PAGESIZE=$END | |
export PAGE=1 | |
while : | |
do | |
clear | |
# echo "$START$END" | |
echo -e "OPT BRANCH LAST_ACCESSED\n$(git reflog show --date=iso --all -v | grep -v rebase | grep -n checkout | grep -vE "\S{40} to [a-z0-9]{7}$" | sed "s/.*[{]\(.*\)[}].* to \(.*\)/ \2 \1/" | grep . -n)" | head -n $END | tail -n $PAGESIZE | column -t -c 2 | |
printf "\n ▲ ╔═══╦═══╦═══╗\n" | |
printf " ▲ ║ 7 ║ 8 ║ 9 ║ To SELECT/SWITCH to one of the branches\n" | |
printf " ◤ ╠═══╬═══╬═══╣ shown in the list above, type the OPT\n" | |
printf " ◤ ◀︎ ◀︎║ 4 ║ 5 ║ 6 ║ number listed alongside it, and press\n" | |
printf " ╠═══╬═══╬═══╣ ┌───────────┐\n" | |
printf " ║ 1 ║ 2 ║ 3 ║ │ ENTER │\n" | |
printf " ╚═══╩═══╩═══╝ └───────────┘\n\n" | |
printf " ┌─────┐ GoTo ┌─────┐ GoTo ┌─────┐ GoTo ┌─────┐ \n" | |
printf " │ 0 │ First │ ╺━╸ │ Prev │ ╺╋╸ │ Next │ 𝐐 │ Quit\n" | |
printf " └─────┘ Page └─────┘ Page └─────┘ Page └─────┘\n\n" | |
read -p "[0-9] [-] [+] [Q] Selection? >" PREVBRANCH | |
case $PREVBRANCH in | |
'q'|'Q') echo "Quitting..." | |
break;; | |
'0') | |
PAGE=1 | |
END=$PAGESIZE;; | |
'+') | |
PAGE=$(expr $PAGE + 1) | |
END=$(awk "BEGIN {print $PAGE * $PAGESIZE}");; | |
'-') if [[ $PAGE -gt 1 ]]; then | |
PAGE=$(expr $PAGE - 1) | |
END=$(awk "BEGIN {print $PAGE * $PAGESIZE}") | |
fi | |
;; | |
*) newBranch="$(git reflog show --date=short --all -v | grep -v rebase | grep -n checkout | grep -vE "\S{40} to [a-z0-9]{7}$" | sed "s/.* to \(.*\)/\1/" | sed "s/.* to //g" | head -$PREVBRANCH | tail -1)"; | |
if [[ "$(git branch -l | grep $newBranch)" != "" ]]; then | |
echo -e "\n Switching to branch $newBranch... (COMMAND: git checkout $newBranch)..." | |
git checkout "$newBranch" | |
echo -e "DONE.\n\n" | |
else | |
echo "Cannot load branch $newBranch. It appears to be missing from the local machine." | |
fi | |
break;; | |
esac | |
done | |
} | |
alias 'bk'="BackInGIT $@" |
Author
NerdyDeedsLLC
commented
Mar 30, 2022
Beautiful! I love seeing the classics sed and awk making an appearance
@ptdecker You know me... and with as obsessive as I am about constantly breaking out feature branches (and with my needing to constantly flip between 'em to offer support to the JD's) my bobulated was getting perpetually discom'd. Like a LOT. This is great because it's SEQUENTIAL and will just keep on spanning backwards ad infinitum. I may not recall which branch I was on 45 minutes ago, but I DO know it was "the one two before this current one."
Syntax updated for BASH 5.2.9
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment