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