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