backend

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package backend defines the read-only repository contract that the gitview server is written against. Implementations exist for local git directories (backend/local) and the GitHub REST API (backend/ghapi).

Index

Constants

View Source
const MaxBlobBytes = 10 << 20

MaxBlobBytes caps what backends load into memory for display.

View Source
const TooLargeLines = 3000

TooLargeLines is the per-file changed-line count beyond which hunks are dropped and the UI shows a folded "load diff" box, like github.com.

Variables

View Source
var (
	ErrNotFound    = errors.New("not found")
	ErrUnsupported = errors.New("unsupported")
)

Sentinel errors. Backends wrap these with %w and detail.

Functions

This section is empty.

Types

type BlameHunk

type BlameHunk struct {
	Commit    Commit
	StartLine int
	Lines     int
	Prev      string // SHA to reblame before this commit, "" if none
}

type Blob

type Blob struct {
	Path    string
	Size    int64
	Content []byte
	Binary  bool
	LFS     *LFSPointer
	Symlink bool // Content is the link target path, not file data
	TooBig  bool
}

Blob is file content plus the metadata the viewer needs.

type Commit

type Commit struct {
	SHA       string
	Subject   string
	Body      string
	Author    Signature
	Committer Signature
	Parents   []string
}

type CommitDetail

type CommitDetail struct {
	Commit
	Stats StatTotal
	Files []FileDiff
}

type DiffLine

type DiffLine struct {
	Kind   LineKind
	OldNum int // 0 for additions
	NewNum int // 0 for deletions
	Text   string
	NoEOF  bool
}

type DiffStatus

type DiffStatus string

DiffStatus matches git --name-status letters.

const (
	Added    DiffStatus = "A"
	Modified DiffStatus = "M"
	Deleted  DiffStatus = "D"
	Renamed  DiffStatus = "R"
	Copied   DiffStatus = "C"
)

type EntryKind

type EntryKind int

EntryKind distinguishes tree rows.

const (
	KindFile EntryKind = iota
	KindDir
	KindSymlink
	KindSubmodule
)

type FileDiff

type FileDiff struct {
	OldPath   string
	NewPath   string
	Status    DiffStatus
	Additions int
	Deletions int
	Binary    bool
	TooLarge  bool
	Hunks     []Hunk
}

func ParsePatch

func ParsePatch(patch string) []FileDiff

ParsePatch parses git unified diff output (git diff-tree -p / the patch strings in GitHub's commit API) into FileDiffs. It is forgiving: unknown header lines are skipped, truncated input yields what was parsed.

type Hunk

type Hunk struct {
	Header   string
	OldStart int
	OldLines int
	NewStart int
	NewLines int
	Lines    []DiffLine
}

type Info

type Info struct {
	Owner         string
	Name          string
	Description   string
	DefaultBranch string
	CloneURL      string
	Mirror        bool
}

Info is repository identity.

type LFSPointer

type LFSPointer struct {
	OID  string
	Size int64
}

type LineKind

type LineKind int
const (
	Context LineKind = iota
	Addition
	Deletion
)

type RateLimitError

type RateLimitError struct {
	Reset time.Time // when the quota replenishes; zero if unknown
}

RateLimitError reports an exhausted quota on a metered backend. The server matches it with errors.As; it lives here so the server never has to import a concrete backend.

func (*RateLimitError) Error

func (e *RateLimitError) Error() string

type Ref

type Ref struct {
	Name string
	SHA  string
	Date time.Time
}

type Refs

type Refs struct {
	Branches []Ref
	Tags     []Ref // newest first
}

Refs is the full ref listing.

type Repo

type Repo interface {
	// Info returns identity and defaults. Called once at startup and cached.
	Info(ctx context.Context) (Info, error)

	// Refs returns all branches and tags with their tip SHAs.
	Refs(ctx context.Context) (Refs, error)

	// Resolve turns a ref name or SHA prefix into a full commit SHA.
	Resolve(ctx context.Context, ref string) (string, error)

	// Tree lists the entries of the directory at path ("" = root) at rev,
	// sorted GitHub-style: dirs first, then files, case-insensitive.
	Tree(ctx context.Context, rev, path string) ([]TreeEntry, error)

	// Blob returns file content and metadata at rev:path.
	Blob(ctx context.Context, rev, path string) (Blob, error)

	// Commits lists up to n commits reachable from rev, newest first,
	// optionally limited to those touching path ("" = all), skipping skip.
	// more reports whether older commits exist beyond the window.
	Commits(ctx context.Context, rev, path string, n, skip int) (commits []Commit, more bool, err error)

	// Commit returns one commit with its full diff against its first
	// parent, or against the empty tree for a root commit.
	Commit(ctx context.Context, sha string) (CommitDetail, error)

	// LastCommit returns the most recent commit touching path at rev.
	LastCommit(ctx context.Context, rev, path string) (Commit, error)

	// Blame attributes every line of rev:path to a commit.
	Blame(ctx context.Context, rev, path string) ([]BlameHunk, error)

	// Files returns every file path at rev (recursive, sorted).
	Files(ctx context.Context, rev string) ([]string, error)

	// Archive streams a zip or tar.gz of the tree at rev. format is
	// "zip" or "tar.gz"; prefix is the top-level directory inside the
	// archive.
	Archive(ctx context.Context, rev, format, prefix string, w io.Writer) error
}

Repo is a read-only view of one repository. Implementations must be safe for concurrent use.

type Signature

type Signature struct {
	Name      string
	Email     string
	Date      time.Time
	Login     string // GitHub login when known, else ""
	AvatarURL string // ghapi only
}

Signature is an author or committer stamp.

type StatTotal

type StatTotal struct {
	FilesChanged int
	Additions    int
	Deletions    int
}

type TreeEntry

type TreeEntry struct {
	Name string
	Path string
	Kind EntryKind
	SHA  string // object id; for submodules, the pinned commit
	Size int64  // bytes for files, -1 when unknown
	Mode string
}

Directories

Path Synopsis
Package ghapi implements backend.Repo over the GitHub REST API v3 with a hand-rolled net/http client.
Package ghapi implements backend.Repo over the GitHub REST API v3 with a hand-rolled net/http client.
Package hf implements backend.Repo over the Hugging Face Hub HTTP API with a hand-rolled net/http client, the same shape as the ghapi backend: bearer auth, conditional requests (ETag), Link-header pagination, and rate-limit errors.
Package hf implements backend.Repo over the Hugging Face Hub HTTP API with a hand-rolled net/http client, the same shape as the ghapi backend: bearer auth, conditional requests (ETag), Link-header pagination, and rate-limit errors.
Package local implements backend.Repo for a repository on disk by driving the git command line.
Package local implements backend.Repo for a repository on disk by driving the git command line.

Jump to

Keyboard shortcuts

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