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 # 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
() {
24 _list
="$_list objects/$_shard/$_fn"
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
38 { read -r _err1 ||
:; read -r _err2 ||
:; } <<-EOT
40 exec 4>&3 3>&1 1>&4 4>&-
43 "$cfg_basedir"/bin/darcs-fast-export \
44 --export-marks="$(pwd)/dfe-marks" \
45 --import-marks="$(pwd)/dfe-marks" "$1" 3>&- || _e1=$?
51 --export-marks="$(pwd)/gfi-marks" \
52 --export-pack-edges="$(pwd)/gfi-packs" \
53 --import-marks="$(pwd)/gfi-marks" \
54 --force 3>&- || _e2=$?
60 [ "$_err1" = 0 -a "$_err2" = 0 ]
64 # bzr fast-export | git fast-import with error handling
72 { read -r _err1 ||
:; read -r _err2 ||
:; } <<-EOT
74 exec 4>&3 3>&1 1>&4 4>&-
77 bzr fast-export --plain \
78 --export-marks="$(pwd)/dfe-marks" \
79 --import-marks="$(pwd)/dfe-marks" "$1" 3>&- || _e1=$?
85 --export-marks="$(pwd)/gfi-marks" \
86 --export-pack-edges="$(pwd)/gfi-packs" \
87 --import-marks="$(pwd)/gfi-marks" \
88 --force 3>&- || _e2=$?
94 [ "$_err1" = 0 -a "$_err2" = 0 ]
98 [ -n "$cfg_mirror" ] ||
{ echo "Mirroring is disabled" >&2; exit 0; }
101 [ "$cfg_permission_control" != "Hooks" ] ||
umask 000
104 cd "$cfg_reporoot/$proj.git"
106 trap 'if [ $? != 0 ]; then echo "update failed dir: $PWD" >&2; fi; rm -f "$bang_log"' EXIT
110 if check_interval lastrefresh
$cfg_min_mirror_interval; then
111 progress
"= [$proj] update skip (last at $(config_get lastrefresh))"
114 progress
"+ [$proj] update (`date`)"
116 # Any pre-existing FETCH_HEAD from a previous fetch failed or not is garbage
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
131 bang
echo "Project: $proj"
133 mail="$(config_get owner || :)"
134 url
="$(config_get baseurl || :)"
135 case "$url" in *" "*|
*" "*|
"")
136 bang_eval
'echo "Bad mirror URL (\"$url\")"; ! :'
139 bang
echo "Mirroring from URL \"$url\""
141 statusok
="$(git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
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 ||
:
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
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 .
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
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"
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
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
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"
242 unset GIT_ASKPASS_PASSWORD
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"
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://...
261 # Fetch any new updates
262 bang hg
-R "$(pwd)/repo.hg" pull
263 # Do the fast-export | fast-import
267 [ "$url" = "$(git config --get remote.origin.url || :)" ] || bang config_set_raw remote.origin.url
"$url"
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 # remember the starting time so we can easily detect new packs for fast-import mirrors
275 # we sleep for 1 second after creating .gfipack to make sure all packs are newer
276 if is_gfi_mirror_url
"$url" && [ ! -e .gfipack
]; then
281 GIT_SSL_NO_VERIFY
=1 bang git remote update
$pruneopt
282 if is_gfi_mirror_url
"$url"; then
283 find objects
/pack
-type f
-newer .gfipack
-name "pack-$octet20.pack" -print >>gfi-packs
289 # The objects subdirectories permissions must be updated now.
290 # In the case of a dumb http clone, the permissions will not be correct
291 # (missing group write) despite the core.sharedrepository=1 setting!
292 # The objects themselves seem to have the correct permissions.
293 # This problem appears to have been fixed in the most recent git versions.
295 [ "$cfg_permission_control" != "Hooks" ] || perms
=go
+w
296 chmod $perms $
(find objects
-maxdepth 1 -type d
) 2>/dev
/null ||
:
298 bang git update-server-info
300 # We maintain the last refresh date in two places deliberately
301 # so that it's available as part of the config data and also
302 # as a standalone file timestamp that can be accessed without git.
303 bang config_set lastrefresh
"$(date "$datefmt")"
304 { >.last_refresh
; } 2>/dev
/null ||
:
306 # Check to see if any refs changed
307 bang_eval
"git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
308 bang_eval
"LC_ALL=C sort -b -k1,1 <.refs-temp >.refs-after"
310 cmp -s .refs-before .refs-after || refschanged
=1
312 # There's no way to get git svn to fetch packs, so we always need to run
313 # a mini-gc afterwards if svn actually fetched anything, but that's okay
314 # because it will be very quick
315 if [ -z "$refschanged" -a -n "$svnpackcreated" ]; then
316 # we created the .svnpack but didn't actually fetch anything
317 # so remove it to avoid forcing a mini-gc if not necessary
320 if [ -e .svnpack
] && ! [ -e .needsgc
]; then
324 # Activate a mini-gc if there are at least 20 packs present now
325 if ! [ -e .needsgc
]; then
326 packs
="$(list_packs --quiet --count --exclude-no-idx objects/pack || :)"
327 if [ -n "$packs" ] && [ "$packs" -ge 20 ]; then
332 # Look at which refs changed and trigger ref-change for these
333 sockpath
="$cfg_chroot/etc/taskd.socket"
334 if [ -n "$refschanged" ]; then
335 bang config_set lastreceive
"$(date '+%a, %d %b %Y %T %z')"
336 # We freshen the mod time to now on any old or new ref that is a loose object
337 # For old refs we do it so we will be able to keep them around for 1 day
338 # For new refs we do it in case we are about to run gc and the new ref
339 # actually points to an oldish loose object that had been unreachable
340 # We probably do not need to do it for new refs as Git tries to do that,
341 # but since we're already doing it for old refs (which Git does not do),
342 # it's almost no extra work for new refs, just in case.
344 echo "ref-changes %$proj% $proj"
345 LC_ALL
=C
join .refs-before .refs-after |
346 LC_ALL
=C
sed -e '/^[^ ][^ ]* \([^ ][^ ]*\) \1$/d' |
347 while read ref old new
; do
348 freshen_loose_objects
"$old" "$new"
349 echo "$old $new $ref"
351 LC_ALL
=C
join -v 1 .refs-before .refs-after |
352 while read ref old
; do
353 freshen_loose_objects
"$old"
354 echo "$old 0000000000000000000000000000000000000000 $ref"
356 LC_ALL
=C
join -v 2 .refs-before .refs-after |
357 while read ref new
; do
358 freshen_loose_objects
"$new"
359 echo "0000000000000000000000000000000000000000 $new $ref"
361 git for-each-ref
--format='%(objectname) %(objectname) %(refname)' refs
/heads
363 if [ -S "$sockpath" ]; then
364 nc_openbsd
-w 15 -U "$sockpath" <.refs-temp ||
:
366 bang config_set lastchange
"$(date '+%a, %d %b %Y %T %z')"
367 bang_eval
"git for-each-ref --sort=-committerdate --format='%(committerdate:iso8601)' \
368 --count=1 refs/heads > info/lastactivity"
369 ! [ -d htmlcache
] ||
{ >htmlcache
/changed
; } 2>/dev
/null ||
:
370 rm -f .delaygc .allowgc
373 rm -f .refs-before .refs-after .refs-temp FETCH_HEAD
376 [ -z "$mailaddrs" ] ||
378 echo "$proj update succeeded - failure recovery"
379 echo "this status message may be disabled on the project admin page"
380 } |
mail -s "[$cfg_name] $proj update succeeded" "$mailaddrs" ||
:
384 progress
"- [$proj] update (`date`)"