]>
Commit | Line | Data |
---|---|---|
3f571e0b | 1 | #!/bin/sh |
e95ab1ed JH |
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() { | |
aae4f42c | 9 | echo >&2 "* git clone [-l [-s]] [-q] [-u <upload-pack>] <repo> <dir>" |
e95ab1ed JH |
10 | exit 1 |
11 | } | |
12 | ||
ba375acf LT |
13 | get_repo_base() { |
14 | (cd "$1" && (cd .git ; pwd)) 2> /dev/null | |
15 | } | |
16 | ||
167a4a33 | 17 | quiet= |
e95ab1ed | 18 | use_local=no |
aae4f42c | 19 | local_shared=no |
6ec311da | 20 | upload_pack= |
e95ab1ed JH |
21 | while |
22 | case "$#,$1" in | |
23 | 0,*) break ;; | |
1cadb5a2 | 24 | *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;; |
aae4f42c JH |
25 | *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared) |
26 | local_shared=yes ;; | |
167a4a33 | 27 | *,-q|*,--quiet) quiet=-q ;; |
1cadb5a2 | 28 | 1,-u|1,--upload-pack) usage ;; |
6ec311da JH |
29 | *,-u|*,--upload-pack) |
30 | shift | |
1cadb5a2 | 31 | upload_pack="--exec=$1" ;; |
e95ab1ed JH |
32 | *,-*) usage ;; |
33 | *) break ;; | |
34 | esac | |
35 | do | |
36 | shift | |
37 | done | |
38 | ||
ba375acf LT |
39 | # Turn the source into an absolute path if |
40 | # it is local | |
3f571e0b | 41 | repo="$1" |
ba375acf LT |
42 | local=no |
43 | if base=$(get_repo_base "$repo"); then | |
44 | repo="$base" | |
45 | local=yes | |
46 | fi | |
47 | ||
3f571e0b | 48 | dir="$2" |
e95ab1ed JH |
49 | mkdir "$dir" && |
50 | D=$( | |
51 | (cd "$dir" && git-init-db && pwd) | |
52 | ) && | |
53 | test -d "$D" || usage | |
54 | ||
55 | # We do local magic only when the user tells us to. | |
ba375acf LT |
56 | case "$local,$use_local" in |
57 | yes,yes) | |
e95ab1ed | 58 | ( cd "$repo/objects" ) || { |
ab6625e0 JH |
59 | echo >&2 "-l flag seen but $repo is not local." |
60 | exit 1 | |
e95ab1ed JH |
61 | } |
62 | ||
aae4f42c JH |
63 | case "$local_shared" in |
64 | no) | |
65 | # See if we can hardlink and drop "l" if not. | |
66 | sample_file=$(cd "$repo" && \ | |
67 | find objects -type f -print | sed -e 1q) | |
e95ab1ed | 68 | |
aae4f42c JH |
69 | # objects directory should not be empty since we are cloning! |
70 | test -f "$repo/$sample_file" || exit | |
e95ab1ed | 71 | |
aae4f42c JH |
72 | l= |
73 | if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev/null | |
74 | then | |
75 | l=l | |
76 | fi && | |
77 | rm -f "$D/.git/objects/sample" && | |
78 | cd "$repo" && | |
79 | find objects -type f -print | | |
80 | cpio -puamd$l "$D/.git/" || exit 1 | |
81 | ;; | |
82 | yes) | |
83 | mkdir -p "$D/.git/objects/info" | |
0f87f893 JH |
84 | { |
85 | test -f "$repo/objects/info/alternates" && | |
86 | cat "$repo/objects/info/alternates"; | |
87 | echo "$repo/objects" | |
88 | } >"$D/.git/objects/info/alternates" | |
aae4f42c JH |
89 | ;; |
90 | esac | |
e95ab1ed JH |
91 | |
92 | # Make a duplicate of refs and HEAD pointer | |
93 | HEAD= | |
94 | if test -f "$repo/HEAD" | |
95 | then | |
96 | HEAD=HEAD | |
97 | fi | |
98 | tar Ccf "$repo" - refs $HEAD | tar Cxf "$D/.git" - || exit 1 | |
7558ef89 LT |
99 | ;; |
100 | *) | |
1cadb5a2 JH |
101 | case "$repo" in |
102 | rsync://*) | |
103 | rsync $quiet -avz --ignore-existing "$repo/objects/" "$D/.git/objects/" && | |
104 | rsync $quiet -avz --ignore-existing "$repo/refs/" "$D/.git/refs/" | |
105 | ;; | |
106 | http://*) | |
c0a58fbb JH |
107 | git-clone-dumb-http "$repo" "$D" |
108 | case "$?" in | |
109 | 2) | |
110 | echo "Somebody should define smarter http server protocol" >&2 | |
111 | exit 1 | |
112 | ;; | |
113 | 0) | |
114 | ;; | |
115 | *) | |
116 | exit | |
117 | esac | |
1cadb5a2 JH |
118 | ;; |
119 | *) | |
120 | cd "$D" && case "$upload_pack" in | |
121 | '') git-clone-pack $quiet "$repo" ;; | |
122 | *) git-clone-pack $quiet "$upload_pack" "$repo" ;; | |
123 | esac | |
124 | ;; | |
6ec311da | 125 | esac |
7558ef89 LT |
126 | ;; |
127 | esac | |
1cadb5a2 JH |
128 | |
129 | # Update origin. | |
130 | mkdir -p "$D/.git/branches/" && | |
131 | rm -f "$D/.git/branches/origin" && | |
132 | echo "$repo" >"$D/.git/branches/origin" |