mirrors: implement refs namespace cleaning
[girocco.git] / jobd / update.sh
blob2704c8c9678deacccbe9b3c381160cfdfa74d8f9
1 #!/bin/sh
3 . @basedir@/shlib.sh
5 set -e
7 if [ $# -ne 1 ]; then
8 echo "Usage: update.sh projname" >&2
9 exit 1
12 # date -R is linux-only, POSIX equivalent is '+%a, %d %b %Y %T %z'
13 datefmt='+%a, %d %b %Y %T %z'
15 # freshen_loose_objects full-sha ...
16 # if "$n" is a loose object, set its modification time to now
17 # otherwise silently do nothing with no error. To facilitate conversion
18 # of mirror projects to push projects we also add group write permission.
19 freshen_loose_objects() {
20 _list=
21 for _sha; do
22 _fn="${_sha#??}"
23 _shard="${_sha%$_fn}"
24 _list="$_list objects/$_shard/$_fn"
25 done
26 if [ -n "$_list" ]; then
27 chmod ug+w $_list 2>/dev/null || :
28 touch -c $_list 2>/dev/null || :
32 # darcs fast-export | git fast-import with error handling
33 git_darcs_fetch() (
34 set_utf8_locale
35 _err1=
36 _err2=
37 exec 3>&1
38 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
40 exec 4>&3 3>&1 1>&4 4>&-
42 _e1=0
43 "$cfg_basedir"/bin/darcs-fast-export \
44 --export-marks="$(pwd)/dfe-marks" \
45 --import-marks="$(pwd)/dfe-marks" "$1" 3>&- || _e1=$?
46 echo $_e1 >&3
47 } | \
49 _e2=0
50 git fast-import \
51 --export-marks="$(pwd)/gfi-marks" \
52 --export-pack-edges="$(pwd)/gfi-packs" \
53 --import-marks="$(pwd)/gfi-marks" \
54 --force 3>&- || _e2=$?
55 echo $_e2 >&3
58 EOT
59 exec 3>&-
60 [ "$_err1" = 0 -a "$_err2" = 0 ]
61 return $?
64 # bzr fast-export | git fast-import with error handling
65 git_bzr_fetch() (
66 set_utf8_locale
67 BZR_LOG=/dev/null
68 export BZR_LOG
69 _err1=
70 _err2=
71 exec 3>&1
72 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
74 exec 4>&3 3>&1 1>&4 4>&-
76 _e1=0
77 bzr fast-export --plain \
78 --export-marks="$(pwd)/dfe-marks" \
79 --import-marks="$(pwd)/dfe-marks" "$1" 3>&- || _e1=$?
80 echo $_e1 >&3
81 } | \
83 _e2=0
84 git fast-import \
85 --export-marks="$(pwd)/gfi-marks" \
86 --export-pack-edges="$(pwd)/gfi-packs" \
87 --import-marks="$(pwd)/gfi-marks" \
88 --force 3>&- || _e2=$?
89 echo $_e2 >&3
92 EOT
93 exec 3>&-
94 [ "$_err1" = 0 -a "$_err2" = 0 ]
95 return $?
98 [ -n "$cfg_mirror" ] || { echo "Mirroring is disabled" >&2; exit 0; }
100 umask 002
101 [ "$cfg_permission_control" != "Hooks" ] || umask 000
103 proj="${1%.git}"
104 cd "$cfg_reporoot/$proj.git"
106 trap 'if [ $? != 0 ]; then echo "update failed dir: $PWD" >&2; fi; rm -f "$bang_log"' EXIT
107 trap 'exit 130' INT
108 trap 'exit 143' TERM
110 if check_interval lastrefresh $cfg_min_mirror_interval; then
111 progress "= [$proj] update skip (last at $(config_get lastrefresh))"
112 exit 0
114 progress "+ [$proj] update (`date`)"
116 # Any pre-existing FETCH_HEAD from a previous fetch failed or not is garbage
117 rm -f FETCH_HEAD
119 # A previous failed update attempt can leave a huge tmp_pack_XXXXXX file behind.
120 # Since no pushes are allowed to mirrors, we know that any such files that exist
121 # at this point in time are garbage and can be safely deleted, we do not even
122 # need to check how old they are. A tmp_idx_XXXXXX file is also created during
123 # the later stages of the fetch process, so we kill any of those as well.
124 find objects/pack -maxdepth 1 -type f -name "tmp_pack_?*" -print0 | xargs -0 rm -f
125 find objects/pack -maxdepth 1 -type f -name "tmp_idx_?*" -print0 | xargs -0 rm -f
127 bang_setup
128 bang_once=1
129 bang_action="update"
131 bang echo "Project: $proj"
132 bang echo ""
133 mail="$(config_get owner || :)"
134 url="$(config_get baseurl || :)"
135 case "$url" in *" "*|*" "*|"")
136 bang_eval 'echo "Bad mirror URL (\"$url\")"; ! :'
137 exit 1
138 esac
139 bang echo "Mirroring from URL \"$url\""
140 bang echo ""
141 statusok="$(git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
142 mailaddrs=''
143 [ "$statusok" = "false" -o -z "$mail" ] || mailaddrs="$mail"
144 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] || \
145 if [ -z "$mailaddrs" ]; then mailaddrs="$cfg_admin"; else mailaddrs="$mailaddrs,$cfg_admin"; fi
147 bang_eval "git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
148 bang_eval "LC_ALL=C sort -b -k1,1 <.refs-temp >.refs-before"
149 ! [ -e .delaygc ] || >.allowgc || :
150 svnpackcreated=
152 case "$url" in
153 svn://* | svn+http://* | svn+https://* | svn+file://* | svn+ssh://*)
154 [ -n "$cfg_mirror_svn" ] || { echo "Mirroring svn is disabled" >&2; exit 0; }
155 # Use an 'anonsvn' username as is commonly used for anonymous svn
156 # Use an 'anonsvn' password as is commonly used for anonymous svn
157 GIT_ASKPASS_PASSWORD=anonsvn
158 export GIT_ASKPASS_PASSWORD
159 # Update the git svn url to match baseurl but be cognizant of any
160 # needed prefix changes. See the comments in taskd/clone.sh about
161 # why we need to put up with a prefix in the first place.
162 case "$url" in svn+ssh://*) svnurl="$url";; *) svnurl="${url#svn+}";; esac
163 svnurl="${svnurl%/}"
164 svnurlold="$(config_get svnurl || :)"
165 if [ "$svnurl" != "$svnurlold" ]; then
166 # We better already have an svn-remote.svn.fetch setting
167 bang test -n "$(git config --get-all svn-remote.svn.fetch || :)"
168 # the only way to truly know what the proper prefix is
169 # is to attempt a fresh git-svn init -s on the new url
170 rm -rf svn-new-url || :
171 # We require svn info to succeed on the URL otherwise it's
172 # simply not a valid URL and without using -s on the init it
173 # will not otherwise be tested until the fetch
174 bang eval 'svn --non-interactive --username anonsvn --password anonsvn info "$svnurl" > /dev/null'
175 bang mkdir svn-new-url
176 GIT_DIR=svn-new-url bang git init --bare --quiet
177 # We initially use -s for the init which will possibly shorten
178 # the URL. However, the shortening can fail if a password is
179 # not required for the longer version but is for the shorter,
180 # so try again without -s if the -s version fails.
181 cmdstr='git svn init --username=anonsvn --prefix "" -s "$svnurl" < /dev/null > /dev/null 2>&1 || '
182 cmdstr="$cmdstr"'git svn init --username=anonsvn --prefix "" "$svnurl" < /dev/null > /dev/null 2>&1'
183 GIT_DIR=svn-new-url bang eval "$cmdstr"
184 gitsvnurl="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.url || :)"
185 gitsvnfetch="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.fetch || :)"
186 gitsvnprefixnew="${gitsvnfetch%%:*}"
187 gitsvnsuffixnew="${gitsvnprefixnew##*/}"
188 gitsvnprefixnew="${gitsvnprefixnew%$gitsvnsuffixnew}"
189 rm -rf svn-new-url || :
190 # Using GIT_DIR= with bang leaves it set to svn-new-url, so reset it to .
191 GIT_DIR=.
192 if [ "$gitsvnurl" != "$(git config --get svn-remote.svn.url || :)" ]; then
193 # The url has been changed.
194 # We must update the url and replace the prefix on all config items
195 gitsvnfetch="$(git config --get-all svn-remote.svn.fetch | head -1 || :)"
196 gitsvnprefixold="${gitsvnfetch%%:*}"
197 gitsvnsuffixold="${gitsvnprefixold##*/}"
198 gitsvnprefixold="${gitsvnprefixold%$gitsvnsuffixold}"
199 git config --remove-section 'svn-remote.svnnew' 2>/dev/null || :
200 git config 'svn-remote.svnnew.url' "$gitsvnurl"
201 { git config --get-regexp '^svn-remote\.svn\.' || :; } | \
202 { while read sname sval; do
203 case "$sname" in
204 svn-remote.svn.fetch|svn-remote.svn.branches|svn-remote.svn.tags)
205 sname="${sname#svn-remote.svn.}"
206 sval="${sval#$gitsvnprefixold}"
207 bang git config --add "svn-remote.svnnew.$sname" "${gitsvnprefixnew}$sval"
208 esac
209 done; }
210 bang git config -f svn/.metadata svn-remote.svn.reposRoot "$gitsvnurl"
211 bang git config --remove-section svn-remote.svn
212 bang git config --rename-section svn-remote.svnnew svn-remote.svn
214 bang config_set svnurl "$svnurl"
216 # remove any stale *.lock files greater than 1 hour old in case
217 # git-svn was killed on the last update because it took too long
218 find svn -type f -name '*.lock' -mmin +60 -print0 2>/dev/null | xargs -0 rm -f
219 # remember the starting time so we can easily combine fetched loose objects
220 # we sleep for 1 second after creating .svnpack to make sure all objects are newer
221 if ! [ -e .svnpack ]; then
222 svnpackcreated=1
223 rm -f .svnpack
224 >.svnpack
225 sleep 1
227 GIT_DIR=. bang git svn fetch --log-window-size=$var_log_window_size --username=anonsvn --quiet < /dev/null
228 # git svn does not preserve group permissions in the svn subdirectory
229 chmod -R ug+rw,o+r svn
230 # git svn also leaves behind ref turds that end with @nnn
231 # We get rid of them now
232 git for-each-ref --format='%(objectname) %(refname)' | \
233 { while read sha1 ref; do
234 case "$ref" in
235 ?*@[1-9]|?*@[1-9][0-9]|?*@[1-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9]|\
236 ?*@[1-9][0-9][0-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9][0-9][0-9]|\
237 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9]|\
238 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])
239 git update-ref -d "$ref"
240 esac
241 done; }
242 unset GIT_ASKPASS_PASSWORD
244 darcs://*)
245 [ -n "$cfg_mirror_darcs" ] || { echo "Mirroring darcs is disabled" >&2; exit 0; }
246 httpurl="http://${url#darcs://}"
247 # remove any stale lock files greater than 1 hour old in case
248 # darcs_fast_export was killed on the last update because it took too long
249 find *.darcs -maxdepth 2 -type f -name 'lock' -mmin +60 -print0 2>/dev/null | xargs -0 rm -f
250 bang git_darcs_fetch "$httpurl"
252 bzr://*)
253 [ -n "$cfg_mirror_bzr" ] || { echo "Mirroring bzr is disabled" >&2; exit 0; }
254 bzrurl="${url#bzr://}"
255 bang git_bzr_fetch "$bzrurl"
257 hg+http://* | hg+https://* | hg+file://* | hg+ssh://*)
258 [ -n "$cfg_mirror_hg" ] || { echo "Mirroring hg is disabled" >&2; exit 0; }
259 # We just remove hg+ here, so hg+http://... becomes http://...
260 hgurl="${url#hg+}"
261 # Fetch any new updates
262 bang hg -R "$(pwd)/repo.hg" pull
263 # Do the fast-export | fast-import
264 bang git_hg_fetch
267 [ "$url" = "$(git config --get remote.origin.url || :)" ] || bang config_set_raw remote.origin.url "$url"
268 pruneopt=--prune
269 [ "$(git config --bool fetch.prune 2>/dev/null || :)" != "false" ] || pruneopt=
270 git_add_config 'fetch.unpackLimit=1'
271 # Note the git config documentation is wrong
272 # transfer.unpackLimit, if set, overrides fetch.unpackLimit
273 git_add_config 'transfer.unpackLimit=1'
274 if ! is_gfi_mirror_url "$url"; then
275 lastwasclean=
276 [ "$(git config --bool girocco.lastupdateclean 2>/dev/null || :)" != "true" ] || lastwasclean=1
277 nextisclean=
278 [ "$(git config --bool girocco.cleanmirror 2>/dev/null || :)" != "true" ] || nextisclean=1
279 if [ "$nextisclean" != "$lastwasclean" ]; then
280 if [ -n "$nextisclean" ]; then
281 git config --replace-all remote.origin.fetch "+refs/heads/*:refs/heads/*"
282 git config --add remote.origin.fetch "+refs/tags/*:refs/tags/*"
283 git config --add remote.origin.fetch "+refs/notes/*:refs/notes/*"
284 else
285 git config --replace-all remote.origin.fetch "+refs/*:refs/*"
289 # remember the starting time so we can easily detect new packs for fast-import mirrors
290 # we sleep for 1 second after creating .gfipack to make sure all packs are newer
291 if is_gfi_mirror_url "$url" && [ ! -e .gfipack ]; then
292 rm -f .gfipack
293 >.gfipack
294 sleep 1
296 GIT_SSL_NO_VERIFY=1 bang git remote update $pruneopt
297 if ! is_gfi_mirror_url "$url" && [ "$nextisclean" != "$lastwasclean" ]; then
298 if [ -n "$nextisclean" ]; then
299 # We must manually purge the unclean refs now as even prune won't do it
300 git for-each-ref --format='%(refname)' |
301 while read -r aref; do
302 case "$aref" in refs/heads/*|refs/tags/*|refs/notes/*) :;; *)
303 git update-ref -d "$aref"
304 esac
305 done
307 git config --bool girocco.lastupdateclean ${nextisclean:-0}
309 if [ -e .gfipack ] && is_gfi_mirror_url "$url"; then
310 find objects/pack -type f -newer .gfipack -name "pack-$octet20.pack" -print >>gfi-packs
311 rm -f .gfipack
314 esac
316 # The objects subdirectories permissions must be updated now.
317 # In the case of a dumb http clone, the permissions will not be correct
318 # (missing group write) despite the core.sharedrepository=1 setting!
319 # The objects themselves seem to have the correct permissions.
320 # This problem appears to have been fixed in the most recent git versions.
321 perms=g+w
322 [ "$cfg_permission_control" != "Hooks" ] || perms=go+w
323 chmod $perms $(find objects -maxdepth 1 -type d) 2>/dev/null || :
325 bang git update-server-info
327 # We maintain the last refresh date in two places deliberately
328 # so that it's available as part of the config data and also
329 # as a standalone file timestamp that can be accessed without git.
330 bang config_set lastrefresh "$(date "$datefmt")"
331 { >.last_refresh; } 2>/dev/null || :
333 # Check to see if any refs changed
334 bang_eval "git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
335 bang_eval "LC_ALL=C sort -b -k1,1 <.refs-temp >.refs-after"
336 refschanged=
337 cmp -s .refs-before .refs-after || refschanged=1
339 # There's no way to get git svn to fetch packs, so we always need to run
340 # a mini-gc afterwards if svn actually fetched anything, but that's okay
341 # because it will be very quick
342 if [ -z "$refschanged" -a -n "$svnpackcreated" ]; then
343 # we created the .svnpack but didn't actually fetch anything
344 # so remove it to avoid forcing a mini-gc if not necessary
345 rm -f .svnpack
347 if [ -e .svnpack ] && ! [ -e .needsgc ]; then
348 >.needsgc
351 # Force a mini-gc if $Girocco::Config::delay_gfi_redelta is false and there's
352 # at least one gfi pack present now
353 if [ -z "$cfg_delay_gfi_redelta" ] && ! [ -e .needsgc ] && \
354 [ -f gfi-packs -a -s gfi-packs ] && is_gfi_mirror_url "$url"; then
355 >.needsgc
358 # Activate a mini-gc if there are at least 20 packs present now
359 if ! [ -e .needsgc ]; then
360 packs=
361 { packs="$(list_packs --quiet --count --exclude-no-idx objects/pack || :)" || :; } 2>/dev/null
362 if [ -n "$packs" ] && [ "$packs" -ge 20 ]; then
363 >.needsgc
367 # Look at which refs changed and trigger ref-change for these
368 sockpath="$cfg_chroot/etc/taskd.socket"
369 if [ -n "$refschanged" ]; then
370 bang config_set lastreceive "$(date '+%a, %d %b %Y %T %z')"
371 # We freshen the mod time to now on any old or new ref that is a loose object
372 # For old refs we do it so we will be able to keep them around for 1 day
373 # For new refs we do it in case we are about to run gc and the new ref
374 # actually points to an oldish loose object that had been unreachable
375 # We probably do not need to do it for new refs as Git tries to do that,
376 # but since we're already doing it for old refs (which Git does not do),
377 # it's almost no extra work for new refs, just in case.
379 echo "ref-changes %$proj% $proj"
380 LC_ALL=C join .refs-before .refs-after |
381 LC_ALL=C sed -e '/^[^ ][^ ]* \([^ ][^ ]*\) \1$/d' |
382 while read ref old new; do
383 freshen_loose_objects "$old" "$new"
384 echo "$old $new $ref"
385 done
386 LC_ALL=C join -v 1 .refs-before .refs-after |
387 while read ref old; do
388 freshen_loose_objects "$old"
389 echo "$old 0000000000000000000000000000000000000000 $ref"
390 done
391 LC_ALL=C join -v 2 .refs-before .refs-after |
392 while read ref new; do
393 freshen_loose_objects "$new"
394 echo "0000000000000000000000000000000000000000 $new $ref"
395 done
396 git for-each-ref --format='%(objectname) %(objectname) %(refname)' refs/heads
397 } >.refs-temp
398 if [ -S "$sockpath" ]; then
399 nc_openbsd -w 15 -U "$sockpath" <.refs-temp || :
401 bang config_set lastchange "$(date '+%a, %d %b %Y %T %z')"
402 bang_eval "git for-each-ref --sort=-committerdate --format='%(committerdate:iso8601)' \
403 --count=1 refs/heads > info/lastactivity"
404 ! [ -d htmlcache ] || { >htmlcache/changed; } 2>/dev/null || :
405 rm -f .delaygc .allowgc
408 rm -f .refs-before .refs-after .refs-temp FETCH_HEAD
410 if is_banged; then
411 [ -z "$mailaddrs" ] ||
413 echo "$proj update succeeded - failure recovery"
414 echo "this status message may be disabled on the project admin page"
415 } | mail -s "[$cfg_name] $proj update succeeded" "$mailaddrs" || :
416 bang_reset
419 progress "- [$proj] update (`date`)"