Skip to content

Instantly share code, notes, and snippets.

@Iranon
Created October 11, 2022 15:42
Show Gist options
  • Save Iranon/684e0175d5d71ca1778f7d7504202010 to your computer and use it in GitHub Desktop.
Save Iranon/684e0175d5d71ca1778f7d7504202010 to your computer and use it in GitHub Desktop.
A Gum based Git tool
#!/bin/sh
# GiGomma
# LICENSE: GPL-3.0-or-later [https://spdx.org/licenses/GPL-3.0-or-later.html]
#| Matteo Vinci [Iranon] (c) 2022 |
#NOTE: Gum needed [https://github.com/charmbracelet/gum]
#- Colors
PRIMARY="#ffcdb2"
SECONDARY="#ffadad"
SELECTION="#dddf00"
PROMPT="#34a0a4"
MATCH="#ccff33"
WARN="#e56b6f"
#- Flags
_f="-f" #)fuzzy search branches
_h="-h" #)print help
#- Functions
set_color_text() {
gum style --foreground "$1" "$2"
}
get_branches() {
git branch --format="%(refname:short)"
}
move_to() {
echo;
if [ $# -gt 0 -a "$1" = "$_f" ]; then
git checkout $(get_branches | gum filter \
--indicator.foreground $SELECTION \
--match.foreground $MATCH \
--prompt.foreground $PROMPT \
--limit 1)
else
git checkout $(gum choose --cursor.foreground $SELECTION --limit 1 $(get_branches))
fi
}
show_title() {
echo;
gum style \
--border rounded \
--margin "0" \
--padding "0 1" \
--border-foreground $SECONDARY \
"$(set_color_text $PRIMARY 'GiGomma')"
}
print_help() {
echo \
">Call the script with no arguments to simply move between branches.\n"\
"\n"\
">Use the -f flag to fuzzy search between branches and then move.\n"\
| gum format
exit 0;
}
main() {
if [ $# -eq 1 -a "$1" = "$_h" ]; then
show_title
print_help
fi
git rev-parse --is-inside-work-tree > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo; set_color_text $WARN "_> ERROR: not a Git repository"; echo;
exit 1;
fi
if [ $# -gt 0 ]; then
case "$1" in
"$_f")
move_to "$_f";;
*)
print_help;;
esac
else
move_to
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment