Write a zsh alias which uses gum to implement a functionality which lists my local git branches one by one which does not have tracking remote branch and asks me to delete it. I can either click on yes or no it should keep showing me the next in list. If yes, delete the branch, no then skip that branch and show next one
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Certainly! Below is a zsh alias that uses gum to list local Git branches without a tracking remote branch and asks you whether to delete each one. If you choose "yes," it deletes the branch; if "no," it moves to the next branch.
First, ensure you have gum installed. You can install it using the following command:
sh
brew install gum
Then, add the following alias to your .zshrc file:
sh
alias delete-untracked-branches='f() {
# Get list of branches without a tracking remote branch
local branches=($(git branch --no-color | grep -v "\\[*\\]" | grep -v "$(git branch --no-color | grep "\\*" | cut -d " " -f2)"))
for branch in $branches; do
local has_remote=$(git rev-parse --abbrev-ref --symbolic-full-name "${branch}@{upstream}" 2>/dev/null)
if [[ -z $has_remote ]]; then
local choice=$(gum choose "Yes" "No" "Quit" --prompt "Delete branch $branch?")
case $choice in
"Yes")
git branch -D $branch
echo "Deleted branch $branch"
;;
"No")
echo "Skipped branch $branch"
;;
"Quit")
echo "Quitting..."
break
;;
esac
fi
done
}; f'
Explanation:
Function Definition: The alias defines a function f that will perform the required operations.
Get Branches: It fetches the list of local branches and filters out the current branch and any branches that have a tracking remote.
Loop Through Branches: For each branch, it checks if it has a tracking remote. If not, it uses gum to prompt whether to delete the branch.
User Choice: Depending on the user’s choice ("Yes," "No," or "Quit"), it either deletes the branch, skips it, or exits the loop.
Usage:
Simply run the alias in your terminal:
sh
delete-untracked-branches
This will start the process of listing and prompting for deletion of branches without a tracking remote.