With rails, whenever you run a generate script, it often creates large numbers of new files in many different directories. You then may want to commit all of these to a subversion repository.

Unfortunately, ‘svn add *’ is not recursive. Luckily, we can accomplish this with good ol’ shell scripting:


svn status | grep "^\?" | awk '{print $2}' | xargs svn add

And much less pretty, but for files with spaces in them:


svn status | grep "^\?" | sed -e 's/? *//' | sed -e 's/ /\\ /g' | xargs svn add

11 Responses to “Adding all new files with SVN”

  1. Nika Says:

    you can also do script/generate –svn
    and it will add everything it generates to svn

  2. Britt Says:

    Strange that the recursive flag is a subset of –force.

  3. kumar303 Says:

    simple and elegant. nicely done ;)

  4. greg Says:

    does not handle files or directories with spaces in their names. bad!

  5. Britt Says:

    files with spaces in them, bad!
    But ok :)

    (updated post)

  6. lobster Says:

    what about Windows….without grep….

  7. lobster Says:

    Just wrote a little script employing WScript.Shell to execute svn commands and add newly created files/dirs to svn repository.

    #####################
    # Author:Yufan Shi
    # Email:yufanshi AT gmail.com
    #####################
    require ‘win32ole’

    shell = WIN32OLE.new(’Wscript.Shell’)
    objExec = shell.exec("svn status")
    while !objExec.stdout.atendofstream
    l = objExec.stdout.readline

    if l =~ /^\?/
    l = l.sub(/^\?( )*/,’svn add ‘)
    objResult = shell.exec(l)
    while !objResult.stdout.atendofstream
    print objResult.stdout.readline,"\n"
    end
    end
    end

    ##########################
    Save above script to a file(etc. svn_addgenerated.rb) and drop it into your rails app’s script directory. And every time after rails generate some files, you can type in command shell:ruby script/svn_addgenerated.rb and enter , script will automatically find new files need to be added and run ’svn add’ against them.

  8. hussein Says:

    Sorry, but is it not easier to use this:

    svn add * –force

  9. lobster Says:

    Welll…It seems much much easier…using ‘force’


  10. [...] about this.. most of the time people consider that the only way is using a nice little script (see here for example). But the –force flag induces recursivity, so this command does the stuff [...]


Comments are closed.