[PATCH] Documentation/repository-layout.txt typo
[git.git] / git-clone.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005, Linus Torvalds
4 # Copyright (c) 2005, Junio C Hamano
5
6 # Clone a repository into a different directory that does not yet exist.
7
8 usage() {
9         echo >&2 "* git clone [-l [-s]] [-q] [-u <upload-pack>] <repo> <dir>"
10         exit 1
11 }
12
13 get_repo_base() {
14         (cd "$1" && (cd .git ; pwd)) 2> /dev/null
15 }
16
17 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
18     curl_extra_args="-k"
19 fi
20
21 http_fetch () {
22         # $1 = Remote, $2 = Local
23         curl -nsf $curl_extra_args "$1" >"$2"
24 }
25
26 clone_dumb_http () {
27         # $1 - remote, $2 - local
28         cd "$2" &&
29         clone_tmp='.git/clone-tmp' &&
30         mkdir -p "$clone_tmp" || exit 1
31         http_fetch "$1/info/refs" "$clone_tmp/refs" &&
32         http_fetch "$1/objects/info/packs" "$clone_tmp/packs" || {
33                 echo >&2 "Cannot get remote repository information.
34 Perhaps git-update-server-info needs to be run there?"
35                 exit 1;
36         }
37         while read type name
38         do
39                 case "$type" in
40                 P) ;;
41                 *) continue ;;
42                 esac &&
43
44                 idx=`expr "$name" : '\(.*\)\.pack'`.idx
45                 http_fetch "$1/objects/pack/$name" ".git/objects/pack/$name" &&
46                 http_fetch "$1/objects/pack/$idx" ".git/objects/pack/$idx" &&
47                 git-verify-pack ".git/objects/pack/$idx" || exit 1
48         done <"$clone_tmp/packs"
49
50         while read sha1 refname
51         do
52                 name=`expr "$refname" : 'refs/\(.*\)'` &&
53                 git-http-fetch -v -a -w "$name" "$name" "$1/" || exit 1
54         done <"$clone_tmp/refs"
55         rm -fr "$clone_tmp"
56 }
57
58 quiet=
59 use_local=no
60 local_shared=no
61 upload_pack=
62 while
63         case "$#,$1" in
64         0,*) break ;;
65         *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
66         *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared) 
67           local_shared=yes ;;
68         *,-q|*,--quiet) quiet=-q ;;
69         1,-u|1,--upload-pack) usage ;;
70         *,-u|*,--upload-pack)
71                 shift
72                 upload_pack="--exec=$1" ;;
73         *,-*) usage ;;
74         *) break ;;
75         esac
76 do
77         shift
78 done
79
80 # Turn the source into an absolute path if
81 # it is local
82 repo="$1"
83 local=no
84 if base=$(get_repo_base "$repo"); then
85         repo="$base"
86         local=yes
87 fi
88
89 dir="$2"
90 mkdir "$dir" &&
91 D=$(
92         (cd "$dir" && git-init-db && pwd)
93 ) &&
94 test -d "$D" || usage
95
96 # We do local magic only when the user tells us to.
97 case "$local,$use_local" in
98 yes,yes)
99         ( cd "$repo/objects" ) || {
100                 echo >&2 "-l flag seen but $repo is not local."
101                 exit 1
102         }
103
104         case "$local_shared" in
105         no)
106             # See if we can hardlink and drop "l" if not.
107             sample_file=$(cd "$repo" && \
108                           find objects -type f -print | sed -e 1q)
109
110             # objects directory should not be empty since we are cloning!
111             test -f "$repo/$sample_file" || exit
112
113             l=
114             if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev/null
115             then
116                     l=l
117             fi &&
118             rm -f "$D/.git/objects/sample" &&
119             cd "$repo" &&
120             find objects -type f -print |
121             cpio -puamd$l "$D/.git/" || exit 1
122             ;;
123         yes)
124             mkdir -p "$D/.git/objects/info"
125             {
126                 test -f "$repo/objects/info/alternates" &&
127                 cat "$repo/objects/info/alternates";
128                 echo "$repo/objects"
129             } >"$D/.git/objects/info/alternates"
130             ;;
131         esac
132
133         # Make a duplicate of refs and HEAD pointer
134         HEAD=
135         if test -f "$repo/HEAD"
136         then
137                 HEAD=HEAD
138         fi
139         tar Ccf "$repo" - refs $HEAD | tar Cxf "$D/.git" - || exit 1
140         ;;
141 *)
142         case "$repo" in
143         rsync://*)
144                 rsync $quiet -avz --ignore-existing "$repo/objects/" "$D/.git/objects/" &&
145                 rsync $quiet -avz --ignore-existing "$repo/refs/" "$D/.git/refs/"
146                 ;;
147         http://*)
148                 clone_dumb_http "$repo" "$D"
149                 ;;
150         *)
151                 cd "$D" && case "$upload_pack" in
152                 '') git-clone-pack $quiet "$repo" ;;
153                 *) git-clone-pack $quiet "$upload_pack" "$repo" ;;
154                 esac
155                 ;;
156         esac
157         ;;
158 esac
159
160 # Update origin.
161 mkdir -p "$D/.git/remotes/" &&
162 rm -f "$D/.git/remotes/origin" &&
163 echo >"$D/.git/remotes/origin" \
164 "URL: $repo
165 Pull: master:origin"