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