Skip to content

Commit 80e5066

Browse files
committed
fix: simplify logic by propagating arg handling
1 parent 4c09b2a commit 80e5066

File tree

1 file changed

+33
-42
lines changed

1 file changed

+33
-42
lines changed

bin/git-forgit

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ _forgit_previous_commit() {
6565
fi
6666
}
6767

68+
_forgit_all_non_flags() {
69+
while (("$#")); do
70+
case "$1" in
71+
-*)
72+
return 1
73+
;;
74+
esac
75+
shift
76+
done
77+
return 0
78+
}
79+
6880
_forgit_contains_non_flags() {
6981
while (("$#")); do
7082
case "$1" in
@@ -1216,6 +1228,16 @@ _forgit_worktree_select() {
12161228
echo "$tree"
12171229
}
12181230

1231+
_forgit_worktree_lock_max_args() {
1232+
local usage
1233+
usage=$(git worktree lock --help 2>/dev/null | grep -E 'usage: git worktree lock' | head -n1)
1234+
usage=${usage#*:}
1235+
rest=$(echo "$usage" | awk '{for(i=4;i<=NF;++i)printf "%s ",$i; print ""}')
1236+
# shellcheck disable=SC2206
1237+
local args=($rest)
1238+
echo "${#args[@]}"
1239+
}
1240+
12191241
_forgit_git_worktree_lock() {
12201242
_forgit_worktree_lock_git_opts=()
12211243
_forgit_parse_array _forgit_worktree_lock_git_opts "$FORGIT_WORKTREE_LOCK_GIT_OPTS"
@@ -1225,21 +1247,12 @@ _forgit_git_worktree_lock() {
12251247
_forgit_worktree_lock() {
12261248
_forgit_inside_work_tree || _forgit_inside_git_dir || return 1
12271249

1228-
local tree opts reason
1229-
if [[ $# -ne 0 ]]; then
1230-
if [[ "$1" == "--reason" ]]; then
1231-
if [[ -z "$2" ]]; then
1232-
_forgit_warn "Option \`--reason' requires a value"
1233-
return 1
1234-
fi
1235-
reason="$2"
1236-
shift 2
1237-
fi
1250+
local tree opts max_args
1251+
max_args=$(_forgit_worktree_lock_max_args)
12381252

1239-
if [[ $# -eq 0 ]] || _forgit_contains_non_flags "$@"; then
1240-
git worktree lock "$@" --reason "$reason"
1241-
return $?
1242-
fi
1253+
if [[ $# -ge "$max_args" ]] || { [[ $# -ne 0 ]] && _forgit_all_non_flags "$@"; }; then
1254+
git worktree lock "$@"
1255+
return $?
12431256
fi
12441257

12451258
opts="
@@ -1251,12 +1264,7 @@ _forgit_worktree_lock() {
12511264

12521265
tree=$(git worktree list | grep -vE "\(bare\)|locked" | awk '{print $1}' | FZF_DEFAULT_OPTS="$opts" fzf)
12531266
[[ -z "$tree" ]] && return 1
1254-
1255-
if [[ -z "$reason" ]]; then
1256-
_forgit_git_worktree_lock "$tree"
1257-
else
1258-
_forgit_git_worktree_lock "$tree" --reason "$reason"
1259-
fi
1267+
_forgit_git_worktree_lock "$tree" "$@"
12601268
}
12611269

12621270
_forgit_git_worktree_remove() {
@@ -1267,28 +1275,11 @@ _forgit_git_worktree_remove() {
12671275

12681276
_forgit_worktree_remove() {
12691277
_forgit_inside_work_tree || _forgit_inside_git_dir || return 1
1270-
local worktree_list tree opts force_flags
1271-
1272-
force_flags=()
1273-
1274-
while (("$#")); do
1275-
case "$1" in
1276-
-f | --force)
1277-
force_flags+=("$1")
1278-
shift
1279-
;;
1280-
-*) shift ;;
1281-
*)
1282-
tree="$1"
1283-
shift
1284-
;;
1285-
esac
1286-
done
1278+
local worktree_list tree opts
12871279

1288-
if [[ -n "$tree" ]]; then
1289-
_forgit_git_worktree_remove "${force_flags[@]}" "$tree"
1290-
worktree_remove_status=$?
1291-
return $worktree_remove_status
1280+
if [[ $# -ne 0 ]] && _forgit_contains_non_flags "$@"; then
1281+
git worktree remove "$@"
1282+
return $?
12921283
fi
12931284

12941285
worktree_list=$(git worktree list | grep -v "(bare)")
@@ -1305,7 +1296,7 @@ _forgit_worktree_remove() {
13051296

13061297
tree=$(echo "$worktree_list" | awk '{print $1}' | FZF_DEFAULT_OPTS="$opts" fzf)
13071298
[[ -z "$tree" ]] && return 1
1308-
_forgit_git_worktree_remove "${force_flags[@]}" "$tree"
1299+
_forgit_git_worktree_remove "$tree" "$@"
13091300
}
13101301

13111302
_forgit_worktree_unlock() {

0 commit comments

Comments
 (0)