8 echo "Usage: update.sh projname" >&2
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
() {
28 _list
="$_list objects/$_shard/$_fn"
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
42 { read -r _err1 ||
:; read -r _err2 ||
:; } <<-EOT
44 exec 4>&3 3>&1 1>&4 4>&-
47 "$cfg_basedir"/bin/darcs-fast-export \
48 --export-marks="$(pwd)/dfe-marks" \
49 --import-marks="$(pwd)/dfe-marks" "$1" 3>&- || _e1=$?
55 --export-marks="$(pwd)/gfi-marks" \
56 --export-pack-edges="$(pwd)/gfi-packs" \
57 --import-marks="$(pwd)/gfi-marks" \
58 --force 3>&- || _e2=$?
64 [ "$_err1" = 0 ] && [ "$_err2" = 0 ]
68 # bzr fast-export | git fast-import with error handling
76 { read -r _err1 ||
:; read -r _err2 ||
:; } <<-EOT
78 exec 4>&3 3>&1 1>&4 4>&-
81 bzr fast-export --plain \
82 --export-marks="$(pwd)/bfe-marks" \
83 --import-marks="$(pwd)/bfe-marks" "$1" 3>&- || _e1=$?
89 --export-marks="$(pwd)/gfi-marks" \
90 --export-pack-edges="$(pwd)/gfi-packs" \
91 --import-marks="$(pwd)/gfi-marks" \
92 --force 3>&- || _e2=$?
98 [ "$_err1" = 0 ] && [ "$_err2" = 0 ]
102 [ -n "$cfg_mirror" ] ||
{ echo "Mirroring is disabled" >&2; exit 0; }
105 [ "$cfg_permission_control" != "Hooks" ] ||
umask 000
109 cd "$cfg_reporoot/$proj.git"
111 # Activate a mini-gc if needed
112 # We do this here as well as after a successful fetch so that if we're stuck
113 # in a fetch loop where fetches are succeeding in fetching new packs but the
114 # ref update is failing for some reason (perhaps a non-commit under refs/heads)
115 # and a previous invokation therefore had a "bang" exit then we will still
116 # get the .needsgc flag set in a timely fashion to avoid excess pack build up.
117 check_and_set_needsgc
119 trap 'if [ $? != 0 ]; then echo "update failed dir: $PWD" >&2; fi; rm -f "$bang_log"' EXIT
123 if [ "${force_update:-0}" = "0" ] && check_interval lastrefresh
$cfg_min_mirror_interval; then
124 progress
"= [$proj] update skip (last at $(config_get lastrefresh))"
127 if [ -e .nofetch
]; then
128 progress
"x [$proj] update disabled (.nofetch exists)"
131 progress
"+ [$proj] update ($(date))"
133 # Any pre-existing FETCH_HEAD from a previous fetch, failed or not, is garbage
136 # Remove any stale ref locks
137 clear_stale_ref_locks
139 # A previous failed update attempt can leave a huge tmp_pack_XXXXXX file behind.
140 # Since no pushes are allowed to mirrors, we know that any such files that exist
141 # at this point in time are garbage and can be safely deleted, we do not even
142 # need to check how old they are. A tmp_idx_XXXXXX file is also created during
143 # the later stages of the fetch process, so we kill any of those as well.
144 find -L objects
/pack
-maxdepth 1 -type f
-name "tmp_pack_?*" -exec rm -f '{}' + ||
:
145 find -L objects
/pack
-maxdepth 1 -type f
-name "tmp_idx_?*" -exec rm -f '{}' + ||
:
147 # Make sure we have a reflogs subdirectory and abort the update if not
148 # This should not count as a normal "bang" failure if unsuccessful
149 [ -d reflogs
] || mkdir
-p reflogs
>/dev
/null
2>&1 ||
:
153 do_check_after_refs
=1
159 # Since gitweb shows the .last_refresh date, it's safe to update
160 # gitweb.lastrefresh to throttle the updates w/o corrupting the
161 # last refresh date display on the gitweb summary page
162 # It's therefore important that we do NOT touch .last_refresh here
163 config_set lastrefresh
"$(date "$datefmt")"
167 bang
echo "Project: $proj"
168 bang
echo " Date: $(TZ=UTC date '+%Y-%m-%d %T UTC')"
170 mail="$(config_get owner)" ||
:
171 url
="$(config_get baseurl)" ||
:
172 case "$url" in *" "*|
*" "*|
"")
173 bang_eval
'echo "Bad mirror URL (\"$url\")"; ! :'
176 bang
echo "Mirroring from URL \"$url\""
178 statusok
="$(git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
180 [ "$statusok" = "false" ] ||
[ -z "$mail" ] || mailaddrs
="$mail"
181 [ -z "$cfg_admincc" ] ||
[ "$cfg_admincc" = "0" ] ||
[ -z "$cfg_admin" ] ||
182 if [ -z "$mailaddrs" ]; then mailaddrs
="$cfg_admin"; else mailaddrs
="$mailaddrs,$cfg_admin"; fi
184 bang_eval
"git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
185 bang_eval
"LC_ALL=C sort -b -k1,1 <.refs-temp >.refs-before"
188 [ -n "$do_check_after_refs" ] ||
return 0
189 bang_eval
"git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
190 bang_eval
"LC_ALL=C sort -b -k1,1 <.refs-temp >.refs-after"
192 cmp -s .refs-before .refs-after || refschanged
=1
196 ! [ -e .delaygc
] ||
>.allowgc ||
:
198 # Make sure we don't get any unwanted loose objects
199 # Starting with Git v2.10.0 fast-import can generate loose objects unless we
200 # tweak its configuration to prevent that
201 git_add_config
'fetch.unpackLimit=1'
202 # Note the git config documentation is wrong
203 # transfer.unpackLimit, if set, overrides fetch.unpackLimit
204 git_add_config
'transfer.unpackLimit=1'
205 # But not the Git v2.10.0 and later fastimport.unpackLimit which improperly uses <= instead of <
206 git_add_config
'fastimport.unpackLimit=0'
208 # remember the starting time so we can easily combine fetched loose objects
209 # we sleep for 1 second after creating .needspack to make sure all objects are newer
210 if ! [ -e .needspack
]; then
217 svn
://* | svn
+http
://* | svn
+https
://* | svn
+file://* | svn
+ssh://*)
218 [ -n "$cfg_mirror_svn" ] ||
{ echo "Mirroring svn is disabled" >&2; exit 0; }
219 # Allow the username to be specified in the "svn-credential.svn.username"
220 # property and the password in the "svn-credential.svn.password" property
221 # Use an 'anonsvn' username by default as is commonly used for anonymous svn
222 # Default the password to the same as the username
223 # The password property will be ignored unless a username has been specified
224 if svnuser
="$(git config --get svn-credential.svn.username)" && [ -n "$svnuser" ]; then
225 if ! svnpass
="$(git config --get svn-credential.svn.password)"; then
230 case "$url1" in ?
*"@"?
*)
231 urlsch
="${url%%://*}"
232 url
="$urlsch://${url#*@}"
235 # As a fallback, check in the URL, just in case
239 case "$url1" in ?
*"@"?
*)
240 urlsch
="${url%%://*}"
241 url
="$urlsch://${url#*@}"
243 svnuser
="${url1%%:*}"
244 if [ -n "$svnuser" ]; then
246 case "$url1" in *":"*)
251 if [ -z "$svnuser" ]; then
256 GIT_ASKPASS_PASSWORD
="$svnpass"
257 export GIT_ASKPASS_PASSWORD
258 # Update the git svn url to match baseurl but be cognizant of any
259 # needed prefix changes. See the comments in taskd/clone.sh about
260 # why we need to put up with a prefix in the first place.
261 case "$url" in svn
+ssh://*) svnurl
="$url";; *) svnurl
="${url#svn+}";; esac
263 svnurlold
="$(config_get svnurl)" ||
:
264 if [ "$svnurl" != "$svnurlold" ]; then
265 # We better already have an svn-remote.svn.fetch setting
266 bang
test -n "$(git config --get-all svn-remote.svn.fetch)" ||
:
267 # the only way to truly know what the proper prefix is
268 # is to attempt a fresh git-svn init -s on the new url
269 rm -rf svn-new-url ||
:
270 # We require svn info to succeed on the URL otherwise it's
271 # simply not a valid URL and without using -s on the init it
272 # will not otherwise be tested until the fetch
273 bang
eval 'svn --non-interactive --username "$svnuser" --password "$svnpass" info "$svnurl" >/dev/null'
274 bang mkdir svn-new-url
275 GIT_DIR
=svn-new-url bang git init
--bare --quiet
276 # We initially use -s for the init which will possibly shorten
277 # the URL. However, the shortening can fail if a password is
278 # not required for the longer version but is for the shorter,
279 # so try again without -s if the -s version fails.
280 cmdstr
='git svn init --username="$svnuser" --prefix "" -s "$svnurl" <"$mtlinesfile" >/dev/null 2>&1 || '
281 cmdstr
="$cmdstr"'git svn init --username="$svnuser" --prefix "" "$svnurl" <"$mtlinesfile" >/dev/null 2>&1'
282 GIT_DIR
=svn-new-url bang
eval "$cmdstr"
283 gitsvnurl
="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.url)" ||
:
284 gitsvnfetch
="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.fetch)" ||
:
285 gitsvnprefixnew
="${gitsvnfetch%%:*}"
286 gitsvnsuffixnew
="${gitsvnprefixnew##*/}"
287 gitsvnprefixnew
="${gitsvnprefixnew%$gitsvnsuffixnew}"
288 rm -rf svn-new-url ||
:
289 # Using GIT_DIR= with bang leaves it set to svn-new-url, so reset it to .
291 if [ "$gitsvnurl" != "$(git config --get svn-remote.svn.url || :)" ]; then
292 # The url has been changed.
293 # We must update the url and replace the prefix on all config items
294 gitsvnfetch
="$(git config --get-all svn-remote.svn.fetch | head -1)" ||
:
295 gitsvnprefixold
="${gitsvnfetch%%:*}"
296 gitsvnsuffixold
="${gitsvnprefixold##*/}"
297 gitsvnprefixold
="${gitsvnprefixold%$gitsvnsuffixold}"
298 git config
--remove-section 'svn-remote.svnnew' 2>/dev
/null ||
:
299 git config
'svn-remote.svnnew.url' "$gitsvnurl"
300 git config
--get-regexp '^svn-remote\.svn\.' |
301 while read -r sname sval
; do
303 svn-remote.svn.fetch|svn-remote.svn.branches|svn-remote.svn.tags
)
304 sname
="${sname#svn-remote.svn.}"
305 sval
="${sval#$gitsvnprefixold}"
306 bang git config
--add "svn-remote.svnnew.$sname" "${gitsvnprefixnew}$sval"
310 bang git config
-f svn
/.metadata svn-remote.svn.reposRoot
"$gitsvnurl"
311 bang git config
--remove-section svn-remote.svn
312 bang git config
--rename-section svn-remote.svnnew svn-remote.svn
314 bang config_set svnurl
"$svnurl"
316 # remove any stale *.lock files greater than 1 hour old in case
317 # git-svn was killed on the last update because it took too long
318 find -L svn
-type f
-name '*.lock' -mmin +60 -exec rm -f '{}' + 2>/dev
/null ||
:
319 GIROCCO_DIVERT_GIT_SVN_AUTO_GC
=1
320 export GIROCCO_DIVERT_GIT_SVN_AUTO_GC
321 unset GIROCCO_SUPPRESS_AUTO_GC_UPDATE
322 saveconfig
="$GIT_CONFIG_PARAMETERS"
323 git_add_config
'gc.auto=1'
324 git_add_config
'gc.autoPackLimit=1'
325 GIT_DIR
=. bang git svn fetch
--log-window-size=$var_log_window_size --username="$svnuser" --quiet <"$mtlinesfile"
326 GIROCCO_SUPPRESS_AUTO_GC_UPDATE
=1
327 export GIROCCO_SUPPRESS_AUTO_GC_UPDATE
328 unset GIROCCO_DIVERT_GIT_SVN_AUTO_GC
329 unset GIT_CONFIG_PARAMETERS
330 [ -z "$saveconfig" ] ||
{
331 GIT_CONFIG_PARAMETERS
="$saveconfig"
332 export GIT_CONFIG_PARAMETERS
334 # git svn does not preserve group permissions in the svn subdirectory
335 chmod -R ug
+rw
,o
+r svn
336 # git svn also leaves behind ref turds that end with @nnn
337 # We get rid of them now
338 git for-each-ref
--format='%(refname)' |
339 LC_ALL
=C
sed '/^..*@[1-9][0-9]*$/!d; s/^/delete /' |
341 # handle old-style svn setup if it exists
342 if [ -n "$(git config --get remote.origin.url || :)" ]; then
343 GIT_DIR
=. bang git fetch
345 unset GIT_ASKPASS_PASSWORD
347 darcs
://* | darcs
+http
://* | darcs
+https
://*)
348 [ -n "$cfg_mirror_darcs" ] ||
{ echo "Mirroring darcs is disabled" >&2; exit 0; }
350 darcs
://*) darcsurl
="http://${url#darcs://}";;
351 *) darcsurl
="${url#darcs+}";;
353 # remove any stale lock files greater than 1 hour old in case
354 # darcs_fast_export was killed on the last update because it took too long
355 find -L *.darcs
-maxdepth 2 -type f
-name 'lock' -mmin +60 -exec rm -f '{}' + 2>/dev
/null ||
:
356 bang git_darcs_fetch
"$darcsurl"
359 [ -n "$cfg_mirror_bzr" ] ||
{ echo "Mirroring bzr is disabled" >&2; exit 0; }
360 bzrurl
="${url#bzr://}"
361 bang git_bzr_fetch
"$bzrurl"
363 hg
+http
://* | hg
+https
://* | hg
+file://* | hg
+ssh://*)
364 [ -n "$cfg_mirror_hg" ] ||
{ echo "Mirroring hg is disabled" >&2; exit 0; }
365 # We just remove hg+ here, so hg+http://... becomes http://...
367 # Fetch any new updates
368 bang hg
-R "$(pwd)/repo.hg" pull
369 # Do the fast-export | fast-import
373 [ "$url" = "$(git config --get remote.origin.url || :)" ] || bang config_set_raw remote.origin.url
"$url"
375 [ "$(git config --bool fetch.prune 2>/dev/null || :)" != "false" ] || pruneopt
=
376 if ! is_gfi_mirror_url
"$url"; then
378 [ "$(git config --bool girocco.lastupdateclean 2>/dev/null || :)" != "true" ] || lastwasclean
=1
380 [ "$(git config --bool girocco.cleanmirror 2>/dev/null || :)" != "true" ] || nextisclean
=1
381 if [ "$nextisclean" != "$lastwasclean" ]; then
382 if [ -n "$nextisclean" ]; then
383 git config
--replace-all remote.origin.fetch
"+refs/heads/*:refs/heads/*"
384 git config
--add remote.origin.fetch
"+refs/tags/*:refs/tags/*"
385 git config
--add remote.origin.fetch
"+refs/notes/*:refs/notes/*"
386 git config
--add remote.origin.fetch
"+refs/top-bases/*:refs/top-bases/*"
388 git config
--replace-all remote.origin.fetch
"+refs/*:refs/*"
392 # remember the starting time so we can easily detect new packs for fast-import mirrors
393 # we sleep for 1 second after creating .gfipack to make sure all packs are newer
394 if is_gfi_mirror_url
"$url" && [ ! -e .gfipack
]; then
400 git config remotes.default
>/dev
/null
2>&1 || fetcharg
="--all"
402 [ "$show_progress" != "0" ] || fetchcmd
="git fetch -q"
403 if [ -n "$var_have_git_171" ] && [ "${show_progress:-0}" != "0" ]; then
404 # git fetch learned --progress in v1.7.1
405 case "$show_progress" in
407 # full volume progress with all the spammy noise
408 fetchcmd
="git fetch --progress"
411 # a kinder, gentler progress that doesn't leave one
412 # covered all over in exploded bits of spam afterwards
413 fetchcmd
="git_fetch_q_progress"
417 # It's possible for a fetch to actually do something while still returning
418 # a non-zero result (perhaps some of the refs were updated but some were
419 # not -- a malicious Git-impersonation trying to set refs/heads/... refs
420 # to non-commit objects for example).
421 GIT_SSL_NO_VERIFY
=1 bang_catch
eval "$fetchcmd" $pruneopt --multiple "$fetcharg"
422 # If we did fetch anything, don't treat it as an error, but do keep the log;
423 # otherwise invoke bang_failed as for a normal failure
424 if [ "${bang_errcode:-0}" != "0" ]; then
425 save_bang_errcode
="$bang_errcode"
427 if [ -n "$refschanged" ]; then
428 keep_bang_log
="git fetch${pruneopt:+ $pruneopt} --multiple $fetcharg"
430 bang_cmd
="git fetch${pruneopt:+ $pruneopt} --multiple $fetcharg"
431 bang_errcode
="$save_bang_errcode"
435 if ! is_gfi_mirror_url
"$url" && [ "$nextisclean" != "$lastwasclean" ]; then
436 if [ -n "$nextisclean" ]; then
437 # We must manually purge the unclean refs now as even prune won't do it
438 git for-each-ref
--format='%(refname)' |
440 -e '/^refs\/heads\//d' \
441 -e '/^refs\/tags\//d' \
442 -e '/^refs\/notes\//d' \
443 -e '/^refs\/top-bases\//d' \
447 git config
--bool girocco.lastupdateclean
${nextisclean:-0}
449 if [ -e .gfipack
] && is_gfi_mirror_url
"$url"; then
450 find -L objects
/pack
-type f
-newer .gfipack
-name "pack-$octet20*.pack" -print >>gfi-packs
456 # The objects subdirectories permissions must be updated now.
457 # In the case of a dumb http clone, the permissions will not be correct
458 # (missing group write) despite the core.sharedrepository=1 setting!
459 # The objects themselves seem to have the correct permissions.
460 # This problem appears to have been fixed in the most recent git versions.
462 [ "$cfg_permission_control" != "Hooks" ] || perms
=go
+w
463 chmod $perms $
(find -L objects
-maxdepth 1 -type d
) 2>/dev
/null ||
:
465 bang git update-server-info
467 # We maintain the last refresh date in two places deliberately
468 # so that it's available as part of the config data and also
469 # as a standalone file timestamp that can be accessed without git.
470 bang config_set lastrefresh
"$(date "$datefmt")"
471 { >.last_refresh
; } 2>/dev
/null ||
:
473 # Check to see if any refs changed
476 # Pack all refs if any changed to keep things as efficient as possible
477 # Project mirror updates do not occur that often therefore this is a win
478 # However, if pack-refs fails for some reason, we can just ignore and continue
479 # The "--prune" option is the default since v1.5.0 but it serves as "documentation" here
480 [ -z "$refschanged" ] || git pack-refs
--all --prune ||
:
482 # Force a mini-gc if $Girocco::Config::delay_gfi_redelta is false and there's
483 # at least one gfi pack present now
484 if [ -z "$cfg_delay_gfi_redelta" ] && ! [ -e .needsgc
] &&
485 [ -f gfi-packs
] && [ -s gfi-packs
] && is_gfi_mirror_url
"$url"; then
489 # Activate a mini-gc if needed
490 check_and_set_needsgc
492 # Look at which refs changed and trigger ref-change for these
493 sockpath
="$cfg_chroot/etc/taskd.socket"
494 if [ -n "$refschanged" ]; then
495 bang config_set lastreceive
"$(date '+%a, %d %b %Y %T %z')"
496 # We always use UTC for the log timestamp so that chroot and non-chroot match up.
497 # We don't have to worry about multiple log files since only one update runs
498 lognamets
="$(TZ=UTC date '+%Y%m%d_%H%M%S')"
499 loghhmmss
="${lognamets##*_}"
500 logname
="reflogs/${lognamets%%_*}"
501 # We freshen the mod time to now on any old or new ref that is a loose object
502 # For old refs we do it so we will be able to keep them around for 1 day
503 # For new refs we do it in case we are about to run gc and the new ref
504 # actually points to an oldish loose object that had been unreachable
505 # We probably do not need to do it for new refs as Git tries to do that,
506 # but since we're already doing it for old refs (which Git does not do),
507 # it's almost no extra work for new refs, just in case.
509 echo "ref-changes %$proj% $proj"
510 LC_ALL
=C
join .refs-before .refs-after |
511 LC_ALL
=C
sed -e '/^[^ ][^ ]* \([^ ][^ ]*\) \1$/d' |
512 while read ref old new
; do
513 echo "$loghhmmss $old $new $ref" >&3
514 freshen_loose_objects
"$old" "$new"
515 echo "$old $new $ref"
517 LC_ALL
=C
join -v 1 .refs-before .refs-after |
518 while read ref old
; do
519 echo "$loghhmmss $old 0000000000000000000000000000000000000000 $ref" >&3
520 freshen_loose_objects
"$old"
521 echo "$old 0000000000000000000000000000000000000000 $ref"
523 LC_ALL
=C
join -v 2 .refs-before .refs-after |
524 while read ref new
; do
525 echo "$loghhmmss 0000000000000000000000000000000000000000 $new $ref" >&3
526 freshen_loose_objects
"$new"
527 echo "0000000000000000000000000000000000000000 $new $ref"
529 git for-each-ref
--format='%(objectname) %(objectname) %(refname)' refs
/heads
530 echo "done ref-changes %$proj% $proj"
531 } >.refs-temp
3>>"$logname"
532 if [ -S "$sockpath" ]; then
534 nc_openbsd
-w 15 -U "$sockpath" <.refs-temp ||
:
537 bang config_set lastchange
"$(date '+%a, %d %b %Y %T %z')"
538 bang_eval
"git for-each-ref --sort=-committerdate --format='%(committerdate:iso8601)' \
539 --count=1 refs/heads >info/lastactivity"
540 ! [ -d htmlcache
] ||
{ >htmlcache
/changed
; } 2>/dev
/null ||
:
541 rm -f .delaygc .allowgc
543 [ "${cfg_autogchack:-0}" != "0" ] &&
544 [ "$(git config --get --bool girocco.autogchack 2>/dev/null)" != "false" ]
546 mv -f .refs-after .refs-last
550 # If the repository does not yet have a valid HEAD symref try to set one
551 # If an empty repository was cloned and then later becomes unempty you just
552 # lose out on the fancy "symref=HEAD:" logic and get this version instead
553 check_and_set_head ||
:
555 rm -f .refs-before .refs-after .refs-temp FETCH_HEAD
558 [ -z "$mailaddrs" ] ||
! was_banged_message_sent ||
560 echo "$proj update succeeded - failure recovery"
561 echo "this status message may be disabled on the project admin page"
562 } | mailref
"update@$cfg_gitweburl/$proj.git" -s "[$cfg_name] $proj update succeeded" "$mailaddrs" ||
:
566 if [ -n "$keep_bang_log" ] && [ -s "$bang_log" ]; then
567 cat "$bang_log" >.banglog
569 echo "$keep_bang_log failed with error code $save_bang_errcode" >>.banglog
572 progress
"- [$proj] update ($(date))"