update.sh: tame the wild show_progress=1 beast
[girocco/readme.git] / jobd / update.sh
blob8c65545450ba7f78d4d1cb16bee361f0c7ab103a
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 git_fetch_q_progress() {
16 PATH="$var_git_exec_path:$cfg_basedir/bin:$PATH" @basedir@/jobd/git-fetch-q-progress.sh "$@"
19 # freshen_loose_objects full-sha ...
20 # if "$n" is a loose object, set its modification time to now
21 # otherwise silently do nothing with no error. To facilitate conversion
22 # of mirror projects to push projects we also add group write permission.
23 freshen_loose_objects() {
24 _list=
25 for _sha; do
26 _fn="${_sha#??}"
27 _shard="${_sha%$_fn}"
28 _list="$_list objects/$_shard/$_fn"
29 done
30 if [ -n "$_list" ]; then
31 chmod ug+w $_list 2>/dev/null || :
32 touch -c $_list 2>/dev/null || :
36 # darcs fast-export | git fast-import with error handling
37 git_darcs_fetch() (
38 set_utf8_locale
39 _err1=
40 _err2=
41 exec 3>&1
42 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
44 exec 4>&3 3>&1 1>&4 4>&-
46 _e1=0
47 "$cfg_basedir"/bin/darcs-fast-export \
48 --export-marks="$(pwd)/dfe-marks" \
49 --import-marks="$(pwd)/dfe-marks" "$1" 3>&- || _e1=$?
50 echo $_e1 >&3
51 } |
53 _e2=0
54 git fast-import \
55 --export-marks="$(pwd)/gfi-marks" \
56 --export-pack-edges="$(pwd)/gfi-packs" \
57 --import-marks="$(pwd)/gfi-marks" \
58 --force 3>&- || _e2=$?
59 echo $_e2 >&3
62 EOT
63 exec 3>&-
64 [ "$_err1" = 0 ] && [ "$_err2" = 0 ]
65 return $?
68 # bzr fast-export | git fast-import with error handling
69 git_bzr_fetch() (
70 set_utf8_locale
71 BZR_LOG=/dev/null
72 export BZR_LOG
73 _err1=
74 _err2=
75 exec 3>&1
76 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
78 exec 4>&3 3>&1 1>&4 4>&-
80 _e1=0
81 bzr fast-export --plain \
82 --export-marks="$(pwd)/bfe-marks" \
83 --import-marks="$(pwd)/bfe-marks" "$1" 3>&- || _e1=$?
84 echo $_e1 >&3
85 } |
87 _e2=0
88 git fast-import \
89 --export-marks="$(pwd)/gfi-marks" \
90 --export-pack-edges="$(pwd)/gfi-packs" \
91 --import-marks="$(pwd)/gfi-marks" \
92 --force 3>&- || _e2=$?
93 echo $_e2 >&3
96 EOT
97 exec 3>&-
98 [ "$_err1" = 0 ] && [ "$_err2" = 0 ]
99 return $?
102 [ -n "$cfg_mirror" ] || { echo "Mirroring is disabled" >&2; exit 0; }
104 umask 002
105 [ "$cfg_permission_control" != "Hooks" ] || umask 000
106 clean_git_env
108 proj="${1%.git}"
109 cd "$cfg_reporoot/$proj.git"
111 trap 'if [ $? != 0 ]; then echo "update failed dir: $PWD" >&2; fi; rm -f "$bang_log"' EXIT
112 trap 'exit 130' INT
113 trap 'exit 143' TERM
115 if [ "${force_update:-0}" = "0" ] && check_interval lastrefresh $cfg_min_mirror_interval; then
116 progress "= [$proj] update skip (last at $(config_get lastrefresh))"
117 exit 0
119 if [ -e .nofetch ]; then
120 progress "x [$proj] update disabled (.nofetch exists)"
121 exit 0
123 progress "+ [$proj] update ($(date))"
125 # Any pre-existing FETCH_HEAD from a previous fetch, failed or not, is garbage
126 rm -f FETCH_HEAD
128 # A previous failed update attempt can leave a huge tmp_pack_XXXXXX file behind.
129 # Since no pushes are allowed to mirrors, we know that any such files that exist
130 # at this point in time are garbage and can be safely deleted, we do not even
131 # need to check how old they are. A tmp_idx_XXXXXX file is also created during
132 # the later stages of the fetch process, so we kill any of those as well.
133 find -L objects/pack -maxdepth 1 -type f -name "tmp_pack_?*" -exec rm -f '{}' + || :
134 find -L objects/pack -maxdepth 1 -type f -name "tmp_idx_?*" -exec rm -f '{}' + || :
136 # Make sure we have a reflogs subdirectory and abort the update if not
137 # This should not count as a normal "bang" failure if unsuccessful
138 [ -d reflogs ] || mkdir -p reflogs >/dev/null 2>&1 || :
139 [ -d reflogs ]
141 bang_setup
142 bang_action="update"
143 bang_trap() {
144 if [ -n "$1" ]; then
145 # Throttle retries
146 # Since gitweb shows the .last_refresh date, it's safe to update
147 # gitweb.lastrefresh to throttle the updates w/o corrupting the
148 # last refresh date display on the gitweb summary page
149 # It's therefore important that we do NOT touch .last_refresh here
150 config_set lastrefresh "$(date "$datefmt")"
154 bang echo "Project: $proj"
155 bang echo ""
156 mail="$(config_get owner)" || :
157 url="$(config_get baseurl)" || :
158 case "$url" in *" "*|*" "*|"")
159 bang_eval 'echo "Bad mirror URL (\"$url\")"; ! :'
160 exit 1
161 esac
162 bang echo "Mirroring from URL \"$url\""
163 bang echo ""
164 statusok="$(git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
165 mailaddrs=
166 [ "$statusok" = "false" ] || [ -z "$mail" ] || mailaddrs="$mail"
167 [ -z "$cfg_admincc" ] || [ "$cfg_admincc" = "0" ] || [ -z "$cfg_admin" ] ||
168 if [ -z "$mailaddrs" ]; then mailaddrs="$cfg_admin"; else mailaddrs="$mailaddrs,$cfg_admin"; fi
170 bang_eval "git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
171 bang_eval "LC_ALL=C sort -b -k1,1 <.refs-temp >.refs-before"
172 ! [ -e .delaygc ] || >.allowgc || :
173 svnpackcreated=
175 # Make sure we don't get any unwanted loose objects
176 # Starting with Git v2.10.0 fast-import can generate loose objects unless we
177 # tweak its configuration to prevent that
178 git_add_config 'fetch.unpackLimit=1'
179 # Note the git config documentation is wrong
180 # transfer.unpackLimit, if set, overrides fetch.unpackLimit
181 git_add_config 'transfer.unpackLimit=1'
182 # But not the Git v2.10.0 and later fastimport.unpackLimit which improperly uses <= instead of <
183 git_add_config 'fastimport.unpackLimit=0'
185 case "$url" in
186 svn://* | svn+http://* | svn+https://* | svn+file://* | svn+ssh://*)
187 [ -n "$cfg_mirror_svn" ] || { echo "Mirroring svn is disabled" >&2; exit 0; }
188 # Use an 'anonsvn' username as is commonly used for anonymous svn
189 # Use an 'anonsvn' password as is commonly used for anonymous svn
190 GIT_ASKPASS_PASSWORD=anonsvn
191 export GIT_ASKPASS_PASSWORD
192 # Update the git svn url to match baseurl but be cognizant of any
193 # needed prefix changes. See the comments in taskd/clone.sh about
194 # why we need to put up with a prefix in the first place.
195 case "$url" in svn+ssh://*) svnurl="$url";; *) svnurl="${url#svn+}";; esac
196 svnurl="${svnurl%/}"
197 svnurlold="$(config_get svnurl)" || :
198 if [ "$svnurl" != "$svnurlold" ]; then
199 # We better already have an svn-remote.svn.fetch setting
200 bang test -n "$(git config --get-all svn-remote.svn.fetch)" || :
201 # the only way to truly know what the proper prefix is
202 # is to attempt a fresh git-svn init -s on the new url
203 rm -rf svn-new-url || :
204 # We require svn info to succeed on the URL otherwise it's
205 # simply not a valid URL and without using -s on the init it
206 # will not otherwise be tested until the fetch
207 bang eval 'svn --non-interactive --username anonsvn --password anonsvn info "$svnurl" >/dev/null'
208 bang mkdir svn-new-url
209 GIT_DIR=svn-new-url bang git init --bare --quiet
210 # We initially use -s for the init which will possibly shorten
211 # the URL. However, the shortening can fail if a password is
212 # not required for the longer version but is for the shorter,
213 # so try again without -s if the -s version fails.
214 cmdstr='git svn init --username=anonsvn --prefix "" -s "$svnurl" </dev/null >/dev/null 2>&1 || '
215 cmdstr="$cmdstr"'git svn init --username=anonsvn --prefix "" "$svnurl" </dev/null >/dev/null 2>&1'
216 GIT_DIR=svn-new-url bang eval "$cmdstr"
217 gitsvnurl="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.url)" || :
218 gitsvnfetch="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.fetch)" || :
219 gitsvnprefixnew="${gitsvnfetch%%:*}"
220 gitsvnsuffixnew="${gitsvnprefixnew##*/}"
221 gitsvnprefixnew="${gitsvnprefixnew%$gitsvnsuffixnew}"
222 rm -rf svn-new-url || :
223 # Using GIT_DIR= with bang leaves it set to svn-new-url, so reset it to .
224 GIT_DIR=.
225 if [ "$gitsvnurl" != "$(git config --get svn-remote.svn.url || :)" ]; then
226 # The url has been changed.
227 # We must update the url and replace the prefix on all config items
228 gitsvnfetch="$(git config --get-all svn-remote.svn.fetch | head -1)" || :
229 gitsvnprefixold="${gitsvnfetch%%:*}"
230 gitsvnsuffixold="${gitsvnprefixold##*/}"
231 gitsvnprefixold="${gitsvnprefixold%$gitsvnsuffixold}"
232 git config --remove-section 'svn-remote.svnnew' 2>/dev/null || :
233 git config 'svn-remote.svnnew.url' "$gitsvnurl"
234 git config --get-regexp '^svn-remote\.svn\.' |
235 while read -r sname sval; do
236 case "$sname" in
237 svn-remote.svn.fetch|svn-remote.svn.branches|svn-remote.svn.tags)
238 sname="${sname#svn-remote.svn.}"
239 sval="${sval#$gitsvnprefixold}"
240 bang git config --add "svn-remote.svnnew.$sname" "${gitsvnprefixnew}$sval"
241 esac
242 done
243 test $? -eq 0
244 bang git config -f svn/.metadata svn-remote.svn.reposRoot "$gitsvnurl"
245 bang git config --remove-section svn-remote.svn
246 bang git config --rename-section svn-remote.svnnew svn-remote.svn
248 bang config_set svnurl "$svnurl"
250 # remove any stale *.lock files greater than 1 hour old in case
251 # git-svn was killed on the last update because it took too long
252 find -L svn -type f -name '*.lock' -mmin +60 -exec rm -f '{}' + 2>/dev/null || :
253 # remember the starting time so we can easily combine fetched loose objects
254 # we sleep for 1 second after creating .svnpack to make sure all objects are newer
255 if ! [ -e .svnpack ]; then
256 svnpackcreated=1
257 rm -f .svnpack
258 >.svnpack
259 sleep 1
261 GIT_DIR=. bang git svn fetch --log-window-size=$var_log_window_size --username=anonsvn --quiet </dev/null
262 # git svn does not preserve group permissions in the svn subdirectory
263 chmod -R ug+rw,o+r svn
264 # git svn also leaves behind ref turds that end with @nnn
265 # We get rid of them now
266 git for-each-ref --format='%(refname)' |
267 LC_ALL=C sed '/^..*@[1-9][0-9]*$/!d; s/^/delete /' |
268 git_updateref_stdin
269 unset GIT_ASKPASS_PASSWORD
271 darcs://*)
272 [ -n "$cfg_mirror_darcs" ] || { echo "Mirroring darcs is disabled" >&2; exit 0; }
273 httpurl="http://${url#darcs://}"
274 # remove any stale lock files greater than 1 hour old in case
275 # darcs_fast_export was killed on the last update because it took too long
276 find -L *.darcs -maxdepth 2 -type f -name 'lock' -mmin +60 -exec rm -f '{}' + 2>/dev/null || :
277 bang git_darcs_fetch "$httpurl"
279 bzr://*)
280 [ -n "$cfg_mirror_bzr" ] || { echo "Mirroring bzr is disabled" >&2; exit 0; }
281 bzrurl="${url#bzr://}"
282 bang git_bzr_fetch "$bzrurl"
284 hg+http://* | hg+https://* | hg+file://* | hg+ssh://*)
285 [ -n "$cfg_mirror_hg" ] || { echo "Mirroring hg is disabled" >&2; exit 0; }
286 # We just remove hg+ here, so hg+http://... becomes http://...
287 hgurl="${url#hg+}"
288 # Fetch any new updates
289 bang hg -R "$(pwd)/repo.hg" pull
290 # Do the fast-export | fast-import
291 bang git_hg_fetch
294 [ "$url" = "$(git config --get remote.origin.url || :)" ] || bang config_set_raw remote.origin.url "$url"
295 pruneopt=--prune
296 [ "$(git config --bool fetch.prune 2>/dev/null || :)" != "false" ] || pruneopt=
297 if ! is_gfi_mirror_url "$url"; then
298 lastwasclean=
299 [ "$(git config --bool girocco.lastupdateclean 2>/dev/null || :)" != "true" ] || lastwasclean=1
300 nextisclean=
301 [ "$(git config --bool girocco.cleanmirror 2>/dev/null || :)" != "true" ] || nextisclean=1
302 if [ "$nextisclean" != "$lastwasclean" ]; then
303 if [ -n "$nextisclean" ]; then
304 git config --replace-all remote.origin.fetch "+refs/heads/*:refs/heads/*"
305 git config --add remote.origin.fetch "+refs/tags/*:refs/tags/*"
306 git config --add remote.origin.fetch "+refs/notes/*:refs/notes/*"
307 git config --add remote.origin.fetch "+refs/top-bases/*:refs/top-bases/*"
308 else
309 git config --replace-all remote.origin.fetch "+refs/*:refs/*"
313 # remember the starting time so we can easily detect new packs for fast-import mirrors
314 # we sleep for 1 second after creating .gfipack to make sure all packs are newer
315 if is_gfi_mirror_url "$url" && [ ! -e .gfipack ]; then
316 rm -f .gfipack
317 >.gfipack
318 sleep 1
320 fetcharg="default"
321 git config remotes.default >/dev/null 2>&1 || fetcharg="--all"
322 fetchcmd="git fetch"
323 if [ -n "$var_have_git_171" ] && [ "${show_progress:-0}" != "0" ]; then
324 # git fetch learned --progress in v1.7.1
325 case "$show_progress" in
326 [2-9]*|1[0-9]*)
327 # full volume progress with all the spammy noise
328 fetchcmd="$fetchcmd --progress"
331 # a kinder, gentler progress that doesn't leave one
332 # covered all over in exploded bits of spam afterwards
333 fetchcmd="git_fetch_q_progress"
335 esac
337 GIT_SSL_NO_VERIFY=1 bang_eval "$fetchcmd" $pruneopt --multiple "$fetcharg"
338 if ! is_gfi_mirror_url "$url" && [ "$nextisclean" != "$lastwasclean" ]; then
339 if [ -n "$nextisclean" ]; then
340 # We must manually purge the unclean refs now as even prune won't do it
341 git for-each-ref --format='%(refname)' |
342 LC_ALL=C sed \
343 -e '/^refs\/heads\//d' \
344 -e '/^refs\/tags\//d' \
345 -e '/^refs\/notes\//d' \
346 -e '/^refs\/top-bases\//d' \
347 -e 's/^/delete /' |
348 git_updateref_stdin
350 git config --bool girocco.lastupdateclean ${nextisclean:-0}
352 if [ -e .gfipack ] && is_gfi_mirror_url "$url"; then
353 find -L objects/pack -type f -newer .gfipack -name "pack-$octet20*.pack" -print >>gfi-packs
354 rm -f .gfipack
357 esac
359 # The objects subdirectories permissions must be updated now.
360 # In the case of a dumb http clone, the permissions will not be correct
361 # (missing group write) despite the core.sharedrepository=1 setting!
362 # The objects themselves seem to have the correct permissions.
363 # This problem appears to have been fixed in the most recent git versions.
364 perms=g+w
365 [ "$cfg_permission_control" != "Hooks" ] || perms=go+w
366 chmod $perms $(find -L objects -maxdepth 1 -type d) 2>/dev/null || :
368 bang git update-server-info
370 # We maintain the last refresh date in two places deliberately
371 # so that it's available as part of the config data and also
372 # as a standalone file timestamp that can be accessed without git.
373 bang config_set lastrefresh "$(date "$datefmt")"
374 { >.last_refresh; } 2>/dev/null || :
376 # Check to see if any refs changed
377 bang_eval "git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
378 bang_eval "LC_ALL=C sort -b -k1,1 <.refs-temp >.refs-after"
379 refschanged=
380 cmp -s .refs-before .refs-after || refschanged=1
382 # There's no way to get git svn to fetch packs, so we always need to run
383 # a mini-gc afterwards if svn actually fetched anything, but that's okay
384 # because it will be very quick
385 if [ -z "$refschanged" ] && [ -n "$svnpackcreated" ]; then
386 # we created the .svnpack but didn't actually fetch anything
387 # so remove it to avoid forcing a mini-gc if not necessary
388 rm -f .svnpack
390 if [ -e .svnpack ] && ! [ -e .needsgc ]; then
391 >.needsgc
394 # Force a mini-gc if $Girocco::Config::delay_gfi_redelta is false and there's
395 # at least one gfi pack present now
396 if [ -z "$cfg_delay_gfi_redelta" ] && ! [ -e .needsgc ] &&
397 [ -f gfi-packs ] && [ -s gfi-packs ] && is_gfi_mirror_url "$url"; then
398 >.needsgc
401 # Activate a mini-gc if needed
402 check_and_set_needsgc
404 # Look at which refs changed and trigger ref-change for these
405 sockpath="$cfg_chroot/etc/taskd.socket"
406 if [ -n "$refschanged" ]; then
407 bang config_set lastreceive "$(date '+%a, %d %b %Y %T %z')"
408 # We always use UTC for the log timestamp so that chroot and non-chroot match up.
409 # We don't have to worry about multiple log files since only one update runs
410 lognamets="$(TZ=UTC date '+%Y%m%d_%H%M%S')"
411 loghhmmss="${lognamets##*_}"
412 logname="reflogs/${lognamets%%_*}"
413 # We freshen the mod time to now on any old or new ref that is a loose object
414 # For old refs we do it so we will be able to keep them around for 1 day
415 # For new refs we do it in case we are about to run gc and the new ref
416 # actually points to an oldish loose object that had been unreachable
417 # We probably do not need to do it for new refs as Git tries to do that,
418 # but since we're already doing it for old refs (which Git does not do),
419 # it's almost no extra work for new refs, just in case.
421 echo "ref-changes %$proj% $proj"
422 LC_ALL=C join .refs-before .refs-after |
423 LC_ALL=C sed -e '/^[^ ][^ ]* \([^ ][^ ]*\) \1$/d' |
424 while read ref old new; do
425 echo "$loghhmmss $old $new $ref" >&3
426 freshen_loose_objects "$old" "$new"
427 echo "$old $new $ref"
428 done
429 LC_ALL=C join -v 1 .refs-before .refs-after |
430 while read ref old; do
431 echo "$loghhmmss $old 0000000000000000000000000000000000000000 $ref" >&3
432 freshen_loose_objects "$old"
433 echo "$old 0000000000000000000000000000000000000000 $ref"
434 done
435 LC_ALL=C join -v 2 .refs-before .refs-after |
436 while read ref new; do
437 echo "$loghhmmss 0000000000000000000000000000000000000000 $new $ref" >&3
438 freshen_loose_objects "$new"
439 echo "0000000000000000000000000000000000000000 $new $ref"
440 done
441 git for-each-ref --format='%(objectname) %(objectname) %(refname)' refs/heads
442 echo "done ref-changes %$proj% $proj"
443 } >.refs-temp 3>>"$logname"
444 if [ -S "$sockpath" ]; then
445 nc_openbsd -w 15 -U "$sockpath" <.refs-temp || :
447 bang config_set lastchange "$(date '+%a, %d %b %Y %T %z')"
448 bang_eval "git for-each-ref --sort=-committerdate --format='%(committerdate:iso8601)' \
449 --count=1 refs/heads >info/lastactivity"
450 ! [ -d htmlcache ] || { >htmlcache/changed; } 2>/dev/null || :
451 rm -f .delaygc .allowgc
453 [ "${cfg_autogchack:-0}" != "0" ] &&
454 [ "$(git config --get --bool girocco.autogchack 2>/dev/null)" != "false" ]
455 then
456 mv -f .refs-after .refs-last
460 # If the repository does not yet have a valid HEAD symref try to set one
461 # If an empty repository was cloned and then later becomes unempty you just
462 # lose out on the fancy "symref=HEAD:" logic and get this version instead
463 check_and_set_head || :
465 rm -f .refs-before .refs-after .refs-temp FETCH_HEAD
467 if is_banged; then
468 [ -z "$mailaddrs" ] || ! was_banged_message_sent ||
470 echo "$proj update succeeded - failure recovery"
471 echo "this status message may be disabled on the project admin page"
472 } | mailref "update@$cfg_gitweburl/$proj.git" -s "[$cfg_name] $proj update succeeded" "$mailaddrs" || :
473 bang_reset
476 progress "- [$proj] update ($(date))"