Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify valid format of URL as a git repo?

Tags:

git

I do NOT want to check if the remote repository exists. I just want to test a string and return true if the string is in the valid format for a git repo.

I'm writing a groovy script and wish to do a quick check if a string represents a valid possible git repo.

For instance if the following strings are entered the test should return true:

http://example.com/my-project.git
file:///absolute/path/to/my-project.git
ssh:[email protected]:my-project
my-project

The following strings should fail the test and cause false to be returned:

fil://example.com/my-project.git
ssh:user|example.com:my-project

I'm hoping there is a git command that can do this quick test for me and I can call git from the groovy script. I say this because I'd like to use whatever is compiled into git to do the test as opposed to re-implementing the regular expression (or parser) that already exists in git. If I try the latter then inevitably I'll miss something.

like image 955
Jason Avatar asked Jun 01 '14 01:06

Jason


People also ask

What is a valid GitHub URL?

Check if a passed string is a valid GitHub URL. Unlike is-git-url, is-github-url is a domain-specific validator. It returns true if passed URL is a part of github.com domain only.

How do I check my git URL?

" 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".

How do I add a URL to a git repository?

To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at. The git remote add command takes two arguments: A unique remote name, for example, “my_awesome_new_remote_repo” A remote URL, which you can find on the Source sub-tab of your Git repo.

How do I find the remote repo URL?

Getting The Remote URL For a Git Repository If you're unsure what the remote is called, simply run “ git remote ,” which will print all of them.


2 Answers

It does seem that there is no way to accomplish this with git. The solution I finally used is

git ls-remote the-url-to-test

This returns zero on success and non-zero otherwise. This doesn't satisfy my original question since I don't want to check if the repo exists and is valid... but it does satisfy that the URL is valid if the repo also exists.

This will have to do for my current script.

like image 112
Jason Avatar answered Oct 22 '22 00:10

Jason


This gets close to Using a regular expression to validate an email address, in that you'll be able to detect some errors but not all. Given that you'll have to cope with failure anyway (wrong hostnames, bad SSH credentials), you're basically putting a few heuristics in place to catch common errors. I don't believe there's any validation code you can borrow from Git for this, although the list of URL formats (Git URLs) should be helpful in implementing this yourself.

like image 22
Joe Avatar answered Oct 21 '22 22:10

Joe