ehime
ehime

Reputation: 8375

GIT's ls-remote test switch always returning false

For some reason my bash switch below will always return false/or, I was looking for a way to suppress the response from git which will as for a User/Pass sequence when the repository doesn't exist, but could not get this working?

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

I tried this and it does work, but will not supress content like I wanted

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

Upvotes: 0

Views: 992

Answers (2)

ehime
ehime

Reputation: 8375

What I was looking for was

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

Full git command for a restricted git shell if anyone wants it

# 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
}

Upvotes: 0

Etan Reisner
Etan Reisner

Reputation: 80921

You aren't running git in that test you are giving test a series of strings (which is almost certainly causing [ to output an error message).

If you are trying to run that command ignoring all output and test only the return code you want to drop the bracketing [ and ]. They aren't part of the if syntax. [ is a binary (synonymous with test).

if git ls-remote ...; then will run git and test its return code.

Additionally, the /dev/null there is not doing anything for you. $>- is closing standard input and standard output already. /dev/null there is the "ref" argument to git ls-remote.

Upvotes: 1

Related Questions