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
base: master
Are you sure you want to change the base?
Changes from 2 commits
0997659
7ab8271
2cf9ad0
1b47033
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# lol | ||
|
||
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 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
####################################### | ||
# git checkout interactive # | ||
####################################### | ||
|
||
function git-checkout-interactive() { | ||
local ITEMS_TO_SHOW=10 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this was supposed to be a |
||
fi | ||
done | ||
} | ||
|
||
alias gci="git-checkout-interactive || return 0" |
There was a problem hiding this comment.
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