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.
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.
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.
Or you could try
svn status | grep [AMCDG]
echo $?
should return 0 if there are changes and 1 if there are none
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With