Skip to content

Commit 8374717

Browse files
unknwonmpsonntag
authored andcommitted
gitutil: infer submodule with baseURL when it is a relative path (#6337)
1 parent af9c626 commit 8374717

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ All notable changes to Gogs are documented in this file.
1717
### Fixed
1818

1919
- _Regression:_ Pages are correctly rendered when requesting `?go-get=1` for subdirectories. [#6314](https://github.com/gogs/gogs/issues/6314)
20+
- _Regression:_ Submodule with a relative path is linked correctly. [#6319](https://github.com/gogs/gogs/issues/6319)
2021

2122
### Removed
2223

internal/assets/templates/templates_gen.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/gitutil/submodule.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,21 @@ import (
1717
var scpSyntax = lazyregexp.New(`^([a-zA-Z0-9_]+@)?([a-zA-Z0-9._-]+):(.*)$`)
1818

1919
// InferSubmoduleURL returns the inferred external URL of the submodule at best effort.
20-
func InferSubmoduleURL(mod *git.Submodule) string {
20+
// The `baseURL` should be the URL of the current repository. If the submodule URL looks
21+
// like a relative path, it assumes that the submodule is another repository on the same
22+
// Gogs instance by appending it to the `baseURL` with the commit.
23+
func InferSubmoduleURL(baseURL string, mod *git.Submodule) string {
24+
if !strings.HasSuffix(baseURL, "/") {
25+
baseURL += "/"
26+
}
27+
2128
raw := strings.TrimSuffix(mod.URL, "/")
2229
raw = strings.TrimSuffix(raw, ".git")
2330

31+
if strings.HasPrefix(raw, "../") {
32+
return fmt.Sprintf("%s%s/commit/%s", baseURL, raw, mod.Commit)
33+
}
34+
2435
parsed, err := url.Parse(raw)
2536
if err != nil {
2637
// Try parse as SCP syntax again

internal/gitutil/submodule_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ func TestInferSubmoduleURL(t *testing.T) {
4141
},
4242
expURL: "http://github.com/gogs/docs-api/commit/6b08f76a5313fa3d26859515b30aa17a5faa2807",
4343
},
44+
{
45+
name: "relative path",
46+
submodule: &git.Submodule{
47+
URL: "../repo2.git",
48+
Commit: "6b08f76a5313fa3d26859515b30aa17a5faa2807",
49+
},
50+
expURL: "https://gogs.example.com/user/repo/../repo2/commit/6b08f76a5313fa3d26859515b30aa17a5faa2807",
51+
},
4452
{
4553
name: "bad URL",
4654
submodule: &git.Submodule{
@@ -52,7 +60,7 @@ func TestInferSubmoduleURL(t *testing.T) {
5260
}
5361
for _, test := range tests {
5462
t.Run(test.name, func(t *testing.T) {
55-
assert.Equal(t, test.expURL, InferSubmoduleURL(test.submodule))
63+
assert.Equal(t, test.expURL, InferSubmoduleURL("https://gogs.example.com/user/repo", test.submodule))
5664
})
5765
}
5866
}

templates/repo/view_list.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
{{if .Submodule}}
2929
<td>
3030
<span class="octicon octicon-file-submodule"></span>
31-
<a href="{{InferSubmoduleURL .Submodule}}">{{.Entry.Name}} @ {{ShortSHA1 .Submodule.Commit}}</a>
31+
<a href="{{InferSubmoduleURL $.RepoLink .Submodule}}">{{.Entry.Name}} @ {{ShortSHA1 .Submodule.Commit}}</a>
3232
</td>
3333
{{else}}
3434
<td class="name">

0 commit comments

Comments
 (0)