开发者

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

I'm trying to wrap a standard sequence of steps in a shell script (linux/bash) and can't seem to fi开发者_JAVA百科gure 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.


You can test the revision number with svnversions from kde.org http://websvn.kde.org/trunk/KDE/kdesdk/scripts/svnversions?view=log


Here's a solution I've used. Works with svn externals and avoids explicitly listing svn statuses in your scripts (which may or may not be a good thing depending on your point of view).

# tested with svn 1.6.11/bash 4.1.2
if [ "$(svn st -q | cut -c -8 | sed '/^[^ ]*$/d' | grep -m 1 '[^ ]')" ]; then
    echo "changes detected"
fi

svn st -q - lists the statuses without the X for external directories.
cut -c -8 - drops all but the first 8 columns.
sed '/^[^ ]*$/d' - drops the 'Performing...' lines seen when working with externals.
grep -m 1 '[^ ]' - the first non-space implies a modification.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜