Joe soap
Joe soap

Reputation:

How do I commit in Subversion and add new files/folders and remove old files/folders automatically?

Having been spoiled by TortoiseSVN, I'm now using the command line on Linux to interact with an SVN repository.

In TortoiseSVN I would just commit changes, and it would show me a list of what was added, what was deleted and what was modified. I'd check all the boxes and click OK.

With the command line, it appears I have to do svn add when I add files and svn rm when I remove files and when that's all done, then I type svn commit, and it commits the added, the removed and the modified.

Is there a command I can use that just commits files/folders I've removed, files/folders I've added and files I've modified all in one go?

Upvotes: 12

Views: 18204

Answers (9)

Melebius
Melebius

Reputation: 6695

Automatically add new files:

svn st | sed -rn '/^\?/s/^.{8}(.+)$/\1/p' | xargs -r svn add

Automatically remove deleted files:

svn st | sed -rn '/^!/s/^.{8}(.+)$/\1/p' | xargs -r svn rm

Explanation

This is basically a condensed version of the luca’s answer that combines grep and two sed calls into one.

sed

  • -r uses extended regular expressions
  • -n does not pass through the input
  • filters lines beginning with ? or ! (/^\?/ and /^!/, respectively)
  • cuts the first 8 characters (containing flags) away: s/^.{8}//
  • prints the rest of the line to the output: s/(.+)$/\1/p

See also: http://www.grymoire.com/Unix/sed.html

xargs

The -r (or --no-run-if-empty) option for xargs (GNU version) stops processing when there is no input from sed, therefore eliminating the SVN error Not enough arguments provided.

Upvotes: 2

TheraDaniel
TheraDaniel

Reputation: 71

Nader Shirazie had the correct command in the posted script. Here's the single line version for Linux:

svn add  $(svn st | sed -n 's/^[A?] *\(.*\)/\1/p')

I've wanted that functionality for a long time and am glad I searched here!

Upvotes: 7

nos
nos

Reputation: 229334

With the standard SVN tools, there's no such thing - it's mentioned in the FAQ as a bad thing.

Upvotes: 2

Gordo
Gordo

Reputation: 111

Under Windows, the following batch file would work:

for /f "tokens=2*" %%i in ('svn status %1 ^| find "?"') do svn add "%%i"  
for /f "tokens=2*" %%i in ('svn status %1 ^| find "!"') do svn delete "%%i"  
svn commit -m "Automatic commit" %1  

Simply save the three lines above in an file called 'autocommit.bat'. If you run it from the working directory, you don't need to specify a parameter. If you are in another directory, you can call it like autocommit.bat c:\MyProjectFolder.

Upvotes: 11

Joel Harkes
Joel Harkes

Reputation: 11671

If you want to do it with PowerShell, here is an easy function that uses svn status:

svn status | ? { $_ -match '^\?\s+(.*)' } | % { svn add $Matches[1] }

Credits go to an answer to Stack Overflow question SVN command to delete all locally missing files. I adjusted his to work for new files.

The PowerShell file I made it look like this:

# Delete missing files
svn status | ? { $_ -match '^!\s+(.*)' } | % { svn rm $Matches[1] }

# Added new files
svn status | ? { $_ -match '^\?\s+(.*)' } | % { svn add $Matches[1] }

# Commit repository
svn commit -m "This commit is done by a PowerShell bat"

Upvotes: 1

luca
luca

Reputation: 12611

To add:

svn status | grep "^\?" | sed -e 's/? *//' | sed -e 's/ /\\ /g' | xargs svn add

To remove:

svn status | grep "^\!" | sed -e 's/! *//' | sed -e 's/ /\\ /g' | xargs svn remove

It works fine for me.

Upvotes: 5

Nader Shirazie
Nader Shirazie

Reputation: 10776

There's no SVN command, but I'm sure there's a script or two that can scan for unversioned/missing files and issue the appropriate commands...

I found one here: http://gael-varoquaux.info/computers/svnautocommit/index.html

Adding the full script

#!/bin/bash


#------------------------------- Subroutines ---------------------------------
usage(){
echo " Usage: $(basename $0) PATH"
echo ""
echo "Automatically commits the changes of svn working copy located in PATH."
echo "The new files are automatically added and the files that have been removed"
echo "are removed."
echo ""
echo "By Gael Varoquaux"
}

#------------------------------- Process the options -------------------------
if [ $# -eq 1 ]
then
    workingdir="$1"
else
    usage
    exit 1
fi

if ! cd $workingdir
then
    echo $workingdir is not a accessible path.
    usage
    exit 1
fi

#------------------------------- Find out what has changed -------------------

# A warning if this fails :
echo "SVN autocommit failed" > $HOME/local/motd

svnstatus=$(svn status $workingdir)
added=$(printf "$svnstatus" | sed -n 's/^[A?] *\(.*\)/\1/p')
removed=$(printf "$svnstatus" | sed -n 's/^! *\(.*\)/\1/p')

if [ "x$added" != "x" ]
then
    echo adding "$added" to repository
    svn add $added
fi

if [ "x$removed" != "x" ]
then
    echo removing "$removed" to repository
    svn remove $removed
fi

svn commit -m "autocommit" && rm $HOME/local/motd

The Python version appears to not be there unfortunately.

You may want to modify the script to take a parameter for comments, but it's a start. You can also modify it to be an easy way to do the add/deletes for you, and do the commit manually.

Upvotes: 3

safetypin
safetypin

Reputation: 13

While succinct commands using line arguments are powerful, they may miss (or fail) to include filenames with spaces. The following command is from an answer to a similar question that will include all files.

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

Upvotes: 0

Nate
Nate

Reputation: 19030

In short, no. You have to svn add and svn delete each item.

If you are not tied to SVN, some other source control systems offer this feature; for example, Mercurial’s hg addremove.

Upvotes: 0

Related Questions