[ANN] svn_auto_add

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 :slight_smile:

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

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 :slight_smile:

  [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!

路路路

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

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

路路路

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.

--
Cristi BALAN

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 :slight_smile:

  [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

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 :slight_smile:

Thanks for your input!

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

路路路

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