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