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