life.i.think: Adding all new files with SVN

Adding all new files with SVN
Scribbled on June 1st. 11 comments.

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
Comments

Leave a response

  1. kumar303June 05, 2005 @ 09:41 PM

    simple and elegant. nicely done ;)

  2. gregJune 17, 2005 @ 04:52 PM

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

  3. BrittJune 29, 2005 @ 03:34 PM

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

    (updated post)

  4. lobsterJuly 15, 2005 @ 04:18 AM

    what about Windows….without grep….

  5. lobsterJuly 15, 2005 @ 11:12 AM

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

    #

    1. Author:Yufan Shi
    2. 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 end

      if l =~ /\?/
        l = l.sub(/\?( )*/,'svn add ')
        objResult = shell.exec(l)
        while !objResult.stdout.atendofstream
          print objResult.stdout.readline,"\n" 
        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.

  6. lobsterJuly 15, 2005 @ 11:22 AM

    Sorry for bad format…A better readable one here:

    http://wiki.rubyonrails.com/rails/show/HowtoUseRailsWithSubversion

  7. husseinJuly 15, 2005 @ 05:17 PM

    Sorry, but is it not easier to use this:

    svn add *—force

  8. lobsterJuly 16, 2005 @ 02:30 AM

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

  9. BrittNovember 29, 2006 @ 09:57 PM

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

  10. NikaAugust 27, 2007 @ 07:36 PM

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

  11. ArunAugust 13, 2008 @ 10:03 PM

    Here is a small python code I wrote to add all files to svn.

    ! /usr/bin/env python

    ””” Adding all files with a question mark to svn Asks if you want to add all or want to confirm each file ””“

    import os

    a = os.popen(‘svn st | grep ”?”’) b = a.read() c = b.split(’\n’)

    1. Checking if any new files exist fch = 0 rall = “n” for f in c: if f.find(”?”) != -1: fch = 1

    if fch == 1: for fc in c: print fc

    rall = raw_input("Add all files (y/n)?")

    for f in c: if f.find(”?”) != -1:

    d = f.split(' ')
    if rall != "y":
        ch = raw_input("add " + d[-1]+" (y/n)?")
    if rall  "y" or ch  "y":
        os.system('svn add ' + d[-1])
Comment