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