Skip to content

Commit 4a6765f

Browse files
authored
Fix submodule paths when AppSubUrl is not root (go-gitea#11098) (go-gitea#11176)
Backport go-gitea#11098 Fix go-gitea#11002 Signed-off-by: Andrew Thornton <[email protected]>
1 parent dca8ef9 commit 4a6765f

File tree

3 files changed

+27
-30
lines changed

3 files changed

+27
-30
lines changed

modules/git/submodule.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"net"
1111
"net/url"
12+
"path"
1213
"regexp"
1314
"strings"
1415
)
@@ -38,7 +39,7 @@ func NewSubModuleFile(c *Commit, refURL, refID string) *SubModuleFile {
3839
}
3940
}
4041

41-
func getRefURL(refURL, urlPrefix, parentPath string) string {
42+
func getRefURL(refURL, urlPrefix, repoFullName string) string {
4243
if refURL == "" {
4344
return ""
4445
}
@@ -51,14 +52,10 @@ func getRefURL(refURL, urlPrefix, parentPath string) string {
5152
urlPrefixHostname = prefixURL.Host
5253
}
5354

55+
// FIXME: Need to consider branch - which will require changes in modules/git/commit.go:GetSubModules
5456
// Relative url prefix check (according to git submodule documentation)
5557
if strings.HasPrefix(refURI, "./") || strings.HasPrefix(refURI, "../") {
56-
// ...construct and return correct submodule url here...
57-
idx := strings.Index(parentPath, "/src/")
58-
if idx == -1 {
59-
return refURI
60-
}
61-
return strings.TrimSuffix(urlPrefix, "/") + parentPath[:idx] + "/" + refURI
58+
return urlPrefix + path.Clean(path.Join(repoFullName, refURI))
6259
}
6360

6461
if !strings.Contains(refURI, "://") {
@@ -69,16 +66,16 @@ func getRefURL(refURL, urlPrefix, parentPath string) string {
6966

7067
m := match[0]
7168
refHostname := m[2]
72-
path := m[3]
69+
pth := m[3]
7370

74-
if !strings.HasPrefix(path, "/") {
75-
path = "/" + path
71+
if !strings.HasPrefix(pth, "/") {
72+
pth = "/" + pth
7673
}
7774

7875
if urlPrefixHostname == refHostname {
79-
return prefixURL.Scheme + "://" + urlPrefixHostname + path
76+
return prefixURL.Scheme + "://" + urlPrefixHostname + path.Join(prefixURL.Path, path.Clean(pth))
8077
}
81-
return "http://" + refHostname + path
78+
return "http://" + refHostname + pth
8279
}
8380
}
8481

@@ -97,7 +94,7 @@ func getRefURL(refURL, urlPrefix, parentPath string) string {
9794
for _, scheme := range supportedSchemes {
9895
if ref.Scheme == scheme {
9996
if urlPrefixHostname == refHostname {
100-
return prefixURL.Scheme + "://" + prefixURL.Host + ref.Path
97+
return prefixURL.Scheme + "://" + prefixURL.Host + path.Join(prefixURL.Path, ref.Path)
10198
} else if ref.Scheme == "http" || ref.Scheme == "https" {
10299
if len(ref.User.Username()) > 0 {
103100
return ref.Scheme + "://" + fmt.Sprintf("%v", ref.User) + "@" + ref.Host + ref.Path
@@ -113,8 +110,8 @@ func getRefURL(refURL, urlPrefix, parentPath string) string {
113110
}
114111

115112
// RefURL guesses and returns reference URL.
116-
func (sf *SubModuleFile) RefURL(urlPrefix string, parentPath string) string {
117-
return getRefURL(sf.refURL, urlPrefix, parentPath)
113+
func (sf *SubModuleFile) RefURL(urlPrefix string, repoFullName string) string {
114+
return getRefURL(sf.refURL, urlPrefix, repoFullName)
118115
}
119116

120117
// RefID returns reference ID.

modules/git/submodule_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ func TestGetRefURL(t *testing.T) {
1717
parentPath string
1818
expect string
1919
}{
20-
{"git://github.com/user1/repo1", "/", "/", "http://github.com/user1/repo1"},
21-
{"https://localhost/user1/repo1.git", "/", "/", "https://localhost/user1/repo1"},
22-
{"http://localhost/user1/repo1.git", "/", "/", "http://localhost/user1/repo1"},
23-
{"[email protected]:user1/repo1.git", "/", "/", "http://github.com/user1/repo1"},
24-
{"ssh://[email protected]:2222/zefie/lge_g6_kernel_scripts.git", "/", "/", "http://git.zefie.net/zefie/lge_g6_kernel_scripts"},
25-
{"[email protected]:2222/zefie/lge_g6_kernel_scripts.git", "/", "/", "http://git.zefie.net/2222/zefie/lge_g6_kernel_scripts"},
26-
{"[email protected]:go-gitea/gitea", "https://try.gitea.io/go-gitea/gitea", "/", "https://try.gitea.io/go-gitea/gitea"},
27-
{"ssh://[email protected]:9999/go-gitea/gitea", "https://try.gitea.io/go-gitea/gitea", "/", "https://try.gitea.io/go-gitea/gitea"},
28-
{"git://[email protected]:9999/go-gitea/gitea", "https://try.gitea.io/go-gitea/log", "/", "https://try.gitea.io/go-gitea/gitea"},
29-
{"ssh://[email protected]:9999/go-gitea/gitea", "https://127.0.0.1:3000/go-gitea/log", "/", "https://127.0.0.1:3000/go-gitea/gitea"},
30-
{"https://gitea.com:3000/user1/repo1.git", "https://127.0.0.1:3000/go-gitea/gitea", "/", "https://gitea.com:3000/user1/repo1"},
31-
{"https://username:[email protected]/username/repository.git", "/", "/", "https://username:[email protected]/username/repository"},
20+
{"git://github.com/user1/repo1", "/", "user1/repo2", "http://github.com/user1/repo1"},
21+
{"https://localhost/user1/repo1.git", "/", "user1/repo2", "https://localhost/user1/repo1"},
22+
{"http://localhost/user1/repo1.git", "/", "owner/reponame", "http://localhost/user1/repo1"},
23+
{"[email protected]:user1/repo1.git", "/", "owner/reponame", "http://github.com/user1/repo1"},
24+
{"ssh://[email protected]:2222/zefie/lge_g6_kernel_scripts.git", "/", "zefie/lge_g6_kernel", "http://git.zefie.net/zefie/lge_g6_kernel_scripts"},
25+
{"[email protected]:2222/zefie/lge_g6_kernel_scripts.git", "/", "zefie/lge_g6_kernel", "http://git.zefie.net/2222/zefie/lge_g6_kernel_scripts"},
26+
{"[email protected]:go-gitea/gitea", "https://try.gitea.io/", "go-gitea/sdk", "https://try.gitea.io/go-gitea/gitea"},
27+
{"ssh://[email protected]:9999/go-gitea/gitea", "https://try.gitea.io/", "go-gitea/sdk", "https://try.gitea.io/go-gitea/gitea"},
28+
{"git://[email protected]:9999/go-gitea/gitea", "https://try.gitea.io/", "go-gitea/sdk", "https://try.gitea.io/go-gitea/gitea"},
29+
{"ssh://[email protected]:9999/go-gitea/gitea", "https://127.0.0.1:3000/", "go-gitea/sdk", "https://127.0.0.1:3000/go-gitea/gitea"},
30+
{"https://gitea.com:3000/user1/repo1.git", "https://127.0.0.1:3000/", "user/repo2", "https://gitea.com:3000/user1/repo1"},
31+
{"https://username:[email protected]/username/repository.git", "/", "username/repository2", "https://username:[email protected]/username/repository"},
3232
{"somethingbad", "https://127.0.0.1:3000/go-gitea/gitea", "/", ""},
33-
{"git@localhost:user/repo", "https://localhost/user/repo2", "/", "https://localhost/user/repo"},
34-
{"../path/to/repo.git/", "https://localhost/user/repo2/src/branch/master/test", "/", "../path/to/repo.git/"},
33+
{"git@localhost:user/repo", "https://localhost/", "user2/repo1", "https://localhost/user/repo"},
34+
{"../path/to/repo.git/", "https://localhost/", "user/repo2", "https://localhost/user/path/to/repo.git"},
3535
}
3636

3737
for _, kase := range kases {

templates/repo/view_list.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<td>
6565
<span class="truncate">
6666
<span class="octicon octicon-file-submodule"></span>
67-
{{$refURL := $commit.RefURL AppUrl $.BranchLink}}
67+
{{$refURL := $commit.RefURL AppUrl $.Repository.FullName}}
6868
{{if $refURL}}
6969
<a href="{{$refURL}}">{{$entry.Name}}</a> @ <a href="{{$refURL}}/commit/{{$commit.RefID}}">{{ShortSha $commit.RefID}}</a>
7070
{{else}}

0 commit comments

Comments
 (0)