12

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

9 Answers9

11

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gordo
  • 111
  • 1
  • 2
  • What %1 does in your command, and what happens if file names include spaces – AaA Jun 20 '17 at 03:36
  • To handle filenames with spaces, try this, which puts everything after the first token (? or !) into %j: `for /f "tokens=1*" %%i in ('svn status %1 ^| find "?"') do svn add "%%j"` – David Pritchard Nov 24 '22 at 10:58
7

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!

Melebius
  • 6,183
  • 4
  • 39
  • 52
TheraDaniel
  • 71
  • 1
  • 1
5

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
luca
  • 12,311
  • 15
  • 70
  • 103
  • Those two `sed` and one `grep` calls can be merged into one, see [my answer](https://stackoverflow.com/a/47772775/711006). – Melebius Dec 12 '17 at 12:34
3

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nader Shirazie
  • 10,736
  • 2
  • 37
  • 43
3

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

Melebius
  • 6,183
  • 4
  • 39
  • 52
nos
  • 223,662
  • 58
  • 417
  • 506
  • I'm more tied to svn but at least I understand why they made this decision. Thanks. –  Jul 31 '09 at 15:45
2

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.

Melebius
  • 6,183
  • 4
  • 39
  • 52
1

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"
Community
  • 1
  • 1
Joel Harkes
  • 10,975
  • 3
  • 46
  • 65
0

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.

Nate
  • 18,752
  • 8
  • 48
  • 54
0

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
Community
  • 1
  • 1
safetypin
  • 13
  • 1
  • 7