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