GIT的ls-remote测试开关总是返回false

时间:2014-09-02 21:41:54

标签: git bash

由于某些原因,下面的bash开关总是会返回false /或者,我正在寻找一种方法来抑制git的响应,当存储库不存在但无法获得这工作吗?

[ git ls-remote https://github.com/ehime/Bash-Tools.git &>- /dev/null ] && { 
    echo Exists 
} || { 
    echo Nope 
}

我试过这个并且它确实有效,但不会像我想要的那样压制内容

[ "$(git ls-remote foo)" ] && { echo Yes; } || { echo No; }

2 个答案:

答案 0 :(得分:1)

您没有在该测试中运行git,您正在测试一系列字符串(这几乎肯定会导致[输出错误消息)。

如果您尝试运行该命令而忽略所有输出,并仅测试要删除包围[]的返回码。它们不是if语法的一部分。 [是二进制文件(与test同义)。

if git ls-remote ...; then将运行git并测试其返回码。

此外,/dev/null没有为您做任何事情。 $>-已经关闭标准输入和标准输出。 /dev/null git ls-remote有“ref”参数。

答案 1 :(得分:0)

我在寻找的是

[ "$(git ls-remote $REPOSITORY_URL 2> /dev/null)" ] && { echo Exists; } || { echo Nope; }

如果有人想要限制git shell的完整git命令

# If no project name is given
if [ $# -eq 0 ]; then
    # Display usage and stop
    echo "Usage: addrepo <project.git>" ; exit 1
fi

# Set the project name, adding .git if necessary
project=$(echo "$*" | sed 's/\.git$\|$/.git/i')

[ "$(git ls-remote $project 2> /dev/null)" ] && {
    cd /home/git/repositories/
    git clone --mirror "${project}"
} || {
    echo "[ERROR] Unable to read from '${project}'"; exit 1
}
相关问题