Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何抓住一个;致命index.lock:文件存在";在shell脚本中调用git签出时出错?_Git_Shell - Fatal编程技术网

如何抓住一个;致命index.lock:文件存在";在shell脚本中调用git签出时出错?

如何抓住一个;致命index.lock:文件存在";在shell脚本中调用git签出时出错?,git,shell,Git,Shell,shell脚本正在为Flyway delta版本创建文件,该版本将新版本的所有更改应用到Oracle数据库中 “git checkout”包含所有新更改的新发布标签 检查“git checkout”调用的返回代码是否正确 检索在版本之间更改的所有文件的列表 已更改的文件基本上被复制到新目录,并接收Flyway2生成的前缀 稍后,这些文件可以轻松应用于Flyway 最近发生的问题:步骤1可能由于“致命:无法创建“…/.git/index.lock”:文件存在”错误而失败(很可能是由于SourceT

shell脚本正在为Flyway delta版本创建文件,该版本将新版本的所有更改应用到Oracle数据库中

  • “git checkout”包含所有新更改的新发布标签
  • 检查“git checkout”调用的返回代码是否正确
  • 检索在版本之间更改的所有文件的列表
  • 已更改的文件基本上被复制到新目录,并接收Flyway2生成的前缀
  • 稍后,这些文件可以轻松应用于Flyway

    最近发生的问题:步骤1可能由于“致命:无法创建“…/.git/index.lock”:文件存在”错误而失败(很可能是由于SourceTree的状态更新)

    不幸的是,在本例中git的returncode为0(表示没有错误),这意味着脚本没有注意到文件在步骤2中被更新,并继续执行步骤3和4(但使用了错误的文件)

    在这个例子中,我试图通过等待“index.lock”被删除来避免这个问题,但是这不是故障安全的!在while循环通过之后,可能会创建index.lock,但在git签出之前,我和我又遇到了同样的问题

    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相同的错误,但同样的答案是适用的。