From ad43486cd3ab65b5b023afb5ad5b62aa862d1345 Mon Sep 17 00:00:00 2001 From: Chongyi Zheng Date: Tue, 29 Aug 2023 11:47:26 -0400 Subject: [PATCH] Fix some slice append usages (#26778) Co-authored-by: delvh --- modules/setting/service.go | 2 +- routers/api/v1/repo/pull.go | 16 +++++++--------- services/repository/files/temp_repo.go | 6 +++--- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/modules/setting/service.go b/modules/setting/service.go index 595ea6528..74a7e90f7 100644 --- a/modules/setting/service.go +++ b/modules/setting/service.go @@ -223,7 +223,7 @@ func loadServiceFrom(rootCfg ConfigProvider) { Service.UserDeleteWithCommentsMaxTime = sec.Key("USER_DELETE_WITH_COMMENTS_MAX_TIME").MustDuration(0) sec.Key("VALID_SITE_URL_SCHEMES").MustString("http,https") Service.ValidSiteURLSchemes = sec.Key("VALID_SITE_URL_SCHEMES").Strings(",") - schemes := make([]string, len(Service.ValidSiteURLSchemes)) + schemes := make([]string, 0, len(Service.ValidSiteURLSchemes)) for _, scheme := range Service.ValidSiteURLSchemes { scheme = strings.ToLower(strings.TrimSpace(scheme)) if scheme != "" { diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index a507c1f44..58f2fc69c 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -326,11 +326,9 @@ func CreatePullRequest(ctx *context.APIContext) { return } - labelIDs = make([]int64, len(form.Labels)) - orgLabelIDs := make([]int64, len(form.Labels)) - - for i := range labels { - labelIDs[i] = labels[i].ID + labelIDs = make([]int64, 0, len(labels)) + for _, label := range labels { + labelIDs = append(labelIDs, label.ID) } if ctx.Repo.Owner.IsOrganization() { @@ -340,12 +338,12 @@ func CreatePullRequest(ctx *context.APIContext) { return } - for i := range orgLabels { - orgLabelIDs[i] = orgLabels[i].ID + orgLabelIDs := make([]int64, 0, len(orgLabels)) + for _, orgLabel := range orgLabels { + orgLabelIDs = append(orgLabelIDs, orgLabel.ID) } + labelIDs = append(labelIDs, orgLabelIDs...) } - - labelIDs = append(labelIDs, orgLabelIDs...) } if form.Milestone > 0 { diff --git a/services/repository/files/temp_repo.go b/services/repository/files/temp_repo.go index a086d15a4..7f6b8137a 100644 --- a/services/repository/files/temp_repo.go +++ b/services/repository/files/temp_repo.go @@ -114,12 +114,12 @@ func (t *TemporaryUploadRepository) LsFiles(filenames ...string) ([]string, erro return nil, err } - filelist := make([]string, len(filenames)) + fileList := make([]string, 0, len(filenames)) for _, line := range bytes.Split(stdOut.Bytes(), []byte{'\000'}) { - filelist = append(filelist, string(line)) + fileList = append(fileList, string(line)) } - return filelist, nil + return fileList, nil } // RemoveFilesFromIndex removes the given files from the index