git-svn: add --prefix= to avoid Git 1.8.5+ warning
[girocco.git] / jobd / update.sh
blobca3d71849a68ec1d9017e733ba386eaf7cc72a02
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 bang_eval "git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
54 bang_eval "sort -k1b,1 <.refs-temp >.refs-before"
56 case "$url" in
57 svn://* | svn+http://* | svn+https://*)
58 # Update the git svn url to match baseurl but be cognizant of any
59 # needed prefix changes. See the comments in taskd/clone.sh about
60 # why we need to put up with a prefix in the first place.
61 svnurl="${url#svn+}"
62 svnurl="${svnurl%/}"
63 svnurlold="$(config_get svnurl || :)"
64 if [ "$svnurl" != "$svnurlold" ]; then
65 # We better already have an svn-remote.svn.fetch setting
66 bang test -n "$(git config --get-all svn-remote.svn.fetch || :)"
67 # the only way to truly know what the proper prefix is
68 # is to attempt a fresh git-svn init -s on the new url
69 rm -rf svn-new-url || :
70 # We require svn info to succeed on the URL otherwise it's
71 # simply not a valid URL and without using -s on the init it
72 # will not otherwise be tested until the fetch
73 bang eval 'svn --non-interactive info "$svnurl" > /dev/null'
74 bang mkdir svn-new-url
75 GIT_DIR=svn-new-url bang git init --bare --quiet
76 # We initially use -s for the init which will possibly shorten
77 # the URL. However, the shortening can fail if a password is
78 # not required for the longer version but is for the shorter,
79 # so try again without -s if the -s version fails.
80 cmdstr='git svn init --prefix= -s "$svnurl" < /dev/null > /dev/null 2>&1 || '
81 cmdstr="$cmdstr"'git svn init --prefix= "$svnurl" < /dev/null > /dev/null 2>&1'
82 GIT_DIR=svn-new-url bang eval "$cmdstr"
83 gitsvnurl="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.url || :)"
84 gitsvnfetch="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.fetch || :)"
85 gitsvnprefixnew="${gitsvnfetch%%:*}"
86 gitsvnsuffixnew="${gitsvnprefixnew##*/}"
87 gitsvnprefixnew="${gitsvnprefixnew%$gitsvnsuffixnew}"
88 rm -rf svn-new-url || :
89 # Using GIT_DIR= with bang leaves it set to svn-new-url, so reset it to .
90 GIT_DIR=.
91 if [ "$gitsvnurl" != "$(git config --get svn-remote.svn.url || :)" ]; then
92 # The url has been changed.
93 # We must update the url and replace the prefix on all config items
94 gitsvnfetch="$(git config --get-all svn-remote.svn.fetch | head -1 || :)"
95 gitsvnprefixold="${gitsvnfetch%%:*}"
96 gitsvnsuffixold="${gitsvnprefixold##*/}"
97 gitsvnprefixold="${gitsvnprefixold%$gitsvnsuffixold}"
98 git config --remove-section 'svn-remote.svnnew' 2>/dev/null || :
99 git config 'svn-remote.svnnew.url' "$gitsvnurl"
100 { git config --get-regexp '^svn-remote\.svn\.' || :; } | \
101 { while read sname sval; do
102 case "$sname" in
103 svn-remote.svn.fetch|svn-remote.svn.branches|svn-remote.svn.tags)
104 sname="${sname#svn-remote.svn.}"
105 sval="${sval#$gitsvnprefixold}"
106 bang git config --add "svn-remote.svnnew.$sname" "${gitsvnprefixnew}$sval"
107 esac
108 done; }
109 bang git config -f svn/.metadata svn-remote.svn.reposRoot "$gitsvnurl"
110 bang git config --remove-section svn-remote.svn
111 bang git config --rename-section svn-remote.svnnew svn-remote.svn
113 bang config_set svnurl "$svnurl"
115 # remove any stale *.lock files greater than 1 hour old in case
116 # git-svn was killed on the last update because it took too long
117 find svn -type f -name '*.lock' -mmin +60 -print0 | xargs -0 rm -f
118 GIT_DIR=. bang git svn fetch --quiet < /dev/null
119 # git svn does not preserve group permissions in the svn subdirectory
120 chmod -R ug+rw,o+r svn
121 # git svn also leaves behind ref turds that end with @nnn
122 # We get rid of them now
123 git show-ref | \
124 { while read sha1 ref; do
125 case "$ref" in
126 ?*@[1-9]|?*@[1-9][0-9]|?*@[1-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9]|\
127 ?*@[1-9][0-9][0-9][0-9][0-9]|?*@[1-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]|\
129 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])
130 git update-ref -d "$ref"
131 esac
132 done; }
134 darcs://*)
135 httpurl="${url/darcs:\/\//http://}"
136 bang git_darcs_fetch "$httpurl"
138 bzr://*)
139 bzrurl="${url#bzr://}"
140 bang git_bzr_fetch "$bzrurl"
143 [ "$url" = "$(git config --get remote.origin.url || :)" ] || bang config_set_raw remote.origin.url "$url"
144 GIT_SSL_NO_VERIFY=1 bang git remote update
145 GIT_SSL_NO_VERIFY=1 bang git remote prune origin
147 esac
149 # The objects subdirectories permissions must be updated now.
150 # In the case of a dumb http clone, the permissions will not be correct
151 # (missing group write) despite the core.sharedrepository=1 setting!
152 # The objects themselves seem to have the correct permissions.
153 # This problem appears to have been fixed in the most recent git versions.
154 perms=g+w
155 [ "$cfg_permission_control" != "Hooks" ] || perms=go+w
156 chmod $perms $(find objects -maxdepth 1 -type d) 2>/dev/null || :
158 bang git update-server-info
159 bang config_set lastrefresh "$(date "$datefmt")"
161 # Look at which refs changed and trigger ref-change for these
162 bang_eval "git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
163 bang_eval "sort -k1b,1 <.refs-temp >.refs-after"
164 sockpath="$cfg_chroot/etc/taskd.socket"
165 if [ -S "$sockpath" ] && ! cmp -s .refs-before .refs-after; then
166 join -j 1 .refs-before .refs-after |
167 while read ref old new; do
168 [ "$old" != "$new" ] || continue
169 echo "ref-change %$proj% $proj $old $new $ref" | nc.openbsd -w 1 -U "$sockpath"
170 done
171 join -j 1 -v 1 .refs-before .refs-after |
172 while read ref old; do
173 echo "ref-change %$proj% $proj $old 0000000000000000000000000000000000000000 $ref" | nc.openbsd -w 1 -U "$sockpath"
174 done
175 join -j 1 -v 2 .refs-before .refs-after |
176 while read ref new; do
177 echo "ref-change %$proj% $proj 0000000000000000000000000000000000000000 $new $ref" | nc.openbsd -w 1 -U "$sockpath"
178 done
179 git config gitweb.lastchange "$(date '+%a, %d %b %Y %T %z')"
180 git for-each-ref --sort=-committerdate --format='%(committerdate:iso8601)' \
181 --count=1 refs/heads > info/lastactivity
184 rm -f .refs-before .refs-after .refs-temp
186 if [ -e .banged ]; then
187 [ -z "$mailaddrs" ] ||
189 echo "$proj update succeeded - failure recovery"
190 echo "this status message may be disabled on the project admin page"
191 } | mail -s "[$cfg_name] $proj update succeeded" "$mailaddrs" || :
192 rm .banged
195 progress "- [$proj] update (`date`)"