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