From 226231ea27d4f2b0f09fa4efb39501507613b284 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 22 Mar 2024 19:17:30 +0800 Subject: [PATCH] Fix some pending problems (#29985) These changes are quite independent and trivial, so I don't want to open too many PRs. * https://github.com/go-gitea/gitea/pull/29882#discussion_r1529607091 * the `f.Close` should be called properly * the error message could be more meaningful (https://github.com/go-gitea/gitea/pull/29882#pullrequestreview-1942557935) * https://github.com/go-gitea/gitea/pull/29859#pullrequestreview-1942324716 * the new translation strings don't take arguments * https://github.com/go-gitea/gitea/pull/28710#discussion_r1443778807 * stale for long time * #28140 * a form was forgotten to be changed to work with backend code --- models/asymkey/ssh_key_authorized_keys.go | 9 ++++----- modules/actions/log.go | 2 +- modules/git/commit.go | 5 ++--- modules/git/repo_stats.go | 6 +++--- modules/markup/csv/csv.go | 5 ++--- routers/web/repo/compare.go | 5 ++--- routers/web/repo/editor.go | 4 ++-- .../asymkey/ssh_key_authorized_principals.go | 9 ++++----- services/doctor/authorizedkeys.go | 6 +++--- templates/repo/issue/view_content/pull.tmpl | 20 +++++++++---------- templates/status/404.tmpl | 12 ++++------- .../js/components/PullRequestMergeForm.vue | 1 + 12 files changed, 37 insertions(+), 47 deletions(-) diff --git a/models/asymkey/ssh_key_authorized_keys.go b/models/asymkey/ssh_key_authorized_keys.go index 7621994866..2e4cd62e5c 100644 --- a/models/asymkey/ssh_key_authorized_keys.go +++ b/models/asymkey/ssh_key_authorized_keys.go @@ -139,6 +139,8 @@ func RegeneratePublicKeys(ctx context.Context, t io.StringWriter) error { if err != nil { return err } + defer f.Close() + scanner := bufio.NewScanner(f) for scanner.Scan() { line := scanner.Text() @@ -148,15 +150,12 @@ func RegeneratePublicKeys(ctx context.Context, t io.StringWriter) error { } _, err = t.WriteString(line + "\n") if err != nil { - f.Close() return err } } - err = scanner.Err() - if err != nil { - return fmt.Errorf("scan: %w", err) + if err = scanner.Err(); err != nil { + return fmt.Errorf("RegeneratePublicKeys scan: %w", err) } - f.Close() } return nil } diff --git a/modules/actions/log.go b/modules/actions/log.go index cdf18646aa..c38082b5dc 100644 --- a/modules/actions/log.go +++ b/modules/actions/log.go @@ -100,7 +100,7 @@ func ReadLogs(ctx context.Context, inStorage bool, filename string, offset, limi } if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("scan: %w", err) + return nil, fmt.Errorf("ReadLogs scan: %w", err) } return rows, nil diff --git a/modules/git/commit.go b/modules/git/commit.go index 789a2e8f69..ef2676762c 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -397,9 +397,8 @@ func (c *Commit) GetSubModules() (*ObjectCache, error) { } } } - err = scanner.Err() - if err != nil { - return nil, fmt.Errorf("scan: %w", err) + if err = scanner.Err(); err != nil { + return nil, fmt.Errorf("GetSubModules scan: %w", err) } return c.submoduleCache, nil diff --git a/modules/git/repo_stats.go b/modules/git/repo_stats.go index ce82946873..83220104bd 100644 --- a/modules/git/repo_stats.go +++ b/modules/git/repo_stats.go @@ -124,9 +124,9 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string) } } } - err = scanner.Err() - if err != nil { - return fmt.Errorf("scan: %w", err) + if err = scanner.Err(); err != nil { + _ = stdoutReader.Close() + return fmt.Errorf("GetCodeActivityStats scan: %w", err) } a := make([]*CodeActivityAuthor, 0, len(authors)) for _, v := range authors { diff --git a/modules/markup/csv/csv.go b/modules/markup/csv/csv.go index 50bb918442..1dd26eb8ac 100644 --- a/modules/markup/csv/csv.go +++ b/modules/markup/csv/csv.go @@ -124,9 +124,8 @@ func (Renderer) fallbackRender(input io.Reader, tmpBlock *bufio.Writer) error { return err } } - err = scan.Err() - if err != nil { - return fmt.Errorf("scan: %w", err) + if err = scan.Err(); err != nil { + return fmt.Errorf("fallbackRender scan: %w", err) } _, err = tmpBlock.WriteString("") diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index bf42b77b66..7b5243e6b7 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -980,9 +980,8 @@ func getExcerptLines(commit *git.Commit, filePath string, idxLeft, idxRight, chu } diffLines = append(diffLines, diffLine) } - err = scanner.Err() - if err != nil { - return nil, fmt.Errorf("scan: %w", err) + if err = scanner.Err(); err != nil { + return nil, fmt.Errorf("getExcerptLines scan: %w", err) } return diffLines, nil } diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index 29395b4013..082666276a 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -333,9 +333,9 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b ctx.Error(http.StatusInternalServerError, err.Error()) } } else if models.IsErrCommitIDDoesNotMatch(err) { - ctx.RenderWithErr(ctx.Tr("repo.editor.commit_id_not_matching", ctx.Repo.RepoLink+"/compare/"+util.PathEscapeSegments(form.LastCommit)+"..."+util.PathEscapeSegments(ctx.Repo.CommitID)), tplEditFile, &form) + ctx.RenderWithErr(ctx.Tr("repo.editor.commit_id_not_matching"), tplEditFile, &form) } else if git.IsErrPushOutOfDate(err) { - ctx.RenderWithErr(ctx.Tr("repo.editor.push_out_of_date", ctx.Repo.RepoLink+"/compare/"+util.PathEscapeSegments(form.LastCommit)+"..."+util.PathEscapeSegments(form.NewBranchName)), tplEditFile, &form) + ctx.RenderWithErr(ctx.Tr("repo.editor.push_out_of_date"), tplEditFile, &form) } else if git.IsErrPushRejected(err) { errPushRej := err.(*git.ErrPushRejected) if len(errPushRej.Message) == 0 { diff --git a/services/asymkey/ssh_key_authorized_principals.go b/services/asymkey/ssh_key_authorized_principals.go index 822dd0ffe7..2838bb5fc7 100644 --- a/services/asymkey/ssh_key_authorized_principals.go +++ b/services/asymkey/ssh_key_authorized_principals.go @@ -109,6 +109,8 @@ func regeneratePrincipalKeys(ctx context.Context, t io.StringWriter) error { if err != nil { return err } + defer f.Close() + scanner := bufio.NewScanner(f) for scanner.Scan() { line := scanner.Text() @@ -118,15 +120,12 @@ func regeneratePrincipalKeys(ctx context.Context, t io.StringWriter) error { } _, err = t.WriteString(line + "\n") if err != nil { - f.Close() return err } } - err = scanner.Err() - if err != nil { - return fmt.Errorf("scan: %w", err) + if err = scanner.Err(); err != nil { + return fmt.Errorf("regeneratePrincipalKeys scan: %w", err) } - f.Close() } return nil } diff --git a/services/doctor/authorizedkeys.go b/services/doctor/authorizedkeys.go index bc0266c4bc..8d6fc9cb5e 100644 --- a/services/doctor/authorizedkeys.go +++ b/services/doctor/authorizedkeys.go @@ -51,11 +51,11 @@ func checkAuthorizedKeys(ctx context.Context, logger log.Logger, autofix bool) e } linesInAuthorizedKeys.Add(line) } - err = scanner.Err() - if err != nil { + if err = scanner.Err(); err != nil { return fmt.Errorf("scan: %w", err) } - f.Close() + // although there is a "defer close" above, here close explicitly before the generating, because it needs to open the file for writing again + _ = f.Close() // now we regenerate and check if there are any lines missing regenerated := &bytes.Buffer{} diff --git a/templates/repo/issue/view_content/pull.tmpl b/templates/repo/issue/view_content/pull.tmpl index c8e8038438..aac30180df 100644 --- a/templates/repo/issue/view_content/pull.tmpl +++ b/templates/repo/issue/view_content/pull.tmpl @@ -374,17 +374,15 @@ */}} {{if and $.StillCanManualMerge (not $showGeneralMergeForm)}}
-
-
- {{.CsrfTokenHtml}} -
- -
- -
-
+
{{/* another similar form is in PullRequestMergeForm.vue*/}} + {{.CsrfTokenHtml}} +
+ +
+ +
{{end}} {{if and .Issue.PullRequest.HeadRepo (not .Issue.PullRequest.HasMerged) (not .Issue.IsClosed)}} diff --git a/templates/status/404.tmpl b/templates/status/404.tmpl index f1f1199665..78f149e67b 100644 --- a/templates/status/404.tmpl +++ b/templates/status/404.tmpl @@ -1,14 +1,10 @@ {{template "base/head" .}} -
+
{{if .IsRepo}}{{template "repo/header" .}}{{end}} -
-

404

+
+ 404

{{if .NotFoundPrompt}}{{.NotFoundPrompt}}{{else}}{{ctx.Locale.Tr "error404"}}{{end}}

- {{if .NotFoundGoBackURL}}{{ctx.Locale.Tr "go_back"}}{{end}} - -
-
- {{if .ShowFooterVersion}}

{{ctx.Locale.Tr "admin.config.app_ver"}}: {{AppVer}}

{{end}} + {{if .NotFoundGoBackURL}}{{ctx.Locale.Tr "go_back"}}{{end}}
{{template "base/footer" .}} diff --git a/web_src/js/components/PullRequestMergeForm.vue b/web_src/js/components/PullRequestMergeForm.vue index b0b10b6252..170d0d85c6 100644 --- a/web_src/js/components/PullRequestMergeForm.vue +++ b/web_src/js/components/PullRequestMergeForm.vue @@ -94,6 +94,7 @@ export default {
+