Merge pull request '[BUG] Sort file list case insensitively' (#2522) from gusted/forgejo-sort-file-list into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2522
Reviewed-by: oliverpool <oliverpool@noreply.codeberg.org>
This commit is contained in:
Gusted 2024-03-01 13:58:38 +00:00
commit eadcbcf7d1
3 changed files with 70 additions and 0 deletions

View file

@ -5,11 +5,13 @@ package base
import ( import (
"math/big" "math/big"
"strings"
"unicode/utf8" "unicode/utf8"
) )
// NaturalSortLess compares two strings so that they could be sorted in natural order // NaturalSortLess compares two strings so that they could be sorted in natural order
func NaturalSortLess(s1, s2 string) bool { func NaturalSortLess(s1, s2 string) bool {
s1, s2 = strings.ToLower(s1), strings.ToLower(s2)
var i1, i2 int var i1, i2 int
for { for {
rune1, j1, end1 := getNextRune(s1, i1) rune1, j1, end1 := getNextRune(s1, i1)

View file

@ -20,4 +20,10 @@ func TestNaturalSortLess(t *testing.T) {
test("a-1-a", "a-1-b", true) test("a-1-a", "a-1-b", true)
test("2", "12", true) test("2", "12", true)
test("a", "ab", true) test("a", "ab", true)
// Test for case insensitive.
test("A", "ab", true)
test("B", "ab", false)
test("a", "AB", true)
test("b", "AB", false)
} }

View file

@ -6,6 +6,7 @@ package integration
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"net/url"
"path" "path"
"strings" "strings"
"testing" "testing"
@ -15,11 +16,13 @@ import (
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
unit_model "code.gitea.io/gitea/models/unit" unit_model "code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/translation" "code.gitea.io/gitea/modules/translation"
repo_service "code.gitea.io/gitea/services/repository" repo_service "code.gitea.io/gitea/services/repository"
files_service "code.gitea.io/gitea/services/repository/files"
"code.gitea.io/gitea/tests" "code.gitea.io/gitea/tests"
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
@ -899,3 +902,62 @@ func TestRepoHomeViewRedirect(t *testing.T) {
assert.Equal(t, "Wiki", txt) assert.Equal(t, "Wiki", txt)
}) })
} }
func TestRepoFilesList(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
// create the repo
repo, _, f := CreateDeclarativeRepo(t, user2, "",
[]unit_model.Type{unit_model.TypeCode}, nil,
[]*files_service.ChangeRepoFile{
{
Operation: "create",
TreePath: "zEta",
ContentReader: strings.NewReader("zeta"),
},
{
Operation: "create",
TreePath: "licensa",
ContentReader: strings.NewReader("licensa"),
},
{
Operation: "create",
TreePath: "licensz",
ContentReader: strings.NewReader("licensz"),
},
{
Operation: "create",
TreePath: "delta",
ContentReader: strings.NewReader("delta"),
},
{
Operation: "create",
TreePath: "Charlie/aa.txt",
ContentReader: strings.NewReader("charlie"),
},
{
Operation: "create",
TreePath: "Beta",
ContentReader: strings.NewReader("beta"),
},
{
Operation: "create",
TreePath: "alpha",
ContentReader: strings.NewReader("alpha"),
},
},
)
defer f()
req := NewRequest(t, "GET", "/"+repo.FullName())
resp := MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
filesList := htmlDoc.Find("#repo-files-table tbody tr").Map(func(_ int, s *goquery.Selection) string {
return s.AttrOr("data-entryname", "")
})
assert.EqualValues(t, []string{"Charlie", "alpha", "Beta", "delta", "licensa", "LICENSE", "licensz", "README.md", "zEta"}, filesList)
})
}