如何抓住一个;致命index.lock:文件存在";在shell脚本中调用git签出时出错?
shell脚本正在为Flyway delta版本创建文件,该版本将新版本的所有更改应用到Oracle数据库中如何抓住一个;致命index.lock:文件存在";在shell脚本中调用git签出时出错?,git,shell,Git,Shell,shell脚本正在为Flyway delta版本创建文件,该版本将新版本的所有更改应用到Oracle数据库中 “git checkout”包含所有新更改的新发布标签 检查“git checkout”调用的返回代码是否正确 检索在版本之间更改的所有文件的列表 已更改的文件基本上被复制到新目录,并接收Flyway2生成的前缀 稍后,这些文件可以轻松应用于Flyway 最近发生的问题:步骤1可能由于“致命:无法创建“…/.git/index.lock”:文件存在”错误而失败(很可能是由于SourceT
GIT="Path to Git.exe"
INDEXLOCK="Path to potential .git/index.lock"
#Unfortunately git checkout returns 0 (everything is ok), when it failed due to an existing index.lock
#The script would then commence, although the sources were not checkout correctly!
#Loop until index.lock does not exist anymore
while [ -f "$INDEXLOCK" ]
do
echo "Index.lock exists! Waiting until open git process is finished."
sleep 5
done
echo " "
echo "Trying to check out tag: $2"
$GIT checkout "$2" -q
if [ $? = 1 ]; then
echo "Error: Can't checkout tag: $2"
exit;
fi
echo "Check out finished"
是否有可能确保shell脚本停止在“.git/index.lock”错误上?捕获标准输出成功。我可以通过在目录中放置index.lock来验证功能
GIT="Path to Git.exe"
INDEXLOCK="Path to potential .git/index.lock"
#Unfortunately git checkout returns 0 (everything is ok), when it failed due to an existing index.lock
#The script would then commence, although the sources were not checkout correctly!
#Loop until index.lock does not exist anymore
while [ -f "$INDEXLOCK" ]
do
echo "Index.lock exists! Waiting until open git process is finished."
sleep 5
done
echo " "
echo "Trying to check out tag: $2"
OUTPUT=$($GIT checkout "$2" -q 2>&1)
RETURNCODE=$?
if [[ $OUTPUT == *"fatal: Unable to create"*".git/index.lock': File exists."* ]];
then
echo "Error: Can't checkout release tag '$2' due to index.lock"
exit;
elif [ $RETURNCODE = 1 ]; then
echo "Error: Can't checkout tag: $2"
exit;
fi
echo " "
echo "Check out attempt finished"
另一方面,
GIT=“Path to GIT.exe”
然后再使用$GIT
不是好的做法。除此之外,如果您的路径包含空格,它将失败。更好的方法是设置PATH=/PATH/to/directory/containing/git/:$PATH
,或者如果您真的必须使事情显式化,请使用一个函数:git(){/PATH/to/directory/containing/git“$@”}
——然后在下面您可以运行git checkout
之类的程序,您所冒的风险是,在检查它和实际启动git之间会创建锁。如果git命令失败,包装器最好实际重试它(当然重试次数有限),而不是试图提前预测失败;最好让您的if
分支处于退出状态:if git checkout“$@”-q;然后回显“签出完成”>&2;else-retval=$?;echo“错误:无法签出标记:$2”>&2;退出“$retval”;fi
——请注意需要显式存储retval
,否则您的exit
将以前面的echo
的成功退出状态退出,而不是之前的git
命令的退出状态……如果git的退出状态没有反映锁,然后,回退是捕获其stdout和/或stderr并在其上进行模式匹配。我们在知识库中已经有了描述如何做到这一点的Q&A条目(对于任何命令,而不仅仅是git)。例如,你没有犯与OP相同的错误,但同样的答案是适用的。