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.
This question is related to
svn
command-line
windows-server-2003
svn add --force .
This will add any unversioned file in the current directory and all versioned child directories.
This is the lazy and dangerous way to synchronize a directory with SVN including new files:
svn rm --keep-local dir
svn add dir
Although this can work in a pinch it can have serious consequences as well. For example, SVN will often lose track of a file's history.
The ideal way to syncronize a directory would be to be able to diff then use svn patch, however this deviates from formats in the diff command and the svn diff command will compare working directory differences rather than differences on the file system level.
I always use:
Copy&paste
svn st | grep "^\?" | awk "{print \$2}" | xargs svn add $1
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
This method should handle filenames which have any number/combination of spaces in them...
svn status /home/websites/website1 | grep -Z "^?" | sed s/^?// | sed s/[[:space:]]*// | xargs -i svn add \"{}\"
Here is an explanation of what that command does:
Use the -i argument to xargs to handle being able to import files names with spaces into 'svn add' - basically, -i sets {} to be used as a placeholder so we can put the " characters around the filename used by 'svn add'.
An advantage of this method is that this should handle filenames with spaces in them.
I think I've done something similar with:
svn add . --recursive
but not sure if my memory is correct ;-p
If you use Linux or use Cygwin or MinGW in windows you can use bash-like solutions like the following. Contrasting with other similar ones presented here, this one takes into account file name spaces:
svn status| grep ^? | while read line ; do svn add "`echo $line|cut --complement -c 1,2`" ;done
What works is this:
c:\work\repo1>svn add . --force
Adds the contents of subdirectories.
Does not add ignored files.
Lists what files were added.
The dot in the command indicates the current directory, this can replaced by a specific directory name or path if you want to add a different directory than the current one.
After spending some time trying to figure out how to recursively add only some of the files, i thought it would be valid to share what did work for me:
FOR /F %F IN ('dir /s /b /a:d') DO svn add --depth=empty "%F"
FOR /F %F IN ('dir /s /b /a *.cs *.csproj *.rpt *.xsd *.resx *.ico *.sql') DO svn add "%F"
Here goes some explanation.
The first command adds all the directories. The second command adds only the files accordingly to the specifed patterns.
Let me give more details:
In the second command, the only differences are the dir command and the svn command, i think it is clear enough.
You can input the following command on Linux:
find ./ -name "*." | xargs svn add
You can use command
svn add * force--
or
svn add <directory/file name>
Recursive adding is default property. You can see in SVN book.
Issue can be in your ignore list or global properties.
I got solution google issue tracker
TortoiseSVN > Properties
.svn:global-ignores with a value of *
star(*)
then it will ignore recursive adding. So remove this property.TortoiseSVN > Settings > General
.star(*)
, remove this property.This guy also explained why this property added in my project.
The most like way that it got there is that someone right-clicked a file without any extension and selected TortoiseSVN -> SVN Ignore -> * (recursively), and then committed this.
You can check the log to see who committed that property change, find out what they were actually trying to do, and ask them to be more careful in future. :)
Since this post is tagged Windows, I thought I would work out a solution for Windows. I wanted to automate the process, and I made a bat file. I resisted making a console.exe in C#.
I wanted to add any files or folders which are not added in my repository when I begin the commit process.
The problem with many of the answers is they will list unversioned files which should be ignored as per my ignore list in TortoiseSVN.
Here is my hook setting and batch file which does that
Tortoise Hook Script:
"start_commit_hook".
(where I checkout) working copy path = C:\Projects
command line: C:\windows\system32\cmd.exe /c C:\Tools\SVN\svnadd.bat
(X) Wait for the script to finish
(X) (Optional) Hide script while running
(X) Always execute the script
svnadd.bat
@echo off
rem Iterates each line result from the command which lists files/folders
rem not added to source control while respecting the ignore list.
FOR /F "delims==" %%G IN ('svn status ^| findstr "^?"') DO call :DoSVNAdd "%%G"
goto end
:DoSVNAdd
set addPath=%1
rem Remove line prefix formatting from svn status command output as well as
rem quotes from the G call (as required for long folder names). Then
rem place quotes back around the path for the SVN add call.
set addPath="%addPath:~9,-1%"
svn add %addPath%
:end
This is as documented on svn book and the simplest and works perfect for me
svn add * --force
This worked for me:
svn add `svn status . | grep "^?" | awk '{print $2}'`
(Source)
As you already solved your problem for Windows, this is a UNIX solution (following Sam). I added here as I think it is still useful for those who reach this question asking for the same thing (as the title does not include the keyword "WINDOWS").
Note (Feb, 2015): As commented by "bdrx", the above command could be further simplified in this way:
svn add `svn status . | awk '/^[?]/{print $2}'`
for /f "usebackq tokens=2*" %%i in (`svn status ^| findstr /r "^\?"`) do svn add "%%i %%j"
Within this implementation, you will get in trouble in the case your folders/filenames have more than one space like below:
"C:\PROJECTS\BACKUP_MGs_via_SVN\TEST-MG-10\data\destinations\Sega Mega 2"
"C:\PROJECTS\BACKUP_MGs_via_SVN\TEST-MG-10\data\destinations\One space"
"C:\PROJECTS\BACKUP_MGs_via_SVN\TEST-MG-10\data\destinations\Double space"
"C:\PROJECTS\BACKUP_MGs_via_SVN\TEST-MG-10\data\destinations\Single"
such cases are covered by simple:
for /f "usebackq tokens=1*" %%i in (`svn status ^| findstr /r "^\?"`) do svn add "%%j"
TortoiseSVN 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...
Use:
svn st | grep ? | cut -d? -f2 | xargs svn add
Since he specified Windows, where awk & sed aren't standard:
for /f "tokens=1*" %e in ('svn status^|findstr "^\?"') do svn add "%f"
or in a batch file:
for /f "tokens=1*" %%e in ('svn status^|findstr "^\?"') do svn add "%%f"
Source: Stackoverflow.com