3 # Clone a remote repository
4 # Copyright (c) Petr Baudis, 2005
6 # This clones a remote GIT repository and checks it out locally.
8 # Takes a parameter specifying the location of the source repository and an
9 # optional second parameter specifying the destination. If the second
10 # parameter is omitted, the basename of the source repository is used as the
13 # For detailed description of the location of the source repository format
14 # (available protocols, specifying different remote branch, etc) please see
15 # the `cg-branch-add` documentation.
19 # -b:: Create a bare repository - do not check out working copy
20 # Create only the bare repository, without a working copy attached;
21 # this is useful e.g. for public repository mirrors. `cg-clone`
22 # is to `cg-init` as `cg-clone -b` is to `cg-admin-setuprepo`.
23 # (Still, if you are setting up a public or a central repository
24 # other people will push into, it might be more practical to just
25 # `cg-admin-setuprepo` it and then populate it with `cg-push`
26 # from the other side.)
28 # -l:: "Borrow" the object database when cloning locally
29 # Instead of hardlinking all the objects, set up an "alternate"
30 # record pointing at the source object database; this will cause
31 # any objects not found locally to be looked up remotely, which
32 # effectively eliminates the need to copy/hardlink the objects
33 # around. This is suitable for very fast cloning of arbitrarily
34 # big repositories, but your repository will become largely useless
35 # if the source repository disappears or gets damaged (note that
36 # it is generally BAD IDEA to prune the original repository if any
37 # repository is borrowing objects from it). The choice is yours.
39 # --reference PATH:: "Borrow" the object database from a third-party source
40 # This does the same thing as '-l' but instead of borrowing the
41 # objects from the source repository, it borrows them from yet
42 # another local repository. E.g. if you have Linus' kernel repository
43 # cloned locally and now want to clone akpm's repository, you can
46 # cg-clone --reference /path/to/linus/repo git://kernel.org/akpm/repo
48 # and it will download and keep only the objects that are really
49 # missing. Same considerations and warnings on the third-party source
50 # as in the case of '-l' apply. Also, you can reference only local
53 # -s:: Clone into the current directory
54 # Clone in the current directory instead of creating a new one.
55 # Specifying both -s and a destination directory makes no sense.
59 # If the clone has been interrupted for any reason, do not panic, calmly
60 # cd to the destination directory and run `cg-fetch`, which will in this case
61 # restart the initial clone. Chances are that you will not actually download
62 # any duplicate data. (At the time of writing this, the chances aren't for
63 # the native git protocol and ssh, but this may change in the future).
67 # If you want to clone the Cogito repository, you can say:
69 # $ cg-clone http://www.kernel.org/pub/scm/cogito/cogito.git
71 # and it will be cloned to the 'cogito' subdirectory of the current directory.
73 # To clone the 'next' branch of the Git repository, do e.g.:
75 # $ cg-clone git://git.kernel.org/pub/scm/git/git.git#next
77 # Testsuite: Checked as part of t-9105-fetch-local but tests for some features
80 USAGE
="cg-clone [-l] [-b] [-s] LOCATION [DESTDIR]"
83 .
"${COGITO_LIB}"cg-Xlib ||
exit 1
92 elif optparse
-b; then
94 elif optparse
--reference=; then
95 reference
[${#reference[@]}]="$OPTARG"
96 elif optparse
-s; then
103 location
="${ARGS[0]}"
105 [ -n "$location" ] || usage
106 location
="${location%/}"
109 if [ "$destdir" ]; then
110 [ ! "$same_dir" ] || die
"specifying both -s and DESTDIR makes no sense"
113 dir
="${location%#*}"; dir="${dir%/.git}"; dir="${dir##*/}"; dir="${dir%.git}"
114 [ "$repoonly" ] && dir
="$dir.git"
117 if ! echo "$location" |
grep -q ":" ; then
118 location
=$
(echo "$location" |
sed -e "s#^[^/]#$(pwd)\/&#")
120 [ ! "$alternate" ] || die
"specifying -l for non-local clone makes no sense"
124 if [ ! "$same_dir" ]; then
125 [ -e "$dir" ] && die
"$dir/ already exists"
126 if [ "$repoonly" ]; then
127 cg-admin-setuprepo
"$dir" ||
exit $?
129 mkdir
-p "$dir" ||
exit $?
136 if [ "$repoonly" ]; then
145 if [ -s "$_git/info/cg-fetch-earlydie" ] && [ ! "$same_dir" ]; then
151 cleanup_trap
"cleanup"
154 if [ ! "$repoonly" ]; then
155 cg-init
-I || die
"init failed"
157 echo $$
>"$_git/info/cg-fetch-earlydie"
160 [ ! -d "$repoloc/.git/objects" ] || repoloc
="$repoloc/.git"
161 [ "$alternate" ] && echo "$repoloc/objects" >> "$_git/objects/info/alternates"
162 for ref
in "${reference[@]}"; do
164 [ "${ref#/}" != "$ref" ] || relpath
=1
165 # Relative path; reference is relative to the object repository; we
166 # need to add one .. for objects, another .. for .git/, another .. for
167 # projdir unless ! same_Dir
169 if [ ! "$same_dir" ] && [ "$relpath" ]; then
170 # Relative path, account for project subdir
174 [ -d "$ref" ] || die
"referenced repository $ref not found"
175 [ ! -d "$ref/.git/objects" ] || ref
="$ref/.git"
176 [ -d "$ref/objects" ] || die
"reference $ref not a git repository"
177 [ ! "$relpath" ] || ref
="../../$ref"
178 echo "$ref/objects" >>"$_git/objects/info/alternates"
181 cg-branch-add origin
"$location"
183 mkdir
-p "$_git/info"
184 [ "$repoonly" ] && echo $$
>"$_git/info/cg-fetch-initial-wcless"
185 echo $$
>"$_git/info/cg-fetch-initial"
187 This is a clone-in-progress GIT working tree containing a GIT repository
188 in the .git subdirectory. If you see this file and noone is fetching or
189 cloning in this repository, the clone has been interrupted; you can restart
190 it by issuing this command (it's enough as-is):
194 cg-fetch origin ||
{ cleanup
; exit 1; }
196 echo "Cloned to $dir/ (origin $location available as branch \"origin\")"