Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
218 lines (190 loc) · 6.77 KB
/
Copy pathinstall.sh
File metadata and controls
218 lines (190 loc) · 6.77 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
#!/bin/sh
# Installs the latest release of lazygit into ~/.local/bin, for the current
# user only. The README tells users to run it with
#
# curl -fsSL https://raw.githubusercontent.com/jesseduffield/lazygit/HEAD/install.sh | sh
#
# so it must stay at this path, and every change to it reaches users as soon
# as it is merged.
#
# Everything happens in main, which is called on the last line, so that a
# partially downloaded script does nothing.
set -eu
repo_url=https://github.com/jesseduffield/lazygit
install_dir="$HOME/.local/bin"
main() {
if [ "$(id -u)" = 0 ]; then
fail "Don't run this script as root. It installs lazygit for the current user only, so run it as the user who wants to use lazygit, without sudo. If you want lazygit for root itself, download it from $repo_url/releases and install it manually."
fi
os=$(detect_os)
arch=$(detect_arch "$os")
echo "Installing the latest release of lazygit..."
# Download into a temporary directory inside the install directory, so
# that the final mv is an atomic rename. This also works while lazygit is
# running.
mkdir -p "$install_dir"
tmp_dir=$(mktemp -d "$install_dir/.lazygit-install.XXXXXX")
trap 'rm -rf "$tmp_dir"' EXIT
trap 'exit 1' HUP INT TERM
# The archive names contain the version, so take the name and the
# version from the checksums of the latest release.
download "$repo_url/releases/latest/download/checksums.txt" "$tmp_dir/checksums.txt"
checksum_line=$(grep " lazygit_[^_]*_${os}_${arch}\.tar\.gz\$" "$tmp_dir/checksums.txt") ||
fail "The latest release has no archive for $os/$arch."
expected_checksum=${checksum_line%% *}
archive=${checksum_line##* }
version=${archive#lazygit_}
version=${version%%_*}
download "$repo_url/releases/download/v$version/$archive" "$tmp_dir/$archive"
actual_checksum=$(compute_checksum "$tmp_dir/$archive")
if [ "$actual_checksum" != "$expected_checksum" ]; then
fail "The checksum of $archive doesn't match the one in checksums.txt."
fi
tar -xzf "$tmp_dir/$archive" -C "$tmp_dir" lazygit
mv -f "$tmp_dir/lazygit" "$install_dir/lazygit"
echo "Installed lazygit $version into $install_dir."
search_path
print_path_hint
warn_about_other_copies
}
detect_os() {
case $(uname -s) in
Linux) echo linux ;;
Darwin) echo darwin ;;
FreeBSD) echo freebsd ;;
MINGW* | MSYS* | CYGWIN*)
fail "This script supports Linux, macOS and FreeBSD only. On Windows, run this command instead:
powershell -c \"irm https://raw.githubusercontent.com/jesseduffield/lazygit/HEAD/install.ps1 | iex\""
;;
*) fail "This script supports Linux, macOS and FreeBSD only. You can download lazygit for other systems from $repo_url/releases." ;;
esac
}
detect_arch() {
machine=$(uname -m)
# Under Rosetta, uname reports x86_64 on Apple silicon too.
if [ "$1" = darwin ] && [ "$machine" = x86_64 ] && [ "$(sysctl -n hw.optional.arm64 2>/dev/null)" = 1 ]; then
machine=arm64
fi
case $machine in
x86_64 | amd64) echo x86_64 ;;
aarch64 | arm64) echo arm64 ;;
arm | armv6* | armv7* | armv8l) echo armv6 ;;
i386 | i486 | i586 | i686) echo 32-bit ;;
*) fail "There is no lazygit release for the $machine architecture." ;;
esac
}
download() {
if has_usable_curl; then
curl -fsSL -o "$2" "$1" || fail "Couldn't download $1."
elif command -v wget > /dev/null 2>&1; then
wget -q -O "$2" "$1" || fail "Couldn't download $1."
else
fail "This script needs curl or wget. A curl that was installed with snap doesn't work, because it can't write into $install_dir."
fi
}
# A curl that was installed with snap can't write into hidden directories in
# the home directory, and the install directory is in one of them.
has_usable_curl() {
case $(command -v curl) in
"" | */snap/*) return 1 ;;
esac
}
compute_checksum() {
if command -v sha256sum > /dev/null 2>&1; then
sha256sum "$1" | cut -d ' ' -f 1
elif command -v shasum > /dev/null 2>&1; then
shasum -a 256 "$1" | cut -d ' ' -f 1
elif command -v sha256 > /dev/null 2>&1; then
sha256 -q "$1"
else
fail "This script needs sha256sum, shasum or sha256 to verify the download."
fi
}
# Looks for the install directory and for other copies of lazygit on the PATH.
# It resolves the symlinks in each directory before it compares them. This way
# it recognizes the install directory however the PATH spells it, and it finds
# a copy in /usr/bin only once if /bin is a symlink to /usr/bin.
search_path() {
resolved_install_dir=$(cd "$install_dir" && pwd -P)
on_path=
other_copies=
shadowing_copy=
newline='
'
seen=:
old_ifs=$IFS
IFS=:
set -f
for dir in $PATH; do
dir=$(cd "$dir" 2> /dev/null && pwd -P) || continue
case $seen in
*":$dir:"*) continue ;;
esac
seen="$seen$dir:"
if [ "$dir" = "$resolved_install_dir" ]; then
on_path=1
elif [ -f "$dir/lazygit" ] && [ -x "$dir/lazygit" ]; then
other_copies="$other_copies $dir/lazygit$newline"
# Typing lazygit runs the first copy on the PATH.
if [ -z "$on_path" ] && [ -z "$shadowing_copy" ]; then
shadowing_copy=$dir/lazygit
fi
fi
done
set +f
IFS=$old_ifs
}
print_path_hint() {
if [ -n "$on_path" ]; then
return
fi
echo
echo "$install_dir is not on your PATH."
case $(basename "${SHELL:-sh}") in
fish)
echo "To add it, run this command:"
echo
echo " fish_add_path ~/.local/bin"
;;
zsh)
print_export_hint ~/.zshrc
;;
bash)
if [ "$(uname -s)" = Darwin ]; then
print_export_hint ~/.bash_profile
else
print_export_hint ~/.bashrc
fi
;;
*)
print_export_hint ~/.profile
;;
esac
echo
echo "Then open a new terminal."
}
print_export_hint() {
echo "To add it, add this line to $1:"
echo
# shellcheck disable=SC2016 # the variables are meant to stay unexpanded
echo ' export PATH="$HOME/.local/bin:$PATH"'
}
warn_about_other_copies() {
if [ -z "$other_copies" ]; then
return
fi
echo
echo "Warning: there are other copies of lazygit on your PATH:"
echo
printf '%s' "$other_copies"
echo
echo "If you installed them with a package manager, uninstall them there."
if [ -n "$shadowing_copy" ]; then
echo "Until then, typing lazygit runs $shadowing_copy, not the copy that was just installed."
fi
}
fail() {
echo "Error: $1" >&2
exit 1
}
main