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