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.
As seen in this issue, you can use git ls-remote to test your address.
You can see it used for detecting an address issue in "
git ls-remote
returns 128 on any repo".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
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:To use this in a bash script, the following will work...
(Note: The
&>-
silences stderr and stdout, so the command won't output anything)