fix: linking commit hashes with leading or trailing punctuation (#8643)

Recognize commit hashes if they have leading or trailing punctuation, `ccc33dd2dfcd8e9d81c7e91f74acf92114a61ea0...` will not be recognized properly. The restriction of two punctuation characters on either side has been lifted.

Resolves #8602

Co-Authored-By: Beowulf <beowulf@beocode.eu>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8643
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: James Anderson <james@jamesa.me>
Co-committed-by: James Anderson <james@jamesa.me>
This commit is contained in:
James Anderson 2025-12-22 02:32:40 +01:00 committed by Gusted
parent 5244253fb9
commit 50b0075266
3 changed files with 9 additions and 1 deletions

View file

@ -49,7 +49,7 @@ var (
// hashCurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae
// Although SHA1 hashes are 40 chars long, SHA256 are 64, the regex matches the hash from 7 to 64 chars in length
// so that abbreviated hash links can be used as well. This matches git and GitHub usability.
hashCurrentPattern = regexp.MustCompile(`(?:^|\s)[^\w\d]{0,2}([0-9a-f]{7,64})[^\w\d]{0,2}(?:\s|$)`)
hashCurrentPattern = regexp.MustCompile(`(?:^|\s)[^\w\d]*([0-9a-f]{7,64})[^\w\d]*(?:\s|$)`)
// shortLinkPattern matches short but difficult to parse [[name|link|arg=test]] syntax
shortLinkPattern = regexp.MustCompile(`\[\[(.*?)\]\](\w*)`)

View file

@ -475,6 +475,9 @@ func TestRegExp_hashCurrentPattern(t *testing.T) {
":abcd3ef",
".abcd3ef",
" (abcd3ef). ",
"abcd3ef...",
"...abcd3ef",
"(!...abcd3ef",
}
falseTestCases := []string{
"test",
@ -484,6 +487,7 @@ func TestRegExp_hashCurrentPattern(t *testing.T) {
"abcdefghijklmnopqrstuvwxyzabcdefghijklmO",
"commit/abcdefd",
"abcd3ef...defabcd",
"f..defabcd",
}
for _, testCase := range trueTestCases {

View file

@ -57,8 +57,10 @@ func TestRender_Commits(t *testing.T) {
}
sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
shaWithExtra := "65f1bf27bc3bf70f64657658635e66094edbcb4d..."
repo := markup.TestRepoURL
commit := util.URLJoin(repo, "commit", sha)
commitWithExtra := util.URLJoin(repo, "commit", shaWithExtra)
tree := util.URLJoin(repo, "tree", sha, "src")
file := util.URLJoin(repo, "commit", sha, "example.txt")
@ -69,9 +71,11 @@ func TestRender_Commits(t *testing.T) {
commitCompareWithHash := commitCompare + "#L2"
test(sha, `<p><a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
test(shaWithExtra, `<p><a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a>...</p>`)
test(sha[:7], `<p><a href="`+commit[:len(commit)-(40-7)]+`" rel="nofollow"><code>65f1bf2</code></a></p>`)
test(sha[:39], `<p><a href="`+commit[:len(commit)-(40-39)]+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
test(commit, `<p><a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
test(commitWithExtra, `<p><a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a>...</p>`)
test(tree, `<p><a href="`+tree+`" rel="nofollow"><code>65f1bf27bc/src</code></a></p>`)
test(file, `<p><a href="`+file+`" rel="nofollow"><code>65f1bf27bc/example.txt</code></a></p>`)