clone/update: match more svn URLs
[girocco.git] / jobd / update.sh
blob452ea5d4b1327e4a568a424039aa5c76b0c81240
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 # darcs fast-export | git fast-import with error handling
16 git_darcs_fetch() (
17 set_utf8_locale
18 _err1=
19 _err2=
20 exec 3>&1
21 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
23 exec 4>&3 3>&1 1>&4 4>&-
25 _e1=0
26 "$cfg_basedir"/bin/darcs-fast-export \
27 --export-marks="$(pwd)/dfe-marks" \
28 --import-marks="$(pwd)/dfe-marks" "$1" 3>&- || _e1=$?
29 echo $_e1 >&3
30 } | \
32 _e2=0
33 git fast-import \
34 --export-marks="$(pwd)/gfi-marks" \
35 --export-pack-edges="$(pwd)/gfi-packs" \
36 --import-marks="$(pwd)/gfi-marks" \
37 --force 3>&- || _e2=$?
38 echo $_e2 >&3
41 EOT
42 exec 3>&-
43 [ "$_err1" = 0 -a "$_err2" = 0 ]
44 return $?
47 # bzr fast-export | git fast-import with error handling
48 git_bzr_fetch() (
49 set_utf8_locale
50 BZR_LOG=/dev/null
51 export BZR_LOG
52 _err1=
53 _err2=
54 exec 3>&1
55 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
57 exec 4>&3 3>&1 1>&4 4>&-
59 _e1=0
60 bzr fast-export --plain \
61 --export-marks="$(pwd)/dfe-marks" \
62 --import-marks="$(pwd)/dfe-marks" "$1" 3>&- || _e1=$?
63 echo $_e1 >&3
64 } | \
66 _e2=0
67 git fast-import \
68 --export-marks="$(pwd)/gfi-marks" \
69 --export-pack-edges="$(pwd)/gfi-packs" \
70 --import-marks="$(pwd)/gfi-marks" \
71 --force 3>&- || _e2=$?
72 echo $_e2 >&3
75 EOT
76 exec 3>&-
77 [ "$_err1" = 0 -a "$_err2" = 0 ]
78 return $?
81 [ -n "$cfg_mirror" ] || { echo "Mirroring is disabled" >&2; exit 0; }
83 umask 002
84 [ "$cfg_permission_control" != "Hooks" ] || umask 000
86 proj="${1%.git}"
87 cd "$cfg_reporoot/$proj.git"
89 trap 'if [ $? != 0 ]; then echo "update failed dir: $PWD" >&2; fi; rm -f "$bang_log"' EXIT
90 trap 'exit 130' INT
91 trap 'exit 143' TERM
93 if check_interval lastrefresh $cfg_min_mirror_interval; then
94 progress "= [$proj] update skip (last at $(config_get lastrefresh))"
95 exit 0
97 progress "+ [$proj] update (`date`)"
99 # Any pre-existing FETCH_HEAD from a previous fetch failed or not is garbage
100 rm -f FETCH_HEAD
102 # A previous failed update attempt can leave a huge tmp_pack_XXXXXX file behind.
103 # Since no pushes are allowed to mirrors, we know that any such files that exist
104 # at this point in time are garbage and can be safely deleted, we do not even
105 # need to check how old they are. A tmp_idx_XXXXXX file is also created during
106 # the later stages of the fetch process, so we kill any of those as well.
107 find objects/pack -maxdepth 1 -type f -name "tmp_pack_?*" -print0 | xargs -0 rm -f
108 find objects/pack -maxdepth 1 -type f -name "tmp_idx_?*" -print0 | xargs -0 rm -f
110 bang_setup
111 bang_once=1
112 bang_action="update"
114 bang echo "Project: $proj"
115 bang echo ""
116 mail="$(config_get owner || :)"
117 url="$(config_get baseurl || :)"
118 case "$url" in *" "*|*" "*|"")
119 bang_eval 'echo "Bad mirror URL (\"$url\")"; ! :'
120 exit 1
121 esac
122 bang echo "Mirroring from URL \"$url\""
123 bang echo ""
124 statusok="$(git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
125 mailaddrs=''
126 [ "$statusok" = "false" -o -z "$mail" ] || mailaddrs="$mail"
127 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] || \
128 if [ -z "$mailaddrs" ]; then mailaddrs="$cfg_admin"; else mailaddrs="$mailaddrs,$cfg_admin"; fi
130 bang_eval "git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
131 bang_eval "LC_ALL=C sort -k1b,1 <.refs-temp >.refs-before"
133 case "$url" in
134 svn://* | svn+http://* | svn+https://* | svn+file://*)
135 [ -n "$cfg_mirror_svn" ] || { echo "Mirroring svn is disabled" >&2; exit 0; }
136 # Use an 'anonsvn' username as is commonly used for anonymous svn
137 # Use an 'anonsvn' password as is commonly used for anonymous svn
138 GIT_ASKPASS_PASSWORD=anonsvn
139 export GIT_ASKPASS_PASSWORD
140 # Update the git svn url to match baseurl but be cognizant of any
141 # needed prefix changes. See the comments in taskd/clone.sh about
142 # why we need to put up with a prefix in the first place.
143 svnurl="${url#svn+}"
144 svnurl="${svnurl%/}"
145 svnurlold="$(config_get svnurl || :)"
146 if [ "$svnurl" != "$svnurlold" ]; then
147 # We better already have an svn-remote.svn.fetch setting
148 bang test -n "$(git config --get-all svn-remote.svn.fetch || :)"
149 # the only way to truly know what the proper prefix is
150 # is to attempt a fresh git-svn init -s on the new url
151 rm -rf svn-new-url || :
152 # We require svn info to succeed on the URL otherwise it's
153 # simply not a valid URL and without using -s on the init it
154 # will not otherwise be tested until the fetch
155 bang eval 'svn --non-interactive --username anonsvn --password anonsvn info "$svnurl" > /dev/null'
156 bang mkdir svn-new-url
157 GIT_DIR=svn-new-url bang git init --bare --quiet
158 # We initially use -s for the init which will possibly shorten
159 # the URL. However, the shortening can fail if a password is
160 # not required for the longer version but is for the shorter,
161 # so try again without -s if the -s version fails.
162 cmdstr='git svn init --username=anonsvn --prefix "" -s "$svnurl" < /dev/null > /dev/null 2>&1 || '
163 cmdstr="$cmdstr"'git svn init --username=anonsvn --prefix "" "$svnurl" < /dev/null > /dev/null 2>&1'
164 GIT_DIR=svn-new-url bang eval "$cmdstr"
165 gitsvnurl="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.url || :)"
166 gitsvnfetch="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.fetch || :)"
167 gitsvnprefixnew="${gitsvnfetch%%:*}"
168 gitsvnsuffixnew="${gitsvnprefixnew##*/}"
169 gitsvnprefixnew="${gitsvnprefixnew%$gitsvnsuffixnew}"
170 rm -rf svn-new-url || :
171 # Using GIT_DIR= with bang leaves it set to svn-new-url, so reset it to .
172 GIT_DIR=.
173 if [ "$gitsvnurl" != "$(git config --get svn-remote.svn.url || :)" ]; then
174 # The url has been changed.
175 # We must update the url and replace the prefix on all config items
176 gitsvnfetch="$(git config --get-all svn-remote.svn.fetch | head -1 || :)"
177 gitsvnprefixold="${gitsvnfetch%%:*}"
178 gitsvnsuffixold="${gitsvnprefixold##*/}"
179 gitsvnprefixold="${gitsvnprefixold%$gitsvnsuffixold}"
180 git config --remove-section 'svn-remote.svnnew' 2>/dev/null || :
181 git config 'svn-remote.svnnew.url' "$gitsvnurl"
182 { git config --get-regexp '^svn-remote\.svn\.' || :; } | \
183 { while read sname sval; do
184 case "$sname" in
185 svn-remote.svn.fetch|svn-remote.svn.branches|svn-remote.svn.tags)
186 sname="${sname#svn-remote.svn.}"
187 sval="${sval#$gitsvnprefixold}"
188 bang git config --add "svn-remote.svnnew.$sname" "${gitsvnprefixnew}$sval"
189 esac
190 done; }
191 bang git config -f svn/.metadata svn-remote.svn.reposRoot "$gitsvnurl"
192 bang git config --remove-section svn-remote.svn
193 bang git config --rename-section svn-remote.svnnew svn-remote.svn
195 bang config_set svnurl "$svnurl"
197 # remove any stale *.lock files greater than 1 hour old in case
198 # git-svn was killed on the last update because it took too long
199 find svn -type f -name '*.lock' -mmin +60 -print0 2>/dev/null | xargs -0 rm -f
200 GIT_DIR=. bang git svn fetch --log-window-size=$var_log_window_size --username=anonsvn --quiet < /dev/null
201 # git svn does not preserve group permissions in the svn subdirectory
202 chmod -R ug+rw,o+r svn
203 # git svn also leaves behind ref turds that end with @nnn
204 # We get rid of them now
205 git for-each-ref --format='%(objectname) %(refname)' | \
206 { while read sha1 ref; do
207 case "$ref" in
208 ?*@[1-9]|?*@[1-9][0-9]|?*@[1-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9]|\
209 ?*@[1-9][0-9][0-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9][0-9][0-9]|\
210 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9]|\
211 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])
212 git update-ref -d "$ref"
213 esac
214 done; }
215 unset GIT_ASKPASS_PASSWORD
217 darcs://*)
218 [ -n "$cfg_mirror_darcs" ] || { echo "Mirroring darcs is disabled" >&2; exit 0; }
219 httpurl="http://${url#darcs://}"
220 # remove any stale lock files greater than 1 hour old in case
221 # darcs_fast_export was killed on the last update because it took too long
222 find *.darcs -maxdepth 2 -type f -name 'lock' -mmin +60 -print0 2>/dev/null | xargs -0 rm -f
223 bang git_darcs_fetch "$httpurl"
225 bzr://*)
226 [ -n "$cfg_mirror_bzr" ] || { echo "Mirroring bzr is disabled" >&2; exit 0; }
227 bzrurl="${url#bzr://}"
228 bang git_bzr_fetch "$bzrurl"
230 hg+http://* | hg+https://*)
231 [ -n "$cfg_mirror_hg" ] || { echo "Mirroring hg is disabled" >&2; exit 0; }
232 # We just remove hg+ here, so hg+http://... becomes http://...
233 hgurl="${url#hg+}"
234 # Fetch any new updates
235 bang hg -R "$(pwd)/repo.hg" pull
236 # Do the fast-export | fast-import
237 bang git_hg_fetch
240 [ "$url" = "$(git config --get remote.origin.url || :)" ] || bang config_set_raw remote.origin.url "$url"
241 pruneopt=--prune
242 [ "$(git config --bool fetch.prune 2>/dev/null || :)" != "false" ] || pruneopt=
243 GIT_SSL_NO_VERIFY=1 bang git remote update $pruneopt
245 esac
247 # The objects subdirectories permissions must be updated now.
248 # In the case of a dumb http clone, the permissions will not be correct
249 # (missing group write) despite the core.sharedrepository=1 setting!
250 # The objects themselves seem to have the correct permissions.
251 # This problem appears to have been fixed in the most recent git versions.
252 perms=g+w
253 [ "$cfg_permission_control" != "Hooks" ] || perms=go+w
254 chmod $perms $(find objects -maxdepth 1 -type d) 2>/dev/null || :
256 bang git update-server-info
257 bang config_set lastrefresh "$(date "$datefmt")"
259 # Look at which refs changed and trigger ref-change for these
260 bang_eval "git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
261 bang_eval "LC_ALL=C sort -k1b,1 <.refs-temp >.refs-after"
262 sockpath="$cfg_chroot/etc/taskd.socket"
263 if ! cmp -s .refs-before .refs-after; then
264 bang config_set lastreceive "$(date '+%a, %d %b %Y %T %z')"
265 if [ -S "$sockpath" ]; then
266 LC_ALL=C join -j 1 .refs-before .refs-after |
267 while read ref old new; do
268 [ "$old" != "$new" ] || continue
269 echo "ref-change %$proj% $proj $old $new $ref" | nc_openbsd -w 1 -U "$sockpath"
270 done
271 LC_ALL=C join -j 1 -v 1 .refs-before .refs-after |
272 while read ref old; do
273 echo "ref-change %$proj% $proj $old 0000000000000000000000000000000000000000 $ref" | \
274 nc_openbsd -w 1 -U "$sockpath"
275 done
276 LC_ALL=C join -j 1 -v 2 .refs-before .refs-after |
277 while read ref new; do
278 echo "ref-change %$proj% $proj 0000000000000000000000000000000000000000 $new $ref" | \
279 nc_openbsd -w 1 -U "$sockpath"
280 done
282 bang config_set lastchange "$(date '+%a, %d %b %Y %T %z')"
283 bang_eval "git for-each-ref --sort=-committerdate --format='%(committerdate:iso8601)' \
284 --count=1 refs/heads > info/lastactivity"
285 ! [ -d htmlcache ] || { >htmlcache/changed; } 2>/dev/null || :
288 rm -f .refs-before .refs-after .refs-temp FETCH_HEAD
290 if is_banged; then
291 [ -z "$mailaddrs" ] ||
293 echo "$proj update succeeded - failure recovery"
294 echo "this status message may be disabled on the project admin page"
295 } | mail -s "[$cfg_name] $proj update succeeded" "$mailaddrs" || :
296 bang_reset
299 progress "- [$proj] update (`date`)"