gitutil: infer submodule with baseURL when it is a relative path (#6337)

# Conflicts:
#	internal/assets/templates/templates_gen.go
#	internal/conf/testdata/TestInit.golden.ini
pull/6378/head
ᴜɴᴋɴᴡᴏɴ 2020-09-18 16:19:31 +08:00
parent 98c65f319f
commit 672625b55c
No known key found for this signature in database
GPG Key ID: B43718D76E30A238
5 changed files with 174 additions and 154 deletions

View File

@ -17,6 +17,7 @@ All notable changes to Gogs are documented in this file.
### Fixed
- _Regression:_ Pages are correctly rendered when requesting `?go-get=1` for subdirectories. [#6314](https://github.com/gogs/gogs/issues/6314)
- _Regression:_ Submodule with a relative path is linked correctly. [#6319](https://github.com/gogs/gogs/issues/6319)
### Removed

File diff suppressed because it is too large Load Diff

View File

@ -17,10 +17,21 @@ import (
var scpSyntax = lazyregexp.New(`^([a-zA-Z0-9_]+@)?([a-zA-Z0-9._-]+):(.*)$`)
// InferSubmoduleURL returns the inferred external URL of the submodule at best effort.
func InferSubmoduleURL(mod *git.Submodule) string {
// The `baseURL` should be the URL of the current repository. If the submodule URL looks
// like a relative path, it assumes that the submodule is another repository on the same
// Gogs instance by appending it to the `baseURL` with the commit.
func InferSubmoduleURL(baseURL string, mod *git.Submodule) string {
if !strings.HasSuffix(baseURL, "/") {
baseURL += "/"
}
raw := strings.TrimSuffix(mod.URL, "/")
raw = strings.TrimSuffix(raw, ".git")
if strings.HasPrefix(raw, "../") {
return fmt.Sprintf("%s%s/commit/%s", baseURL, raw, mod.Commit)
}
parsed, err := url.Parse(raw)
if err != nil {
// Try parse as SCP syntax again

View File

@ -41,6 +41,14 @@ func TestInferSubmoduleURL(t *testing.T) {
},
expURL: "http://github.com/gogs/docs-api/commit/6b08f76a5313fa3d26859515b30aa17a5faa2807",
},
{
name: "relative path",
submodule: &git.Submodule{
URL: "../repo2.git",
Commit: "6b08f76a5313fa3d26859515b30aa17a5faa2807",
},
expURL: "https://gogs.example.com/user/repo/../repo2/commit/6b08f76a5313fa3d26859515b30aa17a5faa2807",
},
{
name: "bad URL",
submodule: &git.Submodule{
@ -52,7 +60,7 @@ func TestInferSubmoduleURL(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expURL, InferSubmoduleURL(test.submodule))
assert.Equal(t, test.expURL, InferSubmoduleURL("https://gogs.example.com/user/repo", test.submodule))
})
}
}

View File

@ -28,7 +28,7 @@
{{if .Submodule}}
<td>
<span class="octicon octicon-file-submodule"></span>
<a href="{{InferSubmoduleURL .Submodule}}">{{.Entry.Name}} @ {{ShortSHA1 .Submodule.Commit}}</a>
<a href="{{InferSubmoduleURL $.RepoLink .Submodule}}">{{.Entry.Name}} @ {{ShortSHA1 .Submodule.Commit}}</a>
</td>
{{else}}
<td class="name">