2007-03-27 01:15:32 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
usage () {
|
|
|
|
echo "usage:" $@
|
|
|
|
exit 127
|
|
|
|
}
|
|
|
|
|
|
|
|
die () {
|
|
|
|
echo $@
|
|
|
|
exit 128
|
|
|
|
}
|
|
|
|
|
2014-11-26 21:38:28 +01:00
|
|
|
failed () {
|
|
|
|
die "unable to create new workdir '$new_workdir'!"
|
|
|
|
}
|
|
|
|
|
2007-03-27 01:15:32 +02:00
|
|
|
if test $# -lt 2 || test $# -gt 3
|
|
|
|
then
|
|
|
|
usage "$0 <repository> <new_workdir> [<branch>]"
|
|
|
|
fi
|
|
|
|
|
|
|
|
orig_git=$1
|
|
|
|
new_workdir=$2
|
|
|
|
branch=$3
|
|
|
|
|
|
|
|
# want to make sure that what is pointed to has a .git directory ...
|
2007-05-27 05:09:27 +02:00
|
|
|
git_dir=$(cd "$orig_git" 2>/dev/null &&
|
|
|
|
git rev-parse --git-dir 2>/dev/null) ||
|
2008-12-22 00:17:32 +01:00
|
|
|
die "Not a git repository: \"$orig_git\""
|
2007-03-27 01:15:32 +02:00
|
|
|
|
2007-08-22 03:50:12 +02:00
|
|
|
case "$git_dir" in
|
|
|
|
.git)
|
2007-06-19 13:44:43 +02:00
|
|
|
git_dir="$orig_git/.git"
|
2007-08-22 03:50:12 +02:00
|
|
|
;;
|
|
|
|
.)
|
|
|
|
git_dir=$orig_git
|
|
|
|
;;
|
|
|
|
esac
|
2007-06-19 13:44:43 +02:00
|
|
|
|
2007-08-22 07:33:49 +02:00
|
|
|
# don't link to a configured bare repository
|
|
|
|
isbare=$(git --git-dir="$git_dir" config --bool --get core.bare)
|
2014-11-26 21:38:28 +01:00
|
|
|
if test ztrue = "z$isbare"
|
2007-08-22 07:33:49 +02:00
|
|
|
then
|
|
|
|
die "\"$git_dir\" has core.bare set to true," \
|
|
|
|
" remove from \"$git_dir/config\" to use $0"
|
|
|
|
fi
|
|
|
|
|
2007-03-27 01:15:32 +02:00
|
|
|
# don't link to a workdir
|
2010-09-22 02:35:59 +02:00
|
|
|
if test -h "$git_dir/config"
|
2007-03-27 01:15:32 +02:00
|
|
|
then
|
|
|
|
die "\"$orig_git\" is a working directory only, please specify" \
|
|
|
|
"a complete repository."
|
|
|
|
fi
|
|
|
|
|
2014-11-26 21:38:28 +01:00
|
|
|
# make sure the links in the workdir have full paths to the original repo
|
|
|
|
git_dir=$(cd "$git_dir" && pwd) || exit 1
|
|
|
|
|
|
|
|
# don't recreate a workdir over an existing directory, unless it's empty
|
|
|
|
if test -d "$new_workdir"
|
2007-09-06 05:33:41 +02:00
|
|
|
then
|
2014-11-26 21:38:28 +01:00
|
|
|
if test $(ls -a1 "$new_workdir/." | wc -l) -ne 2
|
|
|
|
then
|
|
|
|
die "destination directory '$new_workdir' is not empty."
|
|
|
|
fi
|
|
|
|
cleandir="$new_workdir/.git"
|
|
|
|
else
|
|
|
|
cleandir="$new_workdir"
|
2007-09-06 05:33:41 +02:00
|
|
|
fi
|
|
|
|
|
2014-11-26 21:38:28 +01:00
|
|
|
mkdir -p "$new_workdir/.git" || failed
|
|
|
|
cleandir=$(cd "$cleandir" && pwd) || failed
|
2007-03-27 01:15:32 +02:00
|
|
|
|
2014-11-26 21:38:28 +01:00
|
|
|
cleanup () {
|
|
|
|
rm -rf "$cleandir"
|
|
|
|
}
|
|
|
|
siglist="0 1 2 15"
|
|
|
|
trap cleanup $siglist
|
2007-03-27 01:15:32 +02:00
|
|
|
|
2010-08-22 13:12:12 +02:00
|
|
|
# create the links to the original repo. explicitly exclude index, HEAD and
|
2007-03-27 01:15:32 +02:00
|
|
|
# logs/HEAD from the list since they are purely related to the current working
|
|
|
|
# directory, and should not be shared.
|
2008-03-15 03:44:13 +01:00
|
|
|
for x in config refs logs/refs objects info hooks packed-refs remotes rr-cache svn
|
2007-03-27 01:15:32 +02:00
|
|
|
do
|
2014-11-26 21:38:28 +01:00
|
|
|
# create a containing directory if needed
|
2007-03-27 01:15:32 +02:00
|
|
|
case $x in
|
|
|
|
*/*)
|
2014-11-26 21:38:28 +01:00
|
|
|
mkdir -p "$new_workdir/.git/${x%/*}"
|
2007-03-27 01:15:32 +02:00
|
|
|
;;
|
|
|
|
esac
|
2014-11-26 21:38:28 +01:00
|
|
|
|
|
|
|
ln -s "$git_dir/$x" "$new_workdir/.git/$x" || failed
|
2007-03-27 01:15:32 +02:00
|
|
|
done
|
|
|
|
|
2014-11-26 21:38:28 +01:00
|
|
|
# commands below this are run in the context of the new workdir
|
|
|
|
cd "$new_workdir" || failed
|
|
|
|
|
2007-03-27 01:15:32 +02:00
|
|
|
# copy the HEAD from the original repository as a default branch
|
2014-11-26 21:38:28 +01:00
|
|
|
cp "$git_dir/HEAD" .git/HEAD || failed
|
|
|
|
|
|
|
|
# the workdir is set up. if the checkout fails, the user can fix it.
|
|
|
|
trap - $siglist
|
|
|
|
|
|
|
|
# checkout the branch (either the same as HEAD from the original repository,
|
|
|
|
# or the one that was asked for)
|
2007-03-27 01:15:32 +02:00
|
|
|
git checkout -f $branch
|