1
0
Fork 0
mirror of https://github.com/imjasonh/infinite-git synced 2026-07-17 14:24:25 +00:00

Implement infinite Git HTTP server

This server generates a new commit every time someone pulls from it.
Features:
- Pure Go implementation of Git object format and protocols
- No dependency on git CLI commands
- Thread-safe commit generation
- Read-only Git HTTP smart protocol support
- Comprehensive test suite

Each pull creates a new commit with a unique file containing a timestamp
and counter, making the repository grow infinitely with each pull operation.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Jason Hall 2025-07-23 21:19:02 -04:00
parent fefad053a8
commit 30ca57dae3
Failed to extract signature
19 changed files with 2172 additions and 0 deletions

108
internal/server/handlers.go Normal file
View file

@ -0,0 +1,108 @@
package server
import (
"fmt"
"net/http"
"strings"
"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) {
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
s.mu.Lock()
commitSHA, err := s.generator.GenerateCommit()
s.mu.Unlock()
if err != nil {
s.logger.Error("failed to generate commit", "error", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
s.logger.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 {
s.logger.Error("failed to write service line", "error", err)
return
}
if err := pw.Flush(); err != nil {
s.logger.Error("failed to write flush", "error", err)
return
}
// Get current refs
refs, err := s.repo.GetRefs()
if err != nil {
s.logger.Error("failed to get refs", "error", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
// Write capabilities with first ref
capabilities := strings.Join(s.repo.GetCapabilities(), " ")
first := true
for ref, oid := range refs {
if first {
if err := pw.Writef("%s %s\x00%s\n", oid, ref, capabilities); err != nil {
s.logger.Error("failed to write ref with capabilities", "error", err)
return
}
first = false
} else {
if err := pw.Writef("%s %s\n", oid, ref); err != nil {
s.logger.Error("failed to write ref", "error", err)
return
}
}
}
// Final flush
if err := pw.Flush(); err != nil {
s.logger.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) {
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 {
s.logger.Error("upload-pack failed", "error", err)
// Don't send HTTP error here as we may have already started writing response
return
}
s.logger.Info("completed upload-pack")
}

67
internal/server/server.go Normal file
View file

@ -0,0 +1,67 @@
package server
import (
"log/slog"
"net/http"
"sync"
"github.com/imjasonh/infinite-git/internal/generator"
"github.com/imjasonh/infinite-git/internal/repo"
)
// Server handles Git HTTP protocol requests.
type Server struct {
repo *repo.Repository
generator *generator.Generator
mu sync.Mutex
logger *slog.Logger
}
// New creates a new Git HTTP server.
func New(r *repo.Repository, logger *slog.Logger) *Server {
return &Server{
repo: r,
generator: generator.New(r),
logger: logger,
}
}
// Handler returns the HTTP handler for the server.
func (s *Server) Handler() http.Handler {
mux := http.NewServeMux()
// Git smart HTTP endpoints
mux.HandleFunc("/info/refs", s.handleInfoRefs)
mux.HandleFunc("/git-upload-pack", s.handleUploadPack)
mux.HandleFunc("/git-receive-pack", s.handleReceivePack)
// Static file serving for dumb protocol (objects, refs)
mux.HandleFunc("/", s.handleStatic)
return s.logMiddleware(mux)
}
// logMiddleware logs HTTP requests.
func (s *Server) logMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s.logger.Info("request",
"method", r.Method,
"path", r.URL.Path,
"query", r.URL.RawQuery,
"remote", r.RemoteAddr,
)
next.ServeHTTP(w, r)
})
}
// handleReceivePack rejects push operations.
func (s *Server) handleReceivePack(w http.ResponseWriter, r *http.Request) {
s.logger.Info("rejecting push attempt", "path", r.URL.Path)
http.Error(w, "Push access denied", http.StatusForbidden)
}
// handleStatic serves static Git files (for dumb protocol).
func (s *Server) handleStatic(w http.ResponseWriter, r *http.Request) {
// For now, we'll focus on smart protocol only
http.NotFound(w, r)
}