# [PATCH] git-new-workdir: Don't fail if the target directory is empty

4 messages from 2014-11-20 to 2014-11-21. Participants: Paul Smith, Junio C Hamano.
Thread: https://gitlist.dev/t/38017

## Paul Smith, 2014-11-20 15:46

Subject: [PATCH] git-new-workdir: Don't fail if the target directory is empty
Message-ID: <1416498395.23953.8.camel@mad-scientist.net>
URL: https://gitlist.dev/e/1416498395.23953.8.camel%40mad-scientist.net

```
Allow new workdirs to be created in an empty directory (similar to "git
clone").  Provide more error checking and clean up on failure.

Signed-off-by: Paul Smith <paul@mad-scientist.net>
---
 contrib/workdir/git-new-workdir | 54 +++++++++++++++++++++++++++--------------
 1 file changed, 36 insertions(+), 18 deletions(-)

diff --git a/contrib/workdir/git-new-workdir b/contrib/workdir/git-new-workdir
index 75e8b25..7334720 100755
--- a/contrib/workdir/git-new-workdir
+++ b/contrib/workdir/git-new-workdir
@@ -10,6 +10,10 @@ die () {
 	exit 128
 }
 
+failed () {
+	die "unable to create new workdir \"$new_workdir\"!"
+}
+
 if test $# -lt 2 || test $# -gt 3
 then
 	usage "$0 <repository> <new_workdir> [<branch>]"
@@ -35,7 +39,7 @@ esac
 
 # don't link to a configured bare repository
 isbare=$(git --git-dir="$git_dir" config --bool --get core.bare)
-if test ztrue = z$isbare
+if test ztrue = "z$isbare"
 then
 	die "\"$git_dir\" has core.bare set to true," \
 		" remove from \"$git_dir/config\" to use $0"
@@ -48,35 +52,49 @@ then
 		"a complete repository."
 fi
 
-# don't recreate a workdir over an existing repository
-if test -e "$new_workdir"
+# 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"
 then
-	die "destination directory '$new_workdir' already exists."
+	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"
 fi
 
-# make sure the links use full paths
-git_dir=$(cd "$git_dir"; pwd)
+mkdir -p "$new_workdir"/.git || failed
+cleandir=$(cd "$cleandir" && pwd) || failed
 
-# create the workdir
-mkdir -p "$new_workdir/.git" || die "unable to create \"$new_workdir\"!"
+cleanup () {
+	rm -rf "$cleandir"
+}
+siglist="0 1 2 15"
+trap cleanup $siglist
 
 # create the links to the original repo.  explicitly exclude index, HEAD and
 # logs/HEAD from the list since they are purely related to the current working
 # directory, and should not be shared.
 for x in config refs logs/refs objects info hooks packed-refs remotes rr-cache svn
 do
+	# Create a containing directory if needed
 	case $x in
-	*/*)
-		mkdir -p "$(dirname "$new_workdir/.git/$x")"
-		;;
+		*/*) mkdir -p "$new_workdir/.git/${x%/*}" ;;
 	esac
-	ln -s "$git_dir/$x" "$new_workdir/.git/$x"
+
+	ln -s "$git_dir/$x" "$new_workdir/.git/$x" || failed
 done
 
-# now setup the workdir
-cd "$new_workdir"
 # copy the HEAD from the original repository as a default branch
-cp "$git_dir/HEAD" .git/HEAD
-# checkout the branch (either the same as HEAD from the original repository, or
-# the one that was asked for)
-git checkout -f $branch
+cp "$git_dir"/HEAD "$new_workdir"/.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)
+git -C "$new_workdir" checkout -f $branch
-- 
1.8.5.3

```

## Junio C Hamano, 2014-11-20 17:13

Subject: Re: [PATCH] git-new-workdir: Don't fail if the target directory is empty
Message-ID: <xmqqioi94wy0.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqioi94wy0.fsf%40gitster.dls.corp.google.com
In-Reply-To: <1416498395.23953.8.camel@mad-scientist.net>

```
Paul Smith <paul@mad-scientist.net> writes:

> Allow new workdirs to be created in an empty directory (similar to "git
> clone").  Provide more error checking and clean up on failure.
>
> Signed-off-by: Paul Smith <paul@mad-scientist.net>
> ---
>  contrib/workdir/git-new-workdir | 54 +++++++++++++++++++++++++++--------------
>  1 file changed, 36 insertions(+), 18 deletions(-)

Thanks.  I would have liked [PATCH v$n] on the subject line but
that's minor.

I also see you made a few small improvements here and there, like...

>  # don't link to a configured bare repository
>  isbare=$(git --git-dir="$git_dir" config --bool --get core.bare)
> -if test ztrue = z$isbare
> +if test ztrue = "z$isbare"

... this bit, which I think is a good idea.  We are asking "--bool"
so we know we will never get anything other than true/false to cause
trouble by not quoting, but the dq-pair there does not cost anything
and it is a good discipline.

> +# make sure the links in the workdir have full paths to the original repo
> +git_dir=$(cd "$git_dir" && pwd) || exit 1

... so is this bit, which used to just go even if cd failed.

> +# don't recreate a workdir over an existing directory, unless it's empty
> +if test -d "$new_workdir"
>  then
> -	die "destination directory '$new_workdir' already exists."
> +	if test $(ls -a1 "$new_workdir"/. | wc -l) -ne 2

You used to quote this as

	if test $(ls -a1 "$new_workdir/." | wc -l) -ne 2

and I think that made a lot more sense than the new quoting.

The ultimate reason we quote here is because "$new_workdir/." is the
single directory path we want to give "ls", and quoting the whole
thing shows that intention far clearer than the new one, which says
"there may be an $IFS character in $new_workdir so I am quoting to
keep them together as a single string and then appending /. to that
single string will still be a single string", which is not wrong
per-se in that it is saying the same thing, but it is a roundabout
way to do so.

> +	then
> +		die "destination directory '$new_workdir' is not empty."
> +	fi
> +	cleandir="$new_workdir"/.git
> +else
> +	cleandir="$new_workdir"
>  fi
>  
> -# make sure the links use full paths
> -git_dir=$(cd "$git_dir"; pwd)
> +mkdir -p "$new_workdir"/.git || failed
> +cleandir=$(cd "$cleandir" && pwd) || failed
>  
> -# create the workdir
> -mkdir -p "$new_workdir/.git" || die "unable to create \"$new_workdir\"!"
> +cleanup () {
> +	rm -rf "$cleandir"
> +}
> +siglist="0 1 2 15"
> +trap cleanup $siglist
>  
>  # create the links to the original repo.  explicitly exclude index, HEAD and
>  # logs/HEAD from the list since they are purely related to the current working
>  # directory, and should not be shared.
>  for x in config refs logs/refs objects info hooks packed-refs remotes rr-cache svn
>  do
> +	# Create a containing directory if needed
>  	case $x in
> -	*/*)
> -		mkdir -p "$(dirname "$new_workdir/.git/$x")"
> -		;;
> +		*/*) mkdir -p "$new_workdir/.git/${x%/*}" ;;

Case arms come at the same indentation level as "case/esac".

>  	esac
> -	ln -s "$git_dir/$x" "$new_workdir/.git/$x"
> +
> +	ln -s "$git_dir/$x" "$new_workdir/.git/$x" || failed
>  done
>  
> -# now setup the workdir
> -cd "$new_workdir"
>  # copy the HEAD from the original repository as a default branch
> -cp "$git_dir/HEAD" .git/HEAD
> -# checkout the branch (either the same as HEAD from the original repository, or
> -# the one that was asked for)
> -git checkout -f $branch
> +cp "$git_dir"/HEAD "$new_workdir"/.git/HEAD || failed
> +

Why these changes?  I thought you are making sure $cleandir is
absolute so that you do not have to do this and can just "cd" into
the new working tree, the same way the user would do once it is set
up.

Puzzled...  Before seeing this bit, I was about to say "but all the
above nits are minor so no need to resend---I'll locally fix them up
while queueing", but I suspect that I may be missing some obvious
reason why you avoid "cd" and I am reluctant to "fix" this part up
myself without seeing a response, so I'd swallow that "no need to
resend" X-<.

> +# 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)
> +git -C "$new_workdir" checkout -f $branch

```

## Paul Smith, 2014-11-21 15:08

Subject: Re: [PATCH] git-new-workdir: Don't fail if the target directory is empty
Message-ID: <1416582517.23953.18.camel@mad-scientist.net>
URL: https://gitlist.dev/e/1416582517.23953.18.camel%40mad-scientist.net
In-Reply-To: <xmqqioi94wy0.fsf@gitster.dls.corp.google.com>

```
On Thu, 2014-11-20 at 09:13 -0800, Junio C Hamano wrote:
> Paul Smith <paul@mad-scientist.net> writes:
> > +# don't recreate a workdir over an existing directory, unless it's empty
> > +if test -d "$new_workdir"
> >  then
> > -	die "destination directory '$new_workdir' already exists."
> > +	if test $(ls -a1 "$new_workdir"/. | wc -l) -ne 2
> 
> You used to quote this as
> 
> 	if test $(ls -a1 "$new_workdir/." | wc -l) -ne 2
> 
> and I think that made a lot more sense than the new quoting.

Just personal script coding style.  I tend to quote just variables, in
case I want to add globbing or whatever later.  I've run into people who
think that "$foo/bar" needs quotes because it concatenates two strings,
but you don't need to quote $foo by itself; quoting just the variable
helps avoid that impression.

But this doesn't matter much to me.

> Case arms come at the same indentation level as "case/esac".

OK.

> Why these changes?  I thought you are making sure $cleandir is
> absolute so that you do not have to do this and can just "cd" into
> the new working tree, the same way the user would do once it is set
> up.

I made cleandir absolute mainly because you asked me to :-).  I didn't
realize that the implication behind that request was that I'd put back
the original "cd" as well.

I personally distrust and avoid raw cd in shell scripts.  It's
action-at-a-distance and can cause all sorts of problems, sometimes
disastrous ones.  If I need to change directories I localize it, e.g.:

  (cd "$somedir" && do some command)

so it's clear that it's in effect only for that command.

I understand that in this particular situation the "cd" is right before
the end so this doesn't hurt (as the script stands today).  If you
prefer the "cd" explicitly I'll add it back.  You're the one that needs
to be happy with it :-).

What if I move the cd down to right before the checkout command, and use
$new_workdir in the copy of HEAD?  At least it won't be in effect until
after the workdir is completely set up.  I would feel better about it
then :-).

> > -# now setup the workdir
> > -cd "$new_workdir"
> >  # copy the HEAD from the original repository as a default branch
> > -cp "$git_dir/HEAD" .git/HEAD
> > -# checkout the branch (either the same as HEAD from the original repository, or
> > -# the one that was asked for)
> > -git checkout -f $branch

```

## Junio C Hamano, 2014-11-21 17:33

Subject: Re: [PATCH] git-new-workdir: Don't fail if the target directory is empty
Message-ID: <xmqqegsw1msr.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqegsw1msr.fsf%40gitster.dls.corp.google.com
In-Reply-To: <1416582517.23953.18.camel@mad-scientist.net>

```
Paul Smith <paul@mad-scientist.net> writes:

>> Why these changes?  I thought you are making sure $cleandir is
>> absolute so that you do not have to do this and can just "cd" into
>> the new working tree, the same way the user would do once it is set
>> up.
>
> I made cleandir absolute mainly because you asked me to :-).  I didn't
> realize that the implication behind that request was that I'd put back
> the original "cd" as well.

This is about not changing unnecessarily the original that has been
battle-tested and keeping the result resistant to future mistakes
(i.e. somebody going elsewhere without realizing a relative cleandir
will cause problems).  Once $cleandir is fixed, there no longer is a
good reason to change how HEAD is pointed in the $new_workdir or its
working tree populated; the change in the earlier round in that area
was necessary when $cleandir was not absolute, but not anymore.

```
