0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-05-14 17:27:39 +02:00

Render abbreviated commit hash URLs as code links in markup

Previously only full 40-64 character hash URLs were rendered as
`<a><code>hash</code></a>`. Now URLs with abbreviated hashes (7+
characters) get the same treatment, consistent with how other hash
patterns in the codebase already work.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
silverwind 2026-02-23 17:40:52 +01:00
parent a8505269ca
commit ea0b2d8497
No known key found for this signature in database
GPG Key ID: 2E62B41C93869443
3 changed files with 17 additions and 1 deletions

View File

@ -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]+)?`)

View File

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

View File

@ -113,6 +113,16 @@ func TestRender_CrossReferences(t *testing.T) {
test(
inputURL,
`<p><a href="`+inputURL+`" rel="nofollow"><code>0123456789.patch</code></a></p>`)
// abbreviated commit hash URLs should also be rendered as code links
inputURL = setting.AppURL + "owner/repo/commit/01234567890123456789012345"
test(
inputURL,
`<p><a href="`+inputURL+`" rel="nofollow"><code>0123456789</code></a></p>`)
inputURL = setting.AppURL + "owner/repo/commit/0123456"
test(
inputURL,
`<p><a href="`+inputURL+`" rel="nofollow"><code>0123456</code></a></p>`)
}
func TestRender_links(t *testing.T) {