diff --git a/bin/git-forgit b/bin/git-forgit index 5b8c7213..775bbb4d 100755 --- a/bin/git-forgit +++ b/bin/git-forgit @@ -1003,24 +1003,31 @@ _forgit_ignore_clean() { [[ -d "$FORGIT_GI_REPO_LOCAL" ]] && rm -rf "$FORGIT_GI_REPO_LOCAL" } +_forgit_filter_existing_paths() { + while read -r path; do + [[ -d "$path" ]] && echo "$path" + done +} + _forgit_worktree_preview() { local sha # trailing space in grep to avoid matching worktrees with a common path sha=$(git worktree list | grep "$1 " | awk '{print $2}') - # bare git-dir has no history - [[ "$sha" == "(bare)" ]] && return - _forgit_worktree_preview_git_opts=() - _forgit_parse_array _forgit_worktree_preview_git_opts "$FORGIT_WORKTREE_PREVIEW_GIT_OPTS" + if [[ "$sha" == "(bare)" ]]; then + printf "%b(bare)%b %s\n" '\e[0;33m' '\e[0m' 'No history for git dir' + return + fi # the trailing '--' ensures that this works for branches that have a name # that is identical to a file - git log "$sha" "${_forgit_worktree_preview_git_opts[@]}" -- + git log "$sha" "${_forgit_log_preview_options[@]}" -- } _forgit_worktree_jump() { _forgit_inside_work_tree || _forgit_inside_git_dir || return 1 - local count tree opts - count=$(git worktree list | wc -l) + local worktree_list count tree opts + worktree_list=$(git worktree list | grep -vE "prunable$" | awk '{print $1}' | _forgit_filter_existing_paths) + count=$(echo "$worktree_list" | wc -l) [[ $count -eq 1 ]] && return 1 opts=" @@ -1030,7 +1037,7 @@ _forgit_worktree_jump() { $FORGIT_WORKTREE_JUMP_FZF_OPTS " - tree=$(git worktree list | awk '{print $1}' | FZF_DEFAULT_OPTS="$opts" fzf) + tree=$(echo "$worktree_list" | FZF_DEFAULT_OPTS="$opts" fzf) [[ -z "$tree" ]] && return 1 echo "$tree" } @@ -1043,16 +1050,21 @@ _forgit_git_worktree_lock() { _forgit_worktree_lock() { _forgit_inside_work_tree || _forgit_inside_git_dir || return 1 + if [[ $# -ne 0 ]]; then + git worktree lock "$@" + worktree_lock_status=$? + return $worktree_lock_status + fi local tree opts opts=" $FORGIT_FZF_DEFAULT_OPTS - +s -m --tiebreak=index + +s +m --tiebreak=index --preview=\"$FORGIT worktree_preview {1}\" $FORGIT_WORKTREE_LOCK_FZF_OPTS " - tree=$(git worktree list | awk '{print $1}' | grep -v "(bare)" | FZF_DEFAULT_OPTS="$opts" fzf) + tree=$(git worktree list | grep -vE "\(bare\)|locked" | awk '{print $1}' | FZF_DEFAULT_OPTS="$opts" fzf) [[ -z "$tree" ]] && return 1 _forgit_git_worktree_lock "$tree" } @@ -1065,32 +1077,50 @@ _forgit_git_worktree_remove() { _forgit_worktree_remove() { _forgit_inside_work_tree || _forgit_inside_git_dir || return 1 - local tree opts + if [[ $# -ne 0 ]]; then + git worktree remove "$@" + worktree_remove_status=$? + return $worktree_remove_status + fi + local worktree_list tree opts + worktree_list=$(git worktree list | grep -v "(bare)") + + count=$(echo "$worktree_list" | wc -l) + [[ $count -eq 1 ]] && return 1 opts=" $FORGIT_FZF_DEFAULT_OPTS - +s -m --tiebreak=index --header-lines=1 + +s +m --tiebreak=index --preview=\"$FORGIT worktree_preview {1}\" $FORGIT_WORKTREE_REMOVE_FZF_OPTS " - tree=$(git worktree list | awk '{print $1}' | grep -v "(bare)" | FZF_DEFAULT_OPTS="$opts" fzf) + tree=$(echo "$worktree_list" | awk '{print $1}' | FZF_DEFAULT_OPTS="$opts" fzf) [[ -z "$tree" ]] && return 1 _forgit_git_worktree_remove "$tree" } _forgit_worktree_unlock() { _forgit_inside_work_tree || _forgit_inside_git_dir || return 1 - local tree opts + if [[ $# -ne 0 ]]; then + git worktree unlock "$@" + worktree_unlock_status=$? + return $worktree_unlock_status + fi + local worktree_list tree opts + + worktree_list=$(git worktree list | grep -v "(bare)" | grep -E "locked$") + count=$(echo "$worktree_list" | wc -l) + [[ $count -eq 0 ]] && return 1 opts=" $FORGIT_FZF_DEFAULT_OPTS - +s -m --tiebreak=index --header-lines=1 + +s +m --tiebreak=index --preview=\"$FORGIT worktree_preview {1}\" $FORGIT_WORKTREE_UNLOCK_FZF_OPTS " - tree=$(git worktree list | awk '{print $1}' | grep -v "(bare)" | FZF_DEFAULT_OPTS="$opts" fzf) + tree=$(echo "$worktree_list" | awk '{print $1}' | FZF_DEFAULT_OPTS="$opts" fzf) [[ -z "$tree" ]] && return 1 git worktree unlock "$tree" }