]>
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() { | |
9 | echo >&2 "* git clone [-l] <repo> <dir>" | |
10 | exit 1 | |
11 | } | |
12 | ||
ba375acf LT |
13 | get_repo_base() { |
14 | (cd "$1" && (cd .git ; pwd)) 2> /dev/null | |
15 | } | |
16 | ||
e95ab1ed JH |
17 | use_local=no |
18 | while | |
19 | case "$#,$1" in | |
20 | 0,*) break ;; | |
21 | *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;; | |
22 | *,-*) usage ;; | |
23 | *) break ;; | |
24 | esac | |
25 | do | |
26 | shift | |
27 | done | |
28 | ||
ba375acf LT |
29 | # Turn the source into an absolute path if |
30 | # it is local | |
3f571e0b | 31 | repo="$1" |
ba375acf LT |
32 | local=no |
33 | if base=$(get_repo_base "$repo"); then | |
34 | repo="$base" | |
35 | local=yes | |
36 | fi | |
37 | ||
3f571e0b | 38 | dir="$2" |
e95ab1ed JH |
39 | mkdir "$dir" && |
40 | D=$( | |
41 | (cd "$dir" && git-init-db && pwd) | |
42 | ) && | |
43 | test -d "$D" || usage | |
44 | ||
45 | # We do local magic only when the user tells us to. | |
ba375acf LT |
46 | case "$local,$use_local" in |
47 | yes,yes) | |
e95ab1ed JH |
48 | ( cd "$repo/objects" ) || { |
49 | repo="$repo/.git" | |
50 | ( cd "$repo/objects" ) || { | |
51 | echo >&2 "-l flag seen but $repo is not local." | |
52 | exit 1 | |
53 | } | |
54 | } | |
55 | ||
56 | # See if we can hardlink and drop "l" if not. | |
57 | sample_file=$(cd "$repo" && \ | |
58 | find objects -type f -print | sed -e 1q) | |
59 | ||
60 | # objects directory should not be empty since we are cloning! | |
61 | test -f "$repo/$sample_file" || exit | |
62 | ||
63 | l= | |
64 | if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev/null | |
65 | then | |
66 | l=l | |
67 | fi && | |
68 | rm -f "$D/.git/objects/sample" && | |
69 | cp -r$l "$repo/objects" "$D/.git/" || exit 1 | |
70 | ||
71 | # Make a duplicate of refs and HEAD pointer | |
72 | HEAD= | |
73 | if test -f "$repo/HEAD" | |
74 | then | |
75 | HEAD=HEAD | |
76 | fi | |
77 | tar Ccf "$repo" - refs $HEAD | tar Cxf "$D/.git" - || exit 1 | |
78 | exit 0 | |
79 | ;; | |
80 | esac | |
81 | ||
82 | cd "$D" && git clone-pack "$repo" |