views:

2387

answers:

8

I'm looking for a good way to automatically 'svn add' all unversioned files in a working copy to my svn repository.

I have a live server that can create a few files that should be under source control. I would like to have a short script that I can run to automatically add these, instead of going through and adding them one at a time.

My server is running Windows Server 2003 so a unix solution won't work.

+1  A: 

svn add --force .

Naaff
This will add all ignored files, which is not a good thing
Sam Saffron
This is true, but the OP many not have ignored files in his working copy. In which case, this will work just fine.
Naaff
+1  A: 

This is a different question to mine but there is an answer there that belongs on this question:

svn status | grep '?' | sed 's/^.* /svn add /' | bash
Sam Saffron
This uses UNIX tools (grep, sed, bash) which the OP said he didn't have.
Naaff
also you could probably dig up sed and grep on windows if you really wanted.
Sam Saffron
Sorry for that upvote on the cygwin comment, wasn't intentional (and I can't undo it). In fact I think cygwin is as ugly as you can get on Windows. You can still use findstr instead of grep which will work at least since Windows 2000 without installing anything additional.
Joey
@Johannes just for you http://meta.stackoverflow.com/questions/1775/why-can-you-not-take-back-an-upvote-on-a-comment
Sam Saffron
So install cygwin. This probably won't be the last time he needs to automate something. (deleted original comment that was above, to get rid of upvote)
KeyserSoze
A: 

I think I've done something similar with:

svn add . --recursive

but not sure if my memory is correct ;-p

chakrit
just a update for your memory: svn 1.4 and 1.5 don't support --recursive as parameter for svn add :)
Ludwig Wensauer
+1  A: 

Tortoise SVN has this capability built in, if you're willing to use a non-command-line solution. Just right click on the top level folder and select Add...

John Meagher
+8  A: 

This is not that difficult to to in cmd:

svn status

will list all unversioned files with a ? in front. So

svn status | findstr /r "^\?"

will find all those lines. You can then tokenize this by wrapping it into a for command:

for /f "usebackq tokens=2*" %i in (`svn status ^| findstr /r "^\?"`) do svn add "%i %j"

(Note that the tokens option here causes two tokens, %i and %j to be created, hence the slightly weird svn add statement in the end.)

If you use this in a batch file you need to double the %:

for /f "usebackq tokens=2*" %%i in (`svn status ^| findstr /r "^\?"`) do svn add "%%i %%j"
Joey
Wow - very nice. Guess you'll learn something new every day. :-)
mlarsen
+1 very nice, where did you learn to use those arcane commands?
Sam Saffron
Well, for is more or less the all-in-one tool for anything loopy in batch and the rest is more or less pretty standard stuff. I do some stuff in batches which other might consider painful even to look at but sometimes it's fun. Maybe a similar thing what drives people to write makefiles or Java :). But many of those things are pretty well explained in the help files and then you just know how to use thm after havin used them a few dozen times.
Joey
I tried this solution and it worked great on the command line, but when I tried to invoke it from a .bat file, it failed with the error "i was unexpected at this time." It turns the solution is to double-up the "%" symbols so that there are two instead of one for all three places. (HT: http://techrepublic.com.com/5208-11184-0.html?forumID=57leftCol)
Kirk Woll
@Kirk: Yes, that's normal. Just see `help for`: *»To use the `FOR` command in a batch program, specify `%%variable` instead of `%variable`.«* – I usually don't point it out explicitly unless the question deals with batch files directly.
Joey
@Joey, sorry if you took my comment as any sort of criticism of your original answer. Your answer saved my bacon. :) But for the sake of posterity, I was merely trying to help anyone in the future who stumbles upon your answer who happens to be as ignorant as myself in .bat programming -- avoid the chore of googling that error.
Kirk Woll
@Kirk: Sorry, my reply probably cam across a little harsh as well. I'll edit it in.
Joey
+1  A: 

svn st|grep ?|cut -d? -f2|xargs svn add

谢继雷
A: 

Tortoise is nice but only available on Windows. I have personnally a Windows machine and a Mac OS X machine on which SVN seems to have some problems. 'svn status' shows me hundreds of file twice, once with ! and once with ?. It's very hard to see what's to add :(

Adeynack
A: 

svn add --force * --auto-props --parents --depth infinity -q

Ronan