fetch/push: touch htmlcache/changed
[girocco.git] / jobd / update.sh
blobf9a406f3b210eb54c48325f4f295866dbab64da6
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 # A previous failed update attempt can leave a huge tmp_pack_XXXXXX file behind.
100 # Since no pushes are allowed to mirrors, we know that any such files that exist
101 # at this point in time are garbage and can be safely deleted, we do not even
102 # need to check how old they are. A tmp_idx_XXXXXX file is also created during
103 # the later stages of the fetch process, so we kill any of those as well.
104 find objects/pack -maxdepth 1 -type f -name "tmp_pack_?*" -print | \
105 while read packtmp; do
106 rm -f "$packtmp"
107 done
108 find objects/pack -maxdepth 1 -type f -name "tmp_idx_?*" -print | \
109 while read packtmp; do
110 rm -f "$packtmp"
111 done
113 bang_setup
114 bang_once=1
115 bang_action="update"
117 bang echo "Project: $proj"
118 bang echo ""
119 mail="$(config_get owner || :)"
120 url="$(config_get baseurl || :)"
121 case "$url" in *" "*|*" "*|"")
122 bang_eval 'echo "Bad mirror URL (\"$url\")"; ! :'
123 exit 1
124 esac
125 bang echo "Mirroring from URL \"$url\""
126 bang echo ""
127 statusok="$(git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
128 mailaddrs=''
129 [ "$statusok" = "false" -o -z "$mail" ] || mailaddrs="$mail"
130 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] || \
131 if [ -z "$mailaddrs" ]; then mailaddrs="$cfg_admin"; else mailaddrs="$mailaddrs,$cfg_admin"; fi
133 bang_eval "git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
134 bang_eval "LC_ALL=C sort -k1b,1 <.refs-temp >.refs-before"
136 case "$url" in
137 svn://* | svn+http://* | svn+https://*)
138 [ -n "$cfg_mirror_svn" ] || { echo "Mirroring svn is disabled" >&2; exit 0; }
139 # Use an 'anonsvn' username as is commonly used for anonymous svn
140 # Use an 'anonsvn' password as is commonly used for anonymous svn
141 GIT_ASKPASS_PASSWORD=anonsvn
142 export GIT_ASKPASS_PASSWORD
143 # Update the git svn url to match baseurl but be cognizant of any
144 # needed prefix changes. See the comments in taskd/clone.sh about
145 # why we need to put up with a prefix in the first place.
146 svnurl="${url#svn+}"
147 svnurl="${svnurl%/}"
148 svnurlold="$(config_get svnurl || :)"
149 if [ "$svnurl" != "$svnurlold" ]; then
150 # We better already have an svn-remote.svn.fetch setting
151 bang test -n "$(git config --get-all svn-remote.svn.fetch || :)"
152 # the only way to truly know what the proper prefix is
153 # is to attempt a fresh git-svn init -s on the new url
154 rm -rf svn-new-url || :
155 # We require svn info to succeed on the URL otherwise it's
156 # simply not a valid URL and without using -s on the init it
157 # will not otherwise be tested until the fetch
158 bang eval 'svn --non-interactive --username anonsvn --password anonsvn info "$svnurl" > /dev/null'
159 bang mkdir svn-new-url
160 GIT_DIR=svn-new-url bang git init --bare --quiet
161 # We initially use -s for the init which will possibly shorten
162 # the URL. However, the shortening can fail if a password is
163 # not required for the longer version but is for the shorter,
164 # so try again without -s if the -s version fails.
165 cmdstr='git svn init --username=anonsvn --prefix "" -s "$svnurl" < /dev/null > /dev/null 2>&1 || '
166 cmdstr="$cmdstr"'git svn init --username=anonsvn --prefix "" "$svnurl" < /dev/null > /dev/null 2>&1'
167 GIT_DIR=svn-new-url bang eval "$cmdstr"
168 gitsvnurl="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.url || :)"
169 gitsvnfetch="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.fetch || :)"
170 gitsvnprefixnew="${gitsvnfetch%%:*}"
171 gitsvnsuffixnew="${gitsvnprefixnew##*/}"
172 gitsvnprefixnew="${gitsvnprefixnew%$gitsvnsuffixnew}"
173 rm -rf svn-new-url || :
174 # Using GIT_DIR= with bang leaves it set to svn-new-url, so reset it to .
175 GIT_DIR=.
176 if [ "$gitsvnurl" != "$(git config --get svn-remote.svn.url || :)" ]; then
177 # The url has been changed.
178 # We must update the url and replace the prefix on all config items
179 gitsvnfetch="$(git config --get-all svn-remote.svn.fetch | head -1 || :)"
180 gitsvnprefixold="${gitsvnfetch%%:*}"
181 gitsvnsuffixold="${gitsvnprefixold##*/}"
182 gitsvnprefixold="${gitsvnprefixold%$gitsvnsuffixold}"
183 git config --remove-section 'svn-remote.svnnew' 2>/dev/null || :
184 git config 'svn-remote.svnnew.url' "$gitsvnurl"
185 { git config --get-regexp '^svn-remote\.svn\.' || :; } | \
186 { while read sname sval; do
187 case "$sname" in
188 svn-remote.svn.fetch|svn-remote.svn.branches|svn-remote.svn.tags)
189 sname="${sname#svn-remote.svn.}"
190 sval="${sval#$gitsvnprefixold}"
191 bang git config --add "svn-remote.svnnew.$sname" "${gitsvnprefixnew}$sval"
192 esac
193 done; }
194 bang git config -f svn/.metadata svn-remote.svn.reposRoot "$gitsvnurl"
195 bang git config --remove-section svn-remote.svn
196 bang git config --rename-section svn-remote.svnnew svn-remote.svn
198 bang config_set svnurl "$svnurl"
200 # remove any stale *.lock files greater than 1 hour old in case
201 # git-svn was killed on the last update because it took too long
202 find svn -type f -name '*.lock' -mmin +60 -print0 2>/dev/null | xargs -0 rm -f
203 GIT_DIR=. bang git svn fetch --log-window-size=$var_log_window_size --username=anonsvn --quiet < /dev/null
204 # git svn does not preserve group permissions in the svn subdirectory
205 chmod -R ug+rw,o+r svn
206 # git svn also leaves behind ref turds that end with @nnn
207 # We get rid of them now
208 git for-each-ref --format='%(objectname) %(refname)' | \
209 { while read sha1 ref; do
210 case "$ref" in
211 ?*@[1-9]|?*@[1-9][0-9]|?*@[1-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9]|\
212 ?*@[1-9][0-9][0-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9][0-9][0-9]|\
213 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9]|\
214 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])
215 git update-ref -d "$ref"
216 esac
217 done; }
218 unset GIT_ASKPASS_PASSWORD
220 darcs://*)
221 [ -n "$cfg_mirror_darcs" ] || { echo "Mirroring darcs is disabled" >&2; exit 0; }
222 httpurl="http://${url#darcs://}"
223 # remove any stale lock files greater than 1 hour old in case
224 # darcs_fast_export was killed on the last update because it took too long
225 find *.darcs -maxdepth 2 -type f -name 'lock' -mmin +60 -print0 2>/dev/null | xargs -0 rm -f
226 bang git_darcs_fetch "$httpurl"
228 bzr://*)
229 [ -n "$cfg_mirror_bzr" ] || { echo "Mirroring bzr is disabled" >&2; exit 0; }
230 bzrurl="${url#bzr://}"
231 bang git_bzr_fetch "$bzrurl"
233 hg+http://* | hg+https://*)
234 [ -n "$cfg_mirror_hg" ] || { echo "Mirroring hg is disabled" >&2; exit 0; }
235 # We just remove hg+ here, so hg+http://... becomes http://...
236 hgurl="${url#hg+}"
237 # Fetch any new updates
238 bang hg -R "$(pwd)/repo.hg" pull
239 # Do the fast-export | fast-import
240 bang git_hg_fetch
243 [ "$url" = "$(git config --get remote.origin.url || :)" ] || bang config_set_raw remote.origin.url "$url"
244 GIT_SSL_NO_VERIFY=1 bang git remote update --prune
246 esac
248 # The objects subdirectories permissions must be updated now.
249 # In the case of a dumb http clone, the permissions will not be correct
250 # (missing group write) despite the core.sharedrepository=1 setting!
251 # The objects themselves seem to have the correct permissions.
252 # This problem appears to have been fixed in the most recent git versions.
253 perms=g+w
254 [ "$cfg_permission_control" != "Hooks" ] || perms=go+w
255 chmod $perms $(find objects -maxdepth 1 -type d) 2>/dev/null || :
257 bang git update-server-info
258 bang config_set lastrefresh "$(date "$datefmt")"
260 # Look at which refs changed and trigger ref-change for these
261 bang_eval "git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
262 bang_eval "LC_ALL=C sort -k1b,1 <.refs-temp >.refs-after"
263 sockpath="$cfg_chroot/etc/taskd.socket"
264 if ! cmp -s .refs-before .refs-after; then
265 bang config_set lastreceive "$(date '+%a, %d %b %Y %T %z')"
266 if [ -S "$sockpath" ]; then
267 LC_ALL=C join -j 1 .refs-before .refs-after |
268 while read ref old new; do
269 [ "$old" != "$new" ] || continue
270 echo "ref-change %$proj% $proj $old $new $ref" | nc_openbsd -w 1 -U "$sockpath"
271 done
272 LC_ALL=C join -j 1 -v 1 .refs-before .refs-after |
273 while read ref old; do
274 echo "ref-change %$proj% $proj $old 0000000000000000000000000000000000000000 $ref" | \
275 nc_openbsd -w 1 -U "$sockpath"
276 done
277 LC_ALL=C join -j 1 -v 2 .refs-before .refs-after |
278 while read ref new; do
279 echo "ref-change %$proj% $proj 0000000000000000000000000000000000000000 $new $ref" | \
280 nc_openbsd -w 1 -U "$sockpath"
281 done
283 bang config_set lastchange "$(date '+%a, %d %b %Y %T %z')"
284 bang_eval "git for-each-ref --sort=-committerdate --format='%(committerdate:iso8601)' \
285 --count=1 refs/heads > info/lastactivity"
286 ! [ -d htmlcache ] || { >htmlcache/changed; } 2>/dev/null || :
289 rm -f .refs-before .refs-after .refs-temp
291 if is_banged; then
292 [ -z "$mailaddrs" ] ||
294 echo "$proj update succeeded - failure recovery"
295 echo "this status message may be disabled on the project admin page"
296 } | mail -s "[$cfg_name] $proj update succeeded" "$mailaddrs" || :
297 bang_reset
300 progress "- [$proj] update (`date`)"