Skip to content

Commit 68a8488

Browse files
committed
refactor
1 parent a31e6c5 commit 68a8488

16 files changed

+299
-325
lines changed

models/repo/repo.go

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -784,60 +784,10 @@ func GetRepositoryByName(ctx context.Context, ownerID int64, name string) (*Repo
784784
return &repo, err
785785
}
786786

787-
func parseRepositoryURL(ctx context.Context, repoURL string) (ret struct {
788-
OwnerName, RepoName, RemainingPath string
789-
},
790-
) {
791-
// possible urls for git:
792-
// https://my.domain/sub-path/<owner>/<repo>[.git]
793-
// git+ssh://[email protected]/<owner>/<repo>[.git]
794-
// ssh://[email protected]/<owner>/<repo>[.git]
795-
// [email protected]:<owner>/<repo>[.git]
796-
797-
fillPathParts := func(s string) {
798-
s = strings.TrimPrefix(s, "/")
799-
fields := strings.SplitN(s, "/", 3)
800-
if len(fields) >= 2 {
801-
ret.OwnerName = fields[0]
802-
ret.RepoName = strings.TrimSuffix(fields[1], ".git")
803-
if len(fields) == 3 {
804-
ret.RemainingPath = "/" + fields[2]
805-
}
806-
}
807-
}
808-
809-
parsed, err := giturl.ParseGitURL(repoURL)
810-
if err != nil {
811-
return ret
812-
}
813-
if parsed.URL.Scheme == "http" || parsed.URL.Scheme == "https" {
814-
if !httplib.IsCurrentGiteaSiteURL(ctx, repoURL) {
815-
return ret
816-
}
817-
fillPathParts(strings.TrimPrefix(parsed.URL.Path, setting.AppSubURL))
818-
} else if parsed.URL.Scheme == "ssh" || parsed.URL.Scheme == "git+ssh" {
819-
domainSSH := setting.SSH.Domain
820-
domainCur := httplib.GuessCurrentHostDomain(ctx)
821-
urlDomain, _, _ := net.SplitHostPort(parsed.URL.Host)
822-
urlDomain = util.IfZero(urlDomain, parsed.URL.Host)
823-
if urlDomain == "" {
824-
return ret
825-
}
826-
// check whether URL domain is the App domain
827-
domainMatches := domainSSH == urlDomain
828-
// check whether URL domain is current domain from context
829-
domainMatches = domainMatches || (domainCur != "" && domainCur == urlDomain)
830-
if domainMatches {
831-
fillPathParts(parsed.URL.Path)
832-
}
833-
}
834-
return ret
835-
}
836-
837787
// GetRepositoryByURL returns the repository by given url
838788
func GetRepositoryByURL(ctx context.Context, repoURL string) (*Repository, error) {
839-
ret := parseRepositoryURL(ctx, repoURL)
840-
if ret.OwnerName == "" {
789+
ret, err := giturl.ParseRepositoryURL(ctx, repoURL)
790+
if err != nil || ret.OwnerName == "" {
841791
return nil, fmt.Errorf("unknown or malformed repository URL")
842792
}
843793
return GetRepositoryByOwnerAndName(ctx, ret.OwnerName, ret.RepoName)

models/repo/repo_test.go

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@
44
package repo
55

66
import (
7-
"context"
8-
"net/http"
9-
"net/url"
107
"testing"
118

129
"code.gitea.io/gitea/models/db"
1310
"code.gitea.io/gitea/models/unit"
1411
"code.gitea.io/gitea/models/unittest"
1512
user_model "code.gitea.io/gitea/models/user"
16-
"code.gitea.io/gitea/modules/httplib"
1713
"code.gitea.io/gitea/modules/markup"
1814
"code.gitea.io/gitea/modules/optional"
1915
"code.gitea.io/gitea/modules/setting"
@@ -132,79 +128,6 @@ func TestMetas(t *testing.T) {
132128
assert.Equal(t, ",owners,team1,", metas["teams"])
133129
}
134130

135-
func TestParseRepositoryURLPathSegments(t *testing.T) {
136-
defer test.MockVariableValue(&setting.AppURL, "https://localhost:3000")()
137-
138-
ctxURL, _ := url.Parse("https://gitea")
139-
ctxReq := &http.Request{URL: ctxURL, Header: http.Header{}}
140-
ctxReq.Host = ctxURL.Host
141-
ctxReq.Header.Add("X-Forwarded-Proto", ctxURL.Scheme)
142-
ctx := context.WithValue(context.Background(), httplib.RequestContextKey, ctxReq)
143-
cases := []struct {
144-
input string
145-
ownerName, repoName, remaining string
146-
}{
147-
{input: "/user/repo"},
148-
149-
{input: "https://localhost:3000/user/repo", ownerName: "user", repoName: "repo"},
150-
{input: "https://external:3000/user/repo"},
151-
152-
{input: "https://localhost:3000/user/repo.git/other", ownerName: "user", repoName: "repo", remaining: "/other"},
153-
154-
{input: "https://gitea/user/repo", ownerName: "user", repoName: "repo"},
155-
{input: "https://gitea:3333/user/repo"},
156-
157-
{input: "ssh://try.gitea.io:2222/user/repo", ownerName: "user", repoName: "repo"},
158-
{input: "ssh://external:2222/user/repo"},
159-
160-
{input: "git+ssh://[email protected]/user/repo.git", ownerName: "user", repoName: "repo"},
161-
{input: "git+ssh://user@external/user/repo.git"},
162-
163-
{input: "[email protected]:user/repo.git", ownerName: "user", repoName: "repo"},
164-
{input: "root@gitea:user/repo.git", ownerName: "user", repoName: "repo"},
165-
{input: "root@external:user/repo.git"},
166-
}
167-
168-
for _, c := range cases {
169-
t.Run(c.input, func(t *testing.T) {
170-
ret := parseRepositoryURL(ctx, c.input)
171-
assert.Equal(t, c.ownerName, ret.OwnerName)
172-
assert.Equal(t, c.repoName, ret.RepoName)
173-
assert.Equal(t, c.remaining, ret.RemainingPath)
174-
})
175-
}
176-
177-
t.Run("WithSubpath", func(t *testing.T) {
178-
defer test.MockVariableValue(&setting.AppURL, "https://localhost:3000/subpath")()
179-
defer test.MockVariableValue(&setting.AppSubURL, "/subpath")()
180-
cases = []struct {
181-
input string
182-
ownerName, repoName, remaining string
183-
}{
184-
{input: "https://localhost:3000/user/repo"},
185-
{input: "https://localhost:3000/subpath/user/repo.git/other", ownerName: "user", repoName: "repo", remaining: "/other"},
186-
187-
{input: "ssh://try.gitea.io:2222/user/repo", ownerName: "user", repoName: "repo"},
188-
{input: "ssh://external:2222/user/repo"},
189-
190-
{input: "git+ssh://[email protected]/user/repo.git", ownerName: "user", repoName: "repo"},
191-
{input: "git+ssh://user@external/user/repo.git"},
192-
193-
{input: "[email protected]:user/repo.git", ownerName: "user", repoName: "repo"},
194-
{input: "root@external:user/repo.git"},
195-
}
196-
197-
for _, c := range cases {
198-
t.Run(c.input, func(t *testing.T) {
199-
ret := parseRepositoryURL(ctx, c.input)
200-
assert.Equal(t, c.ownerName, ret.OwnerName)
201-
assert.Equal(t, c.repoName, ret.RepoName)
202-
assert.Equal(t, c.remaining, ret.RemainingPath)
203-
})
204-
}
205-
})
206-
}
207-
208131
func TestGetRepositoryByURL(t *testing.T) {
209132
assert.NoError(t, unittest.PrepareTestDatabase())
210133

modules/git/commit_info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ package git
77
type CommitInfo struct {
88
Entry *TreeEntry
99
Commit *Commit
10-
SubModuleFile *CommitSubModuleFile
10+
SubmoduleFile *CommitSubmoduleFile
1111
}

modules/git/commit_info_gogit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath
8585
} else if subModule != nil {
8686
subModuleURL = subModule.URL
8787
}
88-
subModuleFile := NewCommitSubModuleFile(subModuleURL, entry.ID.String())
89-
commitsInfo[i].SubModuleFile = subModuleFile
88+
subModuleFile := NewCommitSubmoduleFile(subModuleURL, entry.ID.String())
89+
commitsInfo[i].SubmoduleFile = subModuleFile
9090
}
9191
}
9292

modules/git/commit_info_nogogit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath
7979
} else if subModule != nil {
8080
subModuleURL = subModule.URL
8181
}
82-
subModuleFile := NewCommitSubModuleFile(subModuleURL, entry.ID.String())
83-
commitsInfo[i].SubModuleFile = subModuleFile
82+
subModuleFile := NewCommitSubmoduleFile(subModuleURL, entry.ID.String())
83+
commitsInfo[i].SubmoduleFile = subModuleFile
8484
}
8585
}
8686

modules/git/commit_submodule.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
package git
55

6+
type SubmoduleWebLink struct {
7+
RepoWebLink, CommitWebLink string
8+
}
9+
610
// GetSubModules get all the submodules of current revision git tree
711
func (c *Commit) GetSubModules() (*ObjectCache[*SubModule], error) {
812
if c.submoduleCache != nil {

modules/git/commit_submodule_file.go

Lines changed: 33 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -5,112 +5,46 @@
55
package git
66

77
import (
8-
"fmt"
9-
"net"
10-
"net/url"
11-
"path"
12-
"regexp"
13-
"strings"
8+
"context"
149

15-
"code.gitea.io/gitea/modules/setting"
10+
giturl "code.gitea.io/gitea/modules/git/url"
1611
)
1712

18-
var scpSyntax = regexp.MustCompile(`^([a-zA-Z0-9_]+@)?([a-zA-Z0-9._-]+):(.*)$`)
19-
20-
// CommitSubModuleFile represents a file with submodule type.
21-
type CommitSubModuleFile struct {
22-
refURL string
23-
refID string
13+
// CommitSubmoduleFile represents a file with submodule type.
14+
type CommitSubmoduleFile struct {
15+
refURL string
16+
parsedURL *giturl.RepositoryURL
17+
parsed bool
18+
refID string
19+
repoLink string
2420
}
2521

26-
// NewCommitSubModuleFile create a new submodule file
27-
func NewCommitSubModuleFile(refURL, refID string) *CommitSubModuleFile {
28-
return &CommitSubModuleFile{
29-
refURL: refURL,
30-
refID: refID,
31-
}
22+
// NewCommitSubmoduleFile create a new submodule file
23+
func NewCommitSubmoduleFile(refURL, refID string) *CommitSubmoduleFile {
24+
return &CommitSubmoduleFile{refURL: refURL, refID: refID}
3225
}
3326

34-
func getRefURL(refURL, repoFullName string) string {
35-
if refURL == "" {
36-
return ""
37-
}
38-
39-
// FIXME: use a more generic way to handle the domain and subpath
40-
urlPrefix := setting.AppURL
41-
sshDomain := setting.SSH.Domain
42-
43-
refURI := strings.TrimSuffix(refURL, ".git")
44-
45-
prefixURL, _ := url.Parse(urlPrefix)
46-
urlPrefixHostname, _, err := net.SplitHostPort(prefixURL.Host)
47-
if err != nil {
48-
urlPrefixHostname = prefixURL.Host
49-
}
50-
51-
urlPrefix = strings.TrimSuffix(urlPrefix, "/")
52-
53-
// FIXME: Need to consider branch - which will require changes in modules/git/commit.go:GetSubModules
54-
// Relative url prefix check (according to git submodule documentation)
55-
if strings.HasPrefix(refURI, "./") || strings.HasPrefix(refURI, "../") {
56-
return urlPrefix + path.Clean(path.Join("/", repoFullName, refURI))
57-
}
58-
59-
if !strings.Contains(refURI, "://") {
60-
// scp style syntax which contains *no* port number after the : (and is not parsed by net/url)
61-
// ex: [email protected]:go-gitea/gitea
62-
match := scpSyntax.FindAllStringSubmatch(refURI, -1)
63-
if len(match) > 0 {
64-
m := match[0]
65-
refHostname := m[2]
66-
pth := m[3]
67-
68-
if !strings.HasPrefix(pth, "/") {
69-
pth = "/" + pth
70-
}
71-
72-
if urlPrefixHostname == refHostname || refHostname == sshDomain {
73-
return urlPrefix + path.Clean(path.Join("/", pth))
74-
}
75-
return "http://" + refHostname + pth
76-
}
77-
}
78-
79-
ref, err := url.Parse(refURI)
80-
if err != nil {
81-
return ""
82-
}
83-
84-
refHostname, _, err := net.SplitHostPort(ref.Host)
85-
if err != nil {
86-
refHostname = ref.Host
87-
}
88-
89-
supportedSchemes := []string{"http", "https", "git", "ssh", "git+ssh"}
90-
91-
for _, scheme := range supportedSchemes {
92-
if ref.Scheme == scheme {
93-
if ref.Scheme == "http" || ref.Scheme == "https" {
94-
if len(ref.User.Username()) > 0 {
95-
return ref.Scheme + "://" + fmt.Sprintf("%v", ref.User) + "@" + ref.Host + ref.Path
96-
}
97-
return ref.Scheme + "://" + ref.Host + ref.Path
98-
} else if urlPrefixHostname == refHostname || refHostname == sshDomain {
99-
return urlPrefix + path.Clean(path.Join("/", ref.Path))
100-
}
101-
return "http://" + refHostname + ref.Path
102-
}
103-
}
104-
105-
return ""
106-
}
107-
108-
// RefURL guesses and returns reference URL.
109-
func (sf *CommitSubModuleFile) RefURL(repoFullName string) string {
110-
return getRefURL(sf.refURL, repoFullName)
27+
func (sf *CommitSubmoduleFile) RefID() string {
28+
return sf.refID
11129
}
11230

113-
// RefID returns reference ID.
114-
func (sf *CommitSubModuleFile) RefID() string {
115-
return sf.refID
31+
func (sf *CommitSubmoduleFile) SubmoduleWebLink(ctx context.Context, optCommitID ...string) *SubmoduleWebLink {
32+
if !sf.parsed {
33+
sf.parsed = true
34+
parsedURL, err := giturl.ParseRepositoryURL(ctx, sf.refURL)
35+
if err != nil {
36+
return nil
37+
}
38+
sf.parsedURL = parsedURL
39+
sf.repoLink = giturl.MakeRepositoryWebLink(sf.parsedURL)
40+
}
41+
var commitLink string
42+
if len(optCommitID) == 2 {
43+
commitLink = sf.repoLink + "/compare/" + optCommitID[0] + "..." + optCommitID[1]
44+
} else if len(optCommitID) == 1 {
45+
commitLink = sf.repoLink + "/commit/" + optCommitID[0]
46+
} else {
47+
commitLink = sf.repoLink + "/commit/" + sf.refID
48+
}
49+
return &SubmoduleWebLink{RepoWebLink: sf.repoLink, CommitWebLink: commitLink}
11650
}

0 commit comments

Comments
 (0)