tags:

views:

56

answers:

3

What will be proper regular expression for git repositories?

example link: [email protected]:someone/someproject.git

so it will be like server can be url or ip Project can contain some other characters than alphanumeric like '-' I'm not sure what is the role of '/'

any suggestions?

+1  A: 

Roughly

^[^@]+@[^:]+:[^/]+/[^.]+\.git$
S.Mark
+1  A: 

Git accepts a large range of repository URL expressions:

* ssh://[email protected]:port/path/to/repo.git/
* ssh://[email protected]/path/to/repo.git/
* ssh://host.xz:port/path/to/repo.git/
* ssh://host.xz/path/to/repo.git/
* ssh://[email protected]/path/to/repo.git/
* ssh://host.xz/path/to/repo.git/
* ssh://[email protected]/~user/path/to/repo.git/
* ssh://host.xz/~user/path/to/repo.git/
* ssh://[email protected]/~/path/to/repo.git
* ssh://host.xz/~/path/to/repo.git
* [email protected]:/path/to/repo.git/
* host.xz:/path/to/repo.git/
* [email protected]:~user/path/to/repo.git/
* host.xz:~user/path/to/repo.git/
* [email protected]:path/to/repo.git
* host.xz:path/to/repo.git
* rsync://host.xz/path/to/repo.git/
* git://host.xz/path/to/repo.git/
* git://host.xz/~user/path/to/repo.git/
* http://host.xz/path/to/repo.git/
* https://host.xz/path/to/repo.git/
* /path/to/repo.git/
* path/to/repo.git/
* ~/path/to/repo.git
* file:///path/to/repo.git/
* file://~/path/to/repo.git/

For an application that I wrote that requires parsing of these expressions (YonderGit), I came up with the following (Python) regular expressions:

    (1) '(\w+://)(.+@)*([\w\d\.]+)(:[\d]+){0,1}/*(.*)'
    (2) 'file://(.*)'       
    (3) '(.+@)*([\w\d\.]+):(.*)'

For most repository URL's encountered "in the wild", I suspect (1) suffices.

Jeet
A: 

Git repositories can come in many shapes and sizes that look nothing like that example. See the git-clone man page for a full list.

Some of the more common ones include using the http or git protocols instead of SSH (or, indeed, manually specifying the ssh:// protocol). Usernames are optional, there doesn't have to be a / or a .git, ports may be specified, etc etc.

At the moment you're basically only allowing private Github repos, or ones which happen to look like them. Is that what you want? If so, S. Mark's answer looks good!

If you want to accept any git repository, the best bet is probably to make sure it's a valid URI, and then use git or a git library to make sure there is a real repo accessible at that URI.

Chris Smith