1
0
Fork 0
mirror of https://github.com/imjasonh/infinite-git synced 2026-07-14 04:35:23 +00:00
infinite-git/internal/server/handlers.go
Claude 29666530cc
Fix four bugs: packfile offset tracking, HTTP error ordering, ref ordering, and race condition
- packfile.go: ReadObject now uses a countingReader to track compressed
  bytes consumed and properly advances r.offset, fixing sequential
  multi-object reads.
- handlers.go: Use commitSHA from GenerateCommit directly instead of
  re-reading refs, which fixes both the HTTP error-after-body-written
  issue and ensures HEAD is always advertised first (Git protocol
  requirement).
- commit.go: GenerateCommit now holds the repo lock for the entire
  read-modify-write cycle, preventing concurrent generates from reading
  the same parent and losing ref updates.
- repo.go: Added Lock/Unlock/GetRefsLocked methods to support holding
  the mutex across multiple repo operations.

https://claude.ai/code/session_015F9BYQoCy2P2zYZBuVeXHH
2026-04-02 05:52:24 +00:00

97 lines
2.8 KiB
Go

package server
import (
"fmt"
"net/http"
"strings"
"github.com/chainguard-dev/clog"
"github.com/imjasonh/infinite-git/internal/pktline"
"github.com/imjasonh/infinite-git/internal/protocol"
)
// handleInfoRefs handles the reference discovery phase.
func (s *Server) handleInfoRefs(w http.ResponseWriter, r *http.Request) {
log := clog.FromContext(r.Context())
service := r.URL.Query().Get("service")
// Only support git-upload-pack (fetch/clone)
if service != "git-upload-pack" {
http.Error(w, "Service not supported", http.StatusForbidden)
return
}
// Generate a new commit before advertising refs
commitSHA, err := s.generator.GenerateCommit()
if err != nil {
log.Error("failed to generate commit", "error", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
log.Info("generated new commit", "sha", commitSHA, "counter", s.generator.GetCounter())
// Set headers
w.Header().Set("Content-Type", fmt.Sprintf("application/x-%s-advertisement", service))
w.Header().Set("Cache-Control", "no-cache")
// Write response
pw := pktline.NewWriter(w)
// Service declaration
if err := pw.Writef("# service=%s\n", service); err != nil {
log.Error("failed to write service line", "error", err)
return
}
if err := pw.Flush(); err != nil {
log.Error("failed to write flush", "error", err)
return
}
// Use the commitSHA directly from GenerateCommit rather than re-reading
// refs. This avoids a race where concurrent requests could all see the
// same latest ref, and ensures HEAD is always advertised first.
capabilities := strings.Join(s.repo.GetCapabilities(), " ")
// Advertise HEAD first (Git protocol requirement), then refs/heads/main.
if err := pw.Writef("%s HEAD\x00%s\n", commitSHA, capabilities); err != nil {
log.Error("failed to write HEAD ref", "error", err)
return
}
if err := pw.Writef("%s refs/heads/main\n", commitSHA); err != nil {
log.Error("failed to write main ref", "error", err)
return
}
// Final flush
if err := pw.Flush(); err != nil {
log.Error("failed to write final flush", "error", err)
return
}
}
// handleUploadPack handles the pack upload phase.
func (s *Server) handleUploadPack(w http.ResponseWriter, r *http.Request) {
log := clog.FromContext(r.Context())
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Set headers
w.Header().Set("Content-Type", "application/x-git-upload-pack-result")
w.Header().Set("Cache-Control", "no-cache")
// Create upload-pack handler
up := protocol.NewUploadPack(s.repo)
// Process the request
if err := up.HandleRequest(r.Body, w); err != nil {
log.Error("upload-pack failed", "error", err)
// Don't send HTTP error here as we may have already started writing response
return
}
log.Info("completed upload-pack")
}