60

Within a bash script, what would be the simplest way to verify that a git URL points to a valid git repo and that the script has access to read from it?

Protocols that should be supported are git@, https://, and git://. Curl fails on the git:// protocol.

git@github.com:UserName/Example.git
https://UserName@github.com/UserName/Example.git
git://github.com/UserName/Example.git

Note: I'm not asking to check to see if a URL is syntactically correct, I need to verify that a repo exists at the URL location entered from within a bash script.

nulltoken
  • 64,429
  • 20
  • 138
  • 130
Highway of Life
  • 22,803
  • 16
  • 52
  • 80

2 Answers2

55

As seen in this issue, you can use git ls-remote to test your address.

If you need to debug the git calls set GIT_TRACE=1. eg:

env GIT_PROXY_COMMAND=myproxy.sh GIT_TRACE=1 git ls-remote https://...

"git ls-remote" is the quickest way I know to test communications with a remote repository without actually cloning it. Hence its utility as a test for this issue.

You can see it used for detecting an address issue in "git ls-remote returns 128 on any repo".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The Google Code link is broken, msysgit and its issue tracker moved to GitHub. I didn't find the former issue on GitHub, maybe because I'm not sure what I should be searching for. – Attila Csipak Jul 23 '20 at 09:25
  • 1
    @AttilaCsipak Thank you. I have resotred or updated the missing links – VonC Jul 23 '20 at 11:02
53

Although VonC's answer is correct, here's what I ended up using:

git ls-remote will return information about a repository, by default this is HEAD, all branches and tags, along with the commit ID for each entry. e.g.:

$ git ls-remote git://github.com/user/repo.git
<commit id>    HEAD
<commit id>    refs/heads/example_branch
<commit id>    refs/heads/master
<commit id>    refs/tags/v1.0.2
<commit id>    refs/tags/v1.0.0

git ls-remote returns code 0 on success, error code 128 on failure.

If the repo is unreachable, for example, if you don't have permission to view the repository, or if a repository doesn't exist at that location, git ls-remote will return:

fatal: The remote end hung up unexpectedly

To use this in a bash script, the following will work...

git ls-remote "$SITE_REPO_URL" > /dev/null 2>&1
if [ "$?" -ne 0 ]; then
    echo "[ERROR] Unable to read from '$SITE_REPO_URL'"
    exit 1;
fi

(Note: The > /dev/null 2>&1 silences stderr and stdout, so the command won't output anything)

Highway of Life
  • 22,803
  • 16
  • 52
  • 80
  • 7
    I'd advise against using `&>-` for portable scripts, as it appears to be a bash extension. In other shells, this syntax won't work as expected. Although there is a similar syntax (`>&- 2>&-`), the functionality is not equivalent -- it will close the file descriptors instead of silently redirecting them, which causes errors in many programs. You can use `> /dev/null 2>&1` instead. – alexia Oct 27 '13 at 15:34
  • I added GIT_ASKPASS=true to prevent getting prompted for Username/password on bad url's. – codeDr Aug 04 '15 at 21:44