diff options
author | Runxi Yu <me@runxiyu.org> | 2025-04-05 20:23:08 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-04-05 20:23:08 +0800 |
commit | 485dcfb6685f13751f7018bec2e058931b938d39 (patch) | |
tree | c9fe357b9a8c2f8853141828443ad1680022781b | |
parent | config shall no longer be a global variable (diff) | |
download | forge-485dcfb6685f13751f7018bec2e058931b938d39.tar.gz forge-485dcfb6685f13751f7018bec2e058931b938d39.tar.zst forge-485dcfb6685f13751f7018bec2e058931b938d39.zip |
misc: Move utils.go's string function to misc
-rw-r--r-- | http_handle_branches.go | 3 | ||||
-rw-r--r-- | http_handle_repo_index.go | 7 | ||||
-rw-r--r-- | misc/misc.go | 13 | ||||
-rw-r--r-- | utils.go | 17 |
4 files changed, 15 insertions, 25 deletions
diff --git a/http_handle_branches.go b/http_handle_branches.go index d386b82..01a162a 100644 --- a/http_handle_branches.go +++ b/http_handle_branches.go @@ -10,6 +10,7 @@ import ( "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 @@ func (s *server) httpHandleRepoBranches(writer http.ResponseWriter, _ *http.Requ 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 c253fa9..c6338cf 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 @@ func (s *server) httpHandleRepoIndex(w http.ResponseWriter, req *http.Request, p _, 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 @@ func (s *server) httpHandleRepoIndex(w http.ResponseWriter, req *http.Request, p 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 bc48486..ee0fd7a 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 8ede372..0000000 --- a/utils.go +++ /dev/null @@ -1,17 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only -// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> - -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 -} |