threads / discuss / 8043

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

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

## tl;dr

8 messages between May 8, 2007 and May 8, 2007.

replies: 7people: 5as markdown or json

Jim Meyering· May 8, 2007, 20:32 UTC · lore

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· May 8, 2007, 20:51 UTC · re: Jim Meyering · lore

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

>>>>> "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· May 8, 2007, 20:53 UTC · re: Randal L. Schwartz · lore

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

merlyn@stonehenge.com (Randal L. Schwartz) writes:
Show 15 quoted lines
>>>>>> "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· May 8, 2007, 23:11 UTC · re: Randal L. Schwartz · lore

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

On Tue, May 08, 2007 at 13:51:01 -0700, Randal L. Schwartz wrote:
Show 15 quoted lines
> >>>>> "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· May 8, 2007, 23:18 UTC · re: Jan Hudec · lore

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

>>>>> "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· May 8, 2007, 23:27 UTC · re: Randal L. Schwartz · lore

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

On 2007-05-08 16:18:40 -0700, Randal L. Schwartz wrote:
Show 8 quoted lines
> >>>>> "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· May 8, 2007, 23:29 UTC · re: Karl Hasselström · lore

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

>>>>> "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· May 8, 2007, 23:38 UTC · re: Randal L. Schwartz · lore

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

On 2007-05-08 16:29:07 -0700, Randal L. Schwartz wrote:
Show 6 quoted lines
> >>>>> "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

← back to recent threads