contrib: make git-new-workdir work with windows symlinks.
[git/mingw/4msysgit.git] / contrib / workdir / git-new-workdir
blobae4c75f7045f543194c817c9184e7fe78155af01
1 #!/bin/sh
3 # Fix some commands on Windows
4 case $(uname -s) in
5 *MINGW*)
6 winpath () {
7 # pwd -W is the only way of getting msys to convert the path, especially mounted paths like /usr
8 # since cmd /c only takes a single parameter preventing msys automatic path conversion.
9 if test "${1:~-1:1}" != "/" ; then
10 echo "$1" | sed 's+/+\\+g'
11 elif test -d "$1" ; then
12 (cd "$1"; pwd -W) | sed 's+/+\\+g'
13 elif test -d "${1%/*}" ; then
14 (cd "${1%/*}"; echo "$(pwd -W)/${1##*/}") | sed 's+/+\\+g'
15 else
16 echo "$1" | sed -e 's+^/\([a-z]\)/+\1:/+' -e 's+/+\\+g'
19 # git sees Windows-style pwd
20 pwd () {
21 builtin pwd -W
23 # use mklink
24 ln () {
26 ln_sym_hard=/H
27 ln_sym_dir=
28 if test "$1" = "-s"
29 then
30 ln_sym_hard=
31 shift
33 pushd $(dirname "$2") 2>&1 > /dev/null
34 builtin test -d "$1" && ln_sym_dir=/D
35 popd > /dev/null 2> /dev/null
36 cmd /c "mklink ${ln_sym_hard}${ln_sym_dir} \"$(winpath "$2")\" \"$(winpath "$1")\">/dev/null " 2>/dev/null
39 test () {
40 case "$1" in
41 -h)
42 test_file=$(cmd /c "@dir /b/a:l \"$(winpath "${2}")\" 2> nul" )
43 builtin test -n "${test_file}"
45 *) builtin test "$@";;
46 esac
48 esac
50 usage () {
51 echo "usage:" $@
52 exit 127
55 die () {
56 echo $@
57 exit 128
60 if test $# -lt 2 || test $# -gt 3
61 then
62 usage "$0 <repository> <new_workdir> [<branch>]"
65 orig_git=$1
66 new_workdir=$2
67 branch=$3
69 # want to make sure that what is pointed to has a .git directory ...
70 git_dir=$(cd "$orig_git" 2>/dev/null &&
71 git rev-parse --git-dir 2>/dev/null) ||
72 die "Not a git repository: \"$orig_git\""
74 case "$git_dir" in
75 .git)
76 git_dir="$orig_git/.git"
79 git_dir=$orig_git
81 esac
83 # don't link to a configured bare repository
84 isbare=$(git --git-dir="$git_dir" config --bool --get core.bare)
85 if test ztrue = z$isbare
86 then
87 die "\"$git_dir\" has core.bare set to true," \
88 " remove from \"$git_dir/config\" to use $0"
91 # don't link to a workdir
92 if test -h "$git_dir/config"
93 then
94 die "\"$orig_git\" is a working directory only, please specify" \
95 "a complete repository."
98 # don't recreate a workdir over an existing repository
99 if test -e "$new_workdir"
100 then
101 die "destination directory '$new_workdir' already exists."
104 # make sure the links use full paths
105 git_dir=$(cd "$git_dir"; pwd)
107 # create the workdir
108 mkdir -p "$new_workdir/.git" || die "unable to create \"$new_workdir\"!"
110 # create the links to the original repo. explicitly exclude index, HEAD and
111 # logs/HEAD from the list since they are purely related to the current working
112 # directory, and should not be shared.
113 for x in config refs logs/refs objects info hooks packed-refs remotes rr-cache svn
115 case $x in
116 */*)
117 mkdir -p "$(dirname "$new_workdir/.git/$x")"
119 esac
120 test -e "$git_dir/$x" || mkdir "$git_dir/$x"
121 ln -s "$git_dir/$x" "$new_workdir/.git/$x"
122 done
124 # now setup the workdir
125 cd "$new_workdir"
126 # copy the HEAD from the original repository as a default branch
127 cp "$git_dir/HEAD" .git/HEAD
128 # checkout the branch (either the same as HEAD from the original repository, or
129 # the one that was asked for)
130 git checkout -f $branch