1
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-12-04 14:46:57 -05:00

Use graceful editorconfig loader to reduce errors when loading malformed editorconfigs (#21257)

The _graceful_ should fail less when the `.editorconfig` file isn't
properly written, e.g. boolean values from YAML or unparseable numbers
(when a number is expected). As is... information is lost as the
_warning_ (a go-multierror.Error) is ignored. If anybody knows how to
send them to the UI as warning; any help is appreciated.

Closes #20694

Signed-off-by: Yoan Blanc <yoan@dosimple.ch>
This commit is contained in:
Yoan Blanc 2023-04-06 22:01:20 +02:00 committed by GitHub
parent 797babbfcb
commit 9b416b2e36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 20 deletions

View File

@ -240,35 +240,34 @@ func (r *Repository) FileExists(path, branch string) (bool, error) {
// GetEditorconfig returns the .editorconfig definition if found in the // GetEditorconfig returns the .editorconfig definition if found in the
// HEAD of the default repo branch. // HEAD of the default repo branch.
func (r *Repository) GetEditorconfig(optCommit ...*git.Commit) (*editorconfig.Editorconfig, error) { func (r *Repository) GetEditorconfig(optCommit ...*git.Commit) (cfg *editorconfig.Editorconfig, warning, err error) {
if r.GitRepo == nil { if r.GitRepo == nil {
return nil, nil return nil, nil, nil
} }
var (
err error var commit *git.Commit
commit *git.Commit
)
if len(optCommit) != 0 { if len(optCommit) != 0 {
commit = optCommit[0] commit = optCommit[0]
} else { } else {
commit, err = r.GitRepo.GetBranchCommit(r.Repository.DefaultBranch) commit, err = r.GitRepo.GetBranchCommit(r.Repository.DefaultBranch)
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
} }
treeEntry, err := commit.GetTreeEntryByPath(".editorconfig") treeEntry, err := commit.GetTreeEntryByPath(".editorconfig")
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
if treeEntry.Blob().Size() >= setting.UI.MaxDisplayFileSize { if treeEntry.Blob().Size() >= setting.UI.MaxDisplayFileSize {
return nil, git.ErrNotExist{ID: "", RelPath: ".editorconfig"} return nil, nil, git.ErrNotExist{ID: "", RelPath: ".editorconfig"}
} }
reader, err := treeEntry.Blob().DataAsync() reader, err := treeEntry.Blob().DataAsync()
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
defer reader.Close() defer reader.Close()
return editorconfig.Parse(reader) return editorconfig.ParseGraceful(reader)
} }
// RetrieveBaseRepo retrieves base repository // RetrieveBaseRepo retrieves base repository

View File

@ -381,7 +381,7 @@ func GetEditorconfig(ctx *context.APIContext) {
// "404": // "404":
// "$ref": "#/responses/notFound" // "$ref": "#/responses/notFound"
ec, err := ctx.Repo.GetEditorconfig(ctx.Repo.Commit) ec, _, err := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
if err != nil { if err != nil {
if git.IsErrNotExist(err) { if git.IsErrNotExist(err) {
ctx.NotFound(err) ctx.NotFound(err)

View File

@ -165,7 +165,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
// GetEditorConfig returns a editorconfig JSON string for given treePath or "null" // GetEditorConfig returns a editorconfig JSON string for given treePath or "null"
func GetEditorConfig(ctx *context.Context, treePath string) string { func GetEditorConfig(ctx *context.Context, treePath string) string {
ec, err := ctx.Repo.GetEditorconfig() ec, _, err := ctx.Repo.GetEditorconfig()
if err == nil { if err == nil {
def, err := ec.GetDefinitionForFilename(treePath) def, err := ec.GetDefinitionForFilename(treePath)
if err == nil { if err == nil {

View File

@ -19,7 +19,7 @@ func SetEditorconfigIfExists(ctx *context.Context) {
return return
} }
ec, err := ctx.Repo.GetEditorconfig() ec, _, err := ctx.Repo.GetEditorconfig()
if err != nil && !git.IsErrNotExist(err) { if err != nil && !git.IsErrNotExist(err) {
description := fmt.Sprintf("Error while getting .editorconfig file: %v", err) description := fmt.Sprintf("Error while getting .editorconfig file: %v", err)

View File

@ -346,11 +346,18 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
ctx.Data["RawFileLink"] = rawLink + "/" + util.PathEscapeSegments(ctx.Repo.TreePath) ctx.Data["RawFileLink"] = rawLink + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
if ctx.Repo.TreePath == ".editorconfig" { if ctx.Repo.TreePath == ".editorconfig" {
_, editorconfigErr := ctx.Repo.GetEditorconfig(ctx.Repo.Commit) _, editorconfigWarning, editorconfigErr := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
ctx.Data["FileError"] = editorconfigErr if editorconfigWarning != nil {
ctx.Data["FileWarning"] = strings.TrimSpace(editorconfigWarning.Error())
}
if editorconfigErr != nil {
ctx.Data["FileError"] = strings.TrimSpace(editorconfigErr.Error())
}
} else if ctx.Repo.IsIssueConfig(ctx.Repo.TreePath) { } else if ctx.Repo.IsIssueConfig(ctx.Repo.TreePath) {
_, issueConfigErr := ctx.Repo.GetIssueConfig(ctx.Repo.TreePath, ctx.Repo.Commit) _, issueConfigErr := ctx.Repo.GetIssueConfig(ctx.Repo.TreePath, ctx.Repo.Commit)
ctx.Data["FileError"] = issueConfigErr if issueConfigErr != nil {
ctx.Data["FileError"] = strings.TrimSpace(issueConfigErr.Error())
}
} }
isDisplayingSource := ctx.FormString("display") == "source" isDisplayingSource := ctx.FormString("display") == "source"

View File

@ -1,9 +1,12 @@
<div class="{{TabSizeClass .Editorconfig .FileName}} non-diff-file-content"> <div class="{{TabSizeClass .Editorconfig .FileName}} non-diff-file-content">
{{- if .FileError}} {{- if .FileError}}
<div class="ui error message">
<div class="text left gt-whitespace-pre">{{.FileError}}</div>
</div>
{{end}}
{{- if .FileWarning}}
<div class="ui warning message"> <div class="ui warning message">
<div class="text left"> <div class="text left gt-whitespace-pre">{{.FileWarning}}</div>
<div>{{.FileError}}</div>
</div>
</div> </div>
{{end}} {{end}}
<h4 class="file-header ui top attached header gt-df gt-ac gt-sb gt-fw"> <h4 class="file-header ui top attached header gt-df gt-ac gt-sb gt-fw">

View File

@ -25,6 +25,7 @@
.gt-overflow-x-scroll { overflow-x: scroll !important; } .gt-overflow-x-scroll { overflow-x: scroll !important; }
.gt-cursor-default { cursor: default !important; } .gt-cursor-default { cursor: default !important; }
.gt-items-start { align-items: flex-start !important; } .gt-items-start { align-items: flex-start !important; }
.gt-whitespace-pre { white-space: pre !important }
.gt-mono { .gt-mono {
font-family: var(--fonts-monospace) !important; font-family: var(--fonts-monospace) !important;