Merge pull request #257 from motemen/vcs-detection

detect VCS backend from URL scheme
This commit is contained in:
Masayuki Matsuki 2020-01-02 02:52:05 +09:00 committed by GitHub
commit 8fcf23784b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View file

@ -3,6 +3,7 @@ package main
import (
"fmt"
"net/url"
"regexp"
"strings"
"github.com/Songmu/gitconfig"
@ -113,6 +114,15 @@ func (repo *OtherRepository) IsValid() bool {
return true
}
var (
vcsSchemeReg = regexp.MustCompile(`^(git|svn|bzr)(?:\+|$)`)
scheme2vcs = map[string]*VCSBackend{
"git": GitBackend,
"svn": SubversionBackend,
"bzr": BazaarBackend,
}
)
// VCS detects VCSBackend of the OtherRepository
func (repo *OtherRepository) VCS() (*VCSBackend, *url.URL) {
// Respect 'ghq.url.https://ghe.example.com/.vcs' config variable
@ -127,6 +137,10 @@ func (repo *OtherRepository) VCS() (*VCSBackend, *url.URL) {
return backend, repo.URL()
}
if m := vcsSchemeReg.FindStringSubmatch(repo.url.Scheme); len(m) > 1 {
return scheme2vcs[m[1]], repo.URL()
}
mayBeSvn := strings.HasPrefix(repo.url.Host, "svn.")
if mayBeSvn && cmdutil.RunSilently("svn", "info", repo.url.String()) == nil {
return SubversionBackend, repo.URL()

View file

@ -35,6 +35,10 @@ func TestNewRemoteRepository(t *testing.T) {
url: "http://hub.darcs.net/foo/bar",
valid: true,
vcsBackend: DarcsBackend,
}, {
url: "svn+ssh://example.com/proj/repo",
valid: true,
vcsBackend: SubversionBackend,
}}
for _, tc := range testCases {