git-svn: use an 'anonsvn' username and 'anonsvn' password
[girocco.git] / jobd / update.sh
blob9f57d81f10642e43a7dcb2aa7777f3c2ab24e6fc
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 # Use an 'anonsvn' username as is commonly used for anonymous svn
113 # Use an 'anonsvn' password as is commonly used for anonymous svn
114 GIT_ASKPASS_PASSWORD=anonsvn
115 export GIT_ASKPASS_PASSWORD
116 # Update the git svn url to match baseurl but be cognizant of any
117 # needed prefix changes. See the comments in taskd/clone.sh about
118 # why we need to put up with a prefix in the first place.
119 svnurl="${url#svn+}"
120 svnurl="${svnurl%/}"
121 svnurlold="$(config_get svnurl || :)"
122 if [ "$svnurl" != "$svnurlold" ]; then
123 # We better already have an svn-remote.svn.fetch setting
124 bang test -n "$(git config --get-all svn-remote.svn.fetch || :)"
125 # the only way to truly know what the proper prefix is
126 # is to attempt a fresh git-svn init -s on the new url
127 rm -rf svn-new-url || :
128 # We require svn info to succeed on the URL otherwise it's
129 # simply not a valid URL and without using -s on the init it
130 # will not otherwise be tested until the fetch
131 bang eval 'svn --non-interactive --username anonsvn --password anonsvn info "$svnurl" > /dev/null'
132 bang mkdir svn-new-url
133 GIT_DIR=svn-new-url bang git init --bare --quiet
134 # We initially use -s for the init which will possibly shorten
135 # the URL. However, the shortening can fail if a password is
136 # not required for the longer version but is for the shorter,
137 # so try again without -s if the -s version fails.
138 cmdstr='git svn init --username=anonsvn --prefix "" -s "$svnurl" < /dev/null > /dev/null 2>&1 || '
139 cmdstr="$cmdstr"'git svn init --username=anonsvn --prefix "" "$svnurl" < /dev/null > /dev/null 2>&1'
140 GIT_DIR=svn-new-url bang eval "$cmdstr"
141 gitsvnurl="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.url || :)"
142 gitsvnfetch="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.fetch || :)"
143 gitsvnprefixnew="${gitsvnfetch%%:*}"
144 gitsvnsuffixnew="${gitsvnprefixnew##*/}"
145 gitsvnprefixnew="${gitsvnprefixnew%$gitsvnsuffixnew}"
146 rm -rf svn-new-url || :
147 # Using GIT_DIR= with bang leaves it set to svn-new-url, so reset it to .
148 GIT_DIR=.
149 if [ "$gitsvnurl" != "$(git config --get svn-remote.svn.url || :)" ]; then
150 # The url has been changed.
151 # We must update the url and replace the prefix on all config items
152 gitsvnfetch="$(git config --get-all svn-remote.svn.fetch | head -1 || :)"
153 gitsvnprefixold="${gitsvnfetch%%:*}"
154 gitsvnsuffixold="${gitsvnprefixold##*/}"
155 gitsvnprefixold="${gitsvnprefixold%$gitsvnsuffixold}"
156 git config --remove-section 'svn-remote.svnnew' 2>/dev/null || :
157 git config 'svn-remote.svnnew.url' "$gitsvnurl"
158 { git config --get-regexp '^svn-remote\.svn\.' || :; } | \
159 { while read sname sval; do
160 case "$sname" in
161 svn-remote.svn.fetch|svn-remote.svn.branches|svn-remote.svn.tags)
162 sname="${sname#svn-remote.svn.}"
163 sval="${sval#$gitsvnprefixold}"
164 bang git config --add "svn-remote.svnnew.$sname" "${gitsvnprefixnew}$sval"
165 esac
166 done; }
167 bang git config -f svn/.metadata svn-remote.svn.reposRoot "$gitsvnurl"
168 bang git config --remove-section svn-remote.svn
169 bang git config --rename-section svn-remote.svnnew svn-remote.svn
171 bang config_set svnurl "$svnurl"
173 # remove any stale *.lock files greater than 1 hour old in case
174 # git-svn was killed on the last update because it took too long
175 find svn -type f -name '*.lock' -mmin +60 -print0 2>/dev/null | xargs -0 rm -f
176 GIT_DIR=. bang git svn fetch --username=anonsvn --quiet < /dev/null
177 # git svn does not preserve group permissions in the svn subdirectory
178 chmod -R ug+rw,o+r svn
179 # git svn also leaves behind ref turds that end with @nnn
180 # We get rid of them now
181 git show-ref | \
182 { while read sha1 ref; do
183 case "$ref" in
184 ?*@[1-9]|?*@[1-9][0-9]|?*@[1-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9]|\
185 ?*@[1-9][0-9][0-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9][0-9][0-9]|\
186 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9]|\
187 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])
188 git update-ref -d "$ref"
189 esac
190 done; }
191 unset GIT_ASKPASS_PASSWORD
193 darcs://*)
194 [ -n "$cfg_mirror_darcs" ] || { echo "Mirroring darcs is disabled" >&2; exit 0; }
195 httpurl="http://${url#darcs://}"
196 # remove any stale lock files greater than 1 hour old in case
197 # darcs_fast_export was killed on the last update because it took too long
198 find *.darcs -maxdepth 2 -type f -name 'lock' -mmin +60 -print0 2>/dev/null | xargs -0 rm -f
199 bang git_darcs_fetch "$httpurl"
201 bzr://*)
202 [ -n "$cfg_mirror_bzr" ] || { echo "Mirroring bzr is disabled" >&2; exit 0; }
203 bzrurl="${url#bzr://}"
204 bang git_bzr_fetch "$bzrurl"
206 hg+http://* | hg+https://*)
207 [ -n "$cfg_mirror_hg" ] || { echo "Mirroring hg is disabled" >&2; exit 0; }
208 # We just remove hg+ here, so hg+http://... becomes http://...
209 hgurl="${url#hg+}"
210 # Fetch any new updates
211 bang hg -R "$(pwd)/repo.hg" pull
212 # Do the fast-export | fast-import
213 bang git_hg_fetch
216 [ "$url" = "$(git config --get remote.origin.url || :)" ] || bang config_set_raw remote.origin.url "$url"
217 GIT_SSL_NO_VERIFY=1 bang git remote update
218 GIT_SSL_NO_VERIFY=1 bang git remote prune origin
220 esac
222 # The objects subdirectories permissions must be updated now.
223 # In the case of a dumb http clone, the permissions will not be correct
224 # (missing group write) despite the core.sharedrepository=1 setting!
225 # The objects themselves seem to have the correct permissions.
226 # This problem appears to have been fixed in the most recent git versions.
227 perms=g+w
228 [ "$cfg_permission_control" != "Hooks" ] || perms=go+w
229 chmod $perms $(find objects -maxdepth 1 -type d) 2>/dev/null || :
231 bang git update-server-info
232 bang config_set lastrefresh "$(date "$datefmt")"
234 # Look at which refs changed and trigger ref-change for these
235 bang_eval "git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
236 bang_eval "LC_ALL=C sort -k1b,1 <.refs-temp >.refs-after"
237 sockpath="$cfg_chroot/etc/taskd.socket"
238 if ! cmp -s .refs-before .refs-after; then
239 bang config_set lastreceive "$(date '+%a, %d %b %Y %T %z')"
240 if [ -S "$sockpath" ]; then
241 LC_ALL=C join -j 1 .refs-before .refs-after |
242 while read ref old new; do
243 [ "$old" != "$new" ] || continue
244 echo "ref-change %$proj% $proj $old $new $ref" | nc_openbsd -w 1 -U "$sockpath"
245 done
246 LC_ALL=C join -j 1 -v 1 .refs-before .refs-after |
247 while read ref old; do
248 echo "ref-change %$proj% $proj $old 0000000000000000000000000000000000000000 $ref" | \
249 nc_openbsd -w 1 -U "$sockpath"
250 done
251 LC_ALL=C join -j 1 -v 2 .refs-before .refs-after |
252 while read ref new; do
253 echo "ref-change %$proj% $proj 0000000000000000000000000000000000000000 $new $ref" | \
254 nc_openbsd -w 1 -U "$sockpath"
255 done
257 bang config_set lastchange "$(date '+%a, %d %b %Y %T %z')"
258 bang_eval "git for-each-ref --sort=-committerdate --format='%(committerdate:iso8601)' \
259 --count=1 refs/heads > info/lastactivity"
262 rm -f .refs-before .refs-after .refs-temp
264 if is_banged; then
265 [ -z "$mailaddrs" ] ||
267 echo "$proj update succeeded - failure recovery"
268 echo "this status message may be disabled on the project admin page"
269 } | mail -s "[$cfg_name] $proj update succeeded" "$mailaddrs" || :
270 bang_reset
273 progress "- [$proj] update (`date`)"