summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaas Lalani <maas@lalani.dev>2022-08-15 13:57:43 -0400
committerMaas Lalani <maas@lalani.dev>2022-08-15 13:57:45 -0400
commit5f4cc48069905c9842416882ba8b4fbb044c2b7d (patch)
tree4f07470b2165216e9869bb7238adea50099638a5
parentbcfded82c87bcaa9e1d804e1d9efc66edd16f1fe (diff)
docs: add git-branch-manager.sh
From @andrew8088's video on Gum: https://www.youtube.com/watch?v=tnikefEuArQ
-rw-r--r--examples/git-branch-manager.sh64
1 files changed, 64 insertions, 0 deletions
diff --git a/examples/git-branch-manager.sh b/examples/git-branch-manager.sh
new file mode 100644
index 0000000..8b6e9bb
--- /dev/null
+++ b/examples/git-branch-manager.sh
@@ -0,0 +1,64 @@
+#! /bin/sh
+
+# This script is used to manage git branches such as delete, update, and rebase
+# them. It prompts the user to choose the branches and the action they want to
+# perform.
+#
+# For an explanation on the script and tutorial on how to create it, watch:
+# https://www.youtube.com/watch?v=tnikefEuArQ
+
+GIT_COLOR="#f14e32"
+
+git_color_text () {
+ gum style --foreground "$GIT_COLOR" "$1"
+}
+
+get_branches () {
+ if [ ${1+x} ]; then
+ gum choose --selected.foreground="$GIT_COLOR" --limit="$1" $(git branch --format="%(refname:short)")
+ else
+ gum choose --selected.foreground="$GIT_COLOR" --no-limit $(git branch --format="%(refname:short)")
+ fi
+}
+
+git rev-parse --git-dir > /dev/null 2>&1
+
+if [ $? -ne 0 ];
+then
+ echo "$(git_color_text "!!") Must be run in a $(git_color_text "git") repo"
+ exit 1
+fi
+
+gum style \
+ --border normal \
+ --margin "1" \
+ --padding "1" \
+ --border-foreground "$GIT_COLOR" \
+ "$(git_color_text ' Git') Branch Manager"
+
+echo "Choose $(git_color_text 'branches') to operate on:"
+branches=$(get_branches)
+
+echo ""
+echo "Choose a $(git_color_text "command"):"
+command=$(gum choose --cursor.foreground="$GIT_COLOR" rebase delete update)
+echo ""
+
+echo $branches | tr " " "\n" | while read -r branch
+do
+ case $command in
+ rebase)
+ base_branch=$(get_branches 1)
+ git fetch origin
+ git checkout "$branch"
+ git rebase "origin/$base_branch"
+ ;;
+ delete)
+ git branch -D "$branch"
+ ;;
+ update)
+ git checkout "$branch"
+ git pull --ff-only
+ ;;
+ esac
+done