simplegit

go fix string/bytes splitting, error matching, and loop modernization.

e448f99c528fb40774143392aa8f664f4d8f9c21
cjosie <administrator@josie-c.com> · 2026-10-21T00:35 · browse files at this commit

parents: aafdf3e

diff --git a/cmd/simplegit/gc_test.go b/cmd/simplegit/gc_test.go
index 5ddd4d6..e0a2f1d 100644
--- a/cmd/simplegit/gc_test.go
+++ b/cmd/simplegit/gc_test.go
@@ -167,7 +167,7 @@ func TestGCVacuum(t *testing.T) {
 	if _, err := db.CreateUser(database, "josie", "hash"); err != nil {
 		t.Fatalf("CreateUser: %v", err)
 	}
-	for i := 0; i < 200; i++ {
+	for i := range 200 {
 		if err := db.CreateSession(database, strconv.Itoa(i), 1, time.Now().Unix()+60); err != nil {
 			t.Fatalf("CreateSession: %v", err)
 		}
diff --git a/cmd/simplegit/main_test.go b/cmd/simplegit/main_test.go
index 950673d..bb41509 100644
--- a/cmd/simplegit/main_test.go
+++ b/cmd/simplegit/main_test.go
@@ -230,7 +230,7 @@ func TestRunHookTagPushIdempotent(t *testing.T) {
 	zero := strings.Repeat("0", 40)
 	sha, _ := git.ResolveCommit(repoPath, "v1.0.0")
 	stdin := zero + " " + sha + " refs/tags/v1.0.0\n"
-	for i := 0; i < 2; i++ {
+	for i := range 2 {
 		if err := runHookWithStdin(t, repoPath, stdin); err != nil {
 			t.Fatalf("runHook %d: %v", i, err)
 		}
diff --git a/internal/git/browse.go b/internal/git/browse.go
index b4a62c9..77efce9 100644
--- a/internal/git/browse.go
+++ b/internal/git/browse.go
@@ -7,6 +7,7 @@ import (
 	"fmt"
 	"io"
 	"os/exec"
+	"slices"
 	"strconv"
 	"strings"
 )
@@ -35,7 +36,7 @@ func LsTree(repoPath, ref, dir string) ([]TreeEntry, error) {
 	}
 
 	var entries []TreeEntry
-	for _, line := range bytes.Split(out, []byte{0}) {
+	for line := range bytes.SplitSeq(out, []byte{0}) {
 		if len(line) == 0 {
 			continue
 		}
@@ -66,7 +67,7 @@ func LsTreePaths(repoPath, ref string) ([]string, error) {
 		return nil, fmt.Errorf("git ls-tree -r %s in %s: %w: %s", ref, repoPath, err, strings.TrimSpace(stderr.String()))
 	}
 	var paths []string
-	for _, p := range bytes.Split(out, []byte{0}) {
+	for p := range bytes.SplitSeq(out, []byte{0}) {
 		if len(p) > 0 {
 			paths = append(paths, string(p))
 		}
@@ -127,7 +128,7 @@ func forEachRefNames(repoPath string, patterns ...string) ([]string, error) {
 		return nil, fmt.Errorf("git for-each-ref in %s: %w: %s", repoPath, err, strings.TrimSpace(stderr.String()))
 	}
 	var names []string
-	for _, line := range strings.Split(string(out), "\n") {
+	for line := range strings.SplitSeq(string(out), "\n") {
 		if name := strings.TrimSpace(line); name != "" {
 			names = append(names, name)
 		}
@@ -199,11 +200,8 @@ func CatFileInfo(repoPath, object string, max int) (objectType string, size int,
 			return "", 0, false, false, fmt.Errorf("read cat-file body: %w", err)
 		}
 		truncated = n > max
-		for _, b := range buf[:min(n, max)] {
-			if b == 0 {
-				isBinary = true
-				break
-			}
+		if slices.Contains(buf[:min(n, max)], 0) {
+			isBinary = true
 		}
 	}
 	done()
@@ -229,7 +227,7 @@ const (
 
 func parseLog(out []byte) []Commit {
 	var commits []Commit
-	for _, rec := range strings.Split(string(out), recordSep) {
+	for rec := range strings.SplitSeq(string(out), recordSep) {
 		rec = strings.TrimPrefix(rec, "\n")
 		if rec == "" {
 			continue
diff --git a/internal/git/tags.go b/internal/git/tags.go
index b82bbf8..a738cba 100644
--- a/internal/git/tags.go
+++ b/internal/git/tags.go
@@ -33,7 +33,7 @@ func Tags(repoPath string) ([]Tag, error) {
 	}
 
 	var tags []Tag
-	for _, line := range strings.Split(string(out), "\n") {
+	for line := range strings.SplitSeq(string(out), "\n") {
 		fields := strings.Fields(line)
 		if len(fields) < 3 {
 			continue
@@ -107,8 +107,8 @@ func TagMessage(repoPath, name string) (subject, body string, found bool, err er
 		return "", "", false, nil
 	}
 	subject = message
-	if idx := strings.IndexByte(message, '\n'); idx >= 0 {
-		subject, body = message[:idx], strings.TrimSpace(message[idx+1:])
+	if before, after, ok := strings.Cut(message, "\n"); ok {
+		subject, body = before, strings.TrimSpace(after)
 	}
 	return subject, body, true, nil
 }
diff --git a/internal/web/git_test.go b/internal/web/git_test.go
index 7b66826..cf74572 100644
--- a/internal/web/git_test.go
+++ b/internal/web/git_test.go
@@ -280,7 +280,7 @@ func TestGitBasicAuthRateLimited(t *testing.T) {
 	createRepo(t, newLoggedInClient(t, httpServer), httpServer, "sec", "private")
 	refsURL := httpServer.URL + "/sec.git/info/refs?service=git-receive-pack"
 
-	for i := 0; i < loginAttempts; i++ {
+	for i := range loginAttempts {
 		req, err := http.NewRequest("GET", refsURL, nil)
 		if err != nil {
 			t.Fatalf("request %d: %v", i+1, err)
diff --git a/internal/web/releases.go b/internal/web/releases.go
index 75338cb..b594993 100644
--- a/internal/web/releases.go
+++ b/internal/web/releases.go
@@ -338,8 +338,7 @@ func (s *Server) handleCreateRelease(w http.ResponseWriter, r *http.Request) {
 
 	r.Body = http.MaxBytesReader(w, r.Body, releaseUploadMax)
 	if err := r.ParseMultipartForm(8 << 20); err != nil {
-		var maxErr *http.MaxBytesError
-		if errors.As(err, &maxErr) {
+		if _, ok := errors.AsType[*http.MaxBytesError](err); ok {
 			http.Error(w, "upload is too large", http.StatusRequestEntityTooLarge)
 			return
 		}
diff --git a/internal/web/web_test.go b/internal/web/web_test.go
index 03d73c2..ff2e4b1 100644
--- a/internal/web/web_test.go
+++ b/internal/web/web_test.go
@@ -355,7 +355,7 @@ func TestHomeListsRepos(t *testing.T) {
 func TestLoginRateLimited(t *testing.T) {
 	httpServer, _, _ := newTestServer(t)
 	client := httpServer.Client()
-	for i := 0; i < loginAttempts; i++ {
+	for i := range loginAttempts {
 		resp, err := client.PostForm(httpServer.URL+"/login",
 			url.Values{"username": {"josie"}, "password": {"wrong"}})
 		if err != nil {
@@ -382,7 +382,7 @@ func TestLoginRateLimited(t *testing.T) {
 func TestLoginSucceedsAfterFailureSpray(t *testing.T) {
 	httpServer, _, _ := newTestServer(t)
 	client := httpServer.Client()
-	for i := 0; i < loginAttempts; i++ {
+	for i := range loginAttempts {
 		resp, err := client.PostForm(httpServer.URL+"/login",
 			url.Values{"username": {"josie"}, "password": {"wrong"}})
 		if err != nil {
@@ -393,7 +393,7 @@ func TestLoginSucceedsAfterFailureSpray(t *testing.T) {
 			t.Fatalf("attempt %d = %d, want 401", i+1, resp.StatusCode)
 		}
 	}
-	for i := 0; i < 3; i++ {
+	for i := range 3 {
 		resp, err := client.PostForm(httpServer.URL+"/login",
 			url.Values{"username": {"josie"}, "password": {"wrong"}})
 		if err != nil {