From 9b8ca2df1d7a3d1e7db4c7ccbe36e89d9b02bc57 Mon Sep 17 00:00:00 2001 From: ucpr Date: Tue, 20 Jan 2026 12:12:14 +0900 Subject: [PATCH] chore: git worktree alias --- zsh/plugins/alias_lazy.zsh | 1 + zsh/plugins/func_lazy.zsh | 54 ++++++++++++++++++-------------------- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/zsh/plugins/alias_lazy.zsh b/zsh/plugins/alias_lazy.zsh index 7663633..a327dc3 100644 --- a/zsh/plugins/alias_lazy.zsh +++ b/zsh/plugins/alias_lazy.zsh @@ -14,6 +14,7 @@ alias grep='grep --color=auto' alias fgrep='fgrep --color=auto' alias egrep='egrep --color=auto' alias date='gdate' +alias gw='git_wt_fzf' alias python='python3' diff --git a/zsh/plugins/func_lazy.zsh b/zsh/plugins/func_lazy.zsh index f1954fc..63a13fc 100644 --- a/zsh/plugins/func_lazy.zsh +++ b/zsh/plugins/func_lazy.zsh @@ -6,38 +6,34 @@ gignore() { curl -L -s "https://www.gitignore.io/api/$@" } -git-worktree-switcher() { - if ! git rev-parse --git-dir > /dev/null 2>&1; then - echo "Not in a git repository" - return 1 - fi +git_wt_fzf() { + local line target key - local worktrees=$(git worktree list | awk '{print $1}') - local selected=$(echo "$worktrees" | fzf --prompt="Select worktree (ESC to create new): " --header="Git Worktrees" --preview="git -C {} log --oneline -n 10" --preview-window=right:50%) + line="$(git wt | tail -n +2 | fzf --ansi \ + --prompt='git wt> ' \ + --header=$'ENTER: switch/create CTRL-D: delete(safe) CTRL-X: delete(force) CTRL-N: new\nESC: cancel' \ + --expect=enter,ctrl-d,ctrl-x,ctrl-n + )" || return - if [[ -n "$selected" ]]; then - cd "$selected" - else - echo -n "Enter branch name for new worktree: " - read branch_name - if [[ -z "$branch_name" ]]; then - echo "Branch name cannot be empty" - return 1 - fi + key="$(printf '%s\n' "$line" | head -n1)" + target="$(printf '%s\n' "$line" | tail -n1 | awk '{print $(NF-1)}')" - echo -n "Enter path for new worktree (default: ../$branch_name): " - read worktree_path - if [[ -z "$worktree_path" ]]; then - worktree_path="../$branch_name" - fi - - if git worktree add "$worktree_path" -b "$branch_name"; then - cd "$worktree_path" - else - echo "Failed to create worktree" - return 1 - fi - fi + case "$key" in + ctrl-n) + printf "new branch/worktree name: " + read -r target + [ -n "$target" ] && git wt "$target" + ;; + ctrl-d) + [ -n "$target" ] && git wt -d "$target" + ;; + ctrl-x) + [ -n "$target" ] && git wt -D "$target" + ;; + enter|"") + [ -n "$target" ] && git wt "$target" + ;; + esac } osascript-system-notify() { -- knotmirror 0.0.1