1
0
Fork 0
mirror of https://github.com/imjasonh/infinite-git synced 2026-07-16 12:21:54 +00:00
infinite-git/internal/pktline/writer.go
Jason Hall 30ca57dae3
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>
2025-07-23 21:19:02 -04:00

55 lines
1.2 KiB
Go

package pktline
import (
"fmt"
"io"
)
// Writer implements the Git packet line protocol for writing.
type Writer struct {
w io.Writer
}
// NewWriter creates a new packet line writer.
func NewWriter(w io.Writer) *Writer {
return &Writer{w: w}
}
// Write writes data as a pkt-line.
func (w *Writer) Write(data []byte) error {
if len(data) == 0 {
return w.Flush()
}
// Maximum pkt-line length is 65520 (65516 bytes of data + 4 bytes length)
if len(data) > 65516 {
return fmt.Errorf("pkt-line too long: %d bytes", len(data))
}
// Write 4-byte hex length prefix
length := len(data) + 4
header := fmt.Sprintf("%04x", length)
if _, err := w.w.Write([]byte(header)); err != nil {
return err
}
// Write data
_, err := w.w.Write(data)
return err
}
// WriteString writes a string as a pkt-line.
func (w *Writer) WriteString(s string) error {
return w.Write([]byte(s))
}
// Writef writes formatted data as a pkt-line.
func (w *Writer) Writef(format string, args ...interface{}) error {
return w.WriteString(fmt.Sprintf(format, args...))
}
// Flush writes a flush packet (0000).
func (w *Writer) Flush() error {
_, err := w.w.Write([]byte("0000"))
return err
}