Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVN: and bash: How to tell if there are uncommitted changes

Tags:

bash

svn

I'm trying to wrap a standard sequence of steps in a shell script (linux/bash) and can't seem to figure out how to tell if the execution of svn status returned anything. For example

~/sandbox/$svn status
?       pat/foo
~/sandbox/$echo $?
0

If I delete the foo file, then the

svn status

return nothing, but the echo $? is still 0

I want to not do some steps if there are uncommitted changes.

Pointers greatly appreciated.

like image 741
fishtoprecords Avatar asked Apr 22 '10 18:04

fishtoprecords


4 Answers

Why not test the result from svn status -q? Something like:

result=`svn status -q`
[ -z "$result" ] && echo "No changes" && exit 1

echo "Changes found"
exit 0

If you are using svn:externals in your repo please see the comments and the answers below for additional methods.

like image 69
AlG Avatar answered Nov 13 '22 02:11

AlG


The accepted answer won't work if your project contains svn:externals references. In that case, svn status -q will still produce output even if the working copy has no local modifications. For example, my project depends on several libraries that are each maintained in a separate part of the repository:

$ svn status -q
X       Externals/ETCKit
X       Externals/RulesParser
X       Externals/XMLRPC

Performing status on external item at 'Externals/ETCKit':

Performing status on external item at 'Externals/XMLRPC':

Performing status on external item at 'Externals/RulesParser':

To account for this additional output, I ended up using awk:

if [[ -n $(svn status -q . | awk '$1 ~ /[!?ABCDGKLMORST]/') ]]; then
    echo "The working copy at $(pwd) appears to have local modifications"
fi

This script takes the output of svn status -q and filters out any lines that don't begin with a status code indicating a local change. If the end result is the empty string, then the working copy is clean.

like image 22
Cody Brimhall Avatar answered Nov 13 '22 02:11

Cody Brimhall


Or you could try

svn status | grep [AMCDG]
echo $?

should return 0 if there are changes and 1 if there are none

like image 3
DrewM Avatar answered Nov 13 '22 03:11

DrewM


I have implemented something similar a while back. You should not rely on the return value of svn status, but parse its output instead. For example you should look for lines starting with "M", "A", "D", etc. You can use perl to help you with this. Based on the result of that parsing you'll certainly know if there are changes or not.

Btw it's not normal for svn status to return 0 if there are no changes - after all this return code simply signifies that no errors occurred.

like image 2
Bozhidar Batsov Avatar answered Nov 13 '22 01:11

Bozhidar Batsov