【发布时间】:2013-07-21 08:07:15
【问题描述】:
我正在为一个 zend 应用程序创建一个部署脚本。脚本几乎完成了,只是我想验证存储库中是否存在标签以强制团队添加标签。目前我有以下代码:
# First update the repo to make sure all the tags are in
cd /git/repo/path
git pull
# Check if the tag exists in the rev-list.
# If it exists output should be zero,
# else an error will be shown which will go to the else statement.
if [ -z "'cd /git/repo/path && git rev-list $1..'" ]; then
echo "gogo"
else
echo "No or no correct GIT tag found"
exit
fi
期待您的反馈!
更新
当我在命令行中执行以下操作时:
cd /git/repo/path && git rev-list v1.4..
我得到 NO 输出,这很好。虽然当我执行时:
cd /git/repo/path && git rev-list **BLA**..
我得到一个错误,这又是好事:
fatal: ambiguous argument 'BLA..': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions
语句中的 -z 表示,如果 sting 为空,则...换句话说,它可以通过命令行正常工作。虽然当我在语句中的 shell 脚本中使用相同的命令时,它似乎不起作用。
[ -z "'cd /git/repo/path && git rev-list $1..'" ]
这个方法灵感来自Validate if commit exists
更新 2
我发现了问题:
见Using if elif fi in shell scripts >
sh 将 && 解释为 shell 运算符。改成-a,就是 [的合取运算符:
[ "$arg1" = "$arg2" -a "$arg1" != "$arg3" ] 另外,您应该始终 引用变量,因为 [ 在您离开时会感到困惑 论据。
换句话说,我将&& 更改为; 并简化了条件。现在效果很好。
if cd /path/to/repo ; git rev-list $1.. >/dev/null
then
echo "gogo"
else
echo "WRONG"
exit
fi
【问题讨论】:
-
$sha变量是否在别处定义? -
另请注意,
git pull在更新引用后会进行合并,您需要改用git fetch -
好吧,让我再解释一下,请参阅说明中的更新。
-
我建议
git rev-parse --verify refs/tags/${tagname}可能是更好的方法来做到这一点......(至少似乎是主要的git在内部这样做......)。