updating: bang again if baseurl is changed
[girocco.git] / jobd / update.sh
blobac4baedb66f8ba8993c0895a4deff23068617a23
1 #!/bin/sh
3 . @basedir@/shlib.sh
5 LC_ALL=C
6 export LC_ALL
8 # date -R is linux-only, POSIX equivalent is '+%a, %d %b %Y %T %z'
9 datefmt='+%a, %d %b %Y %T %z'
11 # darcs fast-export | git fast-import with error handling
12 git_darcs_fetch() {
13 _err1=
14 _err2=
15 exec 3>&1
16 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
18 exec 4>&3 3>&1 1>&4 4>&-
20 _e1=0
21 "$cfg_basedir"/bin/darcs-fast-export \
22 --export-marks="$(pwd)/dfe-marks" \
23 --import-marks="$(pwd)/dfe-marks" "$1" 3>&- || _e1=$?
24 echo $_e1 >&3
25 } | \
27 _e2=0
28 git fast-import \
29 --export-marks="$(pwd)/gfi-marks" \
30 --export-pack-edges="$(pwd)/gfi-packs" \
31 --import-marks="$(pwd)/gfi-marks" \
32 --force 3>&- || _e2=$?
33 echo $_e2 >&3
36 EOT
37 exec 3>&-
38 [ "$_err1" = 0 -a "$_err2" = 0 ]
39 return $?
42 # bzr fast-export | git fast-import with error handling
43 git_bzr_fetch() {
44 _err1=
45 _err2=
46 exec 3>&1
47 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
49 exec 4>&3 3>&1 1>&4 4>&-
51 _e1=0
52 bzr fast-export \
53 --export-marks="$(pwd)/dfe-marks" \
54 --import-marks="$(pwd)/dfe-marks" "$1" 3>&- || _e1=$?
55 echo $_e1 >&3
56 } | \
58 _e2=0
59 git fast-import \
60 --export-marks="$(pwd)/gfi-marks" \
61 --export-pack-edges="$(pwd)/gfi-packs" \
62 --import-marks="$(pwd)/gfi-marks" \
63 --force 3>&- || _e2=$?
64 echo $_e2 >&3
67 EOT
68 exec 3>&-
69 [ "$_err1" = 0 -a "$_err2" = 0 ]
70 return $?
73 set -e
74 [ -n "$cfg_mirror" ] || { echo "Mirroring is disabled" >&2; exit 0; }
75 trap 'if [ $? != 0 ]; then echo "update failed dir: $PWD" >&2; fi; rm -f "$bang_log"' EXIT
77 umask 002
78 [ "$cfg_permission_control" != "Hooks" ] || umask 000
80 proj="$1"
81 cd "$cfg_reporoot/$proj.git"
83 if check_interval lastrefresh $cfg_min_mirror_interval; then
84 progress "= [$proj] update skip (last at $(config_get lastrefresh))"
85 exit 0
87 progress "+ [$proj] update (`date`)"
89 bang_setup
90 bang_once=1
91 bang_action="update"
93 mail="$(config_get owner || :)"
94 url="$(config_get baseurl || :)"
95 case "$url" in *" "*|*" "*|"")
96 bang_eval 'echo "Bad update URL (\"$url\")"; ! :'
97 exit 1
98 esac
99 bang echo "Mirroring from URL \"$url\""
100 bang echo ""
101 statusok="$(git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
102 mailaddrs=''
103 [ "$statusok" = "false" -o -z "$mail" ] || mailaddrs="$mail"
104 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] || \
105 if [ -z "$mailaddrs" ]; then mailaddrs="$cfg_admin"; else mailaddrs="$mailaddrs,$cfg_admin"; fi
107 bang_eval "git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
108 bang_eval "sort -k1b,1 <.refs-temp >.refs-before"
110 case "$url" in
111 svn://* | svn+http://* | svn+https://*)
112 [ -n "$cfg_mirror_svn" ] || { echo "Mirroring svn is disabled" >&2; exit 0; }
113 # Update the git svn url to match baseurl but be cognizant of any
114 # needed prefix changes. See the comments in taskd/clone.sh about
115 # why we need to put up with a prefix in the first place.
116 svnurl="${url#svn+}"
117 svnurl="${svnurl%/}"
118 svnurlold="$(config_get svnurl || :)"
119 if [ "$svnurl" != "$svnurlold" ]; then
120 # We better already have an svn-remote.svn.fetch setting
121 bang test -n "$(git config --get-all svn-remote.svn.fetch || :)"
122 # the only way to truly know what the proper prefix is
123 # is to attempt a fresh git-svn init -s on the new url
124 rm -rf svn-new-url || :
125 # We require svn info to succeed on the URL otherwise it's
126 # simply not a valid URL and without using -s on the init it
127 # will not otherwise be tested until the fetch
128 bang eval 'svn --non-interactive info "$svnurl" > /dev/null'
129 bang mkdir svn-new-url
130 GIT_DIR=svn-new-url bang git init --bare --quiet
131 # We initially use -s for the init which will possibly shorten
132 # the URL. However, the shortening can fail if a password is
133 # not required for the longer version but is for the shorter,
134 # so try again without -s if the -s version fails.
135 cmdstr='git svn init --prefix "" -s "$svnurl" < /dev/null > /dev/null 2>&1 || '
136 cmdstr="$cmdstr"'git svn init --prefix "" "$svnurl" < /dev/null > /dev/null 2>&1'
137 GIT_DIR=svn-new-url bang eval "$cmdstr"
138 gitsvnurl="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.url || :)"
139 gitsvnfetch="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.fetch || :)"
140 gitsvnprefixnew="${gitsvnfetch%%:*}"
141 gitsvnsuffixnew="${gitsvnprefixnew##*/}"
142 gitsvnprefixnew="${gitsvnprefixnew%$gitsvnsuffixnew}"
143 rm -rf svn-new-url || :
144 # Using GIT_DIR= with bang leaves it set to svn-new-url, so reset it to .
145 GIT_DIR=.
146 if [ "$gitsvnurl" != "$(git config --get svn-remote.svn.url || :)" ]; then
147 # The url has been changed.
148 # We must update the url and replace the prefix on all config items
149 gitsvnfetch="$(git config --get-all svn-remote.svn.fetch | head -1 || :)"
150 gitsvnprefixold="${gitsvnfetch%%:*}"
151 gitsvnsuffixold="${gitsvnprefixold##*/}"
152 gitsvnprefixold="${gitsvnprefixold%$gitsvnsuffixold}"
153 git config --remove-section 'svn-remote.svnnew' 2>/dev/null || :
154 git config 'svn-remote.svnnew.url' "$gitsvnurl"
155 { git config --get-regexp '^svn-remote\.svn\.' || :; } | \
156 { while read sname sval; do
157 case "$sname" in
158 svn-remote.svn.fetch|svn-remote.svn.branches|svn-remote.svn.tags)
159 sname="${sname#svn-remote.svn.}"
160 sval="${sval#$gitsvnprefixold}"
161 bang git config --add "svn-remote.svnnew.$sname" "${gitsvnprefixnew}$sval"
162 esac
163 done; }
164 bang git config -f svn/.metadata svn-remote.svn.reposRoot "$gitsvnurl"
165 bang git config --remove-section svn-remote.svn
166 bang git config --rename-section svn-remote.svnnew svn-remote.svn
168 bang config_set svnurl "$svnurl"
170 # remove any stale *.lock files greater than 1 hour old in case
171 # git-svn was killed on the last update because it took too long
172 find svn -type f -name '*.lock' -mmin +60 -print0 2>/dev/null | xargs -0 rm -f
173 GIT_DIR=. bang git svn fetch --quiet < /dev/null
174 # git svn does not preserve group permissions in the svn subdirectory
175 chmod -R ug+rw,o+r svn
176 # git svn also leaves behind ref turds that end with @nnn
177 # We get rid of them now
178 git show-ref | \
179 { while read sha1 ref; do
180 case "$ref" in
181 ?*@[1-9]|?*@[1-9][0-9]|?*@[1-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9]|\
182 ?*@[1-9][0-9][0-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9][0-9][0-9]|\
183 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9]|\
184 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])
185 git update-ref -d "$ref"
186 esac
187 done; }
189 darcs://*)
190 [ -n "$cfg_mirror_darcs" ] || { echo "Mirroring darcs is disabled" >&2; exit 0; }
191 httpurl="http://${url#darcs://}"
192 # remove any stale lock files greater than 1 hour old in case
193 # darcs_fast_export was killed on the last update because it took too long
194 find *.darcs -maxdepth 2 -type f -name 'lock' -mmin +60 -print0 2>/dev/null | xargs -0 rm -f
195 bang git_darcs_fetch "$httpurl"
197 bzr://*)
198 [ -n "$cfg_mirror_bzr" ] || { echo "Mirroring bzr is disabled" >&2; exit 0; }
199 bzrurl="${url#bzr://}"
200 bang git_bzr_fetch "$bzrurl"
202 hg+http://* | hg+https://*)
203 [ -n "$cfg_mirror_hg" ] || { echo "Mirroring hg is disabled" >&2; exit 0; }
204 # We just remove hg+ here, so hg+http://... becomes http://...
205 hgurl="${url#hg+}"
206 # Fetch any new updates
207 bang hg -R "$(pwd)/repo.hg" pull
208 # Do the fast-export | fast-import
209 bang git_hg_fetch
212 [ "$url" = "$(git config --get remote.origin.url || :)" ] || bang config_set_raw remote.origin.url "$url"
213 GIT_SSL_NO_VERIFY=1 bang git remote update
214 GIT_SSL_NO_VERIFY=1 bang git remote prune origin
216 esac
218 # The objects subdirectories permissions must be updated now.
219 # In the case of a dumb http clone, the permissions will not be correct
220 # (missing group write) despite the core.sharedrepository=1 setting!
221 # The objects themselves seem to have the correct permissions.
222 # This problem appears to have been fixed in the most recent git versions.
223 perms=g+w
224 [ "$cfg_permission_control" != "Hooks" ] || perms=go+w
225 chmod $perms $(find objects -maxdepth 1 -type d) 2>/dev/null || :
227 bang git update-server-info
228 bang config_set lastrefresh "$(date "$datefmt")"
230 # Look at which refs changed and trigger ref-change for these
231 bang_eval "git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
232 bang_eval "sort -k1b,1 <.refs-temp >.refs-after"
233 sockpath="$cfg_chroot/etc/taskd.socket"
234 if ! cmp -s .refs-before .refs-after; then
235 bang config_set lastreceive "$(date '+%a, %d %b %Y %T %z')"
236 if [ -S "$sockpath" ]; then
237 join -j 1 .refs-before .refs-after |
238 while read ref old new; do
239 [ "$old" != "$new" ] || continue
240 echo "ref-change %$proj% $proj $old $new $ref" | nc_openbsd -w 1 -U "$sockpath"
241 done
242 join -j 1 -v 1 .refs-before .refs-after |
243 while read ref old; do
244 echo "ref-change %$proj% $proj $old 0000000000000000000000000000000000000000 $ref" | \
245 nc_openbsd -w 1 -U "$sockpath"
246 done
247 join -j 1 -v 2 .refs-before .refs-after |
248 while read ref new; do
249 echo "ref-change %$proj% $proj 0000000000000000000000000000000000000000 $new $ref" | \
250 nc_openbsd -w 1 -U "$sockpath"
251 done
253 bang config_set lastchange "$(date '+%a, %d %b %Y %T %z')"
254 bang_eval "git for-each-ref --sort=-committerdate --format='%(committerdate:iso8601)' \
255 --count=1 refs/heads > info/lastactivity"
258 rm -f .refs-before .refs-after .refs-temp
260 if [ -e .banged ]; then
261 [ -z "$mailaddrs" ] ||
263 echo "$proj update succeeded - failure recovery"
264 echo "this status message may be disabled on the project admin page"
265 } | mail -s "[$cfg_name] $proj update succeeded" "$mailaddrs" || :
266 rm -f .banged .bangagain
269 progress "- [$proj] update (`date`)"