Skip to content

Commit a339901

Browse files
committed
fix: address reviewer comments
1 parent 7e50ba6 commit a339901

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

bin/git-forgit

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,24 +1003,31 @@ _forgit_ignore_clean() {
10031003
[[ -d "$FORGIT_GI_REPO_LOCAL" ]] && rm -rf "$FORGIT_GI_REPO_LOCAL"
10041004
}
10051005

1006+
_forgit_filter_existing_paths() {
1007+
while read -r path; do
1008+
[[ -d "$path" ]] && echo "$path"
1009+
done
1010+
}
1011+
10061012
_forgit_worktree_preview() {
10071013
local sha
10081014
# trailing space in grep to avoid matching worktrees with a common path
10091015
sha=$(git worktree list | grep "$1 " | awk '{print $2}')
1010-
# bare git-dir has no history
1011-
[[ "$sha" == "(bare)" ]] && return
1012-
_forgit_worktree_preview_git_opts=()
1013-
_forgit_parse_array _forgit_worktree_preview_git_opts "$FORGIT_WORKTREE_PREVIEW_GIT_OPTS"
1016+
if [[ "$sha" == "(bare)" ]]; then
1017+
printf "%b(bare)%b %s\n" '\e[0;33m' '\e[0m' 'No history for git dir'
1018+
return
1019+
fi
10141020
# the trailing '--' ensures that this works for branches that have a name
10151021
# that is identical to a file
1016-
git log "$sha" "${_forgit_worktree_preview_git_opts[@]}" --
1022+
git log "$sha" "${_forgit_log_preview_options[@]}" --
10171023
}
10181024

10191025
_forgit_worktree_jump() {
10201026
_forgit_inside_work_tree || _forgit_inside_git_dir || return 1
1021-
local count tree opts
1022-
count=$(git worktree list | wc -l)
1027+
local worktree_list count tree opts
1028+
worktree_list=$(git worktree list | grep -vE "prunable$" | awk '{print $1}' | _forgit_filter_existing_paths)
10231029

1030+
count=$(echo "$worktree_list" | wc -l)
10241031
[[ $count -eq 1 ]] && return 1
10251032

10261033
opts="
@@ -1030,7 +1037,7 @@ _forgit_worktree_jump() {
10301037
$FORGIT_WORKTREE_JUMP_FZF_OPTS
10311038
"
10321039

1033-
tree=$(git worktree list | awk '{print $1}' | FZF_DEFAULT_OPTS="$opts" fzf)
1040+
tree=$(echo "$worktree_list" | FZF_DEFAULT_OPTS="$opts" fzf)
10341041
[[ -z "$tree" ]] && return 1
10351042
echo "$tree"
10361043
}
@@ -1043,16 +1050,21 @@ _forgit_git_worktree_lock() {
10431050

10441051
_forgit_worktree_lock() {
10451052
_forgit_inside_work_tree || _forgit_inside_git_dir || return 1
1053+
if [[ $# -ne 0 ]]; then
1054+
git worktree lock "$@"
1055+
worktree_lock_status=$?
1056+
return $worktree_lock_status
1057+
fi
10461058
local tree opts
10471059

10481060
opts="
10491061
$FORGIT_FZF_DEFAULT_OPTS
1050-
+s -m --tiebreak=index
1062+
+s +m --tiebreak=index
10511063
--preview=\"$FORGIT worktree_preview {1}\"
10521064
$FORGIT_WORKTREE_LOCK_FZF_OPTS
10531065
"
10541066

1055-
tree=$(git worktree list | awk '{print $1}' | grep -v "(bare)" | FZF_DEFAULT_OPTS="$opts" fzf)
1067+
tree=$(git worktree list | grep -vE "\(bare\)|locked" | awk '{print $1}' | FZF_DEFAULT_OPTS="$opts" fzf)
10561068
[[ -z "$tree" ]] && return 1
10571069
_forgit_git_worktree_lock "$tree"
10581070
}
@@ -1065,32 +1077,50 @@ _forgit_git_worktree_remove() {
10651077

10661078
_forgit_worktree_remove() {
10671079
_forgit_inside_work_tree || _forgit_inside_git_dir || return 1
1068-
local tree opts
1080+
if [[ $# -ne 0 ]]; then
1081+
git worktree remove "$@"
1082+
worktree_remove_status=$?
1083+
return $worktree_remove_status
1084+
fi
1085+
local worktree_list tree opts
1086+
worktree_list=$(git worktree list | grep -v "(bare)")
1087+
1088+
count=$(echo "$worktree_list" | wc -l)
1089+
[[ $count -eq 1 ]] && return 1
10691090

10701091
opts="
10711092
$FORGIT_FZF_DEFAULT_OPTS
1072-
+s -m --tiebreak=index --header-lines=1
1093+
+s +m --tiebreak=index
10731094
--preview=\"$FORGIT worktree_preview {1}\"
10741095
$FORGIT_WORKTREE_REMOVE_FZF_OPTS
10751096
"
10761097

1077-
tree=$(git worktree list | awk '{print $1}' | grep -v "(bare)" | FZF_DEFAULT_OPTS="$opts" fzf)
1098+
tree=$(echo "$worktree_list" | awk '{print $1}' | FZF_DEFAULT_OPTS="$opts" fzf)
10781099
[[ -z "$tree" ]] && return 1
10791100
_forgit_git_worktree_remove "$tree"
10801101
}
10811102

10821103
_forgit_worktree_unlock() {
10831104
_forgit_inside_work_tree || _forgit_inside_git_dir || return 1
1084-
local tree opts
1105+
if [[ $# -ne 0 ]]; then
1106+
git worktree unlock "$@"
1107+
worktree_unlock_status=$?
1108+
return $worktree_unlock_status
1109+
fi
1110+
local worktree_list tree opts
1111+
1112+
worktree_list=$(git worktree list | grep -v "(bare)" | grep -E "locked$")
1113+
count=$(echo "$worktree_list" | wc -l)
1114+
[[ $count -eq 0 ]] && return 1
10851115

10861116
opts="
10871117
$FORGIT_FZF_DEFAULT_OPTS
1088-
+s -m --tiebreak=index --header-lines=1
1118+
+s +m --tiebreak=index
10891119
--preview=\"$FORGIT worktree_preview {1}\"
10901120
$FORGIT_WORKTREE_UNLOCK_FZF_OPTS
10911121
"
10921122

1093-
tree=$(git worktree list | awk '{print $1}' | grep -v "(bare)" | FZF_DEFAULT_OPTS="$opts" fzf)
1123+
tree=$(echo "$worktree_list" | awk '{print $1}' | FZF_DEFAULT_OPTS="$opts" fzf)
10941124
[[ -z "$tree" ]] && return 1
10951125
git worktree unlock "$tree"
10961126
}

0 commit comments

Comments
 (0)