如何判断`hg push`失败(与“无变化”相反)

时间:2013-08-30 15:48:58

标签: bash mercurial

根据hg help push,它

  

如果推送成功则返回0,如果无法推送,则返回1.

现在,我没有胡子,但这似乎不像“Unixy”的方式。

例如

set -e
hg push -R ~/some-repo
# never get here if push aborts for any reason
# ...OR if some-repo has no changes
exit 0

我无法想象为什么我希望hg push以这种方式行事,特别是因为信息命令hg out返回完全相同的状态代码。

所以我的问题是,如何判断hg push何时失败?我是否必须阅读流输出?

(顺便提一下,2012年Janaruy的某人pointed out没有按照这种方式工作,他们修复了程序而不是文档。)

(我也知道set -e has issues。这不是那个。)

2 个答案:

答案 0 :(得分:2)

第一个例子:

read -a ERREXITSAVE < <(shopt -o -p errexit)
set +o errexit

hg push -R "$repo"
[[ $? == [01] ]] || exit 1

"${ERREXITSAVE[@]}"

第二个例子:

read -a ERREXITSAVE < <(shopt -o -p errexit)
read -a LASTPIPESAVE < <(shopt -o -p lastpipe)

set +o errexit
set -o lastpipe

... | ( hg push -R "$repo"; [[ $? == [01] ]]; ) | ... || exit 1

"${ERREXITSAVE[@]}"
"${LASTPIPESAVE[@]}"

答案 1 :(得分:1)

正如@iamnotmaynard在评论中指出的那样,hg push退出255以查找错误。所以你可以做这样的事情

set +e
hg push -R $repo
status=$?
set -e
if [[ ! "01" =~ $status ]]; then
    exit 1
fi

这对我来说仍然没有意义,但继续前进。