diff --git a/modules/markup/html.go b/modules/markup/html.go index 1c2ae6918d..5b47dea828 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -62,7 +62,7 @@ var globalVars = sync.OnceValue(func() *globalVarsType { v.shortLinkPattern = regexp.MustCompile(`\[\[(.*?)\]\](\w*)`) // anyHashPattern splits url containing SHA into parts - v.anyHashPattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{40,64})((\.\w+)*)(/[-+~%./\w]+)?(\?[-+~%.\w&=]+)?(#[-+~%.\w]+)?`) + v.anyHashPattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{7,64})((\.\w+)*)(/[-+~%./\w]+)?(\?[-+~%.\w&=]+)?(#[-+~%.\w]+)?`) // comparePattern matches "http://domain/org/repo/compare/COMMIT1...COMMIT2#hash" v.comparePattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{7,64})(\.\.\.?)([0-9a-f]{7,64})?(#[-+~_%.a-zA-Z0-9]+)?`) diff --git a/modules/markup/html_internal_test.go b/modules/markup/html_internal_test.go index ca2579c8ea..34d18de2cb 100644 --- a/modules/markup/html_internal_test.go +++ b/modules/markup/html_internal_test.go @@ -376,6 +376,12 @@ func TestRegExp_anySHA1Pattern(t *testing.T) { CommitID: "d8a994ef243349f321568f9e36d5c3f444b99cae", QueryHash: "diff-2", }, + "https://github.com/jquery/jquery/commit/0705be475092aede1eddae01": { + CommitID: "0705be475092aede1eddae01", + }, + "https://github.com/jquery/jquery/commit/0705be4": { + CommitID: "0705be4", + }, "non-url": {}, "http://a/b/c/d/e/1234567812345678123456781234567812345678123456781234567812345678?a=b#L1-L2": { CommitID: "1234567812345678123456781234567812345678123456781234567812345678", diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index 5f873d2985..b163ea896c 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -113,6 +113,16 @@ func TestRender_CrossReferences(t *testing.T) { test( inputURL, `

0123456789.patch

`) + + // abbreviated commit hash URLs should also be rendered as code links + inputURL = setting.AppURL + "owner/repo/commit/01234567890123456789012345" + test( + inputURL, + `

0123456789

`) + inputURL = setting.AppURL + "owner/repo/commit/0123456" + test( + inputURL, + `

0123456

`) } func TestRender_links(t *testing.T) {