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