Finding all empty directories in a subversion checkout

Discussion in 'Ruby' started by Markus Fischer, Mar 3, 2011.

  1. 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
     
    Markus Fischer, Mar 3, 2011
    #1
    1. Advertising

  2. Markus Fischer

    屋国遥 Guest

    2011/3/3 Markus Fischer <>:
    > =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
     
    屋国遥, Mar 3, 2011
    #2
    1. Advertising

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

    On Thu, Mar 3, 2011 at 1:03 AM, Adam Prescott <> 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" }
     
    Adam Prescott, Mar 3, 2011
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    On Thu, Mar 3, 2011 at 12:15 AM, Markus Fischer <> 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" }
     
    Adam Prescott, Mar 3, 2011
    #4
  5. On Mar 3, 2011, at 2:05 AM, Adam Prescott wrote:

    > On Thu, Mar 3, 2011 at 1:03 AM, Adam Prescott <> =

    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
    --
     
    Sandor Szuecs, Mar 3, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Mamet
    Replies:
    0
    Views:
    402
    Eric Mamet
    Jan 30, 2004
  2. Brian

    SourceSafe Checkout or Edit??

    Brian, May 6, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    400
    Brian
    May 12, 2004
  3. Replies:
    5
    Views:
    824
    John B. Matthews
    Apr 30, 2010
  4. Replies:
    0
    Views:
    393
  5. Adam Petrie
    Replies:
    8
    Views:
    338
    Adam Petrie
    Oct 11, 2004
Loading...

Share This Page