Skip to content

Commit 4f01748

Browse files
qurJunio C Hamano
authored andcommitted
contrib/workdir: add a simple script to create a working directory
Add a simple script to create a working directory that uses symlinks to point at an exisiting repository. This allows having different branches in different working directories but all from the same repository. Based on a description from Junio of how he creates multiple working directories[1]. With the following caveat: "This risks confusion for an uninitiated if you update a ref that is checked out in another working tree, but modulo that caveat it works reasonably well." [1] http://article.gmane.org/gmane.comp.version-control.git/41513/ Signed-off-by: Julian Phillips <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4557e0d commit 4f01748

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

contrib/workdir/git-new-workdir

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/sh
2+
3+
usage () {
4+
echo "usage:" $@
5+
exit 127
6+
}
7+
8+
die () {
9+
echo $@
10+
exit 128
11+
}
12+
13+
if test $# -lt 2 || test $# -gt 3
14+
then
15+
usage "$0 <repository> <new_workdir> [<branch>]"
16+
fi
17+
18+
orig_git=$1
19+
new_workdir=$2
20+
branch=$3
21+
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!"
24+
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."
30+
fi
31+
32+
# make sure the the links use full paths
33+
orig_git=$(cd "$orig_git"; pwd)
34+
35+
# create the workdir
36+
mkdir -p "$new_workdir/.git" || die "unable to create \"$new_workdir\"!"
37+
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
42+
do
43+
case $x in
44+
*/*)
45+
mkdir -p "$(dirname "$new_workdir/.git/$x")"
46+
;;
47+
esac
48+
ln -s "$orig_git/.git/$x" "$new_workdir/.git/$x"
49+
done
50+
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

0 commit comments

Comments
 (0)