Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds plugin for git interactive checkout #12244

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions plugins/git-checkout-interactive/README.md
@@ -0,0 +1,15 @@
# lol
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be replaced with a proper title


This plugin adds quick switch between your branches.
adamsir marked this conversation as resolved.
Show resolved Hide resolved

To use it, add `git-checkout-interactive` to the plugins array in your `.zshrc` file:

```zsh
plugins=(... git-checkout-interactive)
```

## Usage Examples

```sh
gci
```
@@ -0,0 +1,57 @@
#######################################
# git checkout interactive #
#######################################

function git-checkout-interactive() {
local ITEMS_TO_SHOW=10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't necessarily need to be changed (it's not a problem, nor something I consider a significant problem), but why have a function-local constant as a variable if it can't be configured in some way?

# 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should arguably return a non-zero code, as it's an error state. Even then, why is this a loop at all? Both branches break, so there's no looping in any scenario

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this was supposed to be a continue and not a break, the echo -n on line 39 should be moved into the loop as well

fi
done
}

alias gci="git-checkout-interactive || return 0"