# git-clean fails to remove a file whose name contains \\, ", or \n, TAB, etc.

8 messages from 2007-05-08 to 2007-05-08. Participants: Jim Meyering, Randal L. Schwartz, Junio C Hamano, Jan Hudec, Karl Hasselström.
Thread: https://gitlist.dev/t/8043

## Jim Meyering, 2007-05-08 20:32

Subject: git-clean fails to remove a file whose name contains \\, ", or \n, TAB, etc.
Message-ID: <87ps5bhx8t.fsf@rho.meyering.net>
URL: https://gitlist.dev/e/87ps5bhx8t.fsf%40rho.meyering.net

```
Not that it matters (or maybe this is a feature :-), because people
who create such files in their working directory deserve what they
get, Eh? :-)

But if leaving it, then perhaps git-clean should at least warn
that it's not doing its job (i.e. remove the uses of rm's "-f").

To reproduce, run these commands:

nl='
'
git-init > /dev/null && touch "x\\n\"$nl" && git-clean && ls -b

Here's the output I get:

    Removing "x\\n\"\n"
    .git/  x\\n"\n

git-clean.sh needs to strip off leading and trailing double quotes (easy)
as well as convert escapees back to originals (not easy as you might
think, in sh) before running rm.  Good excuse to rewrite it in perl.

```

## Randal L. Schwartz, 2007-05-08 20:51

Subject: Re: git-clean fails to remove a file whose name contains \\, ", or \n, TAB, etc.
Message-ID: <86k5vj9gzu.fsf@blue.stonehenge.com>
URL: https://gitlist.dev/e/86k5vj9gzu.fsf%40blue.stonehenge.com
In-Reply-To: <87ps5bhx8t.fsf@rho.meyering.net>

```
>>>>> "Jim" == Jim Meyering <jim@meyering.net> writes:

Jim> Not that it matters (or maybe this is a feature :-), because people
Jim> who create such files in their working directory deserve what they
Jim> get, Eh? :-)

The problem is the newline in the string, since

  git-ls-files --others --directory $excl ${excl_info:+"$excl_info"} -- "$@" |
  while read -r file; do

is using newline as a delimiter.  Any file with a newline would mess this up.

Not being a shell programming expert, is there a way we could use -z and xargs
-0 here instead?

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

```

## Junio C Hamano, 2007-05-08 20:53

Subject: Re: git-clean fails to remove a file whose name contains \\, ", or \n, TAB, etc.
Message-ID: <7vbqgv2g1h.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vbqgv2g1h.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <86k5vj9gzu.fsf@blue.stonehenge.com>

```
merlyn@stonehenge.com (Randal L. Schwartz) writes:

>>>>>> "Jim" == Jim Meyering <jim@meyering.net> writes:
>
> Jim> Not that it matters (or maybe this is a feature :-), because people
> Jim> who create such files in their working directory deserve what they
> Jim> get, Eh? :-)
>
> The problem is the newline in the string, since
>
>   git-ls-files --others --directory $excl ${excl_info:+"$excl_info"} -- "$@" |
>   while read -r file; do
>
> is using newline as a delimiter.  Any file with a newline would mess this up.
>
> Not being a shell programming expert, is there a way we could use -z and xargs
> -0 here instead?

Funny that we had this discussed on this list a few days ago
;-).

```

## Jan Hudec, 2007-05-08 23:11

Subject: Re: git-clean fails to remove a file whose name contains \\, ", or \n, TAB, etc.
Message-ID: <20070508231115.GA14900@efreet.light.src>
URL: https://gitlist.dev/e/20070508231115.GA14900%40efreet.light.src
In-Reply-To: <86k5vj9gzu.fsf@blue.stonehenge.com>

```
On Tue, May 08, 2007 at 13:51:01 -0700, Randal L. Schwartz wrote:
> >>>>> "Jim" == Jim Meyering <jim@meyering.net> writes:
> 
> Jim> Not that it matters (or maybe this is a feature :-), because people
> Jim> who create such files in their working directory deserve what they
> Jim> get, Eh? :-)
> 
> The problem is the newline in the string, since
> 
>   git-ls-files --others --directory $excl ${excl_info:+"$excl_info"} -- "$@" |
>   while read -r file; do
> 
> is using newline as a delimiter.  Any file with a newline would mess this up.
> 
> Not being a shell programming expert, is there a way we could use -z and xargs
> -0 here instead?

Unfortunately read does not have zero-delimited mode (at all). Unfortunately
the backquote expansion does not preserve whitespace correctly, so it's not
possible to use something like head.

Unfortunately there does not seem to be a way to feed newline to read
(without -r flag), because the rules say that '\<NL>' => '' and
'\<something>' => '<something>'.

Than I can't think of anything other than xargs -0. Unfortunately that is an
external command, which only handles simple commands. So the question becomes
how to give it a simple command. Well, there would be two ways:

 - A simple command might be:
	sh -c 'arbitrarily complex command' dummy arguments...
   (the "dummy" will become $0),
   so you can simply put the whole loop in single quotes, use for file; do
   instead of while read -r file; do and be done.

 - Reinvoke the program with special argument meaning it should run the inner
   loop. Ie. add an option --inner-loop option, that would run the
   inner loop and terminate and use xargs -0 "$0" --inner-loop

Both solutions require exporting all the necessary variables.

-- 
						 Jan 'Bulb' Hudec <bulb@ucw.cz>

```

## Randal L. Schwartz, 2007-05-08 23:18

Subject: Re: git-clean fails to remove a file whose name contains \\, ", or \n, TAB, etc.
Message-ID: <86wszi9a5r.fsf@blue.stonehenge.com>
URL: https://gitlist.dev/e/86wszi9a5r.fsf%40blue.stonehenge.com
In-Reply-To: <20070508231115.GA14900@efreet.light.src>

```
>>>>> "Jan" == Jan Hudec <bulb@ucw.cz> writes:

Jan> Than I can't think of anything other than xargs -0.

git-ls-files -z .... |
perl -0lne '
   $_ is each name here
'

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

```

## Karl Hasselström, 2007-05-08 23:27

Subject: Re: git-clean fails to remove a file whose name contains \\, ", or \n, TAB, etc.
Message-ID: <20070508232702.GB5866@diana.vm.bytemark.co.uk>
URL: https://gitlist.dev/e/20070508232702.GB5866%40diana.vm.bytemark.co.uk
In-Reply-To: <86wszi9a5r.fsf@blue.stonehenge.com>

```
On 2007-05-08 16:18:40 -0700, Randal L. Schwartz wrote:

> >>>>> "Jan" == Jan Hudec <bulb@ucw.cz> writes:
>
> Jan> Than I can't think of anything other than xargs -0.
>
> git-ls-files -z .... |
> perl -0lne '
>    $_ is each name here
> '

Have you seen this?

  http://xkcd.com/c224.html

-- 
Karl Hasselström, kha@treskal.com
      www.treskal.com/kalle

```

## Randal L. Schwartz, 2007-05-08 23:29

Subject: Re: git-clean fails to remove a file whose name contains \\, ", or \n, TAB, etc.
Message-ID: <86sla699oc.fsf@blue.stonehenge.com>
URL: https://gitlist.dev/e/86sla699oc.fsf%40blue.stonehenge.com
In-Reply-To: <20070508232702.GB5866@diana.vm.bytemark.co.uk>

```
>>>>> "Karl" == Karl Hasselström <kha@treskal.com> writes:

Karl> Have you seen this?

Karl>   http://xkcd.com/c224.html

Yes.  You would be, oh, the 92nd person to point that out to me. :)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

```

## Karl Hasselström, 2007-05-08 23:38

Subject: Re: git-clean fails to remove a file whose name contains \\, ", or \n, TAB, etc.
Message-ID: <20070508233819.GC5866@diana.vm.bytemark.co.uk>
URL: https://gitlist.dev/e/20070508233819.GC5866%40diana.vm.bytemark.co.uk
In-Reply-To: <86sla699oc.fsf@blue.stonehenge.com>

```
On 2007-05-08 16:29:07 -0700, Randal L. Schwartz wrote:

> >>>>> "Karl" == Karl Hasselström <kha@treskal.com> writes:
>
> Karl> Have you seen this?
> Karl>   http://xkcd.com/c224.html
>
> Yes. You would be, oh, the 92nd person to point that out to me. :)

Ah well. At least I sent it to the list too, so everyone can have
their choice of snickering at the comic, or me, whichever seems
silliest. :-)

-- 
Karl Hasselström, kha@treskal.com
      www.treskal.com/kalle

```
