Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Finding all empty directories in a subversion checkout

Reply
Thread Tools

Finding all empty directories in a subversion checkout

 
 
Markus Fischer
Guest
Posts: n/a
 
      03-03-2011
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


 
Reply With Quote
 
 
 
 
屋国遥
Guest
Posts: n/a
 
      03-03-2011
2011/3/3 Markus Fischer <(E-Mail Removed)>:
> =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
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Adam Prescott
Guest
Posts: n/a
 
      03-03-2011
[Note: parts of this message were removed to make it a legal post.]

On Thu, Mar 3, 2011 at 1:03 AM, Adam Prescott <(E-Mail Removed)> wrote:

> 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" }

 
Reply With Quote
 
Adam Prescott
Guest
Posts: n/a
 
      03-03-2011
[Note: parts of this message were removed to make it a legal post.]

On Thu, Mar 3, 2011 at 12:15 AM, Markus Fischer <(E-Mail Removed)> wrote:

> 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" }

 
Reply With Quote
 
Sandor Szuecs
Guest
Posts: n/a
 
      03-03-2011

On Mar 3, 2011, at 2:05 AM, Adam Prescott wrote:

> On Thu, Mar 3, 2011 at 1:03 AM, Adam Prescott <(E-Mail Removed)> =

wrote:
>=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
--




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Free Online Subversion Training - All About Subversion Hook Scripts george.powell@wandisco.com Java 0 05-12-2010 09:43 AM
Free Online Subversion Training - Introduction to Subversion forDevelopers george.powell@wandisco.com Java 5 04-30-2010 02:57 AM
Getting all directories/files from current directory and using -d flag for the directories Adam Petrie Perl Misc 8 10-11-2004 01:28 PM
SourceSafe Checkout or Edit?? Brian ASP .Net 2 05-12-2004 01:58 PM
Why does Visual Studio always checkout files when looking Web User Control in design mode? Eric Mamet ASP .Net 0 01-30-2004 10:38 AM



Advertisments