Skip to content

Commit d888a32

Browse files
greutsilverwind
authored andcommitted
fix: use graceful Editorconfig loader
Signed-off-by: Yoan Blanc <yoan@dosimple.ch>
1 parent 0983b23 commit d888a32

File tree

7 files changed

+30
-20
lines changed

7 files changed

+30
-20
lines changed

modules/context/repo.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -240,35 +240,34 @@ func (r *Repository) FileExists(path, branch string) (bool, error) {
240240

241241
// GetEditorconfig returns the .editorconfig definition if found in the
242242
// HEAD of the default repo branch.
243-
func (r *Repository) GetEditorconfig(optCommit ...*git.Commit) (*editorconfig.Editorconfig, error) {
243+
func (r *Repository) GetEditorconfig(optCommit ...*git.Commit) (cfg *editorconfig.Editorconfig, warning, err error) {
244244
if r.GitRepo == nil {
245-
return nil, nil
245+
return nil, nil, nil
246246
}
247-
var (
248-
err error
249-
commit *git.Commit
250-
)
247+
248+
var commit *git.Commit
249+
251250
if len(optCommit) != 0 {
252251
commit = optCommit[0]
253252
} else {
254253
commit, err = r.GitRepo.GetBranchCommit(r.Repository.DefaultBranch)
255254
if err != nil {
256-
return nil, err
255+
return nil, nil, err
257256
}
258257
}
259258
treeEntry, err := commit.GetTreeEntryByPath(".editorconfig")
260259
if err != nil {
261-
return nil, err
260+
return nil, nil, err
262261
}
263262
if treeEntry.Blob().Size() >= setting.UI.MaxDisplayFileSize {
264-
return nil, git.ErrNotExist{ID: "", RelPath: ".editorconfig"}
263+
return nil, nil, git.ErrNotExist{ID: "", RelPath: ".editorconfig"}
265264
}
266265
reader, err := treeEntry.Blob().DataAsync()
267266
if err != nil {
268-
return nil, err
267+
return nil, nil, err
269268
}
270269
defer reader.Close()
271-
return editorconfig.Parse(reader)
270+
return editorconfig.ParseGraceful(reader)
272271
}
273272

274273
// RetrieveBaseRepo retrieves base repository

routers/api/v1/repo/file.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ func GetEditorconfig(ctx *context.APIContext) {
381381
// "404":
382382
// "$ref": "#/responses/notFound"
383383

384-
ec, err := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
384+
ec, _, err := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
385385
if err != nil {
386386
if git.IsErrNotExist(err) {
387387
ctx.NotFound(err)

routers/web/repo/editor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
165165

166166
// GetEditorConfig returns a editorconfig JSON string for given treePath or "null"
167167
func GetEditorConfig(ctx *context.Context, treePath string) string {
168-
ec, err := ctx.Repo.GetEditorconfig()
168+
ec, _, err := ctx.Repo.GetEditorconfig()
169169
if err == nil {
170170
def, err := ec.GetDefinitionForFilename(treePath)
171171
if err == nil {

routers/web/repo/middlewares.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func SetEditorconfigIfExists(ctx *context.Context) {
1919
return
2020
}
2121

22-
ec, err := ctx.Repo.GetEditorconfig()
22+
ec, _, err := ctx.Repo.GetEditorconfig()
2323

2424
if err != nil && !git.IsErrNotExist(err) {
2525
description := fmt.Sprintf("Error while getting .editorconfig file: %v", err)

routers/web/repo/view.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,18 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
346346
ctx.Data["RawFileLink"] = rawLink + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
347347

348348
if ctx.Repo.TreePath == ".editorconfig" {
349-
_, editorconfigErr := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
350-
ctx.Data["FileError"] = editorconfigErr
349+
_, editorconfigWarning, editorconfigErr := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
350+
if editorconfigWarning != nil {
351+
ctx.Data["FileWarning"] = strings.TrimSpace(editorconfigWarning.Error())
352+
}
353+
if editorconfigErr != nil {
354+
ctx.Data["FileError"] = strings.TrimSpace(editorconfigErr.Error())
355+
}
351356
} else if ctx.Repo.IsIssueConfig(ctx.Repo.TreePath) {
352357
_, issueConfigErr := ctx.Repo.GetIssueConfig(ctx.Repo.TreePath, ctx.Repo.Commit)
353-
ctx.Data["FileError"] = issueConfigErr
358+
if issueConfigErr != nil {
359+
ctx.Data["FileError"] = strings.TrimSpace(issueConfigErr.Error())
360+
}
354361
}
355362

356363
isDisplayingSource := ctx.FormString("display") == "source"

templates/repo/view_file.tmpl

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<div class="{{TabSizeClass .Editorconfig .FileName}} non-diff-file-content">
22
{{- if .FileError}}
3+
<div class="ui error message">
4+
<div class="text left gt-whitespace-pre">{{.FileError}}</div>
5+
</div>
6+
{{end}}
7+
{{- if .FileWarning}}
38
<div class="ui warning message">
4-
<div class="text left">
5-
<div>{{.FileError}}</div>
6-
</div>
9+
<div class="text left gt-whitespace-pre">{{.FileWarning}}</div>
710
</div>
811
{{end}}
912
<h4 class="file-header ui top attached header gt-df gt-ac gt-sb gt-fw">

web_src/css/helpers.css

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
.gt-overflow-x-scroll { overflow-x: scroll !important; }
2626
.gt-cursor-default { cursor: default !important; }
2727
.gt-items-start { align-items: flex-start !important; }
28+
.gt-whitespace-pre { white-space: pre !important }
2829

2930
.gt-mono {
3031
font-family: var(--fonts-monospace) !important;

0 commit comments

Comments
 (0)