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