Merge branch 'maint'
[git.git] / contrib / workdir / git-new-workdir
blob9877b98508497c9377876bd08e8f130660b9ba05
1 #!/bin/sh
3 usage () {
4 echo "usage:" $@
5 exit 127
8 die () {
9 echo $@
10 exit 128
13 if test $# -lt 2 || test $# -gt 3
14 then
15 usage "$0 <repository> <new_workdir> [<branch>]"
18 orig_git=$1
19 new_workdir=$2
20 branch=$3
22 # want to make sure that what is pointed to has a .git directory ...
23 test -d "$orig_git/.git" || die "\"$orig_git\" is not a git repository!"
25 # don't link to a workdir
26 if test -L "$orig_git/.git/config"
27 then
28 die "\"$orig_git\" is a working directory only, please specify" \
29 "a complete repository."
32 # make sure the the links use full paths
33 orig_git=$(cd "$orig_git"; pwd)
35 # create the workdir
36 mkdir -p "$new_workdir/.git" || die "unable to create \"$new_workdir\"!"
38 # create the links to the original repo. explictly exclude index, HEAD and
39 # logs/HEAD from the list since they are purely related to the current working
40 # directory, and should not be shared.
41 for x in config refs logs/refs objects info hooks packed-refs remotes rr-cache
43 case $x in
44 */*)
45 mkdir -p "$(dirname "$new_workdir/.git/$x")"
47 esac
48 ln -s "$orig_git/.git/$x" "$new_workdir/.git/$x"
49 done
51 # now setup the workdir
52 cd "$new_workdir"
53 # copy the HEAD from the original repository as a default branch
54 cp "$orig_git/.git/HEAD" .git/HEAD
55 # checkout the branch (either the same as HEAD from the original repository, or
56 # the one that was asked for)
57 git checkout -f $branch