2010-03-25 528 views
13

git倉庫的正則表達式是什麼?git倉庫的正則表達式

例如鏈接: [email protected]:有人/ someproject.git

所以它會像 [用戶] @ [服務器]:[項目] git的

服務器,可以將網址或IP 項目可以包含一些其他字符比字母數字像' - ' 我不確定'/'的作用是什麼

有什麼建議嗎?

+0

我不相信一個正則表達式解析此語法的最佳解決方案,雖然很多人會不同意。請參閱https://blog.codinghorror。com/regular-expressions-now-you-have-two-problems/ – 2016-07-12 16:00:22

回答

11

的Git接受大範圍的資源庫URL的表達式:

* 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/ 

對於我寫的,需要這些表達式(YonderGit)的分析應用程序,我想出了以下(Python)正則表達式:

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

對於大多數人在野外遇到的pository網址,我懷疑(1)就足夠了。

+1

這不適用於像[email protected]這樣的網址:group/project.git – Anonymous 2014-10-29 11:07:20

2

大約

^[^@][email protected][^:]+:[^/]+/[^.]+\.git$ 
+0

這不處理Gerrit的git uri。 – 2017-02-07 07:34:49

0

Git存儲庫可能有許多形狀和大小,看起來沒有這樣的例子。請參閱git-clone手冊頁獲取完整列表。

一些比較常見的包括使用httpgit協議代替SSH(或者實際上,手動指定ssh://協議)。用戶名是可選的,不一定是/.git,可指定端口等。

目前,您基本上只允許私人Github回購站,或者看起來像他們的那些。那是你要的嗎?如果是這樣,S.馬克的答案看起來不錯!

如果你想接受任何git倉庫,最好的辦法可能是確保它是一個有效的URI,然後使用git或git庫來確保在該URI可以訪問真正的倉庫。

30

我使用網上遠程倉庫以下正則表達式:

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

View on Debuggex

Regular expression visualization

+5

我來這裏尋找正則表達式語句,所以我不必花時間在上面,我發現了最好的正則表達式調試器製作。謝謝。 – blockloop 2016-03-14 22:09:41

+0

我同意Debuggex。不幸的是,上述表達式中的第4組除了「git」之外不適用於其他用戶。 – 2016-07-12 15:57:53

+0

稍微調整一下javascript的用法:/((git|ssh|http(s)?)|([email protected][\w.]+))(:(\/\/)?)([\[email protected]: /\-~]+)(\.git)(\/)?/ – Alexee 2018-01-02 03:41:15

相關問題