gitpush

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: BSD-3-Clause Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gitpush is the forge side of git push (git-receive-pack, gitprotocol-pack): it decides, in process, who may move which ref, from the parsed command list and the received pack, BEFORE any ref moves. There are no hooks and no callback token: the policy is code in this package, fed by the admin API's collaborators and protected refs.

The protocol bytes and the object store are behind two narrow interfaces, so the forge does not care who implements them:

  • Wire parses the command list, writes the advertisement and the report-status. StdWire is the implementation in this package; it is to be replaced by go-vcs/gitproto/receivepack when that ships.
  • Objects ingests a pack into a quarantine, checks connectivity, and moves refs by compare-and-swap. GitObjects drives the git binary (index-pack, unpack-objects, rev-list, update-ref), the way git's own receive-pack does; gitproto's pack indexer may replace it where measurement says so.

Index

Constants

View Source
const (
	DefaultMaxCommands      = 100
	DefaultMaxRefNameBytes  = 1024
	DefaultMaxRequestBytes  = 1 << 20
	DefaultMaxPackBytes     = 512 << 20
	DefaultMaxObjects       = 1_000_000
	DefaultMaxObjectBytes   = 100 << 20
	DefaultMaxInflatedBytes = 1 << 30
)

Default bounds.

View Source
const DefaultScanTimeout = 2 * time.Minute

DefaultScanTimeout bounds the scan of one pack.

View Source
const (

	// DefaultUnpackMaxBytes is what unpack-objects may be asked to hold.
	DefaultUnpackMaxBytes = 32 << 20
)

Variables

View Source
var DefaultNamespaces = []string{"refs/heads/", "refs/tags/"}

DefaultNamespaces are the ref prefixes a push may touch by default.

View Source
var ErrDenied = errors.New("permission denied: you may not push to this repository")

ErrDenied is returned for an account that may not push to a repository it can read.

View Source
var ZeroID = strings.Repeat("0", 40)

ZeroID is the all-zero object id: "no object" in a command.

Functions

func CheckRefName

func CheckRefName(name string) error

CheckRefName applies git-check-ref-format(1) to a full ref name that lives under refs/.

Types

type AccessReader

type AccessReader interface {
	GetRepoAccess(ctx context.Context, owner, repo string) (store.RepoAccess, error)
}

AccessReader reads a repository's collaborators and protected refs (the store, behind the admin API).

type Caps

type Caps struct {
	ReportStatus bool
	Atomic       bool
	SideBand64k  bool
	Quiet        bool
	OfsDelta     bool
	Agent        string
}

Caps are the capabilities the client asked for.

type Command

type Command struct {
	Old string // the id the client believes the ref has; ZeroID creates it
	New string // the id to set; ZeroID deletes the ref
	Ref string // full ref name, already checked like git check-ref-format
}

Command is one reference update command.

func (Command) Create

func (c Command) Create() bool

Create reports whether the command creates the ref.

func (Command) Delete

func (c Command) Delete() bool

Delete reports whether the command deletes the ref.

type GitObjects

type GitObjects struct {
	Dir string // the bare repository
	Git string // the git executable ("" for the one on PATH)
	// UnpackLimit is the object count under which a pack is unpacked to loose
	// objects instead of kept as a pack (git's receive.unpackLimit; 0 is 100).
	UnpackLimit uint32
	// SkipFsck turns off the check git runs on every object it ingests (git's
	// receive.fsckObjects, here on by default): a tree entry named ".git", a
	// ".gitmodules" that is a symlink or holds a hostile URL or path, a malformed
	// commit are refused instead of stored for a client to be hurt by.
	SkipFsck bool
	// StaleAfter is the age after which a quarantine left by a CRASHED push is
	// removed (0 is one hour). A quarantine of a push this process still serves
	// is never removed, however old.
	StaleAfter time.Duration
	// UnpackMaxBytes is the most a pack may be charged (every inflated byte and
	// delta result) to be unpacked by git unpack-objects, which holds its deltas
	// in memory; a larger pack goes to index-pack (0: DefaultUnpackMaxBytes).
	UnpackMaxBytes int64
	// PackTimeout bounds one git process that builds objects (0: 10 minutes).
	PackTimeout time.Duration
	// Observe, if set, is called with the arguments and the ProcessState of every
	// git process this runs once it has ended: a way to measure what git cost
	// (SysUsage), used by the tests that bound the memory a pack can make it hold.
	Observe func(args []string, ps *os.ProcessState)
	// Log receives what git said about a refused pack: the client is told only
	// that it was refused (nil: slog.Default()).
	Log *slog.Logger
}

GitObjects implements Objects with the git binary, the way git's own receive-pack does (git-receive-pack(1)): the pack is ingested into a quarantine directory that sees the repository's objects as an alternate, the objects are checked there, and only then migrated into the repository. No hook is ever run (core.hooksPath is /dev/null) and nothing the client sent is executed.

func (*GitObjects) ClearStaleLocks

func (g *GitObjects) ClearStaleLocks() int

ClearStaleLocks removes the lock files a killed git left in the repository (refs/**.lock, packed-refs.lock, HEAD.lock) and returns how many. The CALLER must hold the repository: no process of ours may be running git on it, which is what the push lock says. A file younger than lockSettle stays.

func (*GitObjects) Receive

func (g *GitObjects) Receive(ctx context.Context, pack io.Reader, lim PackLimits) (Incoming, error)

Receive implements Objects.

func (*GitObjects) Refs

func (g *GitObjects) Refs(ctx context.Context) ([]Ref, string, error)

Refs implements Objects.

func (*GitObjects) Update

func (g *GitObjects) Update(ctx context.Context, ups []Update, atomic bool) []string

Update implements Objects with git update-ref, whose lock files and old-value check are the compare-and-swap: a ref that moved since the client looked is refused, never overwritten.

type Incoming

type Incoming interface {
	// Connected checks that everything reachable from tips is present, in the
	// pack or already in the repository.
	Connected(ctx context.Context, tips []string) error
	// Type returns "commit", "tree", "blob" or "tag" for an object.
	Type(ctx context.Context, id string) (string, error)
	// IsAncestor reports whether old is an ancestor of new (or equal).
	IsAncestor(ctx context.Context, old, new string) (bool, error)
	// Commit makes the objects visible in the repository.
	Commit() error
	// Abort discards them. It is safe after Commit and may be called twice.
	Abort()
}

Incoming is a received pack that is not yet visible in the repository.

type Limits

type Limits struct {
	MaxCommands     int   // ref updates in one push (100)
	MaxRefNameBytes int   // one ref name (1024)
	MaxRequestBytes int64 // the command list (1 MiB)
	MaxPackBytes    int64 // the pack (512 MiB)
	MaxObjects      uint32
	// MaxObjectBytes bounds one inflated object and one delta's result (100 MiB);
	// MaxInflatedBytes bounds all of them together (1 GiB). Both are checked
	// before git builds anything. With the 4 sessions an account may hold, a push
	// is allowed about 4 x (2 x MaxObjectBytes) of git's memory (GIT_ALLOC_LIMIT
	// caps its single allocations at that figure).
	MaxObjectBytes   int64
	MaxInflatedBytes int64
	// ScanTimeout is the deadline of the scan of one pack (and of reading away a
	// refused push's pack), however slowly the client sends it (2 minutes).
	ScanTimeout time.Duration
	// Namespaces are the ref prefixes a push may create, move or delete (default
	// refs/heads/ and refs/tags/): refs/replace/, refs/notes/ and the rest change
	// what readers see and are an operator's choice.
	Namespaces []string
}

Limits bound one push. A zero field takes its default.

type Locks

type Locks struct {
	// contains filtered or unexported fields
}

Locks serializes pushes per repository. A lock that is waited for gives way to its context, so a stalled client cannot queue others behind a dead one.

func (*Locks) Held

func (l *Locks) Held() int

Held reports how many repositories have a lock held or awaited.

func (*Locks) Lock

func (l *Locks) Lock(ctx context.Context, key string) (func(), error)

Lock takes the lock of key and returns its release.

type Objects

type Objects interface {
	// Refs lists the references and the branch HEAD points to ("" if none).
	Refs(ctx context.Context) (refs []Ref, head string, err error)
	// Receive ingests a pack into a quarantine, enforcing lim.
	Receive(ctx context.Context, pack io.Reader, lim PackLimits) (Incoming, error)
	// Update applies ups by compare-and-swap and returns, per update, "" when it
	// was applied or the reason it was not. With atomic, all are applied or none.
	Update(ctx context.Context, ups []Update, atomic bool) []string
}

Objects is the repository side: the part gitproto's pack reader is to provide.

type PackLimits

type PackLimits struct {
	MaxBytes   int64  // bytes of pack read from the client
	MaxObjects uint32 // objects the pack header may announce
	// MaxObjectBytes bounds the inflated size of one object, and the size a delta
	// builds (0: DefaultMaxObjectBytes).
	MaxObjectBytes int64
	// MaxInflatedBytes bounds the sum of every object's inflated size and every
	// delta's result size (0: DefaultMaxInflatedBytes).
	MaxInflatedBytes int64
	// Timeout is the deadline of the scan of one pack, however slowly the client
	// sends it (0: DefaultScanTimeout).
	Timeout time.Duration
}

PackLimits bound one received pack.

type ProtocolError

type ProtocolError struct{ Msg string }

ProtocolError is a refusal caused by the client's request.

func (*ProtocolError) Error

func (e *ProtocolError) Error() string

type Pusher

type Pusher struct {
	Repos  *gitrepo.Service
	Access AccessReader
	Wire   Wire
	// Objects opens the object store of the bare repository in dir.
	Objects func(dir string) Objects
	Limits  Limits
	Agent   string
	// contains filtered or unexported fields
}

Pusher serves git-receive-pack for the forge.

func New

func New(repos *gitrepo.Service, access AccessReader, lim Limits) *Pusher

New returns a Pusher over the git binary, with StdWire.

func (*Pusher) ClearStaleLocks

func (p *Pusher) ClearStaleLocks() int

ClearStaleLocks removes, in every repository under the root, the ref lock files a killed git left, for each repository no push of this process holds. It returns how many it removed. Run it at startup and it finds no holder at all.

func (*Pusher) Serve

func (p *Pusher) Serve(ctx context.Context, n gitrepo.Name, login string, in io.Reader, out io.Writer) error

Serve runs one git-receive-pack session for login on repository n: it advertises the references, reads the commands and the pack, applies the push policy and answers. It returns ErrDenied for an account that may read the repository but not push to it, and gitrepo.ErrNotFound for one that may not read it. A push the policy refuses is reported to the client, not returned.

type Ref

type Ref struct{ Name, ID string }

Ref is an advertised reference.

type RefResult

type RefResult struct {
	Ref string
	Err string
}

RefResult is the outcome of one command; Err is "" for success, else one line shown to the client.

type Refused

type Refused struct{ Msg string }

Refused is returned by Objects.Receive for a pack the policy refuses (too big, too many objects, malformed); the message is shown to the client.

func (*Refused) Error

func (e *Refused) Error() string

type Report

type Report struct {
	UnpackErr string // "" for "unpack ok"
	Results   []RefResult
}

Report is the outcome of a whole push.

type Request

type Request struct {
	Commands []Command
	Caps     Caps
	// WantsPack is false only when every command is a delete: then no pack is sent.
	WantsPack bool
}

Request is a parsed command list. The pack, if any, follows it on the stream.

type RequestLimits

type RequestLimits struct {
	MaxCommands     int   // commands in one push
	MaxRefNameBytes int   // one ref name
	MaxBytes        int64 // bytes read before the pack
}

RequestLimits bound what the command list may cost.

type StdWire

type StdWire struct{}

StdWire implements Wire from gitprotocol-pack(5): "Reference Discovery" (receive-pack), "Reference Update Request and Packfile Transfer" and "Report Status", with the capabilities of gitprotocol-capabilities(5) it supports: report-status, delete-refs, side-band-64k, quiet, atomic, ofs-delta, object-format=sha1 and agent. Anything else is refused.

func (StdWire) Advertise

func (StdWire) Advertise(w io.Writer, refs []Ref, agent string) error

Advertise implements Wire.

func (StdWire) Message

func (StdWire) Message(w io.Writer, caps Caps, msg string) error

Message implements Wire.

func (StdWire) ParseRequest

func (StdWire) ParseRequest(r io.Reader, lim RequestLimits) (*Request, error)

ParseRequest implements Wire.

func (StdWire) WriteReport

func (StdWire) WriteReport(w io.Writer, caps Caps, rep *Report) error

WriteReport implements Wire.

type Update

type Update struct{ Ref, Old, New string }

Update is one compare-and-swap ref change: Ref must hold Old (ZeroID: must not exist) and becomes New (ZeroID: deleted).

type Wire

type Wire interface {
	// Advertise writes the receive-pack reference advertisement.
	Advertise(w io.Writer, refs []Ref, agent string) error
	// ParseRequest reads the command list and nothing past it. It returns
	// io.EOF for a client that found nothing to push, and *ProtocolError for
	// anything malformed or over lim.
	ParseRequest(r io.Reader, lim RequestLimits) (*Request, error)
	// WriteReport writes the report-status reply in the form caps asked for.
	WriteReport(w io.Writer, caps Caps, rep *Report) error
	// Message writes a human-readable line the client shows as "remote: ...".
	Message(w io.Writer, caps Caps, msg string) error
}

Wire is the protocol side: the part gitproto/receivepack is to provide.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL