Finding all empty directories in a subversion checkout

M

Markus Fischer

Hi,

I tried to write a script to find all empty directories in a subversion
checkout. In my view there were two main problems to solve:

1) empty directories also contain .svn control directories

2) directories containing only further empty directories should be
detected as such too.

I came up with https://gist.github.com/852051 and on my simple tests so
far it worked well. I'd like to get feedback on the code and how I used
certain idioms, or rather, miss-used certain idioms or areas where I can
improve it "the ruby way".

For example, I'm not really fond of the big case statement in the fourth
loop block:

dirs_count[dir] =
Dir.entries(dir).collect { |e|
case e
when '.svn'
nil
when '.'
nil
when '..'
nil
else
e
end
}.compact.count

Seems pretty redundant to me, in other languages I'd have written e.g.

switch(e) {
case '.svn':
case '.':
case '..':
return nil;
default:
return e;
}

But I'm not sure how to apply that in ruby and I also think that these
could be improved anyway.

I'm sure a trained mind can spot other areas for improvement, I'm glad
for any suggestions!

thanks,
- Markus
 


$B209qMZ(B

2011/3/3 Markus Fischer said:
=A0 =A0dirs_count[dir] =3D
=A0 =A0 =A0 =A0Dir.entries(dir).collect { |e|
=A0 =A0 =A0 =A0 =A0 =A0case e
=A0 =A0 =A0 =A0 =A0 =A0when '.svn'
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0nil
=A0 =A0 =A0 =A0 =A0 =A0when '.'
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0nil
=A0 =A0 =A0 =A0 =A0 =A0when '..'
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0nil
=A0 =A0 =A0 =A0 =A0 =A0else
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0e
=A0 =A0 =A0 =A0 =A0 =A0end
=A0 =A0 =A0 =A0}.compact.count
case ... is the same as follows.
case e
when '.svn', '.', '..'
nil
else
e
end

Haruka YAGNI
(e-mail address removed)
 
A

Adam Prescott

[Note: parts of this message were removed to make it a legal post.]

Dir.entries(dir)[2..-1].reject { |e| e == ".svn" }

And of course I forgot the .sort :)

Dir.entries(dir).sort[2..-1].reject { |e| e == ".svn" }
 
A

Adam Prescott

[Note: parts of this message were removed to make it a legal post.]

dirs_count[dir] =
Dir.entries(dir).collect { |e|
case e
when '.svn'
nil
when '.'
nil
when '..'
nil
else
e
end
}.compact.count

Instead of mapping values to nil and using compact, you can simply use
Enumerable#reject or #select. For this, though, since I think you can rely
on Dir.entries(dir).sort having "." and ".." as the first two entries, you
could get away with this:

Dir.entries(dir)[2..-1].reject { |e| e == ".svn" }
 
S

Sandor Szuecs

=20
Dir.entries(dir)[2..-1].reject { |e| e =3D=3D ".svn" }
=20
=20
And of course I forgot the .sort :)
=20
Dir.entries(dir).sort[2..-1].reject { |e| e =3D=3D ".svn" }

2..-1 is not correct. It's mostly correct, but what if valid files with=20=

name.ord < ".".ord (String#ord ruby19) exists?
p.e.

[sz@suessapfel:/tmp/foo]$ irb
irb:0> FileUtils.touch '-r'
=3D> ["-r"]
irb:0> Dir.entries(".").sort[-1..2]
=3D> [".."]
irb:0> ^D[sz@suessapfel:/tmp/foo]$ l
total 0
-rw------- 1 sz wheel 0B Mar 3 15:30 -r
drwx------ 3 sz wheel 102B Mar 3 15:30 ./
drwxrwxrwt@ 11 root wheel 374B Mar 3 15:30 ../


All the best, Sandor Sz=FCcs
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top