update.sh/gc.sh: complain and exit with wrong number of args
[girocco.git] / jobd / update.sh
blobfee789f73ca439ec6c3acb533ed96c0cf99dc48f
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 mail="$(config_get owner || :)"
119 url="$(config_get baseurl || :)"
120 case "$url" in *" "*|*" "*|"")
121 bang_eval 'echo "Bad mirror URL (\"$url\")"; ! :'
122 exit 1
123 esac
124 bang echo "Mirroring from URL \"$url\""
125 bang echo ""
126 statusok="$(git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
127 mailaddrs=''
128 [ "$statusok" = "false" -o -z "$mail" ] || mailaddrs="$mail"
129 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] || \
130 if [ -z "$mailaddrs" ]; then mailaddrs="$cfg_admin"; else mailaddrs="$mailaddrs,$cfg_admin"; fi
132 bang_eval "git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
133 bang_eval "LC_ALL=C sort -k1b,1 <.refs-temp >.refs-before"
135 case "$url" in
136 svn://* | svn+http://* | svn+https://*)
137 [ -n "$cfg_mirror_svn" ] || { echo "Mirroring svn is disabled" >&2; exit 0; }
138 # Use an 'anonsvn' username as is commonly used for anonymous svn
139 # Use an 'anonsvn' password as is commonly used for anonymous svn
140 GIT_ASKPASS_PASSWORD=anonsvn
141 export GIT_ASKPASS_PASSWORD
142 # Update the git svn url to match baseurl but be cognizant of any
143 # needed prefix changes. See the comments in taskd/clone.sh about
144 # why we need to put up with a prefix in the first place.
145 svnurl="${url#svn+}"
146 svnurl="${svnurl%/}"
147 svnurlold="$(config_get svnurl || :)"
148 if [ "$svnurl" != "$svnurlold" ]; then
149 # We better already have an svn-remote.svn.fetch setting
150 bang test -n "$(git config --get-all svn-remote.svn.fetch || :)"
151 # the only way to truly know what the proper prefix is
152 # is to attempt a fresh git-svn init -s on the new url
153 rm -rf svn-new-url || :
154 # We require svn info to succeed on the URL otherwise it's
155 # simply not a valid URL and without using -s on the init it
156 # will not otherwise be tested until the fetch
157 bang eval 'svn --non-interactive --username anonsvn --password anonsvn info "$svnurl" > /dev/null'
158 bang mkdir svn-new-url
159 GIT_DIR=svn-new-url bang git init --bare --quiet
160 # We initially use -s for the init which will possibly shorten
161 # the URL. However, the shortening can fail if a password is
162 # not required for the longer version but is for the shorter,
163 # so try again without -s if the -s version fails.
164 cmdstr='git svn init --username=anonsvn --prefix "" -s "$svnurl" < /dev/null > /dev/null 2>&1 || '
165 cmdstr="$cmdstr"'git svn init --username=anonsvn --prefix "" "$svnurl" < /dev/null > /dev/null 2>&1'
166 GIT_DIR=svn-new-url bang eval "$cmdstr"
167 gitsvnurl="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.url || :)"
168 gitsvnfetch="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.fetch || :)"
169 gitsvnprefixnew="${gitsvnfetch%%:*}"
170 gitsvnsuffixnew="${gitsvnprefixnew##*/}"
171 gitsvnprefixnew="${gitsvnprefixnew%$gitsvnsuffixnew}"
172 rm -rf svn-new-url || :
173 # Using GIT_DIR= with bang leaves it set to svn-new-url, so reset it to .
174 GIT_DIR=.
175 if [ "$gitsvnurl" != "$(git config --get svn-remote.svn.url || :)" ]; then
176 # The url has been changed.
177 # We must update the url and replace the prefix on all config items
178 gitsvnfetch="$(git config --get-all svn-remote.svn.fetch | head -1 || :)"
179 gitsvnprefixold="${gitsvnfetch%%:*}"
180 gitsvnsuffixold="${gitsvnprefixold##*/}"
181 gitsvnprefixold="${gitsvnprefixold%$gitsvnsuffixold}"
182 git config --remove-section 'svn-remote.svnnew' 2>/dev/null || :
183 git config 'svn-remote.svnnew.url' "$gitsvnurl"
184 { git config --get-regexp '^svn-remote\.svn\.' || :; } | \
185 { while read sname sval; do
186 case "$sname" in
187 svn-remote.svn.fetch|svn-remote.svn.branches|svn-remote.svn.tags)
188 sname="${sname#svn-remote.svn.}"
189 sval="${sval#$gitsvnprefixold}"
190 bang git config --add "svn-remote.svnnew.$sname" "${gitsvnprefixnew}$sval"
191 esac
192 done; }
193 bang git config -f svn/.metadata svn-remote.svn.reposRoot "$gitsvnurl"
194 bang git config --remove-section svn-remote.svn
195 bang git config --rename-section svn-remote.svnnew svn-remote.svn
197 bang config_set svnurl "$svnurl"
199 # remove any stale *.lock files greater than 1 hour old in case
200 # git-svn was killed on the last update because it took too long
201 find svn -type f -name '*.lock' -mmin +60 -print0 2>/dev/null | xargs -0 rm -f
202 GIT_DIR=. bang git svn fetch --username=anonsvn --quiet < /dev/null
203 # git svn does not preserve group permissions in the svn subdirectory
204 chmod -R ug+rw,o+r svn
205 # git svn also leaves behind ref turds that end with @nnn
206 # We get rid of them now
207 git for-each-ref --format='%(objectname) %(refname)' | \
208 { while read sha1 ref; do
209 case "$ref" in
210 ?*@[1-9]|?*@[1-9][0-9]|?*@[1-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9]|\
211 ?*@[1-9][0-9][0-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9][0-9][0-9]|\
212 ?*@[1-9][0-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][0-9])
214 git update-ref -d "$ref"
215 esac
216 done; }
217 unset GIT_ASKPASS_PASSWORD
219 darcs://*)
220 [ -n "$cfg_mirror_darcs" ] || { echo "Mirroring darcs is disabled" >&2; exit 0; }
221 httpurl="http://${url#darcs://}"
222 # remove any stale lock files greater than 1 hour old in case
223 # darcs_fast_export was killed on the last update because it took too long
224 find *.darcs -maxdepth 2 -type f -name 'lock' -mmin +60 -print0 2>/dev/null | xargs -0 rm -f
225 bang git_darcs_fetch "$httpurl"
227 bzr://*)
228 [ -n "$cfg_mirror_bzr" ] || { echo "Mirroring bzr is disabled" >&2; exit 0; }
229 bzrurl="${url#bzr://}"
230 bang git_bzr_fetch "$bzrurl"
232 hg+http://* | hg+https://*)
233 [ -n "$cfg_mirror_hg" ] || { echo "Mirroring hg is disabled" >&2; exit 0; }
234 # We just remove hg+ here, so hg+http://... becomes http://...
235 hgurl="${url#hg+}"
236 # Fetch any new updates
237 bang hg -R "$(pwd)/repo.hg" pull
238 # Do the fast-export | fast-import
239 bang git_hg_fetch
242 [ "$url" = "$(git config --get remote.origin.url || :)" ] || bang config_set_raw remote.origin.url "$url"
243 GIT_SSL_NO_VERIFY=1 bang git remote update --prune
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"
287 rm -f .refs-before .refs-after .refs-temp
289 if is_banged; then
290 [ -z "$mailaddrs" ] ||
292 echo "$proj update succeeded - failure recovery"
293 echo "this status message may be disabled on the project admin page"
294 } | mail -s "[$cfg_name] $proj update succeeded" "$mailaddrs" || :
295 bang_reset
298 progress "- [$proj] update (`date`)"