SVN:和 bash:如何判断是否有未提交的更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2693467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SVN: and bash: How to tell if there are uncommitted changes
提问by fishtoprecords
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 statusreturned anything. For example
我试图在 shell 脚本(linux/bash)中包装一个标准的步骤序列,但似乎无法弄清楚如何判断执行是否svn status返回任何内容。例如
~/sandbox/$svn status
? pat/foo
~/sandbox/$echo $?
0
If I delete the foo file, then the
如果我删除了 foo 文件,那么
svn status
return nothing, but the echo $?is still 0
什么都不返回,但echo $?仍然是 0
I want to not do some steps if there are uncommitted changes.
如果有未提交的更改,我不想执行某些步骤。
Pointers greatly appreciated.
指针非常感谢。
回答by AlG
Why not test the result from svn status -q? Something like:
为什么不测试结果svn status -q?就像是:
result=`svn status -q`
[ -z "$result" ] && echo "No changes" && exit 1
echo "Changes found"
exit 0
If you are using svn:externalsin your repo please see the comments and the answers below for additional methods.
如果你svn:externals在你的 repo中使用,请查看下面的评论和答案以了解其他方法。
回答by Cody Brimhall
The accepted answer won't work if your project contains svn:externalsreferences. In that case, svn status -qwill 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:externals引用,则接受的答案将不起作用。在这种情况下,svn status -q即使工作副本没有本地修改,仍然会产生输出。例如,我的项目依赖于几个库,每个库都维护在存储库的单独部分中:
$ 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:
为了解决这个额外的输出,我最终使用了awk:
if [[ -n $(svn status -q . | awk ' ~ /[!?ABCDGKLMORST]/') ]]; then
echo "The working copy at $(pwd) appears to have local modifications"
fi
This script takes the output of svn status -qand 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.
此脚本获取输出svn status -q并过滤掉所有不以指示本地更改的状态代码开头的行。如果最终结果是空字符串,则工作副本是干净的。
回答by DrewM
Or you could try
或者你可以试试
svn status | grep [AMCDG]
echo $?
should return 0 if there are changes and 1 if there are none
如果有变化应该返回 0 如果没有变化应该返回 1
回答by Bozhidar Batsov
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 perlto help you with this. Based on the result of that parsing you'll certainly know if there are changes or not.
不久前我已经实现了类似的东西。您不应依赖 的返回值svn status,而应解析其输出。例如,您应该查找以“M”、“A”、“D”等开头的行。您可以使用perl来帮助您。根据解析的结果,您肯定会知道是否有更改。
Btw it's not normal for svn statusto return 0 if there are no changes - after all this return code simply signifies that no errors occurred.
顺便说一句,svn status如果没有更改,返回 0是不正常的- 毕竟这个返回代码只是表示没有发生错误。
回答by Zakri
You can test the revision number with svnversions from kde.org http://websvn.kde.org/trunk/KDE/kdesdk/scripts/svnversions?view=log
您可以使用来自 kde.org http://websvn.kde.org/trunk/KDE/kdesdk/scripts/svnversions?view=log 的svnversions 测试修订号
回答by slickmb
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).
这是我使用过的解决方案。与 svn externals 一起工作并避免在脚本中显式列出 svn 状态(这可能是也可能不是一件好事,这取决于您的观点)。
# 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.
svn st -q- 列出外部目录不带 X 的状态。cut -c -8- 删除除前 8 列之外的所有列。sed '/^[^ ]*$/d'- 删除与外部工作时看到的“正在执行...”行。grep -m 1 '[^ ]'- 第一个非空格意味着修改。

