Repository navigation
Expand file tree
/
Copy pathlibgit.sh
More file actions
233 lines (217 loc) · 5.74 KB
/
Copy pathlibgit.sh
File metadata and controls
233 lines (217 loc) · 5.74 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
224
225
226
227
228
229
230
231
232
233
#!/bin/bash
## dependency: binary_blob.bin
# Try to load github token from encrypted file, but don't fail if it doesn't exist
github_token=""
if [ -f "${adamlibpath:-$(dirname "${BASH_SOURCE[0]}")}/binary_blob.bin" ]; then
github_token=$(openssl enc -d -in "${adamlibpath:-$(dirname "${BASH_SOURCE[0]}")}/binary_blob.bin" -pbkdf2 -aes-256-cbc -pass pass:BASH_REMATCH 2>/dev/null) || github_token=""
fi
function get_latest_github_release_name() { #source: https://gist.github.com/lukechilds/a83e1d7127b78fef38c2914c4ececc3c
# github_token is now loaded globally at script initialization
# set +x
local skip_v=$2
if ! which curl >/dev/null; then
install_apt_package curl curl
return 0
fi
ans=$(curl "$github_token" --silent "https://api.github.com/repos/$1/releases/latest" | # Get latest release from GitHub api
grep '"tag_name":' | # Get tag line
sed -E 's/.*"([^"]+)".*/\1/') # Pluck JSON value
if [ -n "$skip_v" ]; then
pattern='v(.*)$'
if [[ ! "$ans" =~ $pattern ]]; then
echo "Cannot strip \"v\" prefix from $version"
return 253
else
ans=${BASH_REMATCH[1]}
fi
fi
echo "$ans"
}
function get_latest_github_tag() {
local skip_v=$2
local offset=$3
if [[ "$offset" == "" ]]; then
offset=1
fi
if ! which curl >/dev/null; then
install_apt_package curl curl
return 0
fi
if ! which jq >/dev/null; then
install_apt_package jq jq
return 0
fi
ans=$(curl "$github_token" --silent "https://api.github.com/repos/$1/tags" |
jq ".[${offset}].name")
if [ -n "$skip_v" ]; then
pattern='v(.*).{1}$'
if [[ ! "$ans" =~ $pattern ]]; then
echo "Cannot strip \"v\" prefix from $version"
return 253
else
ans=${BASH_REMATCH[1]}
fi
fi
echo "$ans"
}
function get_app_link_gh() {
local name="$1" # e.g. "BurntSushi/ripgrep"
local _arch="$2"
local _ext="$3"
local releases
if [[ "${_arch}" == "" ]]; then
_arch=$(cpu_arch)
fi
if [[ "${_ext}" == "" ]]; then
_ext=".*$"
else
_ext="\.${_ext}$"
fi
releases="$(get_github_releases "$name")"
if [[ "$_arch" == "amd64" ]]; then
to_try=("amd64.*linux" "x86_64.*linux" "i686.*linux" "amd64" "x86_64" "i686" )
elif [[ "$_arch" == "arm64" ]]; then
to_try=("arm64.*linux" "arm.*linux" "arm64" "arm")
elif [[ "$_arch" == "i386" ]]; then
to_try=("i386.*linux" "i386")
elif [[ "$_arch" == "none" ]]; then
to_try=(".*")
else
return 0
fi
for phrase in "${to_try[@]}"; do
links="$(is_arch_supported "$releases" "${phrase}.*${_ext}")"
if [ $? == 1 ] ; then
echo "$links" | head -n 1
return 0
fi
done
echo ""
return 0
}
#Gets the file from latest release of github, or specific release
# example: file=$(get_latest_github_release kee-org/keepassrpc KeePassRPC.plgx)
function get_latest_github_release() {
local local_filename="$3"
local file
if [ -z "$local_filename" ]; then
local_filename="$2"
fi
link=$(get_latest_github_release_link "$@")
file=$(get_cached_file "${local_filename}" "$link")
echo "$file"
}
function get_latest_github_release_link() {
#set -x
local github_name="$1"
local remote_filename="$2"
local local_filename="$3"
local release
if [ -z "$remote_filename" ]; then
return 253
fi
remote_filename=$(basename -- "$remote_filename") #We need only file name, no folders
release="$4"
if [ -z "$release" ]; then
release=$(get_latest_github_release_name "$github_name")
fi
local extension="${remote_filename##*.}"
local noextension="${remote_filename%.*}"
if [ -z "$local_filename" ]; then
local_filename="${noextension}-${release}.${extension}"
fi
echo "https://github.com/${github_name}/releases/download/${release}/${remote_filename}"
}
function make_sure_git_exists() {
local repo_path="$1"
local user="$2"
if [ -z "$user" ]; then
user="$USER"
fi
if [ ! -d "$repo_path" ]; then
logmkdir "$repo_path" "$user"
fi
if [ ! -d "${repo_path}/.git" ]; then
if [ "$USER" == "$user" ] && [ -w "${repo_path}" ]; then
logexec git init "${repo_path}"
return 0
fi
chown_dir "${repo_path}" "$user"
logexec sudo --user "$user" -- git init "${repo_path}"
fi
}
# pulls git repo as a current user $USER
function get_git_repo() {
local repo=$1
local dir=$2
local name
local user
if [ -z ${3+x} ]; then
name=""
else
name="$3"
fi
if [ -z ${4+x} ]; then
user=$USER
else
user="$4"
fi
if [ -z "${name}" ]; then
local pattern='^.*/([^/]+)(\.git)$'
if [[ "$repo" =~ $pattern ]]; then
name=${BASH_REMATCH[1]}
else
local pattern='^.*/([^/]+)$'
if [[ "$repo" =~ $pattern ]]; then
name=${BASH_REMATCH[1]}
else
errcho "${repo} has wrong format"
fi
fi
fi
local dest=${dir}/${name}
install_apt_package git
if [ ! -d "$dir" ]; then
errcho "Cannot find ${dir} directory to clone git repo ${repo}"
return 1
fi
logmkdir "$dest" "$user"
if [ -d "${dest}/.git" ]; then
# update repo
file_owner=$(stat -c '%U' "${dest}/.git")
if [[ "${file_owner}" != "${USER}" ]]; then
logexec sudo -u "${file_owner}" -- git pull
else
logexec pushd "${dest}" >/dev/null || return 1
logexec git pull
logexec popd >/dev/null || return 1
fi
else
if [ -w "${dest}" ]; then
logexec git clone --depth 1 --recursive "${repo}" "${dest}"
else
if [[ "$user" != "$USER" ]]; then
need_root=$(sudo -u "${user}" -H sh -c "if [ -w $dest ] ; then echo 0; else; echo 1; fi")
else
need_root=1
fi
if [[ "${need_root}" == 1 ]]; then
logexec chown "${user}" "${dest}"
fi
logexec sudo -u "${user}" -- git clone --depth 1 --recursive "${repo}" "${dest}"
fi
fi
}
function get_current_git_branch() {
local gitpath=$1
if [[ "$gitpath" != "" ]]; then
if ! pushd "$gitpath" >/dev/null; then
errcho "Cannot change directory to $gitpath"
return 1
fi
fi
git rev-parse --abbrev-ref HEAD
if [[ "$gitpath" != "" ]]; then
popd >/dev/null || return 1
fi
}