add submodule diff links (#33097)
This adds links to submodules in diffs, similar to the existing link when viewing a repo at a specific commit. It does this by expanding diff parsing to recognize changes to submodules, and find the specific refs that are added, deleted or changed. Related #25888 --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
parent
ec84687df9
commit
a8e7caedfa
23 changed files with 688 additions and 339 deletions
|
@ -784,60 +784,10 @@ func GetRepositoryByName(ctx context.Context, ownerID int64, name string) (*Repo
|
|||
return &repo, err
|
||||
}
|
||||
|
||||
func parseRepositoryURL(ctx context.Context, repoURL string) (ret struct {
|
||||
OwnerName, RepoName, RemainingPath string
|
||||
},
|
||||
) {
|
||||
// possible urls for git:
|
||||
// https://my.domain/sub-path/<owner>/<repo>[.git]
|
||||
// git+ssh://user@my.domain/<owner>/<repo>[.git]
|
||||
// ssh://user@my.domain/<owner>/<repo>[.git]
|
||||
// user@my.domain:<owner>/<repo>[.git]
|
||||
|
||||
fillPathParts := func(s string) {
|
||||
s = strings.TrimPrefix(s, "/")
|
||||
fields := strings.SplitN(s, "/", 3)
|
||||
if len(fields) >= 2 {
|
||||
ret.OwnerName = fields[0]
|
||||
ret.RepoName = strings.TrimSuffix(fields[1], ".git")
|
||||
if len(fields) == 3 {
|
||||
ret.RemainingPath = "/" + fields[2]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
parsed, err := giturl.ParseGitURL(repoURL)
|
||||
if err != nil {
|
||||
return ret
|
||||
}
|
||||
if parsed.URL.Scheme == "http" || parsed.URL.Scheme == "https" {
|
||||
if !httplib.IsCurrentGiteaSiteURL(ctx, repoURL) {
|
||||
return ret
|
||||
}
|
||||
fillPathParts(strings.TrimPrefix(parsed.URL.Path, setting.AppSubURL))
|
||||
} else if parsed.URL.Scheme == "ssh" || parsed.URL.Scheme == "git+ssh" {
|
||||
domainSSH := setting.SSH.Domain
|
||||
domainCur := httplib.GuessCurrentHostDomain(ctx)
|
||||
urlDomain, _, _ := net.SplitHostPort(parsed.URL.Host)
|
||||
urlDomain = util.IfZero(urlDomain, parsed.URL.Host)
|
||||
if urlDomain == "" {
|
||||
return ret
|
||||
}
|
||||
// check whether URL domain is the App domain
|
||||
domainMatches := domainSSH == urlDomain
|
||||
// check whether URL domain is current domain from context
|
||||
domainMatches = domainMatches || (domainCur != "" && domainCur == urlDomain)
|
||||
if domainMatches {
|
||||
fillPathParts(parsed.URL.Path)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// GetRepositoryByURL returns the repository by given url
|
||||
func GetRepositoryByURL(ctx context.Context, repoURL string) (*Repository, error) {
|
||||
ret := parseRepositoryURL(ctx, repoURL)
|
||||
if ret.OwnerName == "" {
|
||||
ret, err := giturl.ParseRepositoryURL(ctx, repoURL)
|
||||
if err != nil || ret.OwnerName == "" {
|
||||
return nil, fmt.Errorf("unknown or malformed repository URL")
|
||||
}
|
||||
return GetRepositoryByOwnerAndName(ctx, ret.OwnerName, ret.RepoName)
|
||||
|
|
|
@ -4,16 +4,12 @@
|
|||
package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/models/unit"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/httplib"
|
||||
"code.gitea.io/gitea/modules/markup"
|
||||
"code.gitea.io/gitea/modules/optional"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
@ -132,79 +128,6 @@ func TestMetas(t *testing.T) {
|
|||
assert.Equal(t, ",owners,team1,", metas["teams"])
|
||||
}
|
||||
|
||||
func TestParseRepositoryURLPathSegments(t *testing.T) {
|
||||
defer test.MockVariableValue(&setting.AppURL, "https://localhost:3000")()
|
||||
|
||||
ctxURL, _ := url.Parse("https://gitea")
|
||||
ctxReq := &http.Request{URL: ctxURL, Header: http.Header{}}
|
||||
ctxReq.Host = ctxURL.Host
|
||||
ctxReq.Header.Add("X-Forwarded-Proto", ctxURL.Scheme)
|
||||
ctx := context.WithValue(context.Background(), httplib.RequestContextKey, ctxReq)
|
||||
cases := []struct {
|
||||
input string
|
||||
ownerName, repoName, remaining string
|
||||
}{
|
||||
{input: "/user/repo"},
|
||||
|
||||
{input: "https://localhost:3000/user/repo", ownerName: "user", repoName: "repo"},
|
||||
{input: "https://external:3000/user/repo"},
|
||||
|
||||
{input: "https://localhost:3000/user/repo.git/other", ownerName: "user", repoName: "repo", remaining: "/other"},
|
||||
|
||||
{input: "https://gitea/user/repo", ownerName: "user", repoName: "repo"},
|
||||
{input: "https://gitea:3333/user/repo"},
|
||||
|
||||
{input: "ssh://try.gitea.io:2222/user/repo", ownerName: "user", repoName: "repo"},
|
||||
{input: "ssh://external:2222/user/repo"},
|
||||
|
||||
{input: "git+ssh://user@try.gitea.io/user/repo.git", ownerName: "user", repoName: "repo"},
|
||||
{input: "git+ssh://user@external/user/repo.git"},
|
||||
|
||||
{input: "root@try.gitea.io:user/repo.git", ownerName: "user", repoName: "repo"},
|
||||
{input: "root@gitea:user/repo.git", ownerName: "user", repoName: "repo"},
|
||||
{input: "root@external:user/repo.git"},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.input, func(t *testing.T) {
|
||||
ret := parseRepositoryURL(ctx, c.input)
|
||||
assert.Equal(t, c.ownerName, ret.OwnerName)
|
||||
assert.Equal(t, c.repoName, ret.RepoName)
|
||||
assert.Equal(t, c.remaining, ret.RemainingPath)
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("WithSubpath", func(t *testing.T) {
|
||||
defer test.MockVariableValue(&setting.AppURL, "https://localhost:3000/subpath")()
|
||||
defer test.MockVariableValue(&setting.AppSubURL, "/subpath")()
|
||||
cases = []struct {
|
||||
input string
|
||||
ownerName, repoName, remaining string
|
||||
}{
|
||||
{input: "https://localhost:3000/user/repo"},
|
||||
{input: "https://localhost:3000/subpath/user/repo.git/other", ownerName: "user", repoName: "repo", remaining: "/other"},
|
||||
|
||||
{input: "ssh://try.gitea.io:2222/user/repo", ownerName: "user", repoName: "repo"},
|
||||
{input: "ssh://external:2222/user/repo"},
|
||||
|
||||
{input: "git+ssh://user@try.gitea.io/user/repo.git", ownerName: "user", repoName: "repo"},
|
||||
{input: "git+ssh://user@external/user/repo.git"},
|
||||
|
||||
{input: "root@try.gitea.io:user/repo.git", ownerName: "user", repoName: "repo"},
|
||||
{input: "root@external:user/repo.git"},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.input, func(t *testing.T) {
|
||||
ret := parseRepositoryURL(ctx, c.input)
|
||||
assert.Equal(t, c.ownerName, ret.OwnerName)
|
||||
assert.Equal(t, c.repoName, ret.RepoName)
|
||||
assert.Equal(t, c.remaining, ret.RemainingPath)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetRepositoryByURL(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue