-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathupdate-templates
executable file
·222 lines (186 loc) · 5.67 KB
/
update-templates
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
#!/usr/bin/bash
#
# Update a plugin with new shipdriver templates.
#
# usage: ./update-templates [-t] [treeish]
#
# Merge changes from upstream shipdriver repo into
# current branch.
#
# See UPDATE-TEMPLATES.md for more info
function usage() {
cat << EOT
Usage:
update-templates [-T] <treeish>
update-templates [-h |-l]
Parameters:
treeish:
A shipdriver tag or branch.
Recommended usage is using the latest stable (non-beta) tag
Options:
-l List available shipdriver tags
-h Print this message and exit
-T Test mode, do not auto-update
Examples:
update-templates -l -- List available tags
update-templates sd3.0.2 -- Update from sd3.0.2 tag
update-templates shipdriver/v3.0 -- Update from v3.0 release branch
update-templates shipdriver/master -- Update from development branch
EOT
}
function list_sd_tags() {
# List all tags in shipdriver remote with a 'sd' prefix
git ls-remote --tags shipdriver \
| sed -n -e '/{}/d' -e 's|.*tags/||' -e '/^sd/p'
}
function init_shipdriver_remote() {
# Set up the shipdriver remote
if git ls-remote --exit-code shipdriver &>/dev/null; then
git remote remove shipdriver
echo "Removing existing shipdriver remote."
fi
echo "Adding new shipdriver remote"
git remote add shipdriver https://github.com/Rasbats/shipdriver_pi.git
# Fetch all available shipdriver sd* tags
for t in $(list_sd_tags); do
git fetch -q shipdriver refs/tags/$t:refs/tags/$t --no-tags
done
}
function update_dir() {
# Update directory $1 from shipdriver treeish $2
local d=$1
local treeish=$2
echo "Updating $d"
if [ -d $d ]; then git rm -rf $d/*; fi
git checkout $treeish $d
git add $d
git diff-index --quiet --cached HEAD -- || {
git commit -m "Templates: Updating $d"
}
}
# Exec on tmpfile to avoid file lock when updating script on Windows.
# The directory /c exists on git bash on Windows, and hopefully only there.
if [[ -d /c && "$0" =~ .*update-templates ]]; then
tmpfile=$(mktemp)
cp $0 $tmpfile
exec $tmpfile $@
fi
# Handle options and parameters
usage="Usage: $0 [-t] [-T] [treeish]"
merge_opt=""
while getopts "Thl" opt; do
case "${opt}" in
T) test_mode=true
;;
h) usage; exit 0
;;
l) do_list_tags="true"
;;
*) usage >&2; exit 1
;;
esac
done
shift $((OPTIND-1))
if [ -n "$do_list_tags" ]; then
if [ -z "$test_mode" ]; then init_shipdriver_remote; fi
list_sd_tags
exit 0
fi
if [ -z "$1" ]; then
usage >&2
exit 1
fi
test -z "$test_mode" || set -x
source_treeish=$1
# Refuse to run unless the index is clean:
clean=$(git status --porcelain)
if [ -n "$clean" ]; then
echo "Please commit or stash pending changes. Aborting."
exit 1
fi
if [ -z "$test_mode" ]; then
init_shipdriver_remote
echo "Checking for updates of updates-templates script"
if ! git diff --quiet HEAD $source_treeish -- update-templates; then
git checkout $source_treeish update-templates
cat << EOT
update-templates script is updated to latest version. Please commit
changes and re-run script.
EOT
exit 0
fi
fi
git rev-parse --verify -q $source_treeish || {
echo "Unknown tag or branch $source_treeish -- giving up."
exit 1
}
# Updating standard directories
for dir in cmake ci scripts buildwin; do
update_dir $dir $source_treeish
done
echo "Updating libs"
git checkout $source_treeish libs
git add libs
git diff-index --quiet --cached HEAD -- || {
git commit -m "Templates: Updating libs"
}
# If Plugin.cmake exists we should also update CMakeLists.txt
if [ -f Plugin.cmake ]; then CMakeLists=CMakeLists.txt; fi
echo "Templates: Updating other files"
for f in \
appveyor.yml \
.circleci \
.clang-format \
.cmake-format.yaml \
.drone.yml \
.gitattributes \
.gitignore \
INSTALL.md \
plugin.xml.in \
.travis.yml \
update-templates \
UPDATE_TEMPLATES.md \
$CMakeLists
do
if test -d $f; then git rm -rf $f; fi
git checkout $source_treeish $f
git add $f || git add --renormalize $f
done
git commit -m "Templates: Updating other files"
if [ -e update-ignored ]; then
echo "Revert changes in blacklisted files."
for f in $(cat update-ignored); do
git checkout HEAD $f
done
fi
for f in buildosx buildwin/NSIS* libs/AndroidLibs.cmake mingw; do
if test -e $f; then git rm -rf $f; fi
done
git diff-index --quiet --cached HEAD -- || {
echo "Removing unused files"
git commit -am "Templates: Removing obsoleted and unused files."
}
if [ -f cmake/TemplateVersion ]; then
our_manifest=$(echo flatpak/org.opencpn.OpenCPN.Plugin.*.yaml)
echo "Append shipdriver flatpak manifest's log of changes to $our_manifest"
prev_commit=$(sed -n '/commit:/s/.*: *//p' cmake/TemplateVersion)
src_manifest=flatpak/org.opencpn.OpenCPN.Plugin.shipdriver.yaml
git checkout $source_treeish $src_manifest
git diff $prev_commit $src_manifest | sed 's/^/# /' \
>> $our_manifest
git rm -f $src_manifest
fi
echo "Create or update cmake/TemplateVersion"
echo "# Created by update-templates" > cmake/TemplateVersion
echo "date: $(date -u +'%Y-%m-%d %H:%M UTC')" >> cmake/TemplateVersion
commit=$(git rev-parse --short $source_treeish)
echo "commit: $commit" >> cmake/TemplateVersion
tags=$(git tag --contains $commit)
echo "tags: $tags" >> cmake/TemplateVersion
git add cmake/TemplateVersion
git commit -m "cmake: Update TemplateVersion to $commit"
cat << EOF
Shipdriver templates has been updated. To review changes made:
$ git log --oneline -12
See UPDATE_TEMPLATES.md for more.
EOF