Skip to content

Commit 0677f9b

Browse files
laojianzizhukovraunknwon
authored andcommitted
markup: render SHA links without branch prefix (#6350)
Co-authored-by: Zhukov Roman <[email protected]> Co-authored-by: ᴜɴᴋɴᴡᴏɴ <[email protected]>
1 parent 9521732 commit 0677f9b

File tree

5 files changed

+66
-16
lines changed

5 files changed

+66
-16
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ All notable changes to Gogs are documented in this file.
2121
- Backup can be processed when `--target` is specified on Windows. [#6339](https://github.com/gogs/gogs/issues/6339)
2222
- Commit message contains keywords look like an issue reference no longer fails the push entirely. [#6289](https://github.com/gogs/gogs/issues/6289)
2323
- _Regression:_ When running Gogs on Windows, push commits no longer fail on a daily basis with the error "pre-receive hook declined". [#6316](https://github.com/gogs/gogs/issues/6316)
24+
- Auto-linked commit SHAs now have correct links. [#6300](https://github.com/gogs/gogs/issues/6300)
2425

2526
### Removed
2627

internal/db/repo.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -427,24 +427,29 @@ func (repo *Repository) UpdateSize() error {
427427
return nil
428428
}
429429

430-
// ComposeMetas composes a map of metas for rendering external issue tracker URL.
430+
// ComposeMetas composes a map of metas for rendering SHA1 URL and external issue tracker URL.
431431
func (repo *Repository) ComposeMetas() map[string]string {
432-
if !repo.EnableExternalTracker {
433-
return nil
434-
} else if repo.ExternalMetas == nil {
435-
repo.ExternalMetas = map[string]string{
436-
"format": repo.ExternalTrackerFormat,
437-
"user": repo.MustOwner().Name,
438-
"repo": repo.Name,
439-
}
432+
if repo.ExternalMetas != nil {
433+
return repo.ExternalMetas
434+
}
435+
436+
repo.ExternalMetas = map[string]string{
437+
"repoLink": repo.Link(),
438+
}
439+
440+
if repo.EnableExternalTracker {
441+
repo.ExternalMetas["user"] = repo.MustOwner().Name
442+
repo.ExternalMetas["repo"] = repo.Name
443+
repo.ExternalMetas["format"] = repo.ExternalTrackerFormat
444+
440445
switch repo.ExternalTrackerStyle {
441446
case markup.ISSUE_NAME_STYLE_ALPHANUMERIC:
442447
repo.ExternalMetas["style"] = markup.ISSUE_NAME_STYLE_ALPHANUMERIC
443448
default:
444449
repo.ExternalMetas["style"] = markup.ISSUE_NAME_STYLE_NUMERIC
445450
}
446-
447451
}
452+
448453
return repo.ExternalMetas
449454
}
450455

internal/db/repo_test.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ func TestRepository_ComposeMetas(t *testing.T) {
1919

2020
t.Run("no external tracker is configured", func(t *testing.T) {
2121
repo.EnableExternalTracker = false
22-
assert.Equal(t, map[string]string(nil), repo.ComposeMetas())
2322

24-
// Should be nil even if other settings are present
25-
repo.ExternalTrackerStyle = markup.ISSUE_NAME_STYLE_NUMERIC
26-
assert.Equal(t, map[string]string(nil), repo.ComposeMetas())
23+
metas := repo.ComposeMetas()
24+
assert.Equal(t, metas["repoLink"], repo.Link())
25+
26+
// Should no format and style if no external tracker is configured
27+
_, ok := metas["format"]
28+
assert.False(t, ok)
29+
_, ok = metas["style"]
30+
assert.False(t, ok)
2731
})
2832

2933
t.Run("an external issue tracker is configured", func(t *testing.T) {
34+
repo.ExternalMetas = nil
3035
repo.EnableExternalTracker = true
3136

3237
// Default to numeric issue style

internal/markup/markup.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ func RenderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte {
166166
if com.StrTo(m).MustInt() > 0 {
167167
return m
168168
}
169-
return fmt.Sprintf(`<a href="%s/commit/%s"><code>%s</code></a>`, urlPrefix, m, tool.ShortSHA1(string(m)))
169+
170+
return fmt.Sprintf(`<a href="%s/commit/%s"><code>%s</code></a>`, urlPrefix, m, tool.ShortSHA1(m))
170171
}))
171172
}
172173

@@ -181,7 +182,7 @@ func RenderSpecialLink(rawBytes []byte, urlPrefix string, metas map[string]strin
181182

182183
rawBytes = RenderIssueIndexPattern(rawBytes, urlPrefix, metas)
183184
rawBytes = RenderCrossReferenceIssueIndexPattern(rawBytes, urlPrefix, metas)
184-
rawBytes = RenderSha1CurrentPattern(rawBytes, urlPrefix)
185+
rawBytes = RenderSha1CurrentPattern(rawBytes, metas["repoLink"])
185186
return rawBytes
186187
}
187188

internal/markup/markup_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,41 @@ func Test_RenderIssueIndexPattern(t *testing.T) {
215215
})
216216
})
217217
}
218+
219+
func TestRenderSha1CurrentPattern(t *testing.T) {
220+
metas := map[string]string{
221+
"repoLink": "/someuser/somerepo",
222+
}
223+
224+
tests := []struct {
225+
desc string
226+
input string
227+
prefix string
228+
expVal string
229+
}{
230+
{
231+
desc: "Full SHA (40 symbols)",
232+
input: "ad8ced4f57d9068cb2874557245be3c7f341149d",
233+
prefix: metas["repoLink"],
234+
expVal: `<a href="/someuser/somerepo/commit/ad8ced4f57d9068cb2874557245be3c7f341149d"><code>ad8ced4f57</code></a>`,
235+
},
236+
{
237+
desc: "Short SHA (8 symbols)",
238+
input: "ad8ced4f",
239+
prefix: metas["repoLink"],
240+
expVal: `<a href="/someuser/somerepo/commit/ad8ced4f"><code>ad8ced4f</code></a>`,
241+
},
242+
{
243+
desc: "9 digits",
244+
input: "123456789",
245+
prefix: metas["repoLink"],
246+
expVal: "123456789",
247+
},
248+
}
249+
250+
for _, test := range tests {
251+
t.Run(test.desc, func(t *testing.T) {
252+
assert.Equal(t, test.expVal, string(RenderSha1CurrentPattern([]byte(test.input), test.prefix)))
253+
})
254+
}
255+
}

0 commit comments

Comments
 (0)