Skip to content

Commit 3168285

Browse files
authored
Merge branch 'main' into refactor_api-repo-unit-update
2 parents 0a5a096 + 85b5877 commit 3168285

File tree

13 files changed

+129
-33
lines changed

13 files changed

+129
-33
lines changed

modules/git/commit_submodule_file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (sf *CommitSubmoduleFile) getWebLinkInTargetRepo(ctx context.Context, moreL
4242
return nil
4343
}
4444
if strings.HasPrefix(sf.refURL, "../") {
45-
targetLink := path.Join(sf.repoLink, path.Dir(sf.fullPath), sf.refURL)
45+
targetLink := path.Join(sf.repoLink, sf.refURL)
4646
return &SubmoduleWebLink{RepoWebLink: targetLink, CommitWebLink: targetLink + moreLinkPath}
4747
}
4848
if !sf.parsed {

modules/git/commit_submodule_file_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestCommitSubmoduleLink(t *testing.T) {
3232
assert.Equal(t, "/subpath/user/repo", wl.RepoWebLink)
3333
assert.Equal(t, "/subpath/user/repo/tree/aaaa", wl.CommitWebLink)
3434

35-
sf = NewCommitSubmoduleFile("/subpath/any/repo-home-link", "dir/submodule", "../../../user/repo", "aaaa")
35+
sf = NewCommitSubmoduleFile("/subpath/any/repo-home-link", "dir/submodule", "../../user/repo", "aaaa")
3636
wl = sf.SubmoduleWebLinkCompare(t.Context(), "1111", "2222")
3737
assert.Equal(t, "/subpath/user/repo", wl.RepoWebLink)
3838
assert.Equal(t, "/subpath/user/repo/compare/1111...2222", wl.CommitWebLink)

modules/git/utils.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"strconv"
1212
"strings"
1313
"sync"
14+
15+
"code.gitea.io/gitea/modules/util"
1416
)
1517

1618
// ObjectCache provides thread-safe cache operations.
@@ -106,3 +108,16 @@ func HashFilePathForWebUI(s string) string {
106108
_, _ = h.Write([]byte(s))
107109
return hex.EncodeToString(h.Sum(nil))
108110
}
111+
112+
func SplitCommitTitleBody(commitMessage string, titleRuneLimit int) (title, body string) {
113+
title, body, _ = strings.Cut(commitMessage, "\n")
114+
title, title2 := util.EllipsisTruncateRunes(title, titleRuneLimit)
115+
if title2 != "" {
116+
if body == "" {
117+
body = title2
118+
} else {
119+
body = title2 + "\n" + body
120+
}
121+
}
122+
return title, body
123+
}

modules/git/utils_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,17 @@ func TestHashFilePathForWebUI(t *testing.T) {
1515
HashFilePathForWebUI("foobar"),
1616
)
1717
}
18+
19+
func TestSplitCommitTitleBody(t *testing.T) {
20+
title, body := SplitCommitTitleBody("啊bcdefg", 4)
21+
assert.Equal(t, "啊…", title)
22+
assert.Equal(t, "…bcdefg", body)
23+
24+
title, body = SplitCommitTitleBody("abcdefg\n1234567", 4)
25+
assert.Equal(t, "a…", title)
26+
assert.Equal(t, "…bcdefg\n1234567", body)
27+
28+
title, body = SplitCommitTitleBody("abcdefg\n1234567", 100)
29+
assert.Equal(t, "abcdefg", title)
30+
assert.Equal(t, "1234567", body)
31+
}

modules/util/truncate.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func IsLikelyEllipsisLeftPart(s string) bool {
1919
return strings.HasSuffix(s, utf8Ellipsis) || strings.HasSuffix(s, asciiEllipsis)
2020
}
2121

22-
func ellipsisGuessDisplayWidth(r rune) int {
22+
func ellipsisDisplayGuessWidth(r rune) int {
2323
// To make the truncated string as long as possible,
2424
// CJK/emoji chars are considered as 2-ASCII width but not 3-4 bytes width.
2525
// Here we only make the best guess (better than counting them in bytes),
@@ -48,13 +48,17 @@ func ellipsisGuessDisplayWidth(r rune) int {
4848
// It appends "…" or "..." at the end of truncated string.
4949
// It guarantees the length of the returned runes doesn't exceed the limit.
5050
func EllipsisDisplayString(str string, limit int) string {
51-
s, _, _, _ := ellipsisDisplayString(str, limit)
51+
s, _, _, _ := ellipsisDisplayString(str, limit, ellipsisDisplayGuessWidth)
5252
return s
5353
}
5454

5555
// EllipsisDisplayStringX works like EllipsisDisplayString while it also returns the right part
5656
func EllipsisDisplayStringX(str string, limit int) (left, right string) {
57-
left, offset, truncated, encounterInvalid := ellipsisDisplayString(str, limit)
57+
return ellipsisDisplayStringX(str, limit, ellipsisDisplayGuessWidth)
58+
}
59+
60+
func ellipsisDisplayStringX(str string, limit int, widthGuess func(rune) int) (left, right string) {
61+
left, offset, truncated, encounterInvalid := ellipsisDisplayString(str, limit, widthGuess)
5862
if truncated {
5963
right = str[offset:]
6064
r, _ := utf8.DecodeRune(UnsafeStringToBytes(right))
@@ -68,7 +72,7 @@ func EllipsisDisplayStringX(str string, limit int) (left, right string) {
6872
return left, right
6973
}
7074

71-
func ellipsisDisplayString(str string, limit int) (res string, offset int, truncated, encounterInvalid bool) {
75+
func ellipsisDisplayString(str string, limit int, widthGuess func(rune) int) (res string, offset int, truncated, encounterInvalid bool) {
7276
if len(str) <= limit {
7377
return str, len(str), false, false
7478
}
@@ -81,7 +85,7 @@ func ellipsisDisplayString(str string, limit int) (res string, offset int, trunc
8185
for i, r := range str {
8286
encounterInvalid = encounterInvalid || r == utf8.RuneError
8387
pos = i
84-
runeWidth := ellipsisGuessDisplayWidth(r)
88+
runeWidth := widthGuess(r)
8589
if used+runeWidth+3 > limit {
8690
break
8791
}
@@ -96,7 +100,7 @@ func ellipsisDisplayString(str string, limit int) (res string, offset int, trunc
96100
if nextCnt >= 4 {
97101
break
98102
}
99-
nextWidth += ellipsisGuessDisplayWidth(r)
103+
nextWidth += widthGuess(r)
100104
nextCnt++
101105
}
102106
if nextCnt <= 3 && used+nextWidth <= limit {
@@ -114,6 +118,10 @@ func ellipsisDisplayString(str string, limit int) (res string, offset int, trunc
114118
return str[:offset] + ellipsis, offset, true, encounterInvalid
115119
}
116120

121+
func EllipsisTruncateRunes(str string, limit int) (left, right string) {
122+
return ellipsisDisplayStringX(str, limit, func(r rune) int { return 1 })
123+
}
124+
117125
// TruncateRunes returns a truncated string with given rune limit,
118126
// it returns input string if its rune length doesn't exceed the limit.
119127
func TruncateRunes(str string, limit int) string {

modules/util/truncate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestEllipsisGuessDisplayWidth(t *testing.T) {
2929
t.Run(c.r, func(t *testing.T) {
3030
w := 0
3131
for _, r := range c.r {
32-
w += ellipsisGuessDisplayWidth(r)
32+
w += ellipsisDisplayGuessWidth(r)
3333
}
3434
assert.Equal(t, c.want, w, "hex=% x", []byte(c.r))
3535
})

options/locale/locale_ga-IE.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ buttons.enable_monospace_font=Cumasaigh cló monospace
215215
buttons.disable_monospace_font=Díchumasaigh cló monospace
216216
217217
[filter]
218+
string.asc=A - Z
219+
string.desc=Z - A
218220
219221
[error]
220222
occurred=Tharla earráid

0 commit comments

Comments
 (0)