forked from yankeexe/git-worktree-switcher
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwt
More file actions
executable file
·223 lines (193 loc) · 5.13 KB
/
wt
File metadata and controls
executable file
·223 lines (193 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env bash
set -e
# Switch between git worktrees with speed.
args=("$@")
VERSION="0.2.8-fork"
TMP_PATH=$(mktemp)
BINARY_PATH=$(realpath "$0")
VERSION_CACHE_FILE=${XDG_CACHE_HOME:-$HOME/.cache}/wt_latest_update_check.json
JQ_URL="https://stedolan.github.io/jq/download"
RELEASE_URL="https://github.com/mateusauler/git-worktree-switcher/releases/latest"
RELEASE_API_URL="https://api.github.com/repos/mateusauler/git-worktree-switcher/releases/latest"
CHANGE_DIRECTORY_PREFIX="changedir:"
# Escape forward slash
arg=${args[0]/\//\\\/}
is_interactive=false
for arg in "${args[@]}"; do
if [[ "$arg" == "-i" ]]; then
if command -v fzf &> /dev/null
then is_interactive=true
else
echo "Error: fzf is not installed for interactive mode"
echo "Install from: https://github.com/junegunn/fzf#installation"
exit 1
fi
break
fi
done
worktree_list_names() {
if git rev-parse --git-dir &> /dev/null; then
git worktree list --porcelain \
| awk '/^worktree / { sub("worktree ", ""); print; }' \
| tr \\n \\0 \
| xargs -0 -L 1 basename
fi
}
help_message() {
echo -e "wt lets you switch between your git worktrees with speed.\n"
echo "Usage:"
echo -e "\twt: go to the main worktree"
echo -e "\twt <worktree-name>: search for worktree names and change to that directory."
echo -e "\twt -i: interactively select a worktree using fzf."
echo -e "\twt list: list out all the git worktrees."
echo -e "\twt names: list out only the git worktree names."
echo -e "\twt update: update to the latest release of worktree switcher."
echo -e "\twt version: show the CLI version."
echo -e "\twt init <shell>: print the init script for <shell>."
echo -e "\twt help: shows this help message."
}
download_latest_update() {
download_url=$(curl -sL $RELEASE_API_URL | jq -r '.assets[0].browser_download_url')
echo "Downloading latest version $fetched_tag_name"
curl -sL -o "$TMP_PATH" "$download_url"
echo "Updating to latest version..."
chmod +x "$TMP_PATH"
sudo mv "$TMP_PATH" "$BINARY_PATH"
rm -f "$TMP_PATH"
echo "You are using the latest version of worktree switcher: $fetched_tag_name"
}
check_release_version() {
fetched_tag_name=$(check_and_cache_update)
if [ "$fetched_tag_name" == $VERSION ]; then
echo "You have the latest version of worktree switcher!"
echo "Version: $VERSION"
else
download_latest_update
fi
}
update() {
if [ -z "$(command -v jq)" ]; then
echo "jq is required for updating worktree switcher via this command."
echo -e "Install jq:\n$JQ_URL.\n"
echo -e "Or visit:\n$RELEASE_URL"
else
check_release_version
fi
}
auto_check_update() {
show_updates() {
if [[ "$1" != "$VERSION" ]]; then
echo "Version $1 available! Run 'wt update' to update." 1>&2
echo "Currently running version $VERSION." 1>&2
fi
}
if [[ -f "$VERSION_CACHE_FILE" ]]; then
check_timestamp=$(jq -r '.timestamp' < "$VERSION_CACHE_FILE")
latest_known_version=$(jq -r '.version' < "$VERSION_CACHE_FILE")
one_day_ago_epoch=$(date -d '1 days ago' +%s)
if (( check_timestamp > one_day_ago_epoch )); then
show_updates "$latest_known_version"
return 0
fi
fi
fetched_tag_name=$(check_and_cache_update)
show_updates "$fetched_tag_name"
}
check_and_cache_update() {
echo "Checking for updates..." 1>&2
fetched_tag_name=$(curl -sL $RELEASE_API_URL | jq -r '.tag_name')
echo "$fetched_tag_name"
mkdir -p "$(dirname "$VERSION_CACHE_FILE")"
cat > "$VERSION_CACHE_FILE" <<EOF
{
"timestamp": "$(date +%s)",
"version": "$fetched_tag_name"
}
EOF
}
get_dest_path="\$(echo \"\$result\" | awk '/^$CHANGE_DIRECTORY_PREFIX.*/ {sub(\"$CHANGE_DIRECTORY_PREFIX\", \"\"); print; exit}')"
init_bash() {
cat <<EOF
wt() {
result="\$($BINARY_PATH "\$@")"
dest_path="$get_dest_path"
if [[ -n "\$dest_path" ]]; then
cd "\$dest_path" || return
elif [[ -n \$result ]]; then
echo "\$result"
fi
}
EOF
}
init_fish() {
cat <<EOF
function wt
set -l result "\$($BINARY_PATH \$argv)"
set -l dest_path "$get_dest_path"
if test -n "\$dest_path"
cd "\$dest_path"
else if test -n "\$result"
echo "\$result"
end
end
EOF
}
init() {
shell="${args[1]}"
if [[ -z $shell ]]; then
echo "Please supply a shell."
echo " eg. wt init bash"
exit 1
fi
case "$shell" in
bash|zsh)
init_bash
;;
fish)
init_fish
;;
*)
echo "Unsupported shell: $shell"
exit 1
;;
esac
}
case "${args[0]}" in
list)
worktree_list_names
;;
names)
worktree_list_names
echo -e '\033[0;31mThe '\''names'\'' option is deprecated and will be removed in future versions.\033[0m' >&2
echo -e '\033[0;31mUse '\''list'\'' instead.\033[0m' >&2
;;
update)
update
;;
help)
help_message
;;
version)
echo Version: $VERSION
;;
init)
init
;;
*)
auto_check_update
if [[ "$is_interactive" == true ]]; then
directory=$(git worktree list --porcelain |
awk '/worktree/ {wt=$2} /branch/ {sub("refs/heads/", "", $2); print wt " [" $2 "]"}' |
fzf --query "" --height=10% --no-multi --exit-0 |
awk '{print $1}')
else
directory=$(git worktree list --porcelain | grep -E 'worktree ' | awk '/'"$arg"'/ {print; exit}' | cut -d ' ' -f2-)
fi
;;
esac
# If directory variable is not empty then change worktree
if [ -z "$directory" ]; then
:
else
echo "$CHANGE_DIRECTORY_PREFIX$directory"
fi