Skip to content
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

Show editorconfig warnings when viewing a malformed editorconfig #21257

Merged
merged 3 commits into from
Apr 6, 2023
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
21 changes: 10 additions & 11 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,35 +240,34 @@ func (r *Repository) FileExists(path, branch string) (bool, error) {

// GetEditorconfig returns the .editorconfig definition if found in the
// 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 {
return nil, nil
return nil, nil, nil
}
var (
err error
commit *git.Commit
)

var commit *git.Commit

if len(optCommit) != 0 {
commit = optCommit[0]
} else {
commit, err = r.GitRepo.GetBranchCommit(r.Repository.DefaultBranch)
if err != nil {
return nil, err
return nil, nil, err
}
}
treeEntry, err := commit.GetTreeEntryByPath(".editorconfig")
if err != nil {
return nil, err
return nil, nil, err
}
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()
if err != nil {
return nil, err
return nil, nil, err
}
defer reader.Close()
return editorconfig.Parse(reader)
return editorconfig.ParseGraceful(reader)
}

// RetrieveBaseRepo retrieves base repository
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func GetEditorconfig(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"

ec, err := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
ec, _, err := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
if err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound(err)
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func editFile(ctx *context.Context, isNewFile bool) {

// GetEditorConfig returns a editorconfig JSON string for given treePath or "null"
func GetEditorConfig(ctx *context.Context, treePath string) string {
ec, err := ctx.Repo.GetEditorconfig()
ec, _, err := ctx.Repo.GetEditorconfig()
if err == nil {
def, err := ec.GetDefinitionForFilename(treePath)
if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func SetEditorconfigIfExists(ctx *context.Context) {
return
}

ec, err := ctx.Repo.GetEditorconfig()
ec, _, err := ctx.Repo.GetEditorconfig()

if err != nil && !git.IsErrNotExist(err) {
description := fmt.Sprintf("Error while getting .editorconfig file: %v", err)
Expand Down
13 changes: 10 additions & 3 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,18 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
ctx.Data["RawFileLink"] = rawLink + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)

if ctx.Repo.TreePath == ".editorconfig" {
_, editorconfigErr := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
ctx.Data["FileError"] = editorconfigErr
_, editorconfigWarning, editorconfigErr := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
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) {
_, 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"
Expand Down
9 changes: 6 additions & 3 deletions templates/repo/view_file.tmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<div class="{{TabSizeClass .Editorconfig .FileName}} non-diff-file-content">
{{- 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="text left">
<div>{{.FileError}}</div>
</div>
<div class="text left gt-whitespace-pre">{{.FileWarning}}</div>
</div>
{{end}}
<h4 class="file-header ui top attached header gt-df gt-ac gt-sb gt-fw">
Expand Down
1 change: 1 addition & 0 deletions web_src/css/helpers.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
.gt-overflow-x-scroll { overflow-x: scroll !important; }
.gt-cursor-default { cursor: default !important; }
.gt-items-start { align-items: flex-start !important; }
.gt-whitespace-pre { white-space: pre !important }

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