d12156d95533294607e78900802be1d9b6fe064a

Author
Kieran Klukas <kieran@dunkirk.sh>
Committer
Kieran Klukas <kieran@dunkirk.sh>
Date

Message

refactor: share the missing-repository check across LFS handlers

Eight LFS handlers each repeated the same check and error response for
a request without a repository, and the upload handler looked the
repository up a second time after already having it. They now share one
helper, which keeps the safeguard in one place.

💘 Generated with Crush

Assisted-by: Crush:claude-opus-5-5

Diff

This diff is truncated to protect this page.

  1diff --git a/pkg/web/git_lfs.go b/pkg/web/git_lfs.go
  2index c9b8ba1779f115e51070fe611c1eb5eaa81d8f0c..19d08ba03b56d0e6b438a49028bc0d45f87d1aea 100644
  3--- a/pkg/web/git_lfs.go
  4+++ b/pkg/web/git_lfs.go
  5@@ -15,7 +15,6 @@ import (
  6 
  7 	"charm.land/log/v2"
  8 	"github.com/charmbracelet/soft-serve/pkg/access"
  9-	"github.com/charmbracelet/soft-serve/pkg/backend"
 10 	"github.com/charmbracelet/soft-serve/pkg/config"
 11 	"github.com/charmbracelet/soft-serve/pkg/db"
 12 	"github.com/charmbracelet/soft-serve/pkg/db/models"
 13@@ -77,11 +76,8 @@ func serviceLfsBatch(w http.ResponseWriter, r *http.Request) {
 14 	}
 15 
 16 	name := mux.Vars(r)["repo"]
 17-	repo := proto.RepositoryFromContext(ctx)
 18-	if repo == nil {
 19-		renderJSON(w, http.StatusNotFound, lfs.ErrorResponse{
 20-			Message: "repository not found",
 21-		})
 22+	repo, ok := lfsRepository(w, r)
 23+	if !ok {
 24 		return
 25 	}
 26 
 27@@ -258,11 +254,8 @@ func serviceLfsBasic(w http.ResponseWriter, r *http.Request) {
 28 func serviceLfsBasicDownload(w http.ResponseWriter, r *http.Request) {
 29 	ctx := r.Context()
 30 	oid := mux.Vars(r)["oid"]
 31-	repo := proto.RepositoryFromContext(ctx)
 32-	if repo == nil {
 33-		renderJSON(w, http.StatusNotFound, lfs.ErrorResponse{
 34-			Message: "repository not found",
 35-		})
 36+	repo, ok := lfsRepository(w, r)
 37+	if !ok {
 38 		return
 39 	}
 40 
 41@@ -327,30 +320,18 @@ func serviceLfsBasicUpload(w http.ResponseWriter, r *http.Request) {
 42 
 43 	oid := mux.Vars(r)["oid"]
 44 	cfg := config.FromContext(ctx)
 45-	be := backend.FromContext(ctx)
 46 	dbx := db.FromContext(ctx)
 47 	datastore := store.FromContext(ctx)
 48 	logger := log.FromContext(ctx).WithPrefix("http.lfs-basic")
 49-	repo := proto.RepositoryFromContext(ctx)
 50-	if repo == nil {
 51-		renderJSON(w, http.StatusNotFound, lfs.ErrorResponse{
 52-			Message: "repository not found",
 53-		})
 54+	repo, ok := lfsRepository(w, r)
 55+	if !ok {
 56 		return
 57 	}
 58 
 59 	repoID := strconv.FormatInt(repo.ID(), 10)
 60 	strg := storage.NewLocalStorage(filepath.Join(cfg.DataPath, "lfs", repoID))
 61-	name := mux.Vars(r)["repo"]
 62 
 63 	defer r.Body.Close() //nolint: errcheck
 64-	repo, err := be.Repository(ctx, name)
 65-	if err != nil {
 66-		renderJSON(w, http.StatusNotFound, lfs.ErrorResponse{
 67-			Message: "repository not found",
 68-		})
 69-		return
 70-	}
 71 
 72 	// NOTE: Git LFS client will retry uploading the same object if there was a
 73 	// partial error, so we need to skip existing objects.
 74@@ -406,12 +387,8 @@ func serviceLfsBasicVerify(w http.ResponseWriter, r *http.Request) {
 75 	var pointer lfs.Pointer
 76 	ctx := r.Context()
 77 	logger := log.FromContext(ctx).WithPrefix("http.lfs-basic")
 78-	repo := proto.RepositoryFromContext(ctx)
 79-	if repo == nil {
 80-		logger.Error("error getting repository from context")
 81-		renderJSON(w, http.StatusNotFound, lfs.ErrorResponse{
 82-			Message: "repository not found",
 83-		})
 84+	repo, ok := lfsRepository(w, r)
 85+	if !ok {
 86 		return
 87 	}
 88 
 89@@ -523,12 +500,8 @@ func serviceLfsLocksCreate(w http.ResponseWriter, r *http.Request) {
 90 		return
 91 	}
 92 
 93-	repo := proto.RepositoryFromContext(ctx)
 94-	if repo == nil {
 95-		logger.Error("error getting repository from context")
 96-		renderJSON(w, http.StatusNotFound, lfs.ErrorResponse{
 97-			Message: "repository not found",
 98-		})
 99+	repo, ok := lfsRepository(w, r)
100+	if !ok {
101 		return
102 	}
103 
104@@ -648,12 +621,8 @@ func serviceLfsLocksGet(w http.ResponseWriter, r *http.Request) {