[ANN] svn_auto_add

M

Michael Weller

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
 
M

Michael Weller

Jonathan said:
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
 
C

Cristi BALAN

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
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top