55

Sometimes I include many files in different directories in my project. For now I am adding all files one by one to my project before commit. Is there any Linux terminal command that can add all unversioned files to Subversion?

And what if I want to add all files excepting one or two files?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • possible duplicate of [How to use "svn add" recursively in Windows console?](http://stackoverflow.com/questions/4302888/how-to-use-svn-add-recursively-in-windows-console) – Saul Oct 13 '11 at 18:10

11 Answers11

83
svn add --force <directory>

Add is already recursive. You just have to force it to traverse versioned subdirectories.

cletus
  • 616,129
  • 168
  • 910
  • 942
  • Hi cletus - shared objects under a library dont seem to get added with this. However header files have got added. E.g i had xyz/usr/lib/a.so and xyz/include/a.h and when i did a svn add --force xyz, it added xyz, include and also a.h, whereas it stopped with usr and lib. – badri May 02 '20 at 22:31
  • i then had to do a svn add xyz/lib/* to get the shared objects added. It seems to be happy with binary executables but not shared objects. so again, had xyz/bin/bin1 where bin1 was a binary executable. this command had no issue adding bin1 whereas it seems to not do the same for the so – badri May 02 '20 at 22:41
28

Adds any file with a question mark next to it, while still excluding ignored files:

svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk '{print $2}' | xargs svn add
svn commit

http://codesnippets.joyent.com/posts/show/45

James Skidmore
  • 49,340
  • 32
  • 108
  • 136
  • Hello James. Sorry I downvoted your answer, but it's a mistake. Please edit it, so I can rollback this downvote (and this comment as well). – Guillaume Aug 02 '12 at 14:26
  • I'm not sure why, but I beleive your regex doesn't care about filenames that contains spaces... – genuinefafa Aug 07 '14 at 17:16
  • svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk '{print $2}' | xargs svn add && svn commit -m "test" – Sky Apr 26 '19 at 08:05
23

This will attempt to add all files - but only actually add the ones that are not already in SVN:

svn add --force ./* 
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Steve Truesdale
  • 231
  • 2
  • 2
13

This command will add any un-versioned files listed in svn st command output to subversion.

Note that any filenames containing whitespace in the svn stat output will not be added. Further, odd behavior might occur if any filenames contain '?'s.

svn st | grep ? | tr -s ' ' | cut -d ' ' -f 2 | xargs svn add

or if you are good at awk:

svn st | grep ? | awk '{print $2}' | xargs svn add

Explanation:

Step 1: svn st command

[user@xxx rails]$svn st
?       app/controllers/application.rb
M       app/views/layouts/application.html.erb
?       config/database.yml

Step 2: We grep the un-versioned file with grep command:

[user@xxx rails]$svn st | grep ?
?       app/controllers/application.rb
?       config/database.yml

Step 3: Then remove the squeeze the space between ? and file path by using tr command:

[user@xxx rails]$svn st | grep ? | tr -s ' '
? app/controllers/application.rb
? config/database.yml
</pre>

Step 4: Then select second column from the output by using cut command:

[user@xxx rails]$svn st | grep ? | tr -s ' ' | cut -d ' ' -f 2
app/controllers/application.rb
config/database.yml

Step 5: Finally, passing these file paths as standard input to svn add command:

[user@xxx rails]$svn st | grep ? | tr -s ' ' | cut -d ' ' -f 2 | xargs svn add
A       app/controllers/application.rb
A       config/database.yml
peterpengnz
  • 5,897
  • 2
  • 21
  • 18
10

By default, the svn add command is recursive. Just point it at the top-level directory of your project and it should add any file not already there.

You may want to include the --force option, though keep in mind that this will also add files which are otherwise excluded due to svn:ignore (if you don't use svn:ignore, then you won't have any issues).

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Amber
  • 507,862
  • 82
  • 626
  • 550
2

None of you guys are fully correct I spend a whole night to fix it and it's running properly in my jenkins:

svn status | grep -v "^.[ \t]*\..*" | grep "^?[ \t]*..*" | awk '{print $2}' | sed 's/\\/\//g' | sed 's/\(.*\)\r/"\1"/g' | xargs svn add --parents
svn commit
  1. status
  2. remove .svn
  3. find ? at the line begining to determine whether it is unversioned (modify it to | grep "^?[ \t]*.*cpp" | will only add cpp)
  4. remove ? (one of the above thread memtion the problem of awk, you may try to use tr or sed instead)
  5. replace splash to linux splash(optional, cygwin need)
  6. enclose line with quote
  7. call xargs(you should add --parent to make sure mid level sub folders are also added if you try to filter cpp or certain file pattern)
Alen Wesker
  • 237
  • 3
  • 6
1

The above-mentioned solution with awk '{print $2}' does not unfortunately work if the files contain whitespaces. In such a case, this works fine:

svn st | grep "^?" | sed 's/^?[ \t]*//g' | sed 's/^/"/g' | sed 's/$/"/g' | xargs svn add

where

sed 's/^?[ \t]*//g'

removes the question mark and empty characters from the beginning and

sed 's/^/"/g' | sed 's/$/"/g'

encloses the filenames with quotes.

Habi
  • 11
  • 1
1

An approach using a Perl one-liner to add all files shown with a question mark by "svn st" would be:

svn st | perl -ne 'print "$1\n" if /^\?\s+(.*)/' | xargs svn add

This should also work with space characters in file names.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Twonky
  • 445
  • 9
  • 12
0

This version of the above commands takes care of the case where the committed files or the paths to them contain spaces. This command will still fail, though, if the path contains a single quote.

svn st |grep ^?| cut -c9-| awk '{print "\x27"$0"\x27"}' | xargs svn add

I went for the cut -c9- to get rid of the leading spaces and assuming that the svn st command will start filename at the 9th character on all machines. If you are unsure, test this part of the command first.

Ivin
  • 1,081
  • 11
  • 21
0

This shell script, recursively examines (svn status) directories in your project, removing missing files and adding new files to the repository. It is some sort of "store into the repository the current snapshot of the project".

if [ $# != 1 ]
then
    echo  "usage: doSVNsnapshot.sh DIR"
    exit 0
fi

ROOT=$1

for i in `find ${ROOT} -type d \! -path "*.svn*" `
do

    echo
    echo "--------------------------"
    ( cd $i ; 
    echo $i
    echo "--------------------------"


    svn status | awk '  
            /^[!]/ { system("svn rm " $2) }
            /^[?]/ { system("svn add " $2) }
        '
    )
    echo

done
cibercitizen1
  • 20,944
  • 16
  • 72
  • 95
-3

I haven't used SVN. But the following should work.

svn add foo/bar.file bar/foo.file

This should add file bar.file in the foo directory and foo.file existing in the bar directory.

And svn add foo should add all files in foo to the server. The "recursive" flag is set by default.

And to add all files in a directory except for a few (like the files starting/ending with keywords tmp, test, etc.), I don't think there is a cleaner/simpler way to do it, and better write a shell script that does this.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mallik
  • 5
  • 4
  • 5
    Why would you try and contribute with something that you haven't used or for that matter can't be sure of that it works? – halfpastfour.am Apr 02 '14 at 16:01
  • I must agree to the above comment. Google is clouded with "I've never used X before but..." It has to stop. – steve Aug 04 '14 at 20:00