svn clones: fix prefix computation
[girocco.git] / jobd / update.sh
blobd249ef1a4ef200298a4ef93c86e0c5c456e7eb3e
1 #!/bin/bash
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 "$cfg_basedir"/bin/darcs-fast-export --export-marks=$(pwd)/dfe-marks --import-marks=$(pwd)/dfe-marks "$1" | \
14 git fast-import --export-marks=$(pwd)/gfi-marks --import-marks=$(pwd)/gfi-marks
15 [ ${PIPESTATUS[0]} = 0 -a ${PIPESTATUS[1]} = 0 ]
16 return $?
19 # bzr fast-export | git fast-import with error handling
20 git_bzr_fetch() {
21 bzr fast-export --export-marks=$(pwd)/dfe-marks --import-marks=$(pwd)/dfe-marks "$1" | \
22 git fast-import --export-marks=$(pwd)/gfi-marks --import-marks=$(pwd)/gfi-marks
23 [ ${PIPESTATUS[0]} = 0 -a ${PIPESTATUS[1]} = 0 ]
24 return $?
26 set -e
27 trap 'if [ $? != 0 ]; then echo "update failed dir: $PWD" >&2; fi; rm -f "$bang_log"' EXIT
29 umask 002
30 [ "$cfg_permission_control" != "Hooks" ] || umask 000
32 proj="$1"
33 cd "$cfg_reporoot/$proj.git"
35 if check_interval lastrefresh $cfg_min_mirror_interval; then
36 progress "= [$proj] update skip (last at $(config_get lastrefresh))"
37 exit 0
39 progress "+ [$proj] update (`date`)"
41 bang_setup
42 bang_once=1
43 bang_action="update"
45 url="$(config_get baseurl)"
46 mail="$(config_get owner || :)"
47 statusok="$(git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
48 mailaddrs=''
49 [ "$statusok" = "false" -o -z "$mail" ] || mailaddrs="$mail"
50 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] ||
51 if [ -z "$mailaddrs" ]; then mailaddrs="$cfg_admin"; else mailaddrs="$mailaddrs,$cfg_admin"; fi
53 show_progress=1 bang git for-each-ref --format '%(refname) %(objectname)' | sort -k1b,1 >.refs-before
55 case "$url" in
56 svn://* | svn+http://* | svn+https://*)
57 # Update the git svn url to match baseurl but be cognizant of any
58 # needed prefix changes. See the comments in taskd/clone.sh about
59 # why we need to put up with a prefix in the first place.
60 svnurl="${url#svn+}"
61 svnurl="${svnurl%/}"
62 svnurlold="$(config_get svnurl || :)"
63 if [ "$svnurl" != "$svnurlold" ]; then
64 # We better already have an svn-remote.svn.fetch setting
65 bang test -n "$(git config --get-all svn-remote.svn.fetch || :)"
66 # the only way to truly know what the proper prefix is
67 # is to attempt a fresh git-svn init -s on the new url
68 rm -rf svn-new-url || :
69 # We require svn info to succeed on the URL otherwise it's
70 # simply not a valid URL and without using -s on the init it
71 # will not otherwise be tested until the fetch
72 bang eval 'svn --non-interactive info "$svnurl" > /dev/null'
73 bang mkdir svn-new-url
74 GIT_DIR=svn-new-url bang git init --bare --quiet
75 # We initially use -s for the init which will possibly shorten
76 # the URL. However, the shortening can fail if a password is
77 # not required for the longer version but is for the shorter,
78 # so try again without -s if the -s version fails.
79 cmdstr='git svn init -s "$svnurl" < /dev/null > /dev/null 2>&1 || '
80 cmdstr="$cmdstr"'git svn init "$svnurl" < /dev/null > /dev/null 2>&1'
81 GIT_DIR=svn-new-url bang eval "$cmdstr"
82 gitsvnurl="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.url || :)"
83 gitsvnfetch="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.fetch || :)"
84 gitsvnprefixnew="${gitsvnfetch%%:*}"
85 gitsvnsuffixnew="${gitsvnprefixnew##*/}"
86 gitsvnprefixnew="${gitsvnprefixnew%$gitsvnsuffixnew}"
87 rm -rf svn-new-url || :
88 # Using GIT_DIR= with bang leaves it set to svn-new-url, so reset it to .
89 GIT_DIR=.
90 if [ "$gitsvnurl" != "$(git config --get svn-remote.svn.url || :)" ]; then
91 # The url has been changed.
92 # We must update the url and replace the prefix on all config items
93 gitsvnfetch="$(git config --get-all svn-remote.svn.fetch | head -1 || :)"
94 gitsvnprefixold="${gitsvnfetch%%:*}"
95 gitsvnsuffixold="${gitsvnprefixold##*/}"
96 gitsvnprefixold="${gitsvnprefixold%$gitsvnsuffixold}"
97 git config --remove-section 'svn-remote.svnnew' 2>/dev/null || :
98 git config 'svn-remote.svnnew.url' "$gitsvnurl"
99 { git config --get-regexp '^svn-remote\.svn\.' || :; } | \
100 { while read sname sval; do
101 case "$sname" in
102 svn-remote.svn.fetch|svn-remote.svn.branches|svn-remote.svn.tags)
103 sname="${sname#svn-remote.svn.}"
104 sval="${sval#$gitsvnprefixold}"
105 bang git config --add "svn-remote.svnnew.$sname" "${gitsvnprefixnew}$sval"
106 esac
107 done; }
108 bang git config -f svn/.metadata svn-remote.svn.reposRoot "$gitsvnurl"
109 bang git config --remove-section svn-remote.svn
110 bang git config --rename-section svn-remote.svnnew svn-remote.svn
112 bang config_set svnurl "$svnurl"
114 # remove any stale *.lock files greater than 1 hour old in case
115 # git-svn was killed on the last update because it took too long
116 find svn -type f -name '*.lock' -mmin +60 -print0 | xargs -0 rm -f
117 GIT_DIR=. bang git svn fetch --quiet < /dev/null
118 # git svn does not preserve group permissions in the svn subdirectory
119 chmod -R ug+rw,o+r svn
120 # git svn also leaves behind ref turds that end with @nnn
121 # We get rid of them now
122 git show-ref | \
123 { while read sha1 ref; do
124 case "$ref" in
125 ?*@[1-9]|?*@[1-9][0-9]|?*@[1-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9]|\
126 ?*@[1-9][0-9][0-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9][0-9][0-9]|\
127 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9]|\
128 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])
129 git update-ref -d "$ref"
130 esac
131 done; }
133 darcs://*)
134 httpurl="${url/darcs:\/\//http://}"
135 bang git_darcs_fetch "$httpurl"
137 bzr://*)
138 bzrurl="${url#bzr://}"
139 bang git_bzr_fetch "$bzrurl"
142 [ "$url" = "$(git config --get remote.origin.url || :)" ] || bang config_set_raw remote.origin.url "$url"
143 GIT_SSL_NO_VERIFY=1 bang git remote update
144 GIT_SSL_NO_VERIFY=1 bang git remote prune origin
146 esac
148 # The objects subdirectories permissions must be updated now.
149 # In the case of a dumb http clone, the permissions will not be correct
150 # (missing group write) despite the core.sharedrepository=1 setting!
151 # The objects themselves seem to have the correct permissions.
152 # This problem appears to have been fixed in the most recent git versions.
153 perms=g+w
154 [ "$cfg_permission_control" != "Hooks" ] || perms=go+w
155 chmod $perms $(find objects -maxdepth 1 -type d) 2>/dev/null || :
157 bang git update-server-info
158 bang config_set lastrefresh "$(date "$datefmt")"
160 # Look at which refs changed and trigger ref-change for these
161 show_progress=1 bang git for-each-ref --format '%(refname) %(objectname)' | sort -k1b,1 >.refs-after
162 sockpath="$cfg_chroot/etc/taskd.socket"
163 if [ -S "$sockpath" ] && ! cmp -s .refs-before .refs-after; then
164 join -j 1 .refs-before .refs-after |
165 while read ref old new; do
166 [ "$old" != "$new" ] || continue
167 echo "ref-change %$proj% $proj $old $new $ref" | nc.openbsd -w 1 -U "$sockpath"
168 done
169 join -j 1 -v 1 .refs-before .refs-after |
170 while read ref old; do
171 echo "ref-change %$proj% $proj $old 0000000000000000000000000000000000000000 $ref" | nc.openbsd -w 1 -U "$sockpath"
172 done
173 join -j 1 -v 2 .refs-before .refs-after |
174 while read ref new; do
175 echo "ref-change %$proj% $proj 0000000000000000000000000000000000000000 $new $ref" | nc.openbsd -w 1 -U "$sockpath"
176 done
177 git config gitweb.lastchange "$(date '+%a, %d %b %Y %T %z')"
178 git for-each-ref --sort=-committerdate --format='%(committerdate:iso8601)' \
179 --count=1 refs/heads > info/lastactivity
182 rm -f .refs-before .refs-after
184 if [ -e .banged ]; then
185 [ -z "$mailaddrs" ] ||
187 echo "$proj update succeeded - failure recovery"
188 echo "this status message may be disabled on the project admin page"
189 } | mail -s "[$cfg_name] $proj update succeeded" "$mailaddrs" || :
190 rm .banged
193 progress "- [$proj] update (`date`)"