-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
1 parent
ec84687
commit a8e7cae
Showing
23 changed files
with
690 additions
and
341 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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://[email protected]/<owner>/<repo>[.git] | ||
// ssh://[email protected]/<owner>/<repo>[.git] | ||
// [email protected]:<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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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://[email protected]/user/repo.git", ownerName: "user", repoName: "repo"}, | ||
{input: "git+ssh://user@external/user/repo.git"}, | ||
|
||
{input: "[email protected]: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://[email protected]/user/repo.git", ownerName: "user", repoName: "repo"}, | ||
{input: "git+ssh://user@external/user/repo.git"}, | ||
|
||
{input: "[email protected]: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()) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.