blob: c0762922751a8f906116e74fd8a2c2ce19194b2b [file] [log] [blame]
Linus Torvalds3f571e02005-06-22 18:49:43 -07001#!/bin/sh
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -07002#
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
8usage() {
Junio C Hamanoaae4f422005-08-14 17:25:57 -07009 echo >&2 "* git clone [-l [-s]] [-q] [-u <upload-pack>] <repo> <dir>"
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -070010 exit 1
11}
12
Linus Torvaldsba375ac2005-07-08 15:46:33 -070013get_repo_base() {
14 (cd "$1" && (cd .git ; pwd)) 2> /dev/null
15}
16
Junio C Hamano0516de32005-09-05 00:47:39 -070017if [ -n "$GIT_SSL_NO_VERIFY" ]; then
18 curl_extra_args="-k"
19fi
20
21http_fetch () {
22 # $1 = Remote, $2 = Local
23 curl -nsf $curl_extra_args "$1" >"$2"
24}
25
26clone_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.
34Perhaps 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/\(.*\)'` &&
Junio C Hamano215a7ad2005-09-07 17:26:23 -070053 git-http-fetch -v -a -w "$name" "$name" "$1/" || exit 1
Junio C Hamano0516de32005-09-05 00:47:39 -070054 done <"$clone_tmp/refs"
55 rm -fr "$clone_tmp"
56}
57
Linus Torvalds167a4a32005-07-09 10:52:35 -070058quiet=
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -070059use_local=no
Junio C Hamanoaae4f422005-08-14 17:25:57 -070060local_shared=no
Junio C Hamano6ec311d2005-07-13 20:25:54 -070061upload_pack=
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -070062while
63 case "$#,$1" in
64 0,*) break ;;
Junio C Hamano1cadb5a2005-07-22 19:11:22 -070065 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
Junio C Hamanoaae4f422005-08-14 17:25:57 -070066 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
67 local_shared=yes ;;
Linus Torvalds167a4a32005-07-09 10:52:35 -070068 *,-q|*,--quiet) quiet=-q ;;
Junio C Hamano1cadb5a2005-07-22 19:11:22 -070069 1,-u|1,--upload-pack) usage ;;
Junio C Hamano6ec311d2005-07-13 20:25:54 -070070 *,-u|*,--upload-pack)
71 shift
Junio C Hamano1cadb5a2005-07-22 19:11:22 -070072 upload_pack="--exec=$1" ;;
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -070073 *,-*) usage ;;
74 *) break ;;
75 esac
76do
77 shift
78done
79
Linus Torvaldsba375ac2005-07-08 15:46:33 -070080# Turn the source into an absolute path if
81# it is local
Linus Torvalds3f571e02005-06-22 18:49:43 -070082repo="$1"
Linus Torvaldsba375ac2005-07-08 15:46:33 -070083local=no
84if base=$(get_repo_base "$repo"); then
85 repo="$base"
86 local=yes
87fi
88
Linus Torvalds3f571e02005-06-22 18:49:43 -070089dir="$2"
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -070090mkdir "$dir" &&
91D=$(
92 (cd "$dir" && git-init-db && pwd)
93) &&
94test -d "$D" || usage
95
96# We do local magic only when the user tells us to.
Linus Torvaldsba375ac2005-07-08 15:46:33 -070097case "$local,$use_local" in
98yes,yes)
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -070099 ( cd "$repo/objects" ) || {
Junio C Hamanoab6625e2005-07-11 13:30:54 -0700100 echo >&2 "-l flag seen but $repo is not local."
101 exit 1
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -0700102 }
103
Junio C Hamanoaae4f422005-08-14 17:25:57 -0700104 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)
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -0700109
Junio C Hamanoaae4f422005-08-14 17:25:57 -0700110 # objects directory should not be empty since we are cloning!
111 test -f "$repo/$sample_file" || exit
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -0700112
Junio C Hamanoaae4f422005-08-14 17:25:57 -0700113 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"
Junio C Hamano0f87f892005-08-17 15:18:41 -0700125 {
126 test -f "$repo/objects/info/alternates" &&
127 cat "$repo/objects/info/alternates";
128 echo "$repo/objects"
129 } >"$D/.git/objects/info/alternates"
Junio C Hamanoaae4f422005-08-14 17:25:57 -0700130 ;;
131 esac
Junio C Hamanoe95ab1e2005-07-06 13:04:21 -0700132
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
Linus Torvalds7558ef82005-07-08 17:07:12 -0700140 ;;
141*)
Junio C Hamano1cadb5a2005-07-22 19:11:22 -0700142 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://*)
Junio C Hamano0516de32005-09-05 00:47:39 -0700148 clone_dumb_http "$repo" "$D"
Junio C Hamano1cadb5a2005-07-22 19:11:22 -0700149 ;;
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 ;;
Junio C Hamano6ec311d2005-07-13 20:25:54 -0700156 esac
Linus Torvalds7558ef82005-07-08 17:07:12 -0700157 ;;
158esac
Junio C Hamano1cadb5a2005-07-22 19:11:22 -0700159
160# Update origin.
Junio C Hamano6687f8f2005-08-20 03:05:42 -0700161mkdir -p "$D/.git/remotes/" &&
162rm -f "$D/.git/remotes/origin" &&
163echo >"$D/.git/remotes/origin" \
164"URL: $repo
165Pull: master:origin"