9f960df35dd551c54aa7695517e8dd6d1b74eba6
[girocco.git] / taskd / clone.sh
blob9f960df35dd551c54aa7695517e8dd6d1b74eba6
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 url="$(config_get baseurl)"
53 if [ "$cfg_project_owners" = "source" ]; then
54 config_set owner "$(stat -c %U "$url" 2>/dev/null)"
57 mailaddrs="$(config_get owner || :)"
58 [ -z "$cfg_admin" ] || \
59 if [ -z "$mailaddrs" ]; then mailaddrs="$cfg_admin"; else mailaddrs="$mailaddrs,$cfg_admin"; fi
61 # Initial mirror
62 echo "Initiating mirroring..."
63 case "$url" in
64 svn://* | svn+http://* | svn+https://*)
65 # We just remove svn+ here, so svn+http://... becomes http://...
66 # We also remove a trailing '/' to match what git-svn will do
67 svnurl="${url#svn+}"
68 svnurl="${svnurl%/}"
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 svn --non-interactive info "$svnurl" > /dev/null
73 # We initially use -s for the init which will possibly shorten
74 # the URL. However, the shortening can fail if a password is
75 # not required for the longer version but is for the shorter,
76 # so try again without -s if the -s version fails.
77 # We must use GIT_DIR=. here or ever so "helpful" git-svn will
78 # create a .git subdirectory!
79 GIT_DIR=. git svn init --prefix= -s "$svnurl" < /dev/null || \
80 GIT_DIR=. git svn init --prefix= "$svnurl" < /dev/null
81 # We need to remember this url so we can detect changes because
82 # ever so "helpful" git-svn may shorten it!
83 config_set svnurl "$svnurl"
84 # At this point, since we asked for a standard layout (-s) git-svn
85 # may have been "helpful" and adjusted our $svnurl to a prefix and
86 # then glued the removed suffix onto the front of any svn-remote.svn.*
87 # config items. We could avoid this by not using the '-s' option
88 # but then we might not get all the history. If, for example, we
89 # are cloning an http://svn.example.com/repos/public repository that
90 # early in its history moved trunk => public/trunk we would miss that
91 # earlier history without allowing the funky shorten+prefix behavior.
92 # So we read back the svn-remote.svn.fetch configuration and compute
93 # the prefix. This way we are sure to get the correct prefix.
94 gitsvnurl="$(git config --get svn-remote.svn.url || :)"
95 gitsvnfetch="$(git config --get-all svn-remote.svn.fetch | tail -1 || :)"
96 gitsvnprefix="${gitsvnfetch%%:*}"
97 gitsvnsuffix="${gitsvnprefix##*/}"
98 gitsvnprefix="${gitsvnprefix%$gitsvnsuffix}"
99 # Ask git-svn to store everything in the normal non-remote
100 # locations being careful to use the correct prefix
101 git config --replace-all svn-remote.svn.fetch "${gitsvnprefix}trunk:refs/heads/master"
102 git config --replace-all svn-remote.svn.branches "${gitsvnprefix}branches/*:refs/heads/*"
103 git config --replace-all svn-remote.svn.tags "${gitsvnprefix}tags/*:refs/tags/*"
104 # look for additional non-standard directories to fetch
105 # check for standard layout at the same time
106 foundstd=
107 foundfile=
108 { svn --non-interactive ls "$gitsvnurl/${gitsvnprefix}" 2>/dev/null || :; } | \
109 { while read file; do case $file in
110 # skip the already-handled standard ones and any with a space or tab
111 *' '*|*' '*) :;;
112 trunk/|branches/|tags/) foundstd=1;;
113 # only fetch extra directories from the $svnurl root (not any files)
114 *?/) git config --add svn-remote.svn.fetch \
115 "${gitsvnprefix}${file%/}:refs/heads/${file%/}";;
116 *?) foundfile=1;;
117 esac; done
118 # if files found and no standard directories present use a simpler layout
119 if [ -z "$foundstd" ] && [ -n "$foundfile" ]; then
120 git config --unset svn-remote.svn.branches
121 git config --unset svn-remote.svn.tags
122 git config --replace-all svn-remote.svn.fetch ':refs/heads/master'
123 fi; }
124 # Again, be careful to use GIT_DIR=. here or else new .git subdirectory!
125 GIT_DIR=. git svn fetch --quiet < /dev/null
126 # git svn does not preserve group permissions in the svn subdirectory
127 chmod -R ug+rw,o+r svn
128 # git svn also leaves behind ref turds that end with @nnn
129 # We get rid of them now
130 git show-ref | \
131 { while read sha1 ref; do
132 case "$ref" in
133 ?*@[1-9]|?*@[1-9][0-9]|?*@[1-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9]|\
134 ?*@[1-9][0-9][0-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9][0-9][0-9]|\
135 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9]|\
136 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])
137 git update-ref -d "$ref"
138 esac
139 done; }
141 darcs://*)
142 httpurl="http://${url#darcs://}"
143 # Remove any left-over .darcs dirs from a previous failed attempt
144 rm -rf *.darcs
145 git_darcs_fetch "$httpurl"
147 bzr://*)
148 # we just remove bzr:// here, a typical bzr url is just
149 # "lp:foo"
150 bzrurl="${url#bzr://}"
151 git_bzr_fetch "$bzrurl"
154 git remote rm origin >/dev/null 2>&1 || :
155 # starting with Git 1.7.5 --mirror by itself will spew a warning
156 # since we support older Git versions, just toss the warning
157 git remote add --mirror origin "$url" 2>/dev/null
158 GIT_SSL_NO_VERIFY=1 git remote update
159 GIT_SSL_NO_VERIFY=1 git remote prune origin
161 esac
163 # The objects subdirectories permissions must be updated now.
164 # In the case of a dumb http clone, the permissions will not be correct
165 # (missing group write) despite the core.sharedrepository=1 setting!
166 # The objects themselves seem to have the correct permissions.
167 # This problem appears to have been fixed in the most recent git versions.
168 perms=g+w
169 [ "$cfg_permission_control" != "Hooks" ] || perms=go+w
170 chmod $perms $(find objects -maxdepth 1 -type d) 2>/dev/null || :
172 # Initialize gitweb.lastchange and info/lastactivity
173 git config gitweb.lastchange "$(date '+%a, %d %b %Y %T %z')"
174 git for-each-ref --sort=-committerdate --format='%(committerdate:iso8601)' \
175 --count=1 refs/heads > info/lastactivity
176 [ -s info/lastactivity ] || rm -f info/lastactivity
178 # The rest
179 echo "Final touches..."
180 git update-server-info
181 trap "" EXIT
183 [ -z "$mailaddrs" ] ||
184 mail -s "[$cfg_name] $proj clone completed" "$mailaddrs" <<EOT || :
185 Congratulations! The clone of project $proj just completed.
187 * Source URL: $url
188 * GitWeb interface: $cfg_gitweburl/$projdir
189 * Project settings: $cfg_webadmurl/editproj.cgi?name=$(echo "$proj" | sed -e 's/[+]/%2B/g')
191 Have a lot of fun.
194 echo "Mirroring finished successfuly!"
195 # In case this is a re-mirror, lastgc could have been set already so clear it now
196 git config --unset gitweb.lastgc || :
197 rm .clone_in_progress
198 echo "@OVER@"