1#!/bin/sh 2 3usage () { 4echo"usage:" $@ 5exit127 6} 7 8die () { 9echo $@ 10exit128 11} 12 13iftest$#-lt2||test$#-gt3 14then 15 usage "$0<repository> <new_workdir> [<branch>]" 16fi 17 18orig_git=$1 19new_workdir=$2 20branch=$3 21 22# want to make sure that what is pointed to has a .git directory ... 23git_dir=$(cd"$orig_git"2>/dev/null && 24 git rev-parse --git-dir2>/dev/null) || 25 die "\"$orig_git\"is not a git repository!" 26 27case"$git_dir"in 28.git) 29 git_dir="$orig_git/.git" 30;; 31.) 32 git_dir=$orig_git 33;; 34esac 35 36# don't link to a workdir 37iftest -L"$git_dir/config" 38then 39 die "\"$orig_git\"is a working directory only, please specify" \ 40"a complete repository." 41fi 42 43# make sure the the links use full paths 44git_dir=$(cd "$git_dir"; pwd) 45 46# create the workdir 47mkdir-p"$new_workdir/.git"|| die "unable to create\"$new_workdir\"!" 48 49# create the links to the original repo. explictly exclude index, HEAD and 50# logs/HEAD from the list since they are purely related to the current working 51# directory, and should not be shared. 52for x in config refs logs/refs objects info hooks packed-refs remotes rr-cache 53do 54case$xin 55*/*) 56mkdir-p"$(dirname "$new_workdir/.git/$x")" 57;; 58esac 59ln-s"$git_dir/$x""$new_workdir/.git/$x" 60done 61 62# now setup the workdir 63cd"$new_workdir" 64# copy the HEAD from the original repository as a default branch 65cp"$git_dir/HEAD" .git/HEAD 66# checkout the branch (either the same as HEAD from the original repository, or 67# the one that was asked for) 68git checkout -f$branch