git

package
v0.0.1-alpha83 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2026 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Overview

Package git provides git operations with an abstracted interface. This allows for different implementations (local, remote) while keeping the same API.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound       = errors.New("not found")
	ErrNotARepository = errors.New("not a git repository")
	ErrInvalidRef     = errors.New("invalid ref")
	ErrCloneFailed    = errors.New("clone failed")
	ErrFetchFailed    = errors.New("fetch failed")
	ErrCheckoutFailed = errors.New("checkout failed")
	ErrDirtyWorkTree  = errors.New("working tree has uncommitted changes")
)

Common errors

Functions

func IsGitURL

func IsGitURL(source string) bool

IsGitURL returns true if the source looks like a git URL.

Types

type Branch

type Branch struct {
	Name      string `json:"name"`
	IsRemote  bool   `json:"isRemote"`
	IsCurrent bool   `json:"isCurrent"`
	Commit    string `json:"commit"`   // HEAD commit SHA
	Upstream  string `json:"upstream"` // Upstream branch name (if tracking)
}

Branch represents a git branch.

type Commit

type Commit struct {
	SHA         string    `json:"sha"`
	ShortSHA    string    `json:"shortSha"`
	Message     string    `json:"message"`
	Author      string    `json:"author"`
	AuthorEmail string    `json:"authorEmail"`
	AuthorDate  time.Time `json:"authorDate"`
	Committer   string    `json:"committer"`
	CommitDate  time.Time `json:"commitDate"`
	Parents     []string  `json:"parents"`
}

Commit represents a git commit.

type DiffOptions

type DiffOptions struct {
	// Compare staged changes (git diff --cached)
	Staged bool

	// Compare against a specific ref (default: working tree vs HEAD)
	BaseRef string
	HeadRef string

	// Limit to specific paths
	Paths []string

	// Context lines around changes (default: 3)
	Context int
}

DiffOptions configures what diff to compute.

type FileDiff

type FileDiff struct {
	Path      string `json:"path"`
	OldPath   string `json:"oldPath"` // For renamed files
	Status    string `json:"status"`  // "added", "modified", "deleted", "renamed"
	Binary    bool   `json:"binary"`
	Additions int    `json:"additions"`
	Deletions int    `json:"deletions"`
	Patch     string `json:"patch"` // Unified diff content
}

FileDiff represents the diff of a single file.

type FileEntry

type FileEntry struct {
	Path  string `json:"path"`
	Name  string `json:"name"`
	IsDir bool   `json:"isDir"`
	Size  int64  `json:"size"`
	Mode  string `json:"mode"` // File mode (e.g., "100644")
}

FileEntry represents a file in the repository tree.

type FileStatus

type FileStatus struct {
	Path    string `json:"path"`
	Status  string `json:"status"`  // "added", "modified", "deleted", "renamed", "copied"
	OldPath string `json:"oldPath"` // For renamed/copied files
}

FileStatus represents the status of a single file.

type LocalProvider

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

LocalProvider implements Provider using go-git against local repositories. Workspaces are cloned directly to {baseDir}/{projectID}/workspaces/{workspaceID}.

func NewLocalProvider

func NewLocalProvider(baseDir string, opts ...LocalProviderOption) (*LocalProvider, error)

NewLocalProvider creates a new local git provider. baseDir is the root directory where workspaces will be stored. Structure: {baseDir}/{projectID}/workspaces/{workspaceID}/

func (*LocalProvider) ApplyPatches

func (p *LocalProvider) ApplyPatches(ctx context.Context, workspaceID string, patches []byte) (string, error)

ApplyPatches applies mbox-format patches (from git format-patch) to the workspace. Returns the final commit SHA after all patches are applied. If application fails, the operation is aborted without losing local changes.

func (*LocalProvider) Branches

func (p *LocalProvider) Branches(ctx context.Context, workspaceID string) ([]Branch, error)

Branches lists all branches.

func (*LocalProvider) Checkout

func (p *LocalProvider) Checkout(ctx context.Context, workspaceID, ref string) error

Checkout checks out a specific ref.

func (*LocalProvider) Commit

func (p *LocalProvider) Commit(ctx context.Context, workspaceID, message, authorName, authorEmail string) (*Commit, error)

Commit creates a commit with the staged changes.

func (*LocalProvider) Diff

func (p *LocalProvider) Diff(ctx context.Context, workspaceID string, opts DiffOptions) ([]FileDiff, error)

Diff returns file diffs.

func (*LocalProvider) EnsureWorkspace

func (p *LocalProvider) EnsureWorkspace(ctx context.Context, projectID, workspaceID, source, ref string) (string, string, error)

EnsureWorkspace ensures a workspace has a working copy ready.

func (*LocalProvider) EnsureWorkspaceByID

func (p *LocalProvider) EnsureWorkspaceByID(ctx context.Context, workspaceID string) (string, string, error)

EnsureWorkspaceByID ensures workspace is ready using only workspaceID.

func (*LocalProvider) Fetch

func (p *LocalProvider) Fetch(ctx context.Context, workspaceID string) error

Fetch fetches updates from remote.

func (*LocalProvider) FileTree

func (p *LocalProvider) FileTree(ctx context.Context, workspaceID, ref string) ([]FileEntry, error)

FileTree returns the file listing at a specific ref.

func (*LocalProvider) GetUserConfig

func (p *LocalProvider) GetUserConfig(_ context.Context) (name, email string)

GetUserConfig retrieves the global git user name and email configuration.

func (*LocalProvider) GetWorkDir

func (p *LocalProvider) GetWorkDir(ctx context.Context, workspaceID string) string

GetWorkDir returns the working directory path for a workspace.

func (*LocalProvider) Log

func (p *LocalProvider) Log(ctx context.Context, workspaceID string, opts LogOptions) ([]Commit, error)

Log returns commit history.

func (*LocalProvider) ReadFile

func (p *LocalProvider) ReadFile(ctx context.Context, workspaceID, ref, path string) ([]byte, error)

ReadFile reads a file at a specific ref.

func (*LocalProvider) RemoveWorkspace

func (p *LocalProvider) RemoveWorkspace(_ context.Context, workspaceID string) error

RemoveWorkspace removes the workspace working directory.

func (*LocalProvider) Stage

func (p *LocalProvider) Stage(ctx context.Context, workspaceID string, paths []string) error

Stage stages files for commit.

func (*LocalProvider) Status

func (p *LocalProvider) Status(ctx context.Context, workspaceID string) (*Status, error)

Status returns the current git status.

func (*LocalProvider) WriteFile

func (p *LocalProvider) WriteFile(ctx context.Context, workspaceID, path string, content []byte) error

WriteFile writes content to a file in the working tree.

type LocalProviderOption

type LocalProviderOption func(*LocalProvider)

LocalProviderOption configures a LocalProvider.

func WithWorkspaceSource

func WithWorkspaceSource(src WorkspaceSource) LocalProviderOption

WithWorkspaceSource sets the workspace source for the provider. This enables EnsureWorkspaceByID and auto-recovery in GetWorkDir.

type LogOptions

type LogOptions struct {
	// Maximum number of commits to return (default: 50)
	Limit int

	// Start from this ref (default: HEAD)
	Ref string

	// Only commits affecting these paths
	Paths []string

	// Skip this many commits
	Skip int
}

LogOptions configures commit log retrieval.

type Provider

type Provider interface {
	// EnsureWorkspace ensures a workspace has a working copy ready.
	// For git URLs: clones directly to the workspace directory.
	// For local paths: clones to get an isolated working copy.
	// projectID scopes the clone to a specific project's directory.
	// Returns the absolute path to the working directory and the current HEAD commit SHA.
	EnsureWorkspace(ctx context.Context, projectID, workspaceID, source, ref string) (workDir string, commit string, err error)

	// Fetch fetches updates from remote to the workspace.
	Fetch(ctx context.Context, workspaceID string) error

	// Checkout checks out a specific ref (branch, tag, or commit SHA).
	Checkout(ctx context.Context, workspaceID, ref string) error

	// Status returns the current git status of the workspace.
	Status(ctx context.Context, workspaceID string) (*Status, error)

	// Diff returns file diffs for the workspace.
	Diff(ctx context.Context, workspaceID string, opts DiffOptions) ([]FileDiff, error)

	// Branches lists all branches (local and remote).
	Branches(ctx context.Context, workspaceID string) ([]Branch, error)

	// FileTree returns the file listing at a specific ref (or HEAD if empty).
	FileTree(ctx context.Context, workspaceID, ref string) ([]FileEntry, error)

	// ReadFile reads a file at a specific ref (or working tree if ref is empty).
	ReadFile(ctx context.Context, workspaceID, ref, path string) ([]byte, error)

	// WriteFile writes content to a file in the working tree.
	WriteFile(ctx context.Context, workspaceID, path string, content []byte) error

	// Stage stages files for commit. Use "." to stage all changes.
	Stage(ctx context.Context, workspaceID string, paths []string) error

	// Commit creates a commit with the staged changes.
	Commit(ctx context.Context, workspaceID, message, authorName, authorEmail string) (*Commit, error)

	// Log returns commit history.
	Log(ctx context.Context, workspaceID string, opts LogOptions) ([]Commit, error)

	// GetWorkDir returns the working directory path for a workspace.
	// Returns empty string if workspace doesn't exist.
	GetWorkDir(ctx context.Context, workspaceID string) string

	// RemoveWorkspace removes the workspace working directory.
	RemoveWorkspace(ctx context.Context, workspaceID string) error

	// ApplyPatches applies mbox-format patches (from git format-patch) to the workspace.
	// Returns the final commit SHA after all patches are applied.
	// If application fails, local changes are preserved.
	ApplyPatches(ctx context.Context, workspaceID string, patches []byte) (finalCommit string, err error)

	// GetUserConfig retrieves the global git user name and email configuration.
	// Returns empty strings if not configured.
	GetUserConfig(ctx context.Context) (name, email string)
}

Provider defines the interface for git operations. Implementations can be local (using git CLI) or remote (using a service).

type Status

type Status struct {
	Branch       string       `json:"branch"`
	Commit       string       `json:"commit"`       // Current HEAD commit SHA
	CommitShort  string       `json:"commitShort"`  // Short commit SHA
	Ahead        int          `json:"ahead"`        // Commits ahead of upstream
	Behind       int          `json:"behind"`       // Commits behind upstream
	Staged       []FileStatus `json:"staged"`       // Staged changes
	Unstaged     []FileStatus `json:"unstaged"`     // Unstaged changes
	Untracked    []string     `json:"untracked"`    // Untracked files
	IsClean      bool         `json:"isClean"`      // No uncommitted changes
	HasConflicts bool         `json:"hasConflicts"` // Merge conflicts present
}

Status represents the git status of a repository.

type StoreWorkspaceSource

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

StoreWorkspaceSource implements WorkspaceSource using the store.

func NewStoreWorkspaceSource

func NewStoreWorkspaceSource(s *store.Store) *StoreWorkspaceSource

NewStoreWorkspaceSource creates a new store-backed workspace source.

func (*StoreWorkspaceSource) GetWorkspaceInfo

func (s *StoreWorkspaceSource) GetWorkspaceInfo(ctx context.Context, workspaceID string) (*WorkspaceInfo, error)

GetWorkspaceInfo returns workspace information from the store.

type WorkspaceInfo

type WorkspaceInfo struct {
	WorkspaceID string
	ProjectID   string
	Path        string // git URL or local path
	SourceType  string // "local" or "git"
}

WorkspaceInfo contains workspace details needed for git operations.

type WorkspaceSource

type WorkspaceSource interface {
	GetWorkspaceInfo(ctx context.Context, workspaceID string) (*WorkspaceInfo, error)
}

WorkspaceSource provides workspace information to the git provider. This allows the provider to lookup workspace details from a store.

Jump to

Keyboard shortcuts

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