# [PATCH 1/2] git-clone: split up long &&-command-chain and use a function for cleanup

2 messages from 2007-07-05 to 2007-07-05. Participants: Matthias Lederhofer.
Thread: https://gitlist.dev/t/8885

## Matthias Lederhofer, 2007-07-05 22:54

Subject: [PATCH 1/2] git-clone: split up long &&-command-chain and use a function for cleanup
Message-ID: <20070705225433.GA26061@moooo.ath.cx>
URL: https://gitlist.dev/e/20070705225433.GA26061%40moooo.ath.cx

```
Signed-off-by: Matthias Lederhofer <matled@gmx.net>
---
This is in preparation for the next patch.

Actually the old cleanup code could leave the repository directory if
    D=$(cd "$dir" && pwd)
failed before this patch.
---
 git-clone.sh |   17 +++++++++++++----
 1 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/git-clone.sh b/git-clone.sh
index bd44ce1..59a457b 100755
--- a/git-clone.sh
+++ b/git-clone.sh
@@ -176,15 +176,24 @@ dir="$2"
 # Try using "humanish" part of source repo if user didn't specify one
 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
 [ -e "$dir" ] && die "destination directory '$dir' already exists."
-mkdir -p "$dir" &&
-D=$(cd "$dir" && pwd) &&
-trap 'err=$?; cd ..; rm -rf "$D"; exit $err' 0
+D=
+cleanup() {
+	err=$?
+	test -z "$D" && rm -rf "$dir"
+	cd ..
+	test -n "$D" && rm -rf "$D"
+	exit $err
+}
+trap cleanup 0
+mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
 case "$bare" in
 yes)
 	GIT_DIR="$D" ;;
 *)
 	GIT_DIR="$D/.git" ;;
-esac && export GIT_DIR && git-init ${template+"$template"} || usage
+esac
+export GIT_DIR
+git-init ${template+"$template"} || usage
 
 if test -n "$reference"
 then
-- 
1.5.2.2.647.ga00fe

```

## Matthias Lederhofer, 2007-07-05 23:10

Subject: [PATCH 2/2] make git-clone GIT_WORK_TREE aware
Message-ID: <20070705231044.GB26061@moooo.ath.cx>
URL: https://gitlist.dev/e/20070705231044.GB26061%40moooo.ath.cx
In-Reply-To: <20070705225433.GA26061@moooo.ath.cx>

```
If GIT_WORK_TREE is set git-clone will use that path for the
working tree.

Signed-off-by: Matthias Lederhofer <matled@gmx.net>
---
All those import scripts which call git init too probably have
problems when GIT_WORK_TREE is exported.  Perhaps a simple

    test -n "$GIT_WORK_TREE" ||
    die "GIT_WORK_TREE is not supported by this script yet"

is enough for the moment for those scripts.
---
 git-clone.sh |   25 ++++++++++++++++++-------
 1 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/git-clone.sh b/git-clone.sh
index 59a457b..b72a242 100755
--- a/git-clone.sh
+++ b/git-clone.sh
@@ -176,22 +176,29 @@ dir="$2"
 # Try using "humanish" part of source repo if user didn't specify one
 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
 [ -e "$dir" ] && die "destination directory '$dir' already exists."
+[ yes = "$bare" ] && unset GIT_WORK_TREE
+[ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
+die "working tree '$GIT_WORK_TREE' already exists."
 D=
+W=
 cleanup() {
 	err=$?
 	test -z "$D" && rm -rf "$dir"
+	test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
 	cd ..
 	test -n "$D" && rm -rf "$D"
+	test -n "$W" && rm -rf "$W"
 	exit $err
 }
 trap cleanup 0
 mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
-case "$bare" in
-yes)
-	GIT_DIR="$D" ;;
-*)
-	GIT_DIR="$D/.git" ;;
-esac
+test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
+W=$(cd "$GIT_WORK_TREE" && pwd) && export GIT_WORK_TREE="$W"
+if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
+	GIT_DIR="$D"
+else
+	GIT_DIR="$D/.git"
+fi
 export GIT_DIR
 git-init ${template+"$template"} || usage
 
@@ -347,7 +354,11 @@ then
 	done < "$GIT_DIR/CLONE_HEAD"
 fi
 
-cd "$D" || exit
+if test -n "$W"; then
+	cd "$W" || exit
+else
+	cd "$D" || exit
+fi
 
 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
 then
-- 
1.5.2.2.647.ga00fe

```
