Browse Source

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
ᴜɴᴋɴᴡᴏɴ 3 years ago
parent
commit
672625b55c

+ 1 - 0
CHANGELOG.md

@@ -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
+ 112 - 112
internal/assets/templates/templates_gen.go


+ 12 - 1
internal/gitutil/submodule.go

@@ -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

+ 9 - 1
internal/gitutil/submodule_test.go

@@ -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))
 		})
 	}
 }

+ 1 - 1
templates/repo/view_list.tmpl

@@ -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">

Some files were not shown because too many files changed in this diff