3 . /sys/lib/git/common.rc
5 flagfmt='d:debug, b:branch branch'; args='remote [local]'
6 eval `''{aux/getflags $*} || exec aux/usage
10 remote=`{echo $1 | sed 's@/*$@@'}
16 local=`{basename $remote .git}
18 branchflag=(-b $branch)
21 die 'repository already exists:' $local
26 mkdir -p $local/.git/fs
27 mkdir -p $local/.git/objects/pack/
28 mkdir -p $local/.git/refs/heads/
33 echo '[remote "origin"]'
36 {git/fetch $debug $branchflag $remote >[2=3] | awk '
39 if(ENVIRON["branch"] != "")
40 headref="refs/remotes/origin/"ENVIRON["branch"]
43 /^symref / && headref == "" {
45 gsub("^refs/heads", "refs/remotes/origin", $3)
46 gsub("^refs/tags", "refs/remotes/origin/tags", $3)
52 }else if(match($2, "^refs/(heads|tags)/")){
53 gsub("^refs/heads", "refs/remotes/origin", $2)
54 if($2 == headref || (headref == "" && $3 == headhash))
58 gsub("/?[^/]*/?$", "", outdir)
59 system("mkdir -p "outdir)
68 gsub("/?[^/]*/?$", "", refdir)
69 gsub("^refs/remotes/origin", "refs/heads", headref)
70 system("mkdir -p .git/"refdir);
71 system("cp .git/" remote " .git/" headref)
72 print "ref: " headref > ".git/HEAD"
73 }else if(headhash != ""){
74 print "warning: detached head "headhash > "/fd/2"
75 print headhash > ".git/HEAD"
78 '} |[3] tr '\x0d' '\x0a' || die 'could not clone repository'
80 tree=.git/fs/HEAD/tree
82 rbranch=`{echo $lbranch | subst 'heads' 'remotes/origin'}
83 echo checking out repository...
84 if(test -f .git/refs/$rbranch){
85 cp .git/refs/$rbranch .git/refs/$lbranch
87 @ {builtin cd $tree && tar cif /fd/1 .} | @ {tar xf /fd/0} \
88 || die 'checkout failed:' $status
89 for(f in `$nl{walk -f $tree | drop $tree}){
90 idx=.git/index9/tracked/$f
91 mkdir -p `$nl{basename -d $idx}
96 echo no default branch >[1=2]
97 echo check out your code with git/branch >[1=2]
102 echo cancelled clone $remote: cleaning $local >[1=2]
110 echo failed to clone $remote: cleaning $local >[1=2]