Attempt to avoid update/gc clustering
[girocco.git] / taskd / clone.sh
blobda1494fb8ef6068d07330bb689a90a78a2a869bf
1 #!/bin/bash
3 # Invoked from taskd/taskd.pl
5 . @basedir@/shlib.sh
7 set -e
9 umask 002
10 [ "$cfg_permission_control" != "Hooks" ] || umask 000
12 projdir="$1"
13 proj="${projdir%.git}"
15 cd "$cfg_reporoot/$projdir"
16 trap "echo '@OVER@'; touch .clone_failed" EXIT
17 url="$(config_get baseurl)"
19 if [ "$cfg_project_owners" = "source" ]; then
20 config_set owner "$(stat -c %U "$url" 2>/dev/null)"
23 mailaddrs="$(config_get owner || :)"
24 [ -z "$cfg_admin" ] ||
25 if [ -z "$mailaddrs" ]; then mailaddrs="$cfg_admin"; else mailaddrs="$mailaddrs,$cfg_admin"; fi
27 # Initial mirror
28 echo "Initiating mirroring..."
29 case "$url" in
30 svn://* | svn+http://* | svn+https://*)
31 # We just remove svn+ here, so svn+http://... becomes http://...
32 # We also remove a trailing '/' to match what git-svn will do
33 svnurl="${url#svn+}"
34 svnurl="${svnurl%/}"
35 # We must use GIT_DIR=. here or ever so "helpful" git-svn will
36 # create a .git subdirectory!
37 GIT_DIR=. git svn init -s "$svnurl"
38 # We need to remember this url so we can detect changes because
39 # ever so "helpful" git-svn may shorten it!
40 config_set svnurl "$svnurl"
41 # At this point, since we asked for a standard layout (-s) git-svn
42 # may have been "helpful" and adjusted our $svnurl to a prefix and
43 # then glued the removed suffix onto the front of any svn-remote.svn.*
44 # config items. We could avoid this by not using the '-s' option
45 # but then we might not get all the history. If, for example, we
46 # are cloning an http://svn.example.com/repos/public repository that
47 # early in its history moved trunk => public/trunk we would miss that
48 # earlier history without allowing the funky shorten+prefix behavior.
49 # So we read back the svn-remote.svn.fetch configuration and compute
50 # the prefix. This way we are sure to get the correct prefix.
51 gitsvnurl="$(git config --get svn-remote.svn.url || :)"
52 gitsvnfetch="$(git config --get-all svn-remote.svn.fetch | tail -1 || :)"
53 gitsvnprefix="${gitsvnfetch%trunk:refs/remotes/trunk}"
54 # Ask git-svn to store everything in the normal non-remote
55 # locations being careful to use the correct prefix
56 git config --replace-all svn-remote.svn.fetch "${gitsvnprefix}trunk:refs/heads/master"
57 git config --replace-all svn-remote.svn.branches "${gitsvnprefix}branches/*:refs/heads/*"
58 git config --replace-all svn-remote.svn.tags "${gitsvnprefix}tags/*:refs/tags/*"
59 # look for additional non-standard directories to fetch
60 # check for standard layout at the same time
61 foundstd=
62 foundfile=
63 { svn ls "$gitsvnurl/${gitsvnprefix}" 2>/dev/null || :; } | \
64 { while read file; do case $file in
65 # skip the already-handled standard ones and any with a space or tab
66 *' '*|*' '*) :;;
67 trunk/|branches/|tags/) foundstd=1;;
68 # only fetch extra directories from the $svnurl root (not any files)
69 *?/) git config --add svn-remote.svn.fetch \
70 "${gitsvnprefix}${file%/}:refs/heads/${file%/}";;
71 *?) foundfile=1;;
72 esac; done
73 # if files found and no standard directories present use a simpler layout
74 if [ -z "$foundstd" ] && [ -n "$foundfile" ]; then
75 git config --unset svn-remote.svn.branches
76 git config --unset svn-remote.svn.tags
77 git config --replace-all svn-remote.svn.fetch ':refs/heads/master'
78 fi; }
79 # Again, be careful to use GIT_DIR=. here or else new .git subdirectory!
80 GIT_DIR=. git svn fetch --quiet
81 # git svn does not preserve group permissions in the svn subdirectory
82 chmod -R ug+rw,o+r svn
83 # git svn also leaves behind ref turds that end with @nnn
84 # We get rid of them now
85 git show-ref | \
86 { while read sha1 ref; do
87 case "$ref" in
88 ?*@[1-9]|?*@[1-9][0-9]|?*@[1-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9]|\
89 ?*@[1-9][0-9][0-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9][0-9][0-9]|\
90 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9]|\
91 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])
92 git update-ref -d "$ref"
93 esac
94 done; }
96 darcs://*)
97 httpurl="${url/darcs:\/\//http://}"
98 "$cfg_basedir"/bin/darcs-fast-export --export-marks=$(pwd)/dfe-marks "$httpurl" | \
99 git fast-import --export-marks=$(pwd)/gfi-marks
100 # This is here because by default only the exit code of
101 # git fast-import is checked
102 [ ${PIPESTATUS[0]} = 0 -a ${PIPESTATUS[1]} = 0 ]
104 bzr://*)
105 # we just remove bzr:// here, a typical bzr url is just
106 # "lp:foo"
107 bzrurl="${url#bzr://}"
108 bzr fast-export --export-marks=$(pwd)/bfe-marks "$bzrurl" | \
109 git fast-import --export-marks=$(pwd)/gfi-marks
110 [ ${PIPESTATUS[0]} = 0 -a ${PIPESTATUS[1]} = 0 ]
113 git remote rm origin >/dev/null 2>&1 || :
114 git remote add --mirror origin "$url"
115 GIT_SSL_NO_VERIFY=1 git remote update
116 GIT_SSL_NO_VERIFY=1 git remote prune origin
118 esac
120 # The objects subdirectories permissions must be updated now.
121 # In the case of a dumb http clone, the permissions will not be correct
122 # (missing group write) despite the core.sharedrepository=1 setting!
123 # The objects themselves seem to have the correct permissions.
124 # This problem appears to have been fixed in the most recent git versions.
125 perms=g+w
126 [ "$cfg_permission_control" != "Hooks" ] || perms=go+w
127 chmod $perms $(find objects -maxdepth 1 -type d) 2>/dev/null || :
129 # Initialize gitweb.lastchange and info/lastactivity
130 git config gitweb.lastchange "$(date '+%a, %d %b %Y %T %z')"
131 git for-each-ref --sort=-committerdate --format='%(committerdate:iso8601)' \
132 --count=1 refs/heads > info/lastactivity
133 [ -s info/lastactivity ] || rm -f info/lastactivity
135 # The rest
136 echo "Final touches..."
137 git update-server-info
138 trap "" EXIT
140 [ -z "$mailaddrs" ] ||
141 mail -s "[$cfg_name] $proj clone completed" "$mailaddrs" <<EOT || :
142 Congratulations! The clone of project $proj just completed.
144 * Source URL: $url
145 * GitWeb interface: $cfg_gitweburl/$projdir
146 * Project settings: $cfg_webadmurl/editproj.cgi?name=$proj
148 Have a lot of fun.
151 echo "Mirroring finished successfuly!"
152 rm .clone_in_progress
153 echo "@OVER@"