Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Mar 10, 2025
1 parent c102492 commit f3a1752
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 3,274 deletions.
42 changes: 17 additions & 25 deletions modules/fileicon/material.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ import (
)

type materialIconRulesData struct {
IconDefinitions map[string]*struct {
IconPath string `json:"iconPath"`
} `json:"iconDefinitions"`
FileNames map[string]string `json:"fileNames"`
FolderNames map[string]string `json:"folderNames"`
FileExtensions map[string]string `json:"fileExtensions"`
Expand All @@ -36,6 +33,7 @@ type MaterialIconProvider struct {
var materialIconProvider MaterialIconProvider

func DefaultMaterialIconProvider() *MaterialIconProvider {
materialIconProvider.once.Do(materialIconProvider.loadData)
return &materialIconProvider
}

Expand Down Expand Up @@ -88,8 +86,6 @@ func (m *MaterialIconProvider) renderFileIconSVG(ctx reqctx.RequestContext, name
}

func (m *MaterialIconProvider) FileIcon(ctx reqctx.RequestContext, entry *git.TreeEntry) template.HTML {
m.once.Do(m.loadData)

if m.rules == nil {
return BasicThemeIcon(entry)
}
Expand All @@ -101,7 +97,7 @@ func (m *MaterialIconProvider) FileIcon(ctx reqctx.RequestContext, entry *git.Tr
return svg.RenderHTML("octicon-file-symlink-file") // TODO: find some better icons for them
}

name := m.findIconName(entry)
name := m.findIconNameByGit(entry)
if name == "folder" {
// the material icon pack's "folder" icon doesn't look good, so use our built-in one
return svg.RenderHTML("material-folder-generic")
Expand All @@ -112,34 +108,23 @@ func (m *MaterialIconProvider) FileIcon(ctx reqctx.RequestContext, entry *git.Tr
return svg.RenderHTML("octicon-file")
}

func (m *MaterialIconProvider) findIconName(entry *git.TreeEntry) string {
if entry.IsSubModule() {
return "folder-git"
}

func (m *MaterialIconProvider) FindIconName(name string, isDir bool) string {
iconsData := m.rules
fileName := path.Base(entry.Name())

if entry.IsDir() {
if s, ok := iconsData.FolderNames[fileName]; ok {
return s
}
if s, ok := iconsData.FolderNames[strings.ToLower(fileName)]; ok {
fileNameLower := strings.ToLower(path.Base(name))
if isDir {
if s, ok := iconsData.FolderNames[fileNameLower]; ok {
return s
}
return "folder"
}

if s, ok := iconsData.FileNames[fileName]; ok {
return s
}
if s, ok := iconsData.FileNames[strings.ToLower(fileName)]; ok {
if s, ok := iconsData.FileNames[fileNameLower]; ok {
return s
}

for i := len(fileName) - 1; i >= 0; i-- {
if fileName[i] == '.' {
ext := fileName[i+1:]
for i := len(fileNameLower) - 1; i >= 0; i-- {
if fileNameLower[i] == '.' {
ext := fileNameLower[i+1:]
if s, ok := iconsData.FileExtensions[ext]; ok {
return s
}
Expand All @@ -148,3 +133,10 @@ func (m *MaterialIconProvider) findIconName(entry *git.TreeEntry) string {

return "file"
}

func (m *MaterialIconProvider) findIconNameByGit(entry *git.TreeEntry) string {
if entry.IsSubModule() {
return "folder-git"
}
return m.FindIconName(entry.Name(), entry.IsDir())
}
21 changes: 21 additions & 0 deletions modules/fileicon/material_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fileicon_test

import (
"testing"

"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/fileicon"

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

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{FixtureFiles: []string{}})
}

func TestFindIconName(t *testing.T) {
unittest.PrepareTestEnv(t)
p := fileicon.DefaultMaterialIconProvider()
assert.Equal(t, "php", p.FindIconName("foo.php", false))
assert.Equal(t, "php", p.FindIconName("foo.PHP", false))
}
6 changes: 3 additions & 3 deletions modules/git/commit_info_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath
log.Debug("missing commit for %s", entry.Name())
}

// If the entry if a submodule add a submodule file for this
// If the entry is a submodule add a submodule file for this
if entry.IsSubModule() {
subModuleURL := ""
var fullPath string
Expand All @@ -85,8 +85,8 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath
}

// Retrieve the commit for the treePath itself (see above). We basically
// get it for free during the tree traversal and it's used for listing
// pages to display information about newest commit for a given path.
// get it for free during the tree traversal, and it's used for listing
// pages to display information about the newest commit for a given path.
var treeCommit *Commit
var ok bool
if treePath == "" {
Expand Down
Loading

0 comments on commit f3a1752

Please sign in to comment.