From 09976595be77a4d9871caba8fbcb30b6cf0c5be2 Mon Sep 17 00:00:00 2001 From: Adam Sir <donuteeze@gmail.com> Date: Wed, 1 Nov 2023 12:37:17 +0100 Subject: [PATCH 1/3] feat(plugin): git checkout interactive --- plugins/git-checkout-interactive/README.md | 15 +++++ .../git-checkout-interactive.plugin.zsh | 56 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 plugins/git-checkout-interactive/README.md create mode 100644 plugins/git-checkout-interactive/git-checkout-interactive.plugin.zsh diff --git a/plugins/git-checkout-interactive/README.md b/plugins/git-checkout-interactive/README.md new file mode 100644 index 000000000..845d4c8bf --- /dev/null +++ b/plugins/git-checkout-interactive/README.md @@ -0,0 +1,15 @@ +# lol + +This plugin adds quick switch between your branches. + +To use it, add `git-checkout-interactive` to the plugins array in your `.zshrc` file: + +```zsh +plugins=(... git-checkout-interactive) +``` + +## Usage Examples + +```sh +gci +``` diff --git a/plugins/git-checkout-interactive/git-checkout-interactive.plugin.zsh b/plugins/git-checkout-interactive/git-checkout-interactive.plugin.zsh new file mode 100644 index 000000000..9d99ce71c --- /dev/null +++ b/plugins/git-checkout-interactive/git-checkout-interactive.plugin.zsh @@ -0,0 +1,56 @@ +####################################### +# git checkout interactive # +####################################### + +function git-checkout-interactive() { + # Get all branches sorted by committer date, along with their last commit hash + local branches + branches=$(git for-each-ref --sort=-committerdate --format='%(refname:short) %(objectname:short)' refs/heads/) + + # Parse branches + local branch_list=() + local current_branch + current_branch=$(git rev-parse --abbrev-ref HEAD) + if [[ "$current_branch" == "" ]]; then + return 0 + fi + while read -r branch hash; do + if [[ "$branch" == "$current_branch" ]]; then + echo "On branch $branch \n" + else + branch_list+=("$branch ($hash)") + fi + done <<< "$branches" + + if (( ${#branch_list} == 0 )); then + echo "No other branches available." + return 0 + else + echo "Select a branch to switch to:\n" + fi + + # Display menu + local i=1 + for branch in "${branch_list[@]}"; do + echo "($i) $branch" + ((i++)) + done + echo -n "\nPlease enter your choice: " + + # Handle user input + while :; do + local choice + read -r choice + if (( choice > 0 && choice <= ${#branch_list[@]} )); then + local selected_branch="${branch_list[$((choice))]}" + local target_branch="${selected_branch//->}" + target_branch="${target_branch%% *}" + git checkout "$target_branch" + break + else + break + fi + done +} + +alias gci="git-checkout-interactive || return 0" From 7ab8271cd9bb80d2f928436faa60ac7bfc9d2b9d Mon Sep 17 00:00:00 2001 From: Adam Sir <donuteeze@gmail.com> Date: Wed, 28 Feb 2024 11:28:36 +0100 Subject: [PATCH 2/3] limit output to 10 last branches; configurable --- .../git-checkout-interactive.plugin.zsh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/git-checkout-interactive/git-checkout-interactive.plugin.zsh b/plugins/git-checkout-interactive/git-checkout-interactive.plugin.zsh index 9d99ce71c..58ff6bb26 100644 --- a/plugins/git-checkout-interactive/git-checkout-interactive.plugin.zsh +++ b/plugins/git-checkout-interactive/git-checkout-interactive.plugin.zsh @@ -3,9 +3,10 @@ ####################################### function git-checkout-interactive() { + local ITEMS_TO_SHOW=10 # Get all branches sorted by committer date, along with their last commit hash local branches - branches=$(git for-each-ref --sort=-committerdate --format='%(refname:short) %(objectname:short)' refs/heads/) + branches=$(git for-each-ref --count="$ITEMS_TO_SHOW" --sort=-committerdate --format='%(refname:short) %(objectname:short)' refs/heads/) # Parse branches local branch_list=() From 2cf9ad072d676950eaf36a6881a7383e4d2be837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0=C3=ADr?= <5871877+adamsir@users.noreply.github.com> Date: Thu, 9 May 2024 12:07:06 +0200 Subject: [PATCH 3/3] Update plugins/git-checkout-interactive/README.md Co-authored-by: Olivia (Zoe) <zoe.i2k1@gmail.com> --- plugins/git-checkout-interactive/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/git-checkout-interactive/README.md b/plugins/git-checkout-interactive/README.md index 845d4c8bf..827ec6e2c 100644 --- a/plugins/git-checkout-interactive/README.md +++ b/plugins/git-checkout-interactive/README.md @@ -1,6 +1,6 @@ # lol -This plugin adds quick switch between your branches. +This plugin adds a command that lets you interactively switch between branches. To use it, add `git-checkout-interactive` to the plugins array in your `.zshrc` file: