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
18 freshen_loose_objects
() {
23 _list
="$_list objects/$_shard/$_fn"
25 [ -z "$_list" ] ||
touch -c -m $_list ||
:
28 # darcs fast-export | git fast-import with error handling
34 { read -r _err1 ||
:; read -r _err2 ||
:; } <<-EOT
36 exec 4>&3 3>&1 1>&4 4>&-
39 "$cfg_basedir"/bin/darcs-fast-export \
40 --export-marks="$(pwd)/dfe-marks" \
41 --import-marks="$(pwd)/dfe-marks" "$1" 3>&- || _e1=$?
47 --export-marks="$(pwd)/gfi-marks" \
48 --export-pack-edges="$(pwd)/gfi-packs" \
49 --import-marks="$(pwd)/gfi-marks" \
50 --force 3>&- || _e2=$?
56 [ "$_err1" = 0 -a "$_err2" = 0 ]
60 # bzr fast-export | git fast-import with error handling
68 { read -r _err1 ||
:; read -r _err2 ||
:; } <<-EOT
70 exec 4>&3 3>&1 1>&4 4>&-
73 bzr fast-export --plain \
74 --export-marks="$(pwd)/dfe-marks" \
75 --import-marks="$(pwd)/dfe-marks" "$1" 3>&- || _e1=$?
81 --export-marks="$(pwd)/gfi-marks" \
82 --export-pack-edges="$(pwd)/gfi-packs" \
83 --import-marks="$(pwd)/gfi-marks" \
84 --force 3>&- || _e2=$?
90 [ "$_err1" = 0 -a "$_err2" = 0 ]
94 [ -n "$cfg_mirror" ] ||
{ echo "Mirroring is disabled" >&2; exit 0; }
97 [ "$cfg_permission_control" != "Hooks" ] ||
umask 000
100 cd "$cfg_reporoot/$proj.git"
102 trap 'if [ $? != 0 ]; then echo "update failed dir: $PWD" >&2; fi; rm -f "$bang_log"' EXIT
106 if check_interval lastrefresh
$cfg_min_mirror_interval; then
107 progress
"= [$proj] update skip (last at $(config_get lastrefresh))"
110 progress
"+ [$proj] update (`date`)"
112 # Any pre-existing FETCH_HEAD from a previous fetch failed or not is garbage
115 # A previous failed update attempt can leave a huge tmp_pack_XXXXXX file behind.
116 # Since no pushes are allowed to mirrors, we know that any such files that exist
117 # at this point in time are garbage and can be safely deleted, we do not even
118 # need to check how old they are. A tmp_idx_XXXXXX file is also created during
119 # the later stages of the fetch process, so we kill any of those as well.
120 find objects
/pack
-maxdepth 1 -type f
-name "tmp_pack_?*" -print0 |
xargs -0 rm -f
121 find objects
/pack
-maxdepth 1 -type f
-name "tmp_idx_?*" -print0 |
xargs -0 rm -f
127 bang
echo "Project: $proj"
129 mail="$(config_get owner || :)"
130 url
="$(config_get baseurl || :)"
131 case "$url" in *" "*|
*" "*|
"")
132 bang_eval
'echo "Bad mirror URL (\"$url\")"; ! :'
135 bang
echo "Mirroring from URL \"$url\""
137 statusok
="$(git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
139 [ "$statusok" = "false" -o -z "$mail" ] || mailaddrs
="$mail"
140 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] || \
141 if [ -z "$mailaddrs" ]; then mailaddrs
="$cfg_admin"; else mailaddrs
="$mailaddrs,$cfg_admin"; fi
143 bang_eval
"git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
144 bang_eval
"LC_ALL=C sort -k1b,1 <.refs-temp >.refs-before"
145 ! [ -e .delaygc
] ||
> .allowgc ||
:
148 svn
://* | svn
+http
://* | svn
+https
://* | svn
+file://*)
149 [ -n "$cfg_mirror_svn" ] ||
{ echo "Mirroring svn is disabled" >&2; exit 0; }
150 # Use an 'anonsvn' username as is commonly used for anonymous svn
151 # Use an 'anonsvn' password as is commonly used for anonymous svn
152 GIT_ASKPASS_PASSWORD
=anonsvn
153 export GIT_ASKPASS_PASSWORD
154 # Update the git svn url to match baseurl but be cognizant of any
155 # needed prefix changes. See the comments in taskd/clone.sh about
156 # why we need to put up with a prefix in the first place.
159 svnurlold
="$(config_get svnurl || :)"
160 if [ "$svnurl" != "$svnurlold" ]; then
161 # We better already have an svn-remote.svn.fetch setting
162 bang
test -n "$(git config --get-all svn-remote.svn.fetch || :)"
163 # the only way to truly know what the proper prefix is
164 # is to attempt a fresh git-svn init -s on the new url
165 rm -rf svn-new-url ||
:
166 # We require svn info to succeed on the URL otherwise it's
167 # simply not a valid URL and without using -s on the init it
168 # will not otherwise be tested until the fetch
169 bang
eval 'svn --non-interactive --username anonsvn --password anonsvn info "$svnurl" > /dev/null'
170 bang mkdir svn-new-url
171 GIT_DIR
=svn-new-url bang git init
--bare --quiet
172 # We initially use -s for the init which will possibly shorten
173 # the URL. However, the shortening can fail if a password is
174 # not required for the longer version but is for the shorter,
175 # so try again without -s if the -s version fails.
176 cmdstr
='git svn init --username=anonsvn --prefix "" -s "$svnurl" < /dev/null > /dev/null 2>&1 || '
177 cmdstr
="$cmdstr"'git svn init --username=anonsvn --prefix "" "$svnurl" < /dev/null > /dev/null 2>&1'
178 GIT_DIR
=svn-new-url bang
eval "$cmdstr"
179 gitsvnurl
="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.url || :)"
180 gitsvnfetch
="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.fetch || :)"
181 gitsvnprefixnew
="${gitsvnfetch%%:*}"
182 gitsvnsuffixnew
="${gitsvnprefixnew##*/}"
183 gitsvnprefixnew
="${gitsvnprefixnew%$gitsvnsuffixnew}"
184 rm -rf svn-new-url ||
:
185 # Using GIT_DIR= with bang leaves it set to svn-new-url, so reset it to .
187 if [ "$gitsvnurl" != "$(git config --get svn-remote.svn.url || :)" ]; then
188 # The url has been changed.
189 # We must update the url and replace the prefix on all config items
190 gitsvnfetch
="$(git config --get-all svn-remote.svn.fetch | head -1 || :)"
191 gitsvnprefixold
="${gitsvnfetch%%:*}"
192 gitsvnsuffixold
="${gitsvnprefixold##*/}"
193 gitsvnprefixold
="${gitsvnprefixold%$gitsvnsuffixold}"
194 git config
--remove-section 'svn-remote.svnnew' 2>/dev
/null ||
:
195 git config
'svn-remote.svnnew.url' "$gitsvnurl"
196 { git config
--get-regexp '^svn-remote\.svn\.' ||
:; } | \
197 { while read sname sval
; do
199 svn-remote.svn.fetch|svn-remote.svn.branches|svn-remote.svn.tags
)
200 sname
="${sname#svn-remote.svn.}"
201 sval
="${sval#$gitsvnprefixold}"
202 bang git config
--add "svn-remote.svnnew.$sname" "${gitsvnprefixnew}$sval"
205 bang git config
-f svn
/.metadata svn-remote.svn.reposRoot
"$gitsvnurl"
206 bang git config
--remove-section svn-remote.svn
207 bang git config
--rename-section svn-remote.svnnew svn-remote.svn
209 bang config_set svnurl
"$svnurl"
211 # remove any stale *.lock files greater than 1 hour old in case
212 # git-svn was killed on the last update because it took too long
213 find svn
-type f
-name '*.lock' -mmin +60 -print0 2>/dev
/null |
xargs -0 rm -f
214 GIT_DIR
=. bang git svn fetch
--log-window-size=$var_log_window_size --username=anonsvn
--quiet < /dev
/null
215 # git svn does not preserve group permissions in the svn subdirectory
216 chmod -R ug
+rw
,o
+r svn
217 # git svn also leaves behind ref turds that end with @nnn
218 # We get rid of them now
219 git for-each-ref
--format='%(objectname) %(refname)' | \
220 { while read sha1 ref
; do
222 ?
*@
[1-9]|?
*@
[1-9][0-9]|?
*@
[1-9][0-9][0-9]|?
*@
[1-9][0-9][0-9][0-9]|\
223 ?
*@
[1-9][0-9][0-9][0-9][0-9]|?
*@
[1-9][0-9][0-9][0-9][0-9][0-9]|\
224 ?
*@
[1-9][0-9][0-9][0-9][0-9][0-9][0-9]|\
225 ?
*@
[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])
226 git update-ref
-d "$ref"
229 unset GIT_ASKPASS_PASSWORD
232 [ -n "$cfg_mirror_darcs" ] ||
{ echo "Mirroring darcs is disabled" >&2; exit 0; }
233 httpurl
="http://${url#darcs://}"
234 # remove any stale lock files greater than 1 hour old in case
235 # darcs_fast_export was killed on the last update because it took too long
236 find *.darcs
-maxdepth 2 -type f
-name 'lock' -mmin +60 -print0 2>/dev
/null |
xargs -0 rm -f
237 bang git_darcs_fetch
"$httpurl"
240 [ -n "$cfg_mirror_bzr" ] ||
{ echo "Mirroring bzr is disabled" >&2; exit 0; }
241 bzrurl
="${url#bzr://}"
242 bang git_bzr_fetch
"$bzrurl"
244 hg
+http
://* | hg
+https
://*)
245 [ -n "$cfg_mirror_hg" ] ||
{ echo "Mirroring hg is disabled" >&2; exit 0; }
246 # We just remove hg+ here, so hg+http://... becomes http://...
248 # Fetch any new updates
249 bang hg
-R "$(pwd)/repo.hg" pull
250 # Do the fast-export | fast-import
254 [ "$url" = "$(git config --get remote.origin.url || :)" ] || bang config_set_raw remote.origin.url
"$url"
256 [ "$(git config --bool fetch.prune 2>/dev/null || :)" != "false" ] || pruneopt
=
257 GIT_SSL_NO_VERIFY
=1 bang git remote update
$pruneopt
261 # The objects subdirectories permissions must be updated now.
262 # In the case of a dumb http clone, the permissions will not be correct
263 # (missing group write) despite the core.sharedrepository=1 setting!
264 # The objects themselves seem to have the correct permissions.
265 # This problem appears to have been fixed in the most recent git versions.
267 [ "$cfg_permission_control" != "Hooks" ] || perms
=go
+w
268 chmod $perms $
(find objects
-maxdepth 1 -type d
) 2>/dev
/null ||
:
270 bang git update-server-info
272 # We maintain the last refresh date in two places deliberately
273 # so that it's available as part of the config data and also
274 # as a standalone file timestamp that can be accessed without git.
275 bang config_set lastrefresh
"$(date "$datefmt")"
276 { >.last_refresh
; } 2>/dev
/null ||
:
278 # Look at which refs changed and trigger ref-change for these
279 bang_eval
"git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
280 bang_eval
"LC_ALL=C sort -k1b,1 <.refs-temp >.refs-after"
281 sockpath
="$cfg_chroot/etc/taskd.socket"
282 if ! cmp -s .refs-before .refs-after
; then
283 bang config_set lastreceive
"$(date '+%a, %d %b %Y %T %z')"
284 # We freshen the mod time to now on any old or new ref that is a loose object
285 # For old refs we do it so we will be able to keep them around for 1 day
286 # For new refs we do it in case we are about to run gc and the new ref
287 # actually points to an oldish loose object that had been unreachable
288 # We probably do not need to do it for new refs as Git tries to do that,
289 # but since we're already doing it for old refs (which Git does not do),
290 # it's almost no extra work for new refs, just in case.
293 echo "ref-changes %$proj% $proj" ||
:
294 LC_ALL
=C
join -j 1 .refs-before .refs-after |
295 while read ref old new
; do
296 [ "$old" != "$new" ] ||
continue
297 freshen_loose_objects
"$old" "$new"
298 echo "$old $new $ref" ||
:
300 LC_ALL
=C
join -j 1 -v 1 .refs-before .refs-after |
301 while read ref old
; do
302 freshen_loose_objects
"$old"
303 echo "$old 0000000000000000000000000000000000000000 $ref" ||
:
305 LC_ALL
=C
join -j 1 -v 2 .refs-before .refs-after |
306 while read ref new
; do
307 freshen_loose_objects
"$new"
308 echo "0000000000000000000000000000000000000000 $new $ref" ||
:
311 if [ -S "$sockpath" ]; then
312 nc_openbsd
-w 1 -U "$sockpath" ||
:
314 while read -r line
; do
319 bang config_set lastchange
"$(date '+%a, %d %b %Y %T %z')"
320 bang_eval
"git for-each-ref --sort=-committerdate --format='%(committerdate:iso8601)' \
321 --count=1 refs/heads > info/lastactivity"
322 ! [ -d htmlcache
] ||
{ >htmlcache
/changed
; } 2>/dev
/null ||
:
323 rm -f .delaygc .allowgc
326 rm -f .refs-before .refs-after .refs-temp FETCH_HEAD
329 [ -z "$mailaddrs" ] ||
331 echo "$proj update succeeded - failure recovery"
332 echo "this status message may be disabled on the project admin page"
333 } |
mail -s "[$cfg_name] $proj update succeeded" "$mailaddrs" ||
:
337 progress
"- [$proj] update (`date`)"