Skip to content

Fix various bugs #35177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/git/commit_submodule_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (sf *CommitSubmoduleFile) getWebLinkInTargetRepo(ctx context.Context, moreL
return nil
}
if strings.HasPrefix(sf.refURL, "../") {
targetLink := path.Join(sf.repoLink, path.Dir(sf.fullPath), sf.refURL)
targetLink := path.Join(sf.repoLink, sf.refURL)
return &SubmoduleWebLink{RepoWebLink: targetLink, CommitWebLink: targetLink + moreLinkPath}
}
if !sf.parsed {
Expand Down
2 changes: 1 addition & 1 deletion modules/git/commit_submodule_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestCommitSubmoduleLink(t *testing.T) {
assert.Equal(t, "/subpath/user/repo", wl.RepoWebLink)
assert.Equal(t, "/subpath/user/repo/tree/aaaa", wl.CommitWebLink)

sf = NewCommitSubmoduleFile("/subpath/any/repo-home-link", "dir/submodule", "../../../user/repo", "aaaa")
sf = NewCommitSubmoduleFile("/subpath/any/repo-home-link", "dir/submodule", "../../user/repo", "aaaa")
wl = sf.SubmoduleWebLinkCompare(t.Context(), "1111", "2222")
assert.Equal(t, "/subpath/user/repo", wl.RepoWebLink)
assert.Equal(t, "/subpath/user/repo/compare/1111...2222", wl.CommitWebLink)
Expand Down
15 changes: 15 additions & 0 deletions modules/git/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strconv"
"strings"
"sync"

"code.gitea.io/gitea/modules/util"
)

// ObjectCache provides thread-safe cache operations.
Expand Down Expand Up @@ -106,3 +108,16 @@ func HashFilePathForWebUI(s string) string {
_, _ = h.Write([]byte(s))
return hex.EncodeToString(h.Sum(nil))
}

func SplitCommitTitleBody(commitMessage string, titleRuneLimit int) (title, body string) {
title, body, _ = strings.Cut(commitMessage, "\n")
title, title2 := util.EllipsisTruncateRunes(title, titleRuneLimit)
if title2 != "" {
if body == "" {
body = title2
} else {
body = title2 + "\n" + body
}
}
return title, body
}
14 changes: 14 additions & 0 deletions modules/git/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ func TestHashFilePathForWebUI(t *testing.T) {
HashFilePathForWebUI("foobar"),
)
}

func TestSplitCommitTitleBody(t *testing.T) {
title, body := SplitCommitTitleBody("啊bcdefg", 4)
assert.Equal(t, "啊…", title)
assert.Equal(t, "…bcdefg", body)

title, body = SplitCommitTitleBody("abcdefg\n1234567", 4)
assert.Equal(t, "a…", title)
assert.Equal(t, "…bcdefg\n1234567", body)

title, body = SplitCommitTitleBody("abcdefg\n1234567", 100)
assert.Equal(t, "abcdefg", title)
assert.Equal(t, "1234567", body)
}
20 changes: 14 additions & 6 deletions modules/util/truncate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func IsLikelyEllipsisLeftPart(s string) bool {
return strings.HasSuffix(s, utf8Ellipsis) || strings.HasSuffix(s, asciiEllipsis)
}

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

// EllipsisDisplayStringX works like EllipsisDisplayString while it also returns the right part
func EllipsisDisplayStringX(str string, limit int) (left, right string) {
left, offset, truncated, encounterInvalid := ellipsisDisplayString(str, limit)
return ellipsisDisplayStringX(str, limit, ellipsisDisplayGuessWidth)
}

func ellipsisDisplayStringX(str string, limit int, widthGuess func(rune) int) (left, right string) {
left, offset, truncated, encounterInvalid := ellipsisDisplayString(str, limit, widthGuess)
if truncated {
right = str[offset:]
r, _ := utf8.DecodeRune(UnsafeStringToBytes(right))
Expand All @@ -68,7 +72,7 @@ func EllipsisDisplayStringX(str string, limit int) (left, right string) {
return left, right
}

func ellipsisDisplayString(str string, limit int) (res string, offset int, truncated, encounterInvalid bool) {
func ellipsisDisplayString(str string, limit int, widthGuess func(rune) int) (res string, offset int, truncated, encounterInvalid bool) {
if len(str) <= limit {
return str, len(str), false, false
}
Expand All @@ -81,7 +85,7 @@ func ellipsisDisplayString(str string, limit int) (res string, offset int, trunc
for i, r := range str {
encounterInvalid = encounterInvalid || r == utf8.RuneError
pos = i
runeWidth := ellipsisGuessDisplayWidth(r)
runeWidth := widthGuess(r)
if used+runeWidth+3 > limit {
break
}
Expand All @@ -96,7 +100,7 @@ func ellipsisDisplayString(str string, limit int) (res string, offset int, trunc
if nextCnt >= 4 {
break
}
nextWidth += ellipsisGuessDisplayWidth(r)
nextWidth += widthGuess(r)
nextCnt++
}
if nextCnt <= 3 && used+nextWidth <= limit {
Expand All @@ -114,6 +118,10 @@ func ellipsisDisplayString(str string, limit int) (res string, offset int, trunc
return str[:offset] + ellipsis, offset, true, encounterInvalid
}

func EllipsisTruncateRunes(str string, limit int) (left, right string) {
return ellipsisDisplayStringX(str, limit, func(r rune) int { return 1 })
}

// TruncateRunes returns a truncated string with given rune limit,
// it returns input string if its rune length doesn't exceed the limit.
func TruncateRunes(str string, limit int) string {
Expand Down
2 changes: 1 addition & 1 deletion modules/util/truncate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestEllipsisGuessDisplayWidth(t *testing.T) {
t.Run(c.r, func(t *testing.T) {
w := 0
for _, r := range c.r {
w += ellipsisGuessDisplayWidth(r)
w += ellipsisDisplayGuessWidth(r)
}
assert.Equal(t, c.want, w, "hex=% x", []byte(c.r))
})
Expand Down
13 changes: 9 additions & 4 deletions routers/web/feed/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@ import (
"time"

"code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/services/context"

"github.com/gorilla/feeds"
)

// ShowBranchFeed shows tags and/or releases on the repo as RSS / Atom feed
func ShowBranchFeed(ctx *context.Context, repo *repo.Repository, formatType string) {
commits, err := ctx.Repo.Commit.CommitsByRange(0, 10, "", "", "")
if err != nil {
ctx.ServerError("ShowBranchFeed", err)
return
var commits []*git.Commit
var err error
if ctx.Repo.Commit != nil {
commits, err = ctx.Repo.Commit.CommitsByRange(0, 10, "", "", "")
if err != nil {
ctx.ServerError("ShowBranchFeed", err)
return
}
}

title := "Latest commits for branch " + ctx.Repo.BranchName
Expand Down
7 changes: 2 additions & 5 deletions services/repository/files/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,8 @@ func GetTreeBySHA(ctx context.Context, repo *repo_model.Repository, gitRepo *git
if rangeStart >= len(entries) {
return tree, nil
}
var rangeEnd int
if len(entries) > perPage {
tree.Truncated = true
}
rangeEnd = min(rangeStart+perPage, len(entries))
rangeEnd := min(rangeStart+perPage, len(entries))
tree.Truncated = rangeEnd < len(entries)
tree.Entries = make([]api.GitEntry, rangeEnd-rangeStart)
for e := rangeStart; e < rangeEnd; e++ {
i := e - rangeStart
Expand Down
11 changes: 3 additions & 8 deletions services/repository/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,16 +402,11 @@ func pushUpdateAddTags(ctx context.Context, repo *repo_model.Repository, gitRepo
}

rel, has := relMap[lowerTag]

parts := strings.SplitN(tag.Message, "\n", 2)
note := ""
if len(parts) > 1 {
note = parts[1]
}
title, note := git.SplitCommitTitleBody(tag.Message, 255)
if !has {
rel = &repo_model.Release{
RepoID: repo.ID,
Title: parts[0],
Title: title,
TagName: tags[i],
LowerTagName: lowerTag,
Target: "",
Expand All @@ -430,7 +425,7 @@ func pushUpdateAddTags(ctx context.Context, repo *repo_model.Repository, gitRepo
rel.Sha1 = commit.ID.String()
rel.CreatedUnix = timeutil.TimeStamp(createdAt.Unix())
if rel.IsTag {
rel.Title = parts[0]
rel.Title = title
rel.Note = note
} else {
rel.IsDraft = false
Expand Down
26 changes: 19 additions & 7 deletions tests/integration/api_repo_git_trees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/tests"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAPIReposGitTrees(t *testing.T) {
Expand All @@ -32,13 +36,21 @@ func TestAPIReposGitTrees(t *testing.T) {
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)

// Test a public repo that anyone can GET the tree of
for _, ref := range [...]string{
"master", // Branch
repo1TreeSHA, // Tree SHA
} {
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/trees/%s", user2.Name, repo1.Name, ref)
MakeRequest(t, req, http.StatusOK)
}
_ = MakeRequest(t, NewRequest(t, "GET", "/api/v1/repos/user2/repo1/git/trees/master"), http.StatusOK)

resp := MakeRequest(t, NewRequest(t, "GET", "/api/v1/repos/user2/repo1/git/trees/62fb502a7172d4453f0322a2cc85bddffa57f07a?per_page=1"), http.StatusOK)
var respGitTree api.GitTreeResponse
DecodeJSON(t, resp, &respGitTree)
assert.True(t, respGitTree.Truncated)
require.Len(t, respGitTree.Entries, 1)
assert.Equal(t, "File-WoW", respGitTree.Entries[0].Path)

resp = MakeRequest(t, NewRequest(t, "GET", "/api/v1/repos/user2/repo1/git/trees/62fb502a7172d4453f0322a2cc85bddffa57f07a?page=2&per_page=1"), http.StatusOK)
respGitTree = api.GitTreeResponse{}
DecodeJSON(t, resp, &respGitTree)
assert.False(t, respGitTree.Truncated)
require.Len(t, respGitTree.Entries, 1)
assert.Equal(t, "README.md", respGitTree.Entries[0].Path)

// Tests a private repo with no token so will fail
for _, ref := range [...]string{
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/empty_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func TestEmptyRepoAddFile(t *testing.T) {
req = NewRequest(t, "GET", "/api/v1/repos/user30/empty/raw/main/README.md").AddTokenAuth(token)
session.MakeRequest(t, req, http.StatusNotFound)

// test feed
req = NewRequest(t, "GET", "/user30/empty/rss/branch/main/README.md").AddTokenAuth(token).SetHeader("Accept", "application/rss+xml")
resp = session.MakeRequest(t, req, http.StatusOK)
assert.Contains(t, resp.Body.String(), "</rss>")

// create a new file
req = NewRequest(t, "GET", "/user30/empty/_new/"+setting.Repository.DefaultBranch)
resp = session.MakeRequest(t, req, http.StatusOK)
Expand Down