-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-as-subtree.sh
executable file
·298 lines (230 loc) · 7.07 KB
/
install-as-subtree.sh
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env bash
#
# Installs this repo (latex-lib) as a "git subtree" repo into your own
# LaTeX documentation project repo.
#
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
prefer_ssh=1
git_bin=""
function checkGit() {
if ! command -v git >/dev/null 2>&1 ; then
echo "ERROR: You don't have git in your PATH" >&2
return 1
fi
if [[ ! -f .git/config ]] ; then
echo "ERROR: You're not in the root directory of your git repo" >&2
return 1
fi
git_bin="$(command -v git 2>/dev/null)"
#
# TODO: Add more checks, right git version?
#
return 0
}
function _git() {
(
set -x
"${git_bin}" $@
)
return $?
}
function getRemoteUrl() {
local -r remote_name="$1"
echo "Fetching remote url for ${remote_name}" >&2
local -r remote_url=$("${git_bin}" remote get-url "${remote_name}" 2>/dev/null) rc=$?
if ((rc != 0)) ; then
echo "ERROR: Could not fetch the remote url for ${remote_name} (rc=${rc})" >&2
return 1
fi
echo "Remote url for ${remote_name} is ${remote_url}" >&2
echo -n "${remote_url}"
return 0
}
function getGithubUrl() {
local -r remote_org="$1"
local -r remote_name="$2"
local -r github_repo="${remote_org}/${remote_name}"
if ((prefer_ssh)) ; then
echo "[email protected]:${github_repo}.git"
else
echo "https://github.com/${github_repo}.git"
fi
}
function checkGitRemote() {
local -r remote_org="$1"
local -r remote_name="$2"
local -r remote_url="$3"
local -r github_url="$(getGithubUrl "${remote_org}" "${remote_name}")"
[[ "${remote_url}" == "${github_url}" ]] && return 0
echo "ERROR: Registered url is ${remote_url} whilst preferred url is ${github_url}" >&2
return 1
}
function addGitRemote() {
local -r remote_org="$1"
local -r remote_name="$2"
local -r github_url="$(getGithubUrl "${remote_org}" "${remote_name}")"
echo "Add git remote ${remote_name} at ${github_url}" >&2
_git remote add -f "${remote_name}" "${github_url}" 2>/dev/null || true
local -r remote_url="$(getRemoteUrl "${remote_name}")" rc=$?
((rc == 0)) || return ${rc}
echo "Actual remote url registered: ${remote_url}" >&2
checkGitRemote "${remote_org}" "${remote_name}" "${remote_url}"
}
function getSubTrees() {
"${git_bin}" log | grep git-subtree-dir | tr -d ' ' | cut -d ":" -f2 | sort | uniq
}
function addSubtree() {
local -r remote_org="$1"
local -r remote_name="$2"
local -r mount_point="$3"
local -r remote_branch="$4"
local -r remote_url="$(getRemoteUrl "${remote_name}")" rc=$?
((rc == 0)) || return ${rc}
if ! checkGitRemote "${remote_org}" "${remote_name}" "${remote_url}" ; then
addGitRemote "${remote_org}" "${remote_name}" || return $?
fi
if getSubTrees | grep -q "${mount_point}" ; then
echo "Subtree ${mount_point} has already been installed" >&2
return 0
fi
echo "Add subtree ${remote_name}/${remote_branch} at mount point ${mount_point}" >&2
_git subtree add --prefix="${mount_point}" --squash "${remote_name}/${remote_branch}"
}
function pullLatest() {
local -r remote_org="$1"
local -r remote_name="$2"
local -r mount_point="$3"
local -r remote_branch="$4"
addSubtree "$1" "$2" "$3" "$4" || return $?
echo "Pull Latest ${remote_org}/${remote_name} and mount at ${mount_point}" >&2
_git fetch "${remote_name}" || return $?
_git subtree pull --prefix="${mount_point}" "${remote_name}" "${remote_branch}" --squash
if [[ "${remote_name}" == "latex-lib" ]] ; then
createSymlinks || return $?
fi
return 0
}
# https://unix.stackexchange.com/a/521984
bash_realpath() {
# print the resolved path
# @params
# 1: the path to resolve
# @return
# &1: the resolved link path
local path="${1}"
while [[ -L ${path} && "$(ls -l "${path}")" =~ -\>\ (.*) ]]
do
path="${BASH_REMATCH[1]}"
done
echo "${path}"
}
function symlinkToDir() {
local -r srcDir="$1"
local -r targetDir="${remote_name}/${2:-$1}"
if [[ -d "${srcDir}" ]] ; then
#
# If the directory already exists then check whether it's a link
# to the subtree repo
#
if [[ -L "${srcDir}" ]] ; then
local -r realPath="$(bash_realpath "${srcDir}")"
if [[ "${targetDir}" == "${realPath}" ]] ; then
echo "${srcDir}/ already links to ${targetDir}/" >&2
return 0
fi
unlink "${srcDir}"
else
#
# So it's not a symlink while it should be. Remove its contents
#
echo "Removing ${srcDir} (also from _git), replacing it with link to ${targetDir}" >&2
_git rm -r "${srcDir}"
rm -rf "${srcDir}"
fi
fi
if [[ -d "${targetDir}" ]] ; then
ln -s "${targetDir}" "${srcDir}"
else
echo "ERROR: ${targetDir} does not exist" >&2
return 1
fi
return 0
}
function symlinkToFile() {
local -r srcFile="$1"
local -r targetFile="${remote_name}/${2:-$1}"
if [[ -f "${srcFile}" ]] ; then
#
# If the file already exists then check whether it's a link
# to the subtree repo
#
if [[ -L "${srcFile}" ]] ; then
local -r realFile="$(bash_realpath "${srcFile}")"
if [[ "${targetFile}" == "${realFile}" ]] ; then
echo "${srcFile} already links to ${targetFile}" >&2
return 0
fi
unlink "${srcFile}"
else
#
# So it's not a symlink while it should be. Remove it
#
echo "Removing ${srcFile} (also from _git), replacing it with link to ${targetFile}" >&2
_git rm "${srcFile}"
rm -f "${srcFile}"
fi
fi
if [[ -f "${targetFile}" ]] ; then
ln -s "${targetFile}" "${srcFile}"
else
echo "ERROR: ${targetFile} does not exist" >&2
return 1
fi
return 0
}
function createSymlinks() {
echo "Create symlinks"
symlinkToDir etc || return $?
symlinkToFile .actrc || return $?
symlinkToFile .env || return $?
symlinkToFile .latexmkrc || return $?
symlinkToFile act.sh || return $?
symlinkToFile build.sh || return $?
symlinkToFile build-all.sh || return $?
symlinkToFile clean.sh || return $?
symlinkToFile acronyms.tex || return $?
symlinkToFile bibliography.bib || return $?
symlinkToFile glossary-concepts.tex || return $?
symlinkToFile glossary-main.tex || return $?
symlinkToFile glossary-ekg.tex || return $?
symlinkToFile glossary-ontologies.tex || return $?
return 0
}
function main() {
local remote_org
local remote_name
local remote_branch
checkGit || return $?
local -r remote_url="$(git config --get remote.origin.url 2>/dev/null)"
echo "Remote URL is ${remote_url}" >&2
if [[ "${remote_url}" =~ "[email protected]" ]] ; then
echo "Your preferred method to fetch git content is SSH" >&2
else
echo "Your preferred method to fetch git content is HTTPS" >&2
fi
addGitRemote "agnos-ai" "latex-lib"
addGitRemote "EKGF" "ekg-manifesto"
for mount_point in $(getSubTrees) ; do
echo "sub tree: ${mount_point}" >&2
done
for mount_point in $(getSubTrees) ; do
remote_name=${mount_point/mnt\//}
remote_branch="main"
remote_org="EKGF"
[[ "${remote_name}" == "latex-lib" ]] && remote_org="agnos-ai"
pullLatest "${remote_org}" "${remote_name}" "${mount_point}" "${remote_branch}" || return $?
done
return 0
}
main $@
exit $?