gitbackend

package
v0.48.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrGitNotFound     = errors.New("git binary not found")
	ErrRepoNotFound    = errors.New("repository not found")
	ErrBranchExists    = errors.New("branch already exists")
	ErrBranchNotFound  = errors.New("branch not found")
	ErrMergeConflict   = errors.New("merge conflict")
	ErrNothingToMerge  = errors.New("nothing to merge")
	ErrDirtyWorktree   = errors.New("dirty worktree")
	ErrRemoteNotFound  = errors.New("remote not found")
	ErrTagExists       = errors.New("tag already exists")
	ErrFileNotFound    = errors.New("file not found at revision")
	ErrAuthFailed      = errors.New("authentication failed")
	ErrAlreadyUpToDate = errors.New("already up to date")
	ErrNotAGitRepo     = errors.New("not a git repository")
)

Functions

func IsAuthFailed

func IsAuthFailed(err error) bool

IsAuthFailed checks if an error is an authentication error.

func IsMergeConflict

func IsMergeConflict(err error) bool

IsMergeConflict checks if an error is a merge conflict.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound checks if an error is a not-found error.

func Register

func Register(name string, ctor GitBackendConstructor)

Register registers a GitBackend constructor.

Types

type AdvancedOps

type AdvancedOps interface {
	// RevParse resolves a revision (ref/short-SHA/etc.) to a full SHA.
	RevParse(ctx context.Context, repoPath string, ref string) (string, error)
	// MergeBase returns the best common ancestor of two commits.
	MergeBase(ctx context.Context, repoPath string, a string, b string) (string, error)
	// DiffNames returns the names of files changed between two commits.
	DiffNames(ctx context.Context, repoPath string, from string, to string) ([]string, error)
	// DeletedFiles returns files deleted between two commits.
	DeletedFiles(ctx context.Context, repoPath string, from string, to string) ([]string, error)
	// CheckoutRef force-checks out an arbitrary ref in detached HEAD.
	CheckoutRef(ctx context.Context, repoPath string, ref string) error
	// CheckoutFiles restores the given files from ref into the working tree.
	CheckoutFiles(ctx context.Context, repoPath string, ref string, files []string) error
	// Add stages the given files into the index.
	Add(ctx context.Context, repoPath string, files []string) error
	// CommitWithIdentity creates a commit using an explicit author identity.
	CommitWithIdentity(ctx context.Context, repoPath string, name string, email string, message string) error
}

AdvancedOps covers lower-level revision, index, and identity operations.

type AuthConfig

type AuthConfig struct {
	// Type selects the authentication method. AuthNone performs no auth.
	Type AuthType

	// Username is the HTTP Basic username (ignored for SSH).
	Username string
	// Password is the HTTP Basic password (used together with Username).
	Password string
	// Token is the HTTPS access token (used by AuthHTTPToken).
	Token string

	// SSHKey is the on-disk path to an SSH private key.
	SSHKey string

	// SSHKeyContent holds the raw PEM content of an SSH private key.
	// When set (and SSHKey is empty), a temporary key file is created
	// automatically for the git operation. This is useful for keys stored
	// in a database rather than on disk.
	SSHKeyContent string

	// Passphrase is the passphrase for decrypting the SSH private key.
	// Used together with SSHKey or SSHKeyContent.
	Passphrase string

	// InsecureSkipTLS disables TLS certificate verification for HTTPS
	// operations (equivalent to http.sslVerify=false). It has no effect on
	// SSH operations. Carried on AuthConfig so every network operation that
	// takes auth (Fetch, Push, Clone, Pull, FetchAll, PushTag,
	// TestConnection, ...) honors it uniformly.
	InsecureSkipTLS bool
}

AuthConfig holds authentication credentials for git operations. Build one with the New*Auth helpers in auth.go when possible.

func AutoDetectAuth

func AutoDetectAuth(urlStr string) AuthConfig

AutoDetectAuth attempts to detect the appropriate AuthConfig for a given URL. For SSH URLs, it tries common key file locations and the SSH agent. For HTTP(S) URLs, it returns AuthNone (caller should provide token/password).

func NewHTTPBasicAuth

func NewHTTPBasicAuth(username, password string) AuthConfig

NewHTTPBasicAuth builds an AuthConfig for HTTP Basic authentication.

func NewSSHKeyContentAuth

func NewSSHKeyContentAuth(keyContent, passphrase string) AuthConfig

NewSSHKeyContentAuth builds an AuthConfig for SSH authentication using in-memory key content (e.g. from a database). The backend will create a temporary file automatically if needed.

func NewSSHKeyFileAuth

func NewSSHKeyFileAuth(keyPath, passphrase string) AuthConfig

NewSSHKeyFileAuth builds an AuthConfig for SSH authentication using a key file on disk.

func NewTokenAuth

func NewTokenAuth(token string) AuthConfig

NewTokenAuth builds an AuthConfig for HTTPS token authentication.

When authenticating against git hosting platforms (GitHub, GitLab, Gitea, etc.) over HTTPS the credentials are exchanged via HTTP Basic Auth: the username is an arbitrary non-empty placeholder and the real token is sent as the password. An empty token collapses to AuthNone so callers can simply pass through an optional token without extra branching.

type AuthType

type AuthType string

AuthType represents the authentication method used for a git operation.

const (
	// AuthNone disables authentication (public repositories, local repos).
	AuthNone AuthType = "none"
	// AuthHTTPBasic authenticates over HTTPS using HTTP Basic. The username is
	// an arbitrary placeholder and the secret is carried in Password.
	AuthHTTPBasic AuthType = "http_basic"
	// AuthHTTPToken authenticates over HTTPS using a bearer/token sent as the
	// password of HTTP Basic auth (the convention used by most git hosts).
	AuthHTTPToken AuthType = "http_token"
	// AuthSSH authenticates over the SSH protocol using a private key.
	AuthSSH AuthType = "ssh"
)

type BlobContent

type BlobContent struct {
	// Content is the file payload. When IsBinary it is base64-encoded.
	Content string
	// Encoding is either EncodingUTF8 or EncodingBase64.
	Encoding BlobEncoding
	// Size is the decoded payload size in bytes.
	Size int64
	// IsBinary is true when the blob is detected as binary.
	IsBinary bool
}

BlobContent represents the content of a file at a given revision.

type BlobEncoding

type BlobEncoding string

BlobEncoding is the encoding used for BlobContent.Content.

const (
	EncodingUTF8   BlobEncoding = "utf-8"
	EncodingBase64 BlobEncoding = "base64"
)

type BranchDetail

type BranchDetail struct {
	Name string
	Hash string
	// IsCurrent is true when this is the checked-out branch.
	IsCurrent bool
	// IsRemote is true for remote-tracking branches (refs/remotes/*).
	IsRemote bool
	// Remote is the remote name for remote branches (e.g. "origin").
	Remote string
	// Upstream is the configured upstream tracking branch (e.g. "origin/main").
	Upstream string
	Author   string
	Email    string
	// Date is the tip commit's author date formatted as RFC3339.
	Date string
	// Message is the subject line of the tip commit.
	Message string
}

BranchDetail represents a branch with extended information.

type BranchOps

type BranchOps interface {
	// ListRemoteBranches lists branches that exist on the given remote.
	ListRemoteBranches(ctx context.Context, repoPath string, remote string) ([]string, error)
	// ListLocalBranches lists local branches (refs/heads/*).
	ListLocalBranches(ctx context.Context, repoPath string) ([]string, error)
	// ListBranches lists both local and remote branches with extended details.
	ListBranches(ctx context.Context, repoPath string) ([]BranchDetail, error)
	// CreateBranch creates a branch pointing at ref (or HEAD when empty).
	CreateBranch(ctx context.Context, repoPath string, branch string, ref string) error
	// DeleteBranch deletes a local branch.
	DeleteBranch(ctx context.Context, repoPath string, branch string) error
	// RenameBranch renames a local branch.
	RenameBranch(ctx context.Context, repoPath string, oldName string, newName string) error
	// Checkout switches the working tree to the given branch.
	Checkout(ctx context.Context, repoPath string, branch string) error
	// GetCurrentBranch returns the name of the checked-out branch.
	GetCurrentBranch(ctx context.Context, repoPath string) (string, error)
	// GetBranchSyncInfo returns how many commits branch is ahead/behind upstream.
	GetBranchSyncInfo(ctx context.Context, repoPath string, branch string, upstream string) (ahead int, behind int, err error)
}

BranchOps covers branch listing, creation, deletion, and checkout.

type CloneOptions

type CloneOptions struct {
	// URL is the remote URL (HTTPS or SSH) to clone from.
	URL string
	// Path is the destination directory for the new working tree.
	Path string
	// Branch checks out the given branch after clone (optional).
	Branch string
	// Depth creates a shallow clone with the given history depth.
	// Zero means a full clone.
	Depth int
	Auth  AuthConfig
	// Progress receives human-readable progress output (optional).
	Progress        io.Writer
	NoCheckout      bool
	SingleBranch    bool
	InsecureSkipTLS bool
}

CloneOptions contains options for cloning a repository.

type CommitInfo

type CommitInfo struct {
	// Hash is the full 40-character commit SHA.
	Hash string
	// Message is the commit message.
	Message string
	// Author is the commit author's name.
	Author string
	// Date is the author date formatted as RFC3339.
	Date string
}

CommitInfo represents a git commit.

type CommitOps

type CommitOps interface {
	// GetCommitsBetween returns commits in the range (from, to].
	GetCommitsBetween(ctx context.Context, repoPath string, from string, to string) ([]CommitInfo, error)
	// IsAncestor reports whether ancestor is an ancestor of descendant.
	IsAncestor(ctx context.Context, repoPath string, ancestor string, descendant string) (bool, error)
	// Merge merges branch into the current HEAD according to opts.
	Merge(ctx context.Context, repoPath string, branch string, opts MergeOptions) error
	// CherryPick applies the changes of commitHash onto the current HEAD.
	CherryPick(ctx context.Context, repoPath string, commitHash string) error
	// Rebase rebases the current branch onto onto.
	Rebase(ctx context.Context, repoPath string, onto string) error
	// RebaseAbort aborts an in-progress rebase.
	RebaseAbort(ctx context.Context, repoPath string) error
	// RebaseContinue continues an in-progress rebase after resolving conflicts.
	RebaseContinue(ctx context.Context, repoPath string) error
}

CommitOps covers history traversal and history-rewriting operations.

type ConfigOps

type ConfigOps interface {
	// GetConfig reads a config value by dotted key (e.g. "user.name").
	GetConfig(ctx context.Context, repoPath string, key string) (string, error)
	// SetConfig sets a config value by dotted key (e.g. "user.email").
	SetConfig(ctx context.Context, repoPath string, key string, value string) error
}

ConfigOps covers reading and writing git config values.

type CoreOps

type CoreOps interface {
	// Fetch downloads objects and refs from a remote into the local repository
	// without touching the working tree.
	Fetch(ctx context.Context, opts FetchOptions) (*FetchResult, error)
	// FetchAll fetches tags and every branch from all configured remotes.
	FetchAll(ctx context.Context, repoPath string, auth AuthConfig) error
	// Push uploads local refs to a remote.
	Push(ctx context.Context, opts PushOptions) (*PushResult, error)
	// Pull fetches from and integrates with the current branch (fetch + merge).
	Pull(ctx context.Context, repoPath string, remote string, branch string, auth AuthConfig) error
	// Clone clones a remote repository into a new local working tree.
	Clone(ctx context.Context, opts CloneOptions) error
	// Init creates a new empty git repository at repoPath.
	Init(ctx context.Context, repoPath string) error
	// RunRaw executes an arbitrary git command and returns its combined output.
	// Only supported by the native backend.
	RunRaw(ctx context.Context, repoPath string, args []string) (stdout string, stderr string, err error)
}

CoreOps covers repository-level network and lifecycle operations.

type DiffOptions

type DiffOptions struct {
	// From is the starting commit SHA. When both From and To are empty the
	// diff is computed between the working tree and HEAD.
	From string
	// To is the ending commit SHA.
	To string
	// Paths restricts the diff to the given paths (optional).
	Paths []string
}

DiffOptions contains options for getting a diff.

type FetchOptions

type FetchOptions struct {
	// RepoPath is the local working tree to operate on.
	RepoPath string
	// Remote is the remote name to fetch from. Defaults to "origin" when
	// empty.
	Remote string
	// Branches limits the fetch to the given branch/ref names. When empty all
	// branches are fetched. Full 40-char SHAs are ignored as they are not
	// valid fetch refspecs.
	Branches []string
	// Tags fetches all tags when true.
	Tags bool
	// Prune removes remote-tracking refs that no longer exist on the remote.
	Prune bool
	// Depth limits the fetch to the given number of commits (shallow fetch).
	// Zero means no depth limit.
	Depth           int
	InsecureSkipTLS bool
	Auth            AuthConfig
	// Progress receives human-readable progress output (optional).
	Progress io.Writer
}

FetchOptions contains options for fetching from a remote.

type FetchResult

type FetchResult struct {
	FetchedRefs   []string
	NewBranches   []string
	UpdatedBranch []string
	DeletedBranch []string
	NewTags       []string
}

FetchResult contains the result of a fetch operation.

type FileOps

type FileOps interface {
	// GetFileAtRevision returns the raw bytes of path at the given ref.
	GetFileAtRevision(ctx context.Context, repoPath string, path string, ref string) ([]byte, error)
	// GetFileHistory returns commits that touched path (most recent first),
	// limited to limit entries (0 means all).
	GetFileHistory(ctx context.Context, repoPath string, path string, limit int) ([]CommitInfo, error)
	// GetTree lists entries under dirPath at ref. When recursive is true it
	// lists all files (not directories) below dirPath.
	GetTree(ctx context.Context, repoPath string, ref string, dirPath string, recursive bool) ([]TreeEntry, error)
	// GetBlob returns the content of filePath at ref.
	GetBlob(ctx context.Context, repoPath string, ref string, filePath string) (*BlobContent, error)
	// GetCommit returns metadata for a single commit.
	GetCommit(ctx context.Context, repoPath string, hash string) (*CommitInfo, error)
}

FileOps covers reading files, trees, and blobs at arbitrary revisions.

type FileStatus

type FileStatus struct {
	Path string
	// Worktree is the status of the file in the working tree.
	Worktree StatusCode
	// Staging is the status of the file in the staging area (index).
	Staging StatusCode
}

FileStatus represents the status of a single file.

type GitBackend

GitBackend is the interface for low-level, local git operations.

Every method takes a context (for cancellation/timeouts) and the absolute path of a working tree. Commit/ref arguments are full 40-character SHAs unless stated otherwise; dates are RFC3339 formatted strings. Methods return wrapped errors created by newGitError (see errors.go); use the IsNotFound / IsMergeConflict helpers to classify them.

GitBackend composes the focused sub-interfaces defined in iface.go (CoreOps, BranchOps, StatusDiffOps, CommitOps, RemoteOps, TagOps, FileOps, StashOps, ConfigOps, AdvancedOps). Consumers that only need a subset of capabilities may depend on the narrower sub-interface directly.

func NewGitBackend

func NewGitBackend(opts Options) (GitBackend, error)

NewGitBackend creates a GitBackend using the registry. If opts.Type is empty, it auto-detects (native first, fallback to gogit).

type GitBackendConstructor

type GitBackendConstructor func(opts Options) (GitBackend, error)

GitBackendConstructor creates a GitBackend instance.

type GitError

type GitError struct {
	Op      string // operation name, e.g., "Fetch"
	Path    string // repo path
	Command string // git command that failed
	Stderr  string // stderr output
	Err     error  // underlying error
}

GitError is a structured error from a git operation.

func (*GitError) Error

func (e *GitError) Error() string

func (*GitError) Is

func (e *GitError) Is(target error) bool

func (*GitError) Unwrap

func (e *GitError) Unwrap() error

type GoGitBackend

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

func NewGoGitBackend

func NewGoGitBackend(opts Options) *GoGitBackend

func (*GoGitBackend) Add

func (b *GoGitBackend) Add(ctx context.Context, repoPath string, files []string) error

func (*GoGitBackend) AddRemote

func (b *GoGitBackend) AddRemote(ctx context.Context, repoPath, name, url string) error

func (*GoGitBackend) Checkout

func (b *GoGitBackend) Checkout(ctx context.Context, repoPath, branch string) error

func (*GoGitBackend) CheckoutFiles

func (b *GoGitBackend) CheckoutFiles(ctx context.Context, repoPath, ref string, files []string) error

func (*GoGitBackend) CheckoutRef

func (b *GoGitBackend) CheckoutRef(ctx context.Context, repoPath, ref string) error

func (*GoGitBackend) CherryPick

func (b *GoGitBackend) CherryPick(ctx context.Context, repoPath, commitHash string) error

func (*GoGitBackend) Clone

func (b *GoGitBackend) Clone(ctx context.Context, opts CloneOptions) error

func (*GoGitBackend) CommitWithIdentity

func (b *GoGitBackend) CommitWithIdentity(ctx context.Context, repoPath, name, email, message string) error

func (*GoGitBackend) CreateBranch

func (b *GoGitBackend) CreateBranch(ctx context.Context, repoPath, branch, ref string) error

func (*GoGitBackend) CreateTag

func (b *GoGitBackend) CreateTag(ctx context.Context, repoPath, name, ref string) error

func (*GoGitBackend) DeleteBranch

func (b *GoGitBackend) DeleteBranch(ctx context.Context, repoPath, branch string) error

func (*GoGitBackend) DeleteTag

func (b *GoGitBackend) DeleteTag(ctx context.Context, repoPath, name string) error

func (*GoGitBackend) DeletedFiles

func (b *GoGitBackend) DeletedFiles(ctx context.Context, repoPath, from, to string) ([]string, error)

func (*GoGitBackend) Diff

func (b *GoGitBackend) Diff(ctx context.Context, repoPath string, opts DiffOptions) (string, error)

func (*GoGitBackend) DiffNames

func (b *GoGitBackend) DiffNames(ctx context.Context, repoPath, from, to string) ([]string, error)

func (*GoGitBackend) Fetch

func (b *GoGitBackend) Fetch(ctx context.Context, opts FetchOptions) (*FetchResult, error)

func (*GoGitBackend) FetchAll

func (b *GoGitBackend) FetchAll(ctx context.Context, repoPath string, auth AuthConfig) error

func (*GoGitBackend) GetBlob

func (b *GoGitBackend) GetBlob(ctx context.Context, repoPath, ref, filePath string) (*BlobContent, error)

func (*GoGitBackend) GetBranchSyncInfo

func (b *GoGitBackend) GetBranchSyncInfo(ctx context.Context, repoPath, branch, upstream string) (int, int, error)

func (*GoGitBackend) GetCommit

func (b *GoGitBackend) GetCommit(ctx context.Context, repoPath, hashStr string) (*CommitInfo, error)

func (*GoGitBackend) GetCommitsBetween

func (b *GoGitBackend) GetCommitsBetween(ctx context.Context, repoPath, from, to string) ([]CommitInfo, error)

func (*GoGitBackend) GetConfig

func (b *GoGitBackend) GetConfig(ctx context.Context, repoPath, key string) (string, error)

GetConfig reads any git config value. Supports both simple keys (e.g. "core.bare") and subsection keys (e.g. "remote.origin.url"). The special keys "user.name" and "user.email" are handled via the high-level Author struct for backward compatibility.

func (*GoGitBackend) GetCurrentBranch

func (b *GoGitBackend) GetCurrentBranch(ctx context.Context, repoPath string) (string, error)

func (*GoGitBackend) GetFileAtRevision

func (b *GoGitBackend) GetFileAtRevision(ctx context.Context, repoPath, path, ref string) ([]byte, error)

func (*GoGitBackend) GetFileHistory

func (b *GoGitBackend) GetFileHistory(ctx context.Context, repoPath, path string, limit int) ([]CommitInfo, error)

func (*GoGitBackend) GetRemoteURL

func (b *GoGitBackend) GetRemoteURL(ctx context.Context, repoPath, name string) (string, error)

func (*GoGitBackend) GetRemotes

func (b *GoGitBackend) GetRemotes(ctx context.Context, repoPath string) ([]string, error)

func (*GoGitBackend) GetStatus

func (b *GoGitBackend) GetStatus(ctx context.Context, repoPath string) (*RepoStatus, error)

func (*GoGitBackend) GetTagList

func (b *GoGitBackend) GetTagList(ctx context.Context, repoPath string) ([]TagInfo, error)

func (*GoGitBackend) GetTree

func (b *GoGitBackend) GetTree(ctx context.Context, repoPath, ref, dirPath string, recursive bool) ([]TreeEntry, error)

func (*GoGitBackend) Init

func (b *GoGitBackend) Init(ctx context.Context, repoPath string) error

func (*GoGitBackend) IsAncestor

func (b *GoGitBackend) IsAncestor(ctx context.Context, repoPath, ancestor, descendant string) (bool, error)

func (*GoGitBackend) ListBranches

func (b *GoGitBackend) ListBranches(ctx context.Context, repoPath string) ([]BranchDetail, error)

func (*GoGitBackend) ListLocalBranches

func (b *GoGitBackend) ListLocalBranches(ctx context.Context, repoPath string) ([]string, error)

func (*GoGitBackend) ListRemoteBranches

func (b *GoGitBackend) ListRemoteBranches(ctx context.Context, repoPath, remote string) ([]string, error)

func (*GoGitBackend) Merge

func (b *GoGitBackend) Merge(ctx context.Context, repoPath, branch string, opts MergeOptions) error

func (*GoGitBackend) MergeBase

func (b *GoGitBackend) MergeBase(ctx context.Context, repoPath, a, other string) (string, error)

func (*GoGitBackend) Pull

func (b *GoGitBackend) Pull(ctx context.Context, repoPath, remote, branch string, auth AuthConfig) error

func (*GoGitBackend) Push

func (b *GoGitBackend) Push(ctx context.Context, opts PushOptions) (*PushResult, error)

func (*GoGitBackend) PushTag

func (b *GoGitBackend) PushTag(ctx context.Context, repoPath, remote, name string, auth AuthConfig) error

func (*GoGitBackend) Rebase

func (b *GoGitBackend) Rebase(ctx context.Context, repoPath, onto string) error

func (*GoGitBackend) RebaseAbort

func (b *GoGitBackend) RebaseAbort(ctx context.Context, repoPath string) error

func (*GoGitBackend) RebaseContinue

func (b *GoGitBackend) RebaseContinue(ctx context.Context, repoPath string) error

func (*GoGitBackend) RemoveRemote

func (b *GoGitBackend) RemoveRemote(ctx context.Context, repoPath, name string) error

func (*GoGitBackend) RenameBranch

func (b *GoGitBackend) RenameBranch(ctx context.Context, repoPath, oldName, newName string) error

func (*GoGitBackend) RevParse

func (b *GoGitBackend) RevParse(ctx context.Context, repoPath, ref string) (string, error)

func (*GoGitBackend) RunRaw

func (b *GoGitBackend) RunRaw(ctx context.Context, repoPath string, args []string) (string, string, error)

RunRaw is not supported by the pure-Go (gogit) backend because go-git does not shell out to the git binary. This method exists to satisfy the GitBackend interface; callers that need arbitrary git commands should use the native backend instead.

func (*GoGitBackend) SetConfig

func (b *GoGitBackend) SetConfig(ctx context.Context, repoPath, key, value string) error

SetConfig writes any git config value. Supports both simple keys and subsection keys, matching the same format as GetConfig. The special keys "user.name" and "user.email" are written via the high-level Author struct.

func (*GoGitBackend) StashApply

func (b *GoGitBackend) StashApply(ctx context.Context, repoPath string, stashIdx int) error

func (*GoGitBackend) StashClear

func (b *GoGitBackend) StashClear(ctx context.Context, repoPath string) error

func (*GoGitBackend) StashDrop

func (b *GoGitBackend) StashDrop(ctx context.Context, repoPath string, stashIdx int) error

func (*GoGitBackend) StashList

func (b *GoGitBackend) StashList(ctx context.Context, repoPath string) ([]StashEntry, error)

func (*GoGitBackend) StashPop

func (b *GoGitBackend) StashPop(ctx context.Context, repoPath string, stashIdx int) error

func (*GoGitBackend) StashSave

func (b *GoGitBackend) StashSave(ctx context.Context, repoPath, message string) error

func (*GoGitBackend) TestConnection

func (b *GoGitBackend) TestConnection(ctx context.Context, url string, auth AuthConfig) error

type Logger

type Logger = provider.Logger

Logger reuses the provider.Logger interface. Consumers can inject the same logger for both provider and gitbackend.

func NewNoopLogger

func NewNoopLogger() Logger

NewNoopLogger returns a Logger that discards all output.

type MergeOptions

type MergeOptions struct {
	// Message overrides the auto-generated merge commit message.
	Message string
	// Squash merges the branch as a single squashed change without
	// recording merge ancestry.
	Squash bool
	// NoCommit performs the merge but leaves the result staged without
	// committing.
	NoCommit bool
	// FFOnly only allows a fast-forward merge; aborts otherwise.
	FFOnly bool
	// AllowUnrelated permits merging histories with no common ancestor.
	AllowUnrelated bool
}

MergeOptions contains options for merging a branch into HEAD.

type NativeGitBackend

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

func NewNativeGitBackend

func NewNativeGitBackend(opts Options) (*NativeGitBackend, error)

func (*NativeGitBackend) Add

func (b *NativeGitBackend) Add(ctx context.Context, repoPath string, files []string) error

func (*NativeGitBackend) AddRemote

func (b *NativeGitBackend) AddRemote(ctx context.Context, repoPath, name, url string) error

func (*NativeGitBackend) Checkout

func (b *NativeGitBackend) Checkout(ctx context.Context, repoPath, branch string) error

func (*NativeGitBackend) CheckoutFiles

func (b *NativeGitBackend) CheckoutFiles(ctx context.Context, repoPath, ref string, files []string) error

func (*NativeGitBackend) CheckoutRef

func (b *NativeGitBackend) CheckoutRef(ctx context.Context, repoPath, ref string) error

func (*NativeGitBackend) CherryPick

func (b *NativeGitBackend) CherryPick(ctx context.Context, repoPath, commitHash string) error

func (*NativeGitBackend) Clone

func (b *NativeGitBackend) Clone(ctx context.Context, opts CloneOptions) error

func (*NativeGitBackend) CommitWithIdentity

func (b *NativeGitBackend) CommitWithIdentity(ctx context.Context, repoPath, name, email, message string) error

func (*NativeGitBackend) CreateBranch

func (b *NativeGitBackend) CreateBranch(ctx context.Context, repoPath, branch, ref string) error

func (*NativeGitBackend) CreateTag

func (b *NativeGitBackend) CreateTag(ctx context.Context, repoPath, name, ref string) error

func (*NativeGitBackend) DeleteBranch

func (b *NativeGitBackend) DeleteBranch(ctx context.Context, repoPath, branch string) error

func (*NativeGitBackend) DeleteTag

func (b *NativeGitBackend) DeleteTag(ctx context.Context, repoPath, name string) error

func (*NativeGitBackend) DeletedFiles

func (b *NativeGitBackend) DeletedFiles(ctx context.Context, repoPath, from, to string) ([]string, error)

func (*NativeGitBackend) Diff

func (b *NativeGitBackend) Diff(ctx context.Context, repoPath string, opts DiffOptions) (string, error)

func (*NativeGitBackend) DiffNames

func (b *NativeGitBackend) DiffNames(ctx context.Context, repoPath, from, to string) ([]string, error)

func (*NativeGitBackend) Fetch

func (*NativeGitBackend) FetchAll

func (b *NativeGitBackend) FetchAll(ctx context.Context, repoPath string, auth AuthConfig) error

func (*NativeGitBackend) GetBlob

func (b *NativeGitBackend) GetBlob(ctx context.Context, repoPath, ref, filePath string) (*BlobContent, error)

func (*NativeGitBackend) GetBranchSyncInfo

func (b *NativeGitBackend) GetBranchSyncInfo(ctx context.Context, repoPath, branch, upstream string) (int, int, error)

func (*NativeGitBackend) GetCommit

func (b *NativeGitBackend) GetCommit(ctx context.Context, repoPath, hashStr string) (*CommitInfo, error)

func (*NativeGitBackend) GetCommitsBetween

func (b *NativeGitBackend) GetCommitsBetween(ctx context.Context, repoPath, from, to string) ([]CommitInfo, error)

func (*NativeGitBackend) GetConfig

func (b *NativeGitBackend) GetConfig(ctx context.Context, repoPath, key string) (string, error)

func (*NativeGitBackend) GetCurrentBranch

func (b *NativeGitBackend) GetCurrentBranch(ctx context.Context, repoPath string) (string, error)

func (*NativeGitBackend) GetFileAtRevision

func (b *NativeGitBackend) GetFileAtRevision(ctx context.Context, repoPath, path, ref string) ([]byte, error)

func (*NativeGitBackend) GetFileHistory

func (b *NativeGitBackend) GetFileHistory(ctx context.Context, repoPath, path string, limit int) ([]CommitInfo, error)

func (*NativeGitBackend) GetRemoteURL

func (b *NativeGitBackend) GetRemoteURL(ctx context.Context, repoPath, name string) (string, error)

func (*NativeGitBackend) GetRemotes

func (b *NativeGitBackend) GetRemotes(ctx context.Context, repoPath string) ([]string, error)

func (*NativeGitBackend) GetStatus

func (b *NativeGitBackend) GetStatus(ctx context.Context, repoPath string) (*RepoStatus, error)

func (*NativeGitBackend) GetTagList

func (b *NativeGitBackend) GetTagList(ctx context.Context, repoPath string) ([]TagInfo, error)

func (*NativeGitBackend) GetTree

func (b *NativeGitBackend) GetTree(ctx context.Context, repoPath, ref, dirPath string, recursive bool) ([]TreeEntry, error)

func (*NativeGitBackend) Init

func (b *NativeGitBackend) Init(ctx context.Context, repoPath string) error

func (*NativeGitBackend) IsAncestor

func (b *NativeGitBackend) IsAncestor(ctx context.Context, repoPath, ancestor, descendant string) (bool, error)

func (*NativeGitBackend) ListBranches

func (b *NativeGitBackend) ListBranches(ctx context.Context, repoPath string) ([]BranchDetail, error)

func (*NativeGitBackend) ListLocalBranches

func (b *NativeGitBackend) ListLocalBranches(ctx context.Context, repoPath string) ([]string, error)

func (*NativeGitBackend) ListRemoteBranches

func (b *NativeGitBackend) ListRemoteBranches(ctx context.Context, repoPath, remote string) ([]string, error)

func (*NativeGitBackend) Merge

func (b *NativeGitBackend) Merge(ctx context.Context, repoPath, branch string, opts MergeOptions) error

func (*NativeGitBackend) MergeBase

func (b *NativeGitBackend) MergeBase(ctx context.Context, repoPath, a, other string) (string, error)

func (*NativeGitBackend) Pull

func (b *NativeGitBackend) Pull(ctx context.Context, repoPath, remote, branch string, auth AuthConfig) error

func (*NativeGitBackend) Push

func (*NativeGitBackend) PushTag

func (b *NativeGitBackend) PushTag(ctx context.Context, repoPath, remote, name string, auth AuthConfig) error

func (*NativeGitBackend) Rebase

func (b *NativeGitBackend) Rebase(ctx context.Context, repoPath, onto string) error

func (*NativeGitBackend) RebaseAbort

func (b *NativeGitBackend) RebaseAbort(ctx context.Context, repoPath string) error

func (*NativeGitBackend) RebaseContinue

func (b *NativeGitBackend) RebaseContinue(ctx context.Context, repoPath string) error

func (*NativeGitBackend) RemoveRemote

func (b *NativeGitBackend) RemoveRemote(ctx context.Context, repoPath, name string) error

func (*NativeGitBackend) RenameBranch

func (b *NativeGitBackend) RenameBranch(ctx context.Context, repoPath, oldName, newName string) error

func (*NativeGitBackend) RevParse

func (b *NativeGitBackend) RevParse(ctx context.Context, repoPath, ref string) (string, error)

func (*NativeGitBackend) RunRaw

func (b *NativeGitBackend) RunRaw(ctx context.Context, repoPath string, args []string) (string, string, error)

func (*NativeGitBackend) SetConfig

func (b *NativeGitBackend) SetConfig(ctx context.Context, repoPath, key, value string) error

func (*NativeGitBackend) StashApply

func (b *NativeGitBackend) StashApply(ctx context.Context, repoPath string, index int) error

func (*NativeGitBackend) StashClear

func (b *NativeGitBackend) StashClear(ctx context.Context, repoPath string) error

func (*NativeGitBackend) StashDrop

func (b *NativeGitBackend) StashDrop(ctx context.Context, repoPath string, index int) error

func (*NativeGitBackend) StashList

func (b *NativeGitBackend) StashList(ctx context.Context, repoPath string) ([]StashEntry, error)

func (*NativeGitBackend) StashPop

func (b *NativeGitBackend) StashPop(ctx context.Context, repoPath string, index int) error

func (*NativeGitBackend) StashSave

func (b *NativeGitBackend) StashSave(ctx context.Context, repoPath, message string) error

func (*NativeGitBackend) TestConnection

func (b *NativeGitBackend) TestConnection(ctx context.Context, url string, auth AuthConfig) error

type Options

type Options struct {
	Type   string // "native", "gogit", or "" for auto-detect
	Logger Logger
}

Options holds configuration for creating a GitBackend.

type PushOptions

type PushOptions struct {
	RepoPath string
	// Remote is the remote name to push to (e.g. "origin").
	Remote string
	// RefSpecs are the refs to push (e.g. "refs/heads/main:refs/heads/main").
	// Ignored when Mirror is true.
	RefSpecs []string
	// Force forces the push (overwrites remote refs).
	Force bool
	// Mirror pushes all refs as a mirror (--mirror).
	Mirror          bool
	InsecureSkipTLS bool
	Auth            AuthConfig
	// Progress receives human-readable progress output (optional).
	Progress io.Writer
}

PushOptions contains options for pushing to a remote.

type PushResult

type PushResult struct {
	// PushedRefs are the refs that were pushed.
	PushedRefs []string
	// Errors collects per-ref errors reported by the remote (if any).
	Errors []string
}

PushResult contains the result of a push operation.

type RemoteOps

type RemoteOps interface {
	// GetRemotes returns the names of all configured remotes.
	GetRemotes(ctx context.Context, repoPath string) ([]string, error)
	// AddRemote adds a new remote.
	AddRemote(ctx context.Context, repoPath string, name string, url string) error
	// RemoveRemote removes a configured remote.
	RemoveRemote(ctx context.Context, repoPath string, name string) error
	// GetRemoteURL returns the URL of a named remote.
	GetRemoteURL(ctx context.Context, repoPath string, name string) (string, error)
	// TestConnection verifies that url is reachable with the given credentials.
	TestConnection(ctx context.Context, url string, auth AuthConfig) error
}

RemoteOps covers remote configuration and connectivity checks.

type RepoStatus

type RepoStatus struct {
	// Branch is the name of the current branch (without the refs/heads/ prefix).
	Branch string
	// IsClean is true when there are no staged, unstaged or untracked changes.
	IsClean bool
	// Staged lists files with changes staged for commit.
	Staged []FileStatus
	// Unstaged lists files with changes in the working tree (not yet staged).
	Unstaged []FileStatus
	// Untracked lists untracked files (not ignored).
	Untracked []string
	// Ahead is the number of local commits not pushed to upstream.
	Ahead int
	// Behind is the number of upstream commits not pulled locally.
	Behind int
}

RepoStatus represents the working tree status.

type Repository

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

Repository is a stateful convenience wrapper around GitBackend that binds a repository path together with its authentication and TLS settings. It removes the need for callers to repeat repoPath/auth/insecure on every operation.

Create instances with CloneRepository (clones a remote first) or OpenRepository (operates on an already-present working tree).

func CloneRepository

func CloneRepository(ctx context.Context, b GitBackend, url, path string, auth AuthConfig, insecure bool) (*Repository, error)

CloneRepository clones url into path and returns a Repository bound to it.

func OpenRepository

func OpenRepository(b GitBackend, dir string, auth AuthConfig, insecure bool) *Repository

OpenRepository wraps an existing working tree located at dir with the given auth and TLS settings. The backend must already be created by the caller.

func (*Repository) Auth

func (r *Repository) Auth() AuthConfig

Auth returns the bound authentication configuration.

func (*Repository) Checkout

func (r *Repository) Checkout(ctx context.Context, ref string) error

Checkout checks out the given ref (branch, tag or commit).

func (*Repository) CheckoutDetached

func (r *Repository) CheckoutDetached(ctx context.Context, ref string) error

CheckoutDetached force-checks out the given ref in detached HEAD state.

func (*Repository) CheckoutFiles

func (r *Repository) CheckoutFiles(ctx context.Context, ref string, files []string) error

CheckoutFiles restores the given files from ref into the working tree.

func (*Repository) Close

func (r *Repository) Close() error

Close releases any resources associated with the repository. Currently this is a graceful no-op (the backends do not hold persistent state), but callers should always invoke it via defer so that future resource cleanup (e.g. temporary credential helpers, cached file handles) is wired in automatically.

func (*Repository) CommitWithIdentity

func (r *Repository) CommitWithIdentity(ctx context.Context, name, email, msg string) error

CommitWithIdentity creates a commit using an explicit author identity.

func (*Repository) DeletedFiles

func (r *Repository) DeletedFiles(ctx context.Context, baseSHA, headSHA string) ([]string, error)

DeletedFiles returns the list of files deleted between two commits.

func (*Repository) Diff

func (r *Repository) Diff(ctx context.Context, baseSHA, headSHA string) (string, error)

Diff returns the textual diff between two commits.

func (*Repository) DiffNameOnly

func (r *Repository) DiffNameOnly(ctx context.Context, baseSHA, headSHA string) ([]string, error)

DiffNameOnly returns the list of files changed between two commits.

func (*Repository) Dir

func (r *Repository) Dir() string

Dir returns the on-disk path of the repository working tree.

func (*Repository) Fetch

func (r *Repository) Fetch(ctx context.Context, refspec string) error

Fetch fetches a single refspec from the "origin" remote.

func (*Repository) FetchAll

func (r *Repository) FetchAll(ctx context.Context) error

FetchAll fetches tags and every remote branch from the "origin" remote.

func (*Repository) MergeBase

func (r *Repository) MergeBase(ctx context.Context, a, b string) (string, error)

MergeBase returns the best common ancestor of two commits.

func (*Repository) RevParse

func (r *Repository) RevParse(ctx context.Context, ref string) (string, error)

RevParse resolves a ref to its full object SHA.

type StashEntry

type StashEntry struct {
	// Index is the stash position (0 is the most recent, i.e. stash@{0}).
	Index int
	// Message is the stash list line as produced by `git stash list`.
	Message string
}

StashEntry represents a stash entry.

type StashOps

type StashOps interface {
	// StashList lists the stash entries.
	StashList(ctx context.Context, repoPath string) ([]StashEntry, error)
	// StashSave saves the working tree and index changes to the stash.
	StashSave(ctx context.Context, repoPath string, message string) error
	// StashApply applies a stash entry without removing it from the stash list.
	StashApply(ctx context.Context, repoPath string, index int) error
	// StashPop applies a stash entry and then drops it from the stash list.
	StashPop(ctx context.Context, repoPath string, index int) error
	// StashDrop removes a single stash entry.
	StashDrop(ctx context.Context, repoPath string, index int) error
	// StashClear removes all stash entries.
	StashClear(ctx context.Context, repoPath string) error
}

StashOps covers the stash list and stash lifecycle.

type StatusCode

type StatusCode byte

StatusCode is the porcelain status code used for a file in the staging area or the working tree. These mirror the codes produced by `git status --porcelain`.

const (
	StatusUnmodified StatusCode = ' '
	StatusModified   StatusCode = 'M'
	StatusAdded      StatusCode = 'A'
	StatusDeleted    StatusCode = 'D'
	StatusRenamed    StatusCode = 'R'
	StatusCopied     StatusCode = 'C'
	StatusUntracked  StatusCode = '?'
	StatusIgnored    StatusCode = '!'
)

type StatusDiffOps

type StatusDiffOps interface {
	// GetStatus returns the working tree status.
	GetStatus(ctx context.Context, repoPath string) (*RepoStatus, error)
	// Diff returns a textual diff. With empty From/To it diffs the working tree
	// against HEAD; otherwise it diffs the two commits.
	Diff(ctx context.Context, repoPath string, opts DiffOptions) (string, error)
}

StatusDiffOps covers working-tree status and diff inspection.

type TagInfo

type TagInfo struct {
	Name string
	// Hash is the SHA the tag points at (the tag object for annotated tags,
	// or the commit for lightweight tags).
	Hash    string
	Message string
	Author  string
}

TagInfo represents a git tag.

type TagOps

type TagOps interface {
	// CreateTag creates a tag pointing at ref (or HEAD when empty).
	CreateTag(ctx context.Context, repoPath string, name string, ref string) error
	// DeleteTag deletes a local tag.
	DeleteTag(ctx context.Context, repoPath string, name string) error
	// PushTag pushes a single tag to remote.
	PushTag(ctx context.Context, repoPath string, remote string, name string, auth AuthConfig) error
	// GetTagList lists all tags with their metadata.
	GetTagList(ctx context.Context, repoPath string) ([]TagInfo, error)
}

TagOps covers tag creation, deletion, listing, and pushing.

type TreeEntry

type TreeEntry struct {
	Name string
	// Path is the full path relative to the repository root.
	Path string
	// Type is either TreeEntryFile or TreeEntryDir.
	Type TreeEntryType
	// Mode is the git file mode (e.g. "100644").
	Mode string
	Hash string
	// Size is the file size in bytes (populated for files in recursive mode).
	Size int64
}

TreeEntry represents a file or directory in a git tree.

type TreeEntryType

type TreeEntryType string

TreeEntryType identifies whether a TreeEntry is a file or a directory.

const (
	TreeEntryFile TreeEntryType = "file"
	TreeEntryDir  TreeEntryType = "dir"
)

Jump to

Keyboard shortcuts

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