Add all unversioned files to Subversion using one Linux command
SvnSvn Problem Overview
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?
Svn Solutions
Solution 1 - Svn
svn add --force <directory>
Add is already recursive. You just have to force it to traverse versioned subdirectories.
Solution 2 - Svn
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
Solution 3 - Svn
This will attempt to add all files - but only actually add the ones that are not already in SVN:
svn add --force ./*
Solution 4 - Svn
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
Solution 5 - Svn
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).
Solution 6 - Svn
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
- status
- remove .svn
- find ? at the line begining to determine whether it is unversioned (modify it to | grep "^?[ \t]*.*cpp" | will only add cpp)
- remove ? (one of the above thread memtion the problem of awk, you may try to use tr or sed instead)
- replace splash to linux splash(optional, cygwin need)
- enclose line with quote
- 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)
Solution 7 - Svn
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.
Solution 8 - Svn
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.
Solution 9 - Svn
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.
Solution 10 - Svn
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
Solution 11 - Svn
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.