开发者

Regular expression for git repository

What will be proper regular expression for git repositories?

example link: git@github.com:someone/someproject.git

so it will be like [user]@[server]:[project].git

server can be url or ip Project can contain some other characters than alphanumeric like '-' I'm not sure what is the r开发者_JAVA技巧ole of '/'

any suggestions?


I'm using the following regular expression for online remote repositories:

((git|ssh|http(s)?)|(git@[\w\.]+))(:(//)?)([\w\.@\:/\-~]+)(\.git)(/)?

View on Debuggex

Regular expression for git repository


Git accepts a large range of repository URL expressions:

* ssh://user@host.xz:port/path/to/repo.git/
* ssh://user@host.xz/path/to/repo.git/
* ssh://host.xz:port/path/to/repo.git/
* ssh://host.xz/path/to/repo.git/
* ssh://user@host.xz/path/to/repo.git/
* ssh://host.xz/path/to/repo.git/
* ssh://user@host.xz/~user/path/to/repo.git/
* ssh://host.xz/~user/path/to/repo.git/
* ssh://user@host.xz/~/path/to/repo.git
* ssh://host.xz/~/path/to/repo.git
* user@host.xz:/path/to/repo.git/
* host.xz:/path/to/repo.git/
* user@host.xz:~user/path/to/repo.git/
* host.xz:~user/path/to/repo.git/
* user@host.xz: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.


FYI I make a regex for get owner and repo from github or bitbucket:

(?P<host>(git@|https://)([\w\.@]+)(/|:))(?P<owner>[\w,\-,\_]+)/(?P<repo>[\w,\-,\_]+)(.git){0,1}((/){0,1})

Debuggex Demo


Roughly

^[^@]+@[^:]+:[^/]+/[^.]+\.git$


((git@|http(s)?:\/\/)([\w\.@]+)(\/|:))([\w,\-,\_]+)\/([\w,\-,\_]+)(.git){0,1}((\/){0,1})

This will also give you user and repo in a seperate group.


In bash, you can do this without regex:

basename https://github.com/code-co-ua/exercises-php

Output:

exercises-php


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.


Try this regex:

/^([A-Za-z0-9]+@|http(|s)\:\/\/)([A-Za-z0-9.]+(:\d+)?)(?::|\/)([\d\/\w.-]+?)(\.git)?$/i

It works fine for me.


export const REGEXP_GIT_REPO_URI = new RegExp(/^(((https?\:\/\/)(((([a-zA-Z0-9][a-zA-Z0-9\-\_]{1,252})\.){1,8}[a-zA-Z]{2,63})\/))|((ssh\:\/\/)?git\@)(((([a-zA-Z0-9][a-zA-Z0-9\-\_]{1,252})\.){1,8}[a-zA-Z]{2,63})(\:)))([a-zA-Z0-9][a-zA-Z0-9\_\-]{1,36})(\/)([a-zA-Z0-9][a-zA-Z0-9\_\-]{1,36})((\.git)?)$/);

The only problem is not support IP address.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜