Skip to content

Commit

Permalink
fix: address reviewer comments
Browse files Browse the repository at this point in the history
  • Loading branch information
suft committed Nov 14, 2024
1 parent 7e50ba6 commit b680b3e
Showing 1 changed file with 45 additions and 13 deletions.
58 changes: 45 additions & 13 deletions bin/git-forgit
Original file line number Diff line number Diff line change
Expand Up @@ -1003,12 +1003,20 @@ _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
if [[ "$sha" == "(bare)" ]]; then
printf "%b(bare)%b %s\n" '\e[0;33m' '\e[0m' 'No history for git dir'
return
fi
_forgit_worktree_preview_git_opts=()
_forgit_parse_array _forgit_worktree_preview_git_opts "$FORGIT_WORKTREE_PREVIEW_GIT_OPTS"
# the trailing '--' ensures that this works for branches that have a name
Expand All @@ -1018,9 +1026,10 @@ _forgit_worktree_preview() {

_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="
Expand All @@ -1030,7 +1039,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"
}
Expand All @@ -1043,16 +1052,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"
}
Expand All @@ -1065,32 +1079,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 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_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"
}
Expand Down

0 comments on commit b680b3e

Please sign in to comment.