mirroring: add individual foreign vcs mirror control
[girocco.git] / taskd / clone.sh
blobf11201264a53aa0c8537521e4f1e8441b9ff164a
1 #!/bin/sh
3 # Invoked from taskd/taskd.pl
5 . @basedir@/shlib.sh
7 # darcs fast-export | git fast-import with error handling
8 git_darcs_fetch() {
9 _err1=
10 _err2=
11 exec 3>&1
12 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
14 exec 4>&3 3>&1 1>&4 4>&-
15 { _e1=0; "$cfg_basedir"/bin/darcs-fast-export --export-marks=$(pwd)/dfe-marks "$1" 3>&- || _e1=$?; echo $_e1 >&3; } | \
16 { _e2=0; git fast-import --force --export-marks=$(pwd)/gfi-marks 3>&- || _e2=$?; echo $_e2 >&3; }
18 EOT
19 exec 3>&-
20 [ "$_err1" = 0 -a "$_err2" = 0 ]
21 return $?
24 # bzr fast-export | git fast-import with error handling
25 git_bzr_fetch() {
26 _err1=
27 _err2=
28 exec 3>&1
29 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
31 exec 4>&3 3>&1 1>&4 4>&-
32 { _e1=0; bzr fast-export --export-marks=$(pwd)/bfe-marks "$1" 3>&- || _e1=$?; echo $_e1 >&3; } | \
33 { _e2=0; git fast-import --force --export-marks=$(pwd)/gfi-marks 3>&- || _e2=$?; echo $_e2 >&3; }
35 EOT
36 exec 3>&-
37 [ "$_err1" = 0 -a "$_err2" = 0 ]
38 return $?
41 set -e
43 umask 002
44 [ "$cfg_permission_control" != "Hooks" ] || umask 000
46 projdir="$1"
47 proj="${projdir%.git}"
49 cd "$cfg_reporoot/$projdir"
50 trap "echo '@OVER@'; touch .clone_failed" EXIT
51 [ -n "$cfg_mirror" ] || { echo "Mirroring is disabled" >&2; exit 1; }
52 url="$(config_get baseurl)"
54 if [ "$cfg_project_owners" = "source" ]; then
55 config_set owner "$(stat -c %U "$url" 2>/dev/null)"
58 mailaddrs="$(config_get owner || :)"
59 [ -z "$cfg_admin" ] || \
60 if [ -z "$mailaddrs" ]; then mailaddrs="$cfg_admin"; else mailaddrs="$mailaddrs,$cfg_admin"; fi
62 # Initial mirror
63 echo "Initiating mirroring..."
64 case "$url" in
65 svn://* | svn+http://* | svn+https://*)
66 [ -n "$cfg_mirror_svn" ] || { echo "Mirroring svn is disabled" >&2; exit 1; }
67 # We just remove svn+ here, so svn+http://... becomes http://...
68 # We also remove a trailing '/' to match what git-svn will do
69 svnurl="${url#svn+}"
70 svnurl="${svnurl%/}"
71 # We require svn info to succeed on the URL otherwise it's
72 # simply not a valid URL and without using -s on the init it
73 # will not otherwise be tested until the fetch
74 svn --non-interactive info "$svnurl" > /dev/null
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 # We must use GIT_DIR=. here or ever so "helpful" git-svn will
80 # create a .git subdirectory!
81 GIT_DIR=. git svn init --prefix= -s "$svnurl" < /dev/null || \
82 GIT_DIR=. git svn init --prefix= "$svnurl" < /dev/null
83 # We need to remember this url so we can detect changes because
84 # ever so "helpful" git-svn may shorten it!
85 config_set svnurl "$svnurl"
86 # At this point, since we asked for a standard layout (-s) git-svn
87 # may have been "helpful" and adjusted our $svnurl to a prefix and
88 # then glued the removed suffix onto the front of any svn-remote.svn.*
89 # config items. We could avoid this by not using the '-s' option
90 # but then we might not get all the history. If, for example, we
91 # are cloning an http://svn.example.com/repos/public repository that
92 # early in its history moved trunk => public/trunk we would miss that
93 # earlier history without allowing the funky shorten+prefix behavior.
94 # So we read back the svn-remote.svn.fetch configuration and compute
95 # the prefix. This way we are sure to get the correct prefix.
96 gitsvnurl="$(git config --get svn-remote.svn.url || :)"
97 gitsvnfetch="$(git config --get-all svn-remote.svn.fetch | tail -1 || :)"
98 gitsvnprefix="${gitsvnfetch%%:*}"
99 gitsvnsuffix="${gitsvnprefix##*/}"
100 gitsvnprefix="${gitsvnprefix%$gitsvnsuffix}"
101 # Ask git-svn to store everything in the normal non-remote
102 # locations being careful to use the correct prefix
103 git config --replace-all svn-remote.svn.fetch "${gitsvnprefix}trunk:refs/heads/master"
104 git config --replace-all svn-remote.svn.branches "${gitsvnprefix}branches/*:refs/heads/*"
105 git config --replace-all svn-remote.svn.tags "${gitsvnprefix}tags/*:refs/tags/*"
106 # look for additional non-standard directories to fetch
107 # check for standard layout at the same time
108 foundstd=
109 foundfile=
110 { svn --non-interactive ls "$gitsvnurl/${gitsvnprefix}" 2>/dev/null || :; } | \
111 { while read file; do case $file in
112 # skip the already-handled standard ones and any with a space or tab
113 *' '*|*' '*) :;;
114 trunk/|branches/|tags/) foundstd=1;;
115 # only fetch extra directories from the $svnurl root (not any files)
116 *?/) git config --add svn-remote.svn.fetch \
117 "${gitsvnprefix}${file%/}:refs/heads/${file%/}";;
118 *?) foundfile=1;;
119 esac; done
120 # if files found and no standard directories present use a simpler layout
121 if [ -z "$foundstd" ] && [ -n "$foundfile" ]; then
122 git config --unset svn-remote.svn.branches
123 git config --unset svn-remote.svn.tags
124 git config --replace-all svn-remote.svn.fetch ':refs/heads/master'
125 fi; }
126 # Again, be careful to use GIT_DIR=. here or else new .git subdirectory!
127 GIT_DIR=. git svn fetch --quiet < /dev/null
128 # git svn does not preserve group permissions in the svn subdirectory
129 chmod -R ug+rw,o+r svn
130 # git svn also leaves behind ref turds that end with @nnn
131 # We get rid of them now
132 git show-ref | \
133 { while read sha1 ref; do
134 case "$ref" in
135 ?*@[1-9]|?*@[1-9][0-9]|?*@[1-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9]|\
136 ?*@[1-9][0-9][0-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9][0-9][0-9]|\
137 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9]|\
138 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])
139 git update-ref -d "$ref"
140 esac
141 done; }
143 darcs://*)
144 [ -n "$cfg_mirror_darcs" ] || { echo "Mirroring darcs is disabled" >&2; exit 1; }
145 httpurl="http://${url#darcs://}"
146 # Remove any left-over .darcs dirs from a previous failed attempt
147 rm -rf *.darcs
148 git_darcs_fetch "$httpurl"
150 bzr://*)
151 [ -n "$cfg_mirror_bzr" ] || { echo "Mirroring bzr is disabled" >&2; exit 1; }
152 # we just remove bzr:// here, a typical bzr url is just
153 # "lp:foo"
154 bzrurl="${url#bzr://}"
155 git_bzr_fetch "$bzrurl"
158 git remote rm origin >/dev/null 2>&1 || :
159 # starting with Git 1.7.5 --mirror by itself will spew a warning
160 # since we support older Git versions, just toss the warning
161 git remote add --mirror origin "$url" 2>/dev/null
162 GIT_SSL_NO_VERIFY=1 git remote update
163 GIT_SSL_NO_VERIFY=1 git remote prune origin
165 esac
167 # The objects subdirectories permissions must be updated now.
168 # In the case of a dumb http clone, the permissions will not be correct
169 # (missing group write) despite the core.sharedrepository=1 setting!
170 # The objects themselves seem to have the correct permissions.
171 # This problem appears to have been fixed in the most recent git versions.
172 perms=g+w
173 [ "$cfg_permission_control" != "Hooks" ] || perms=go+w
174 chmod $perms $(find objects -maxdepth 1 -type d) 2>/dev/null || :
176 # Initialize gitweb.lastchange and info/lastactivity
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
180 [ -s info/lastactivity ] || rm -f info/lastactivity
182 # The rest
183 echo "Final touches..."
184 git update-server-info
185 trap "" EXIT
187 [ -z "$mailaddrs" ] ||
188 mail -s "[$cfg_name] $proj clone completed" "$mailaddrs" <<EOT || :
189 Congratulations! The clone of project $proj just completed.
191 * Source URL: $url
192 * GitWeb interface: $cfg_gitweburl/$projdir
193 * Project settings: $cfg_webadmurl/editproj.cgi?name=$(echo "$proj" | sed -e 's/[+]/%2B/g')
195 Have a lot of fun.
198 echo "Mirroring finished successfuly!"
199 # In case this is a re-mirror, lastgc could have been set already so clear it now
200 git config --unset gitweb.lastgc || :
201 rm .clone_in_progress
202 echo "@OVER@"