From 485dcfb6685f13751f7018bec2e058931b938d39 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sat, 05 Apr 2025 20:23:08 +0800 Subject: [PATCH] misc: Move utils.go's string function to misc --- http_handle_branches.go | 3 ++- http_handle_repo_index.go | 7 ------- misc/misc.go | 13 +++++++++++++ utils.go | 17 ----------------- diff --git a/http_handle_branches.go b/http_handle_branches.go index d386b8236e980ab53497e6a83b9dab57b87384f3..01a162a3a6322172129d669d1261e772bde0960a 100644 --- a/http_handle_branches.go +++ b/http_handle_branches.go @@ -10,6 +10,7 @@ "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/storer" + "go.lindenii.runxiyu.org/forge/misc" ) // httpHandleRepoBranches provides the branches page in repos. @@ -24,7 +25,7 @@ var branchesIter storer.ReferenceIter repo, repoName, groupPath = params["repo"].(*git.Repository), params["repo_name"].(string), params["group_path"].([]string) - if strings.Contains(repoName, "\n") || sliceContainsNewlines(groupPath) { + if strings.Contains(repoName, "\n") || misc.SliceContainsNewlines(groupPath) { notes = append(notes, "Path contains newlines; HTTP Git access impossible") } diff --git a/http_handle_repo_index.go b/http_handle_repo_index.go index c253fa95d5839df554b5d43ad245cd16cfa1cbc1..c6338cfccd3fb1b6cb62b64018a4cfc3d4148e82 100644 --- a/http_handle_repo_index.go +++ b/http_handle_repo_index.go @@ -5,7 +5,6 @@ package main import ( "net/http" - "strings" "go.lindenii.runxiyu.org/forge/git2c" "go.lindenii.runxiyu.org/forge/render" @@ -26,11 +25,6 @@ groupPath := params["group_path"].([]string) _, repoPath, _, _, _, _, _ := s.getRepoInfo(req.Context(), groupPath, repoName, "") // TODO: Don't use getRepoInfo - var notes []string - if strings.Contains(repoName, "\n") || sliceContainsNewlines(groupPath) { - notes = append(notes, "Path contains newlines; HTTP Git access impossible") - } - client, err := git2c.NewClient(s.config.Git.Socket) if err != nil { errorPage500(w, params, err.Error()) @@ -47,7 +41,6 @@ params["commits"] = commits params["readme_filename"] = readme.Filename _, params["readme"] = render.Readme(readme.Content, readme.Filename) - params["notes"] = notes renderTemplate(w, "repo_index", params) diff --git a/misc/misc.go b/misc/misc.go index bc48486121fc20ba406e1f4ef8dc18f86fd4e980..ee0fd7ab8bd2cab7d6a8577da7a96d55eb40e058 100644 --- a/misc/misc.go +++ b/misc/misc.go @@ -1,8 +1,21 @@ package misc +import "strings" + func FirstOrPanic[T any](v T, err error) T { if err != nil { panic(err) } return v } + +// sliceContainsNewlines returns true if and only if the given slice contains +// one or more strings that contains newlines. +func SliceContainsNewlines(s []string) bool { + for _, v := range s { + if strings.Contains(v, "\n") { + return true + } + } + return false +} diff --git a/utils.go b/utils.go deleted file mode 100644 index 8ede372a301e54821e71f04f53b1b5d126c7cf50..0000000000000000000000000000000000000000 --- a/utils.go +++ /dev/null @@ -1,17 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only -// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu - -package main - -import "strings" - -// sliceContainsNewlines returns true if and only if the given slice contains -// one or more strings that contains newlines. -func sliceContainsNewlines(s []string) bool { - for _, v := range s { - if strings.Contains(v, "\n") { - return true - } - } - return false -} -- 2.48.1