Documentation/gitprotocol-v2.txt: fix a slight inconsistency in format
[git/gitster.git] / contrib / workdir / git-new-workdir
blob888c34a5215258ad7d5715771f92ab58f3efd363
1 #!/bin/sh
3 usage () {
4 echo "usage:" $@
5 exit 127
8 die () {
9 echo $@
10 exit 128
13 failed () {
14 die "unable to create new workdir '$new_workdir'!"
17 if test $# -lt 2 || test $# -gt 3
18 then
19 usage "$0 <repository> <new_workdir> [<branch>]"
22 orig_git=$1
23 new_workdir=$2
24 branch=$3
26 # want to make sure that what is pointed to has a .git directory ...
27 git_dir=$(cd "$orig_git" 2>/dev/null &&
28 git rev-parse --git-dir 2>/dev/null) ||
29 die "Not a git repository: \"$orig_git\""
31 case "$git_dir" in
32 .git)
33 git_dir="$orig_git/.git"
36 git_dir=$orig_git
38 esac
40 # don't link to a configured bare repository
41 isbare=$(git --git-dir="$git_dir" config --bool --get core.bare)
42 if test ztrue = "z$isbare"
43 then
44 die "\"$git_dir\" has core.bare set to true," \
45 " remove from \"$git_dir/config\" to use $0"
48 # don't link to a workdir
49 if test -h "$git_dir/config"
50 then
51 die "\"$orig_git\" is a working directory only, please specify" \
52 "a complete repository."
55 # make sure the links in the workdir have full paths to the original repo
56 git_dir=$(cd "$git_dir" && pwd) || exit 1
58 # don't recreate a workdir over an existing directory, unless it's empty
59 if test -d "$new_workdir"
60 then
61 if test $(ls -a1 "$new_workdir/." | wc -l) -ne 2
62 then
63 die "destination directory '$new_workdir' is not empty."
65 cleandir="$new_workdir/.git"
66 else
67 cleandir="$new_workdir"
70 mkdir -p "$new_workdir/.git" || failed
71 cleandir=$(cd "$cleandir" && pwd) || failed
73 cleanup () {
74 rm -rf "$cleandir"
76 siglist="0 1 2 15"
77 trap cleanup $siglist
79 # create the links to the original repo. explicitly exclude index, HEAD and
80 # logs/HEAD from the list since they are purely related to the current working
81 # directory, and should not be shared.
82 for x in config refs logs/refs objects info hooks packed-refs remotes rr-cache svn
84 # create a containing directory if needed
85 case $x in
86 */*)
87 mkdir -p "$new_workdir/.git/${x%/*}"
89 esac
91 ln -s "$git_dir/$x" "$new_workdir/.git/$x" || failed
92 done
94 # commands below this are run in the context of the new workdir
95 cd "$new_workdir" || failed
97 # copy the HEAD from the original repository as a default branch
98 cp "$git_dir/HEAD" .git/HEAD || failed
100 # the workdir is set up. if the checkout fails, the user can fix it.
101 trap - $siglist
103 # checkout the branch (either the same as HEAD from the original repository,
104 # or the one that was asked for)
105 git checkout -f $branch