-
Notifications
You must be signed in to change notification settings - Fork 20
/
wt
executable file
·118 lines (99 loc) · 2.62 KB
/
wt
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
#!/usr/bin/env bash
set -e
# Switch between git worktrees with speed.
args=("$@")
VERSION="0.1.1"
TMP_PATH=$(mktemp)
BINARY_PATH=$(which wt)
JQ_URL="https://stedolan.github.io/jq/download"
RELEASE_URL="https://github.com/yankeexe/git-worktree-switcher/releases/latest"
RELEASE_API_URL="https://api.github.com/repos/yankeexe/git-worktree-switcher/releases/latest"
# Escape forward slash
arg=$(echo "${args[0]}" | sed 's/\//\\\//g')
# show worktree list
worktree_list() {
git worktree list
}
help_message() {
echo -e "wt lets you switch between your git worktrees with speed.\n"
echo "Usage:"
echo -e "\twt <worktree-name>: search for worktree names and change to that directory."
echo -e "\twt list: list out all the git worktrees."
echo -e "\twt update: update to the latest release of worktree switcher."
echo -e "\twt version: show the CLI version."
echo -e "\twt help: shows this help message."
}
goto_main_worktree() {
main_worktree=$(git worktree list --porcelain | grep -E 'worktree ' | awk '{print $0; exit}' | cut -d ' ' -f2-)
if [ -z "$main_worktree" ]; then
:
else
echo Changing to main worktree at: "$main_worktree"
cd "$main_worktree"
exec $SHELL
fi
}
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=$(curl -sL $RELEASE_API_URL | jq -r '.tag_name')
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
}
if [ -z "${args[0]}" ]; then
help_message
exit 0
fi
case "${args[0]}" in
list)
worktree_list
;;
update)
update
;;
help)
help_message
;;
version)
echo Version: $VERSION
;;
-)
goto_main_worktree
;;
*)
directory=$(git worktree list --porcelain | grep -E 'worktree ' | awk '/'"$arg"'/ {print; exit}' | cut -d ' ' -f2-)
;;
esac
# Change worktree based on user argument.
change_worktree() {
echo Changing to worktree at: "$directory"
cd "$directory"
exec $SHELL
}
# If directory variable is not empty then change worktree
if [ -z "$directory" ]; then
:
else
change_worktree
fi