mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.0 KiB
112 lines
3.0 KiB
6 years ago
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||
8 years ago
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
||
2 years ago
|
// SPDX-License-Identifier: MIT
|
||
8 years ago
|
|
||
|
package git
|
||
|
|
||
6 years ago
|
import (
|
||
|
"fmt"
|
||
|
"net"
|
||
|
"net/url"
|
||
5 years ago
|
"path"
|
||
6 years ago
|
"regexp"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
var scpSyntax = regexp.MustCompile(`^([a-zA-Z0-9_]+@)?([a-zA-Z0-9._-]+):(.*)$`)
|
||
8 years ago
|
|
||
1 day ago
|
// CommitSubModuleFile represents a file with submodule type.
|
||
|
type CommitSubModuleFile struct {
|
||
8 years ago
|
refURL string
|
||
|
refID string
|
||
|
}
|
||
|
|
||
1 day ago
|
// NewCommitSubModuleFile create a new submodule file
|
||
|
func NewCommitSubModuleFile(refURL, refID string) *CommitSubModuleFile {
|
||
|
return &CommitSubModuleFile{
|
||
8 years ago
|
refURL: refURL,
|
||
|
refID: refID,
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
func getRefURL(refURL, urlPrefix, repoFullName, sshDomain string) string {
|
||
6 years ago
|
if refURL == "" {
|
||
8 years ago
|
return ""
|
||
|
}
|
||
|
|
||
6 years ago
|
refURI := strings.TrimSuffix(refURL, ".git")
|
||
8 years ago
|
|
||
6 years ago
|
prefixURL, _ := url.Parse(urlPrefix)
|
||
|
urlPrefixHostname, _, err := net.SplitHostPort(prefixURL.Host)
|
||
|
if err != nil {
|
||
|
urlPrefixHostname = prefixURL.Host
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
urlPrefix = strings.TrimSuffix(urlPrefix, "/")
|
||
5 years ago
|
|
||
5 years ago
|
// FIXME: Need to consider branch - which will require changes in modules/git/commit.go:GetSubModules
|
||
8 years ago
|
// Relative url prefix check (according to git submodule documentation)
|
||
6 years ago
|
if strings.HasPrefix(refURI, "./") || strings.HasPrefix(refURI, "../") {
|
||
5 years ago
|
return urlPrefix + path.Clean(path.Join("/", repoFullName, refURI))
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
if !strings.Contains(refURI, "://") {
|
||
|
// scp style syntax which contains *no* port number after the : (and is not parsed by net/url)
|
||
|
// ex: git@try.gitea.io:go-gitea/gitea
|
||
|
match := scpSyntax.FindAllStringSubmatch(refURI, -1)
|
||
|
if len(match) > 0 {
|
||
|
m := match[0]
|
||
|
refHostname := m[2]
|
||
5 years ago
|
pth := m[3]
|
||
6 years ago
|
|
||
5 years ago
|
if !strings.HasPrefix(pth, "/") {
|
||
|
pth = "/" + pth
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
if urlPrefixHostname == refHostname || refHostname == sshDomain {
|
||
5 years ago
|
return urlPrefix + path.Clean(path.Join("/", pth))
|
||
6 years ago
|
}
|
||
5 years ago
|
return "http://" + refHostname + pth
|
||
8 years ago
|
}
|
||
6 years ago
|
}
|
||
|
|
||
|
ref, err := url.Parse(refURI)
|
||
|
if err != nil {
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
refHostname, _, err := net.SplitHostPort(ref.Host)
|
||
|
if err != nil {
|
||
|
refHostname = ref.Host
|
||
|
}
|
||
|
|
||
|
supportedSchemes := []string{"http", "https", "git", "ssh", "git+ssh"}
|
||
|
|
||
|
for _, scheme := range supportedSchemes {
|
||
|
if ref.Scheme == scheme {
|
||
4 years ago
|
if ref.Scheme == "http" || ref.Scheme == "https" {
|
||
6 years ago
|
if len(ref.User.Username()) > 0 {
|
||
|
return ref.Scheme + "://" + fmt.Sprintf("%v", ref.User) + "@" + ref.Host + ref.Path
|
||
|
}
|
||
|
return ref.Scheme + "://" + ref.Host + ref.Path
|
||
4 years ago
|
} else if urlPrefixHostname == refHostname || refHostname == sshDomain {
|
||
4 years ago
|
return urlPrefix + path.Clean(path.Join("/", ref.Path))
|
||
6 years ago
|
}
|
||
1 year ago
|
return "http://" + refHostname + ref.Path
|
||
6 years ago
|
}
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
return ""
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
// RefURL guesses and returns reference URL.
|
||
1 day ago
|
// FIXME: template passes AppURL as urlPrefix, it needs to figure out the correct approach (no hard-coded AppURL anymore)
|
||
|
func (sf *CommitSubModuleFile) RefURL(urlPrefix, repoFullName, sshDomain string) string {
|
||
4 years ago
|
return getRefURL(sf.refURL, urlPrefix, repoFullName, sshDomain)
|
||
6 years ago
|
}
|
||
|
|
||
8 years ago
|
// RefID returns reference ID.
|
||
1 day ago
|
func (sf *CommitSubModuleFile) RefID() string {
|
||
8 years ago
|
return sf.refID
|
||
|
}
|