1
0
Fork 0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2024-10-16 11:40:46 +00:00
This commit is contained in:
Adam Šír 2024-09-23 17:33:04 +02:00 committed by GitHub
commit 3e62c1eab0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 72 additions and 0 deletions

View file

@ -0,0 +1,15 @@
# lol
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:
```zsh
plugins=(... git-checkout-interactive)
```
## Usage Examples
```sh
gci
```

View file

@ -0,0 +1,57 @@
#######################################
# git checkout interactive #
#######################################
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 --count="$ITEMS_TO_SHOW" --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"