Work around failures due to bogus quoting of ^ and {}.
[git/platforms.git] / contrib / workdir / git-new-workdir
blob3ff6bd166ab7db5612a419ffcdc93b9b3d7eb2f8
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 git_dir=$(cd "$orig_git" 2>/dev/null &&
24 git rev-parse --git-dir 2>/dev/null) ||
25 die "\"$orig_git\" is not a git repository!"
27 if test "$git_dir" = ".git"
28 then
29 git_dir="$orig_git/.git"
32 # don't link to a workdir
33 if test -L "$git_dir/config"
34 then
35 die "\"$orig_git\" is a working directory only, please specify" \
36 "a complete repository."
39 # make sure the the links use full paths
40 git_dir=$(cd "$git_dir"; pwd)
42 # create the workdir
43 mkdir -p "$new_workdir/.git" || die "unable to create \"$new_workdir\"!"
45 # create the links to the original repo. explictly exclude index, HEAD and
46 # logs/HEAD from the list since they are purely related to the current working
47 # directory, and should not be shared.
48 for x in config refs logs/refs objects info hooks packed-refs remotes rr-cache
50 case $x in
51 */*)
52 mkdir -p "$(dirname "$new_workdir/.git/$x")"
54 esac
55 ln -s "$git_dir/$x" "$new_workdir/.git/$x"
56 done
58 # now setup the workdir
59 cd "$new_workdir"
60 # copy the HEAD from the original repository as a default branch
61 cp "$git_dir/HEAD" .git/HEAD
62 # checkout the branch (either the same as HEAD from the original repository, or
63 # the one that was asked for)
64 git checkout -f $branch