[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[ANN] svn_auto_add

Michael Weller

10/8/2004 12:31:00 PM

Hi!
I wrote a little script for svn users. It will detect any file added to
a working copy which svn doesn't know about. In other words: it calls
'svn add' for any newly added file.
I thought I share it,just in case it might be helpful to somebody... (I
actually don't think so because I'm a Nuby and everybody could write
this script, but you never know :-)

So here it is:
<code lang="ruby">
require 'find'
require 'rexml/document'

def list_dir_files(path)
Dir.new(path).to_a - [".","..",".svn"]
end

def list_svn_files(entries)

doc = REXML::Document.new(IO.read(entries))
files =[]
# don't know why, but "wc-entries/entry" won't work
doc.root.elements.each("//entry") do |el|
val = el.attributes["name"]
files << val unless val.nil? || ""==val
end
return files
end

def check_missing(path, svn_entries)
dir_files = list_dir_files(path)
svn_files = list_svn_files(svn_entries)
(dir_files - svn_files).collect! {|e| path+"/"+e}
end

missing_paths = []

Find.find(".") do |path|
if path =~ /.svn/
Find.prune
end
if FileTest.directory?(path) then
svn_dir = path + "\\.svn"
if File.exists? svn_dir then
missing = check_missing(path, svn_dir+"\\entries")
missing_paths << missing unless missing.nil? || missing.length==0
else
missing_paths << path
end
end
end

missing_paths -= ["."]
missing_paths.flatten!

p "\n\n\t Found missing:"
missing_paths.each do |mp|
# this should be customized, this is for files you never want added
# for me (usually a Java head) these are bak-files, class-files and
# anything in .\classes
unless mp =~ /(.bak)|(.class)/ then
# this is windows only, is there sth. like
# <code lang="Java">File.separatorChar</code> ?
path = mp.gsub(/\//, '\\')
`svn add \"#{path}\"`
end
end
</code>

If somebody reads the script and thinks "Why is he doing this that way?"
feel free to comment, I'm always open for improvements...

Michael


4 Answers

Jonathan Paisley

10/8/2004 1:19:00 PM

0

On Fri, 08 Oct 2004 21:31:21 +0900, Michael Weller wrote:

> I wrote a little script for svn users. It will detect any file added to
> a working copy which svn doesn't know about. In other words: it calls
> 'svn add' for any newly added file.
> I thought I share it,just in case it might be helpful to somebody... (I
> actually don't think so because I'm a Nuby and everybody could write
> this script, but you never know :-)

[snip code]

> If somebody reads the script and thinks "Why is he doing this that way?"
> feel free to comment, I'm always open for improvements...

I wonder whether it might be simpler to just ask the command line tool
what files it doesn't know about (which will take into account the
relevant ignore settings for *.o, *.class etc). For example:

$ svn status
? somefile
? otherfile
M modifiedfile

By looking for lines that begin with a '?', we get precisely the list that
needs to be added. These lines could be turned into the appropriate 'svn
add' commands.

One way (not particularly pleasant to look at) using ruby would be:

$ svn status | ruby -ne 'puts $_[7..-2] if $_[0] == ??'

to actually run the 'svn add' command you could use:

$ svn status | ruby -e 'system("svn","add",$_[7..-2]) if $_[0] == ??'

I think I would normally do it with a shell pipeline, since I find it
easier to compose the simpler steps than come up with the ruby script for
the whole thing:

$ svn add `svn status | grep '^?' | cut -c8-`
(assuming no spaces in the names of the files to be added)

Hope this makes sense!

Michael Weller

10/8/2004 1:39:00 PM

0

Jonathan Paisley wrote:
> On Fri, 08 Oct 2004 21:31:21 +0900, Michael Weller wrote:
>
>
>>I wrote a little script for svn users. It will detect any file added to
>>a working copy which svn doesn't know about. In other words: it calls
>>'svn add' for any newly added file.
>>I thought I share it,just in case it might be helpful to somebody... (I
>>actually don't think so because I'm a Nuby and everybody could write
>>this script, but you never know :-)
>
>
> [snip code]
>
>
>>If somebody reads the script and thinks "Why is he doing this that way?"
>>feel free to comment, I'm always open for improvements...
>
>
> I wonder whether it might be simpler to just ask the command line tool
> what files it doesn't know about (which will take into account the
> relevant ignore settings for *.o, *.class etc). For example:
>
> $ svn status
> ? somefile
> ? otherfile
> M modifiedfile
>
> By looking for lines that begin with a '?', we get precisely the list that
> needs to be added. These lines could be turned into the appropriate 'svn
> add' commands.
>
> One way (not particularly pleasant to look at) using ruby would be:
>
> $ svn status | ruby -ne 'puts $_[7..-2] if $_[0] == ??'
>
> to actually run the 'svn add' command you could use:
>
> $ svn status | ruby -e 'system("svn","add",$_[7..-2]) if $_[0] == ??'
>
> I think I would normally do it with a shell pipeline, since I find it
> easier to compose the simpler steps than come up with the ruby script for
> the whole thing:
>
> $ svn add `svn status | grep '^?' | cut -c8-`
> (assuming no spaces in the names of the files to be added)
>
> Hope this makes sense!
>

Actually it makes a lot of sense... Much more than my approach :-((
But like I said I'm a nuby and happy that I got my script working my
(much more complicated) way...
Yes, shell pipes would be great :-((

Thanks for your input!

Michael



Jonathan Paisley

10/8/2004 1:48:00 PM

0

On Fri, 08 Oct 2004 22:38:51 +0900, Michael Weller wrote:

> But like I said I'm a nuby and happy that I got my script working my
> (much more complicated) way...

Yeah, I have often found that I've made a complicated script that
later turns out to be doable in some simpler way. However, it's usually
a good learning experience :)

> Thanks for your input!

No problem. Sorry if I was a bit terse in my original reply.

Cristi Balan

10/8/2004 9:50:00 PM

0

On Fri, 8 Oct 2004 21:31:21 +0900, Michael Weller <michael@gutschi.de> wrote:
> Hi!
> I wrote a little script for svn users. It will detect any file added to
> a working copy which svn doesn't know about. In other words: it calls
> 'svn add' for any newly added file.

Sorry to break this to you but, svn add --force foo is recursive and
skips already added files too... at least on svn 1.1.0rc1 it does

--
Cristi BALAN