-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgitbridge
296 lines (266 loc) · 11.8 KB
/
gitbridge
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
#!/bin/bash
# Copyright (C) 2009-2013 Team XBMC
# http://xbmc.org
# This Program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This Program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with XBMC; see the file COPYING. If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# http://www.gnu.org/copyleft/gpl.html
# Install these needed packages:
# (7z, xmlstarlet, zip, texturepacker, liblzo2-2, libsdl-image1.2)
export LD_LIBRARY_PATH=/usr/local/lib
regen=0
quiet=0
ignoremaster=0
keepold=2
xmlfile="addon.xml"
masterrename="master"
tempdir="/tmp/repotmp"
texturepacker="/usr/local/bin/TexturePacker"
while getopts ":m:r:d:c:k: q i" options; do
case $options in
r ) repo=$OPTARG;;
d ) outdir=$OPTARG;;
c ) regen=1;;
q ) quiet=1;;
k ) keepold=$OPTARG;;
i ) ignoremaster=1;;
m ) masterrename=$OPTARG;;
* ) echo "Usage: gitbridge [OPTION]... -r [path] -d [path]
-r [path] path to git repo
-d [path] path to destination
-c rewrite entire repo, overrides -k
-m [string] rename master branch to [string] at destination
-i ignore the master branch. overrides -m
-q quiet
-k [integer] keep [integer] old addon zips
By default only changes since the last fetch will be written to the destination.
Use -c to override
"
exit 1;;
esac
done
if [ -z $repo ]; then
echo $repo
echo "no repo specified" >&2
exit 1
fi
if [ -z $outdir ]; then
echo "no destination specified" >&2
exit 1
fi
export GIT_WORK_TREE=${repo}
export GIT_DIR=${repo}/.git
function zipdir {
local branch=$1
local foldername=$2
local revid=$3
if [ "${branch}" = "master" ]; then local branch=${masterrename}; fi
local version=`git show "${revid}:${foldername}/${xmlfile}" | xmlstarlet sel -t -m "/addon" -v "@version"`
local id=`git show "${revid}:${foldername}/${xmlfile}" | xmlstarlet sel -t -m "/addon" -v "@id"`
local isskin="`git show "${revid}:${foldername}/${xmlfile}" | xmlstarlet sel -t -c "/addon/extension[@point='xbmc.gui.skin']"`"
#local isimagepack="`git show "${revid}:${foldername}/${xmlfile}" | xmlstarlet sel -t -c "/addon/extension[@point='kodi.resource.images']"`"
local compile_imagepack="`git show "${revid}:${foldername}/${xmlfile}" | xmlstarlet sel -t -c "/addon/extension[@compile='true']"`"
if [ -z "${version}" ] || [ -z "${id}" ]; then break; fi
if [ "$regen" = "1" ] && [ -d "${outdir}/${branch}/${foldername}" ]; then
rm -rf "${outdir}/${branch}/${foldername}"
fi
mkdir -p "${outdir}/${branch}/${foldername}"
if [ -n "`git ls-tree --name-only -r $revid | grep "${foldername}/icon.png"`" ] && [ -n "$(git show --stat $revid | grep "${foldername}/icon.png")" ]; then
git show "$revid:${foldername}/icon.png" > "${outdir}/${branch}/${foldername}/icon.png"
fi
if [ -n "`git ls-tree --name-only -r $revid | grep "${foldername}/fanart.jpg"`" ] && [ -n "$(git show --stat $revid | grep "${foldername}/fanart.jpg")" ]; then
git show "$revid:${foldername}/fanart.jpg" > "${outdir}/${branch}/${foldername}/fanart.jpg"
fi
if [ -n "`git ls-tree --name-only -r $revid | grep "${foldername}/changelog.txt"`" ]; then
git show "$revid:${foldername}/changelog.txt" > "${outdir}/${branch}/${foldername}/changelog-${version}.txt"
fi
if [ -n "`git ls-tree --name-only -r $revid | grep "${foldername}/${xmlfile}"`" ]; then
git show "${revid}:${foldername}/${xmlfile}" > "${outdir}/${branch}/${foldername}/${xmlfile}"
fi
if [ ! -f "${outdir}/${branch}/${foldername}/${id}-${version}.zip" ]; then
if `git archive --format=zip $revid ${foldername}/ -o "${outdir}/${branch}/${foldername}/${id}-${version}.zip"`; then
if [ "$keepold" = "0" ]; then
local cleanstales=`ls ${outdir}/${branch}/${foldername}/${id}-*.zip ${outdir}/${branch}/${foldername}/changelog-*.txt 2>/dev/null | grep -v ${version}`
else
#local cleanstales=`ls ${outdir}/${branch}/${foldername}/changelog-*.txt 2>/dev/null | grep -v ${version}`
cleanupzips ${outdir}/${branch}/${foldername} ${id} ${version}
fi
[ "$quiet" = "0" ] && [ -n "$cleanstales" ] && echo "removing $cleanstales"
[ -n "$cleanstales" ] && rm $cleanstales
if [ -n "`git ls-tree --name-only -r $revid | grep "${foldername}/_screenshots"`" ]; then
git archive --format=zip -o $revid "${foldername}/_screenshots" > "${outdir}/${branch}/${foldername}/screenshots.zip"
7z d -r ${outdir}/${branch}/${foldername}/${id}-${version}.zip ${foldername}/_screenshots/ >/dev/null
fi
if [ -n "${isskin}" ]; then
mkdir -p "${tempdir}"
git archive --format=tar $revid "${foldername}/media" | tar -xf - -C "${tempdir}"
if [ -d "${tempdir}/${foldername}/media" ]; then
"${texturepacker}" -dupecheck -input "${tempdir}/${foldername}/media" -output "${tempdir}/${foldername}/media/Textures.xbt"
fi
git archive --format=tar $revid "${foldername}/themes" | tar -xf - -C "${tempdir}"
ls "${tempdir}/${foldername}/themes/" 2>/dev/null | while read themename; do
if [ -d "${tempdir}/${foldername}/themes/${themename}" ]; then
"${texturepacker}" -dupecheck -input "${tempdir}/${foldername}/themes/${themename}/" -output "${tempdir}/${foldername}/media/${themename}.xbt"
fi
done
if [ -f "${tempdir}/${foldername}/media/Textures.xbt" ]; then
pushd ${tempdir} >/dev/null
7z d -r ${outdir}/${branch}/${foldername}/${id}-${version}.zip ${foldername}/media/ >/dev/null
7z d -r ${outdir}/${branch}/${foldername}/${id}-${version}.zip ${foldername}/themes/ >/dev/null
zip -q -m ${outdir}/${branch}/${foldername}/${id}-${version}.zip ${foldername}/media/*.xbt
popd >/dev/null
fi
rm -rf "${tempdir}"
fi
if [ -n "${compile_imagepack}" ]; then
mkdir -p "${tempdir}"
git archive --format=tar $revid "${foldername}/resources" | tar -xf - -C "${tempdir}"
if [ -d "${tempdir}/${foldername}/resources" ]; then
"${texturepacker}" -dupecheck -input "${tempdir}/${foldername}/resources" -output "${tempdir}/${foldername}/resources/Textures.xbt"
fi
if [ -f "${tempdir}/${foldername}/resources/Textures.xbt" ]; then
pushd ${tempdir} >/dev/null
7z d -r ${outdir}/${branch}/${foldername}/${id}-${version}.zip ${foldername}/resources/ >/dev/null
zip -q -m ${outdir}/${branch}/${foldername}/${id}-${version}.zip ${foldername}/resources/*.xbt
popd >/dev/null
fi
rm -rf "${tempdir}"
fi
fi
fi
}
function cleanupzips {
local folder=$1
local id=$2
local curversion=$3
local n=0
local allzips=( $(ls ${folder}/${id}-*.zip | grep -v ${curversion}) )
local allchangelogs=( $(ls ${folder}/changelog-*.txt | grep -v ${curversion}) )
(( n=${#allzips[@]} - ${keepold} ))
(( c=${#allchangelogs[@]} - ${keepold} ))
if [ $n -gt 0 ]; then
[ "$quiet" = "0" ] && echo "removing ${allzips[@]:0:$n}"
rm -f ${allzips[@]:0:$n}
fi
if [ $c -gt 0 ]; then
[ "$quiet" = "0" ] && echo "removing ${allchangelogs[@]:0:$n}"
rm -f ${allchangelogs[@]:0:$n}
fi
}
function purgebranch {
local branch=$1
local folder=$2
if [ "${branch}" = "master" ]; then local branch=${masterrename}; fi
if [ -d "${outdir}/${branch}/${folder}" ]; then
if [ "$quiet" = "0" ]; then echo "deleting: ${branch}/${folder}"; fi
rm -rf "${outdir}/${branch}/${folder}"
if [ -z `ls -d ${outdir}/${branch}/*/ 2>/dev/null` ]; then rm -rf ${outdir}/${branch}/; fi
fi
}
function update {
local branch=$1
local prevtoprev=$2
if [ -n "$prevtoprev" ]; then prevtoprev="${prevtoprev}.."; fi
folders=()
for foldername in `git ls-tree -r --name-only origin/$branch | grep /${xmlfile} | cut -f1 -d'/'`; do
folders=( "${folders[@]}" "${foldername}" )
done
#Delete addon folders removed by latest commits
if [ "${regen}" != "1" ]; then
for i in $(git rev-list ${prevtoprev}origin/${branch}); do
for j in $(git diff-tree --name-only -r --diff-filter=D $i | grep /addon.xml); do
rm -rf ${outdir}/${branch}/$(dirname "${j}")
done
done
fi
for i in `git rev-list ${prevtoprev}origin/${branch}`; do
match="`git diff-tree --root --name-only --diff-filter=CRMA -r $i | grep /${xmlfile}`"
for j in ${match}; do
versionstring="`git show ${i}:${j} | xmlstarlet sel -t -m "/addon" -v "@id"`"
if [ -n "`git diff-tree --root -p $i -- "${j}" | grep +.*"${versionstring}"`" ]; then
addonname="`echo "${j}" | cut -f1 -d'/'`"
for (( k=0; k<${#folders[*]}; k++ )); do
if [ "${addonname}" = "${folders[k]}" ]; then
if [ "$quiet" = "0" ]; then echo -e "${branch} - ${folders[k]}" | sed "s/master/${masterrename}/"; fi
zipdir $branch "${folders[k]}" $i
folders[k]=""
if [ -z "`echo ${folders[*]}`" ]; then break 3; fi
break
fi
done
fi
done
done
}
prefetchbranches=()
prefetchrefs=()
#store branch refs before fetch so that we'll know what's changed
for branch in `git show-ref | grep remote | grep -v HEAD | cut -f4 -d'/'`; do
prefetchbranches=( "${prefetchbranches[@]}" "${branch}" )
prefetchrefs=( "${prefetchrefs[@]}" "`git rev-list --max-count=1 origin/${branch}`" )
done
#git fetch -q --prune -- doesn't exist until git 1.6.6. have to use this instead:
#git fetch -q
#git remote prune origin > /dev/null
git fetch -q --prune > /dev/null
#search for branches deleted since last fetch
deletebranch=()
deleteref=()
result=()
for (( j=0; j<${#prefetchbranches[*]}; j++ )); do
if [ "${ignoremaster}" = "1" ] && [ "${j}" = "master" ]; then continue; fi
if [ -z $(git show-ref -s "origin/${prefetchbranches[$j]}") ]; then
if [ "$quiet" = "0" ]; then echo "deleted: ${prefetchbranches[$j]}" | sed "s/master/${masterrename}/"; fi
deletebranch=( "${deletebranch[@]}" "${prefetchbranches[$j]}" )
deleteref=( "${deleteref[@]}" "${prefetchrefs[$j]}" )
result=( "${result[@]}" "${prefetchbranches[$j]}" )
fi
done
#search for branches added or modified since last fetch
addbranch=()
changebranch=()
for i in `git show-ref | grep remote | grep -v HEAD | cut -f4 -d'/'`; do
found=0
if [ "${ignoremaster}" = "1" ] && [ "${i}" = "master" ]; then continue; fi
for (( j=0; j<${#prefetchbranches[*]}; j++ )); do
if [ "${prefetchbranches[${j}]}" = "${i}" ]; then
found=1
break
fi
done
if [ "${found}" = "0" ] || [ "${regen}" = "1" ] ; then
addbranch=( "${addbranch[@]}" "${i}" )
result=( "${result[@]}" "${i}" )
else
if [ "${prefetchrefs[${j}]}" != "`git rev-list --max-count=1 "origin/${prefetchbranches[${j}]}" 2> /dev/null`" ]; then
changebranch=( "${changebranch[@]}" "${prefetchbranches[${j}]}" )
changebrancholdrev=( "${changebrancholdrev[@]}" "${prefetchrefs[${j}]}" )
result=( "${result[@]}" "${prefetchbranches[${j}]}" )
fi
fi
done
for (( l=0; l<${#addbranch[*]}; l++ )); do
update "${addbranch[${l}]}"
done
for (( l=0; l<${#changebranch[*]}; l++ )); do
update "${changebranch[$l]}" "${changebrancholdrev[$l]}"
done
for (( l=0; l<${#deletebranch[*]}; l++ )); do
for folder in `git ls-tree --name-only "${deleteref[$l]}"`; do
purgebranch "${deletebranch[$l]}" ${folder}
done
done
#echo result
for (( l=0; l<${#result[*]}; l++ )); do
echo "${result[l]}"
done