gitcache

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: GPL-3.0 Imports: 29 Imported by: 0

Documentation

Overview

Package gitcache serves Git repositories read-only over smart HTTP from a local cache of bare repositories, refreshed from configured upstreams.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned when the upstream has no such repository.
	ErrNotFound = errors.New("repository not found upstream")
	// ErrRefused covers authentication failures and redirects. The cached
	// copy is not served after one, since the upstream may have withdrawn
	// access to it.
	ErrRefused = errors.New("upstream refused the request")
	// ErrTooLarge is returned for a repository larger than git.max_repo_size,
	// or than git.max_size where that is smaller.
	ErrTooLarge = errors.New("repository is larger than git.max_repo_size or git.max_size")
	// ErrCacheFull is returned for a fetch cut off because repositories in
	// use take what git.max_size leaves.
	ErrCacheFull = errors.New("git cache is full")
)
View Source
var ErrRequestTooLarge = errors.New("request body too large")

ErrRequestTooLarge is returned by ReadRequest for a body past the limit.

Functions

func UpstreamDir

func UpstreamDir(name, baseURL string) string

UpstreamDir names the top-level cache directory of the upstream called name at baseURL. It includes a hash of the URL, so repointing an upstream starts a fresh cache rather than serving the old host's repositories.

Types

type Cache

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

Cache is the Git repository cache. internal/handler serves it at /git.

func New

func New(opts Options) (*Cache, error)

New creates the cache, loading repositories already in CacheDir.

func (*Cache) Close

func (c *Cache) Close() error

Close stops the refreshes in progress and waits for them to finish, so none is still writing to the cache when it returns, then stops the relay. Fetches by ID run on request goroutines; stop the HTTP server first. Closing again does nothing.

func (*Cache) Open

func (c *Cache) Open(ctx context.Context, key, repoURL string, opts OpenOptions) (*Lease, error)

Open returns a lease on the repository cached under key, a relative path such as github-1a2b3c4d/acme/widgets.git, whose upstream is repoURL. A repository not cached yet is cloned first.

func (*Cache) ReadRequest

func (c *Cache) ReadRequest(r *http.Request) (*Request, error)

ReadRequest reads the body of a POST git-upload-pack request, still encoded. A body past the limit fails with ErrRequestTooLarge. Close the request once it is served.

type Lease

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

Lease is a use of a cached repository. The repository is not evicted until the lease is released.

func (*Lease) Hit

func (l *Lease) Hit() bool

Hit reports whether the repository was already cached when it was opened.

func (*Lease) Release

func (l *Lease) Release()

Release ends the lease.

func (*Lease) ServeInfoRefs

func (l *Lease) ServeInfoRefs(w http.ResponseWriter, r *http.Request)

ServeInfoRefs answers GET info/refs?service=git-upload-pack.

func (*Lease) ServeUploadPack

func (l *Lease) ServeUploadPack(w http.ResponseWriter, r *http.Request, req *Request)

ServeUploadPack answers POST git-upload-pack with req, the request's body as ReadRequest read it. With OpenOptions.FetchWants, commits the request wants that the cache lacks are fetched from the upstream first.

type OpenOptions

type OpenOptions struct {
	// Refresh brings a cached repository up to date with the upstream first,
	// or leaves it as it is when the upstream fails or is slow and the copy
	// is recent enough (see Options).
	Refresh bool
	// FetchWants lets ServeUploadPack fetch commits a client asks for by ID
	// that the cache lacks. Leave it off where a fetch by ID could reach
	// commits the client may not read through this repository, such as
	// GitHub forks with credentials configured.
	FetchWants bool
}

OpenOptions says what Open does besides returning the repository.

type Options

type Options struct {
	CacheDir    string
	MaxSize     int64
	MaxRepoSize int64
	RefreshWait time.Duration
	StaleMaxAge time.Duration
	// Client makes every upstream request, including those of the git
	// processes the cache runs. Redirects are never followed.
	Client *http.Client
	Logger *slog.Logger
	// Upstreams maps each configured upstream's name to its directory in
	// CacheDir, as UpstreamDir names it. Only those directories are loaded
	// at startup. A directory UpstreamDir would name for one of these names
	// at another URL, left by repointing the upstream, is removed. Nothing
	// else in CacheDir is touched, so it may hold other data.
	Upstreams map[string]string
	// contains filtered or unexported fields
}

Options configures a Cache.

type Request

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

Request is the body of a POST git-upload-pack request, read in full before anything is started for it. Stateless requests are complete once sent, so a client that sends one slowly, or not at all, holds no upload-pack process and is bound by the server's read timeout.

func (*Request) Close

func (req *Request) Close()

Close removes any temporary file holding the request.

Jump to

Keyboard shortcuts

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