Skip to content

Commit 20ccef4

Browse files
matledgitster
authored andcommitted
make git-clone GIT_WORK_TREE aware
If GIT_WORK_TREE is set git-clone will use that path for the working tree. Signed-off-by: Matthias Lederhofer <matled@gmx.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 68ad891 commit 20ccef4

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

git-clone.sh

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,29 @@ dir="$2"
187187
# Try using "humanish" part of source repo if user didn't specify one
188188
[ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
189189
[ -e "$dir" ] && die "destination directory '$dir' already exists."
190+
[ yes = "$bare" ] && unset GIT_WORK_TREE
191+
[ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
192+
die "working tree '$GIT_WORK_TREE' already exists."
190193
D=
194+
W=
191195
cleanup() {
192196
err=$?
193197
test -z "$D" && rm -rf "$dir"
198+
test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
194199
cd ..
195200
test -n "$D" && rm -rf "$D"
201+
test -n "$W" && rm -rf "$W"
196202
exit $err
197203
}
198204
trap cleanup 0
199205
mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
200-
case "$bare" in
201-
yes)
202-
GIT_DIR="$D" ;;
203-
*)
204-
GIT_DIR="$D/.git" ;;
205-
esac &&
206+
test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
207+
W=$(cd "$GIT_WORK_TREE" && pwd) && export GIT_WORK_TREE="$W"
208+
if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
209+
GIT_DIR="$D"
210+
else
211+
GIT_DIR="$D/.git"
212+
fi &&
206213
export GIT_DIR &&
207214
git-init $quiet ${template+"$template"} || usage
208215

@@ -358,7 +365,11 @@ then
358365
done < "$GIT_DIR/CLONE_HEAD"
359366
fi
360367

361-
cd "$D" || exit
368+
if test -n "$W"; then
369+
cd "$W" || exit
370+
else
371+
cd "$D" || exit
372+
fi
362373

363374
if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
364375
then

0 commit comments

Comments
 (0)