git

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package git provides git repository operations.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidCloneStrategy is returned when an invalid clone strategy is specified.
	ErrInvalidCloneStrategy = errors.New("invalid clone strategy")

	// ErrDirectoryNotGitRepo is returned when a directory exists but is not a git repository.
	ErrDirectoryNotGitRepo = errors.New("directory exists but is not a git repository")

	// ErrNoGitOutput is returned when git command produces no output.
	ErrNoGitOutput = errors.New("no output from git command")
)

Functions

This section is empty.

Types

type Backend

type Backend interface {
	// Clone clones the repository to the specified directory.
	// If the directory already exists and contains a valid clone, this is a no-op.
	Clone(ctx context.Context, repoURL string, directory string) error

	// Pull fetches and merges the latest changes from the remote repository.
	Pull(ctx context.Context, directory string) error

	// GetRevision returns the current git revision (commit SHA) of the repository.
	GetRevision(ctx context.Context, directory string) (string, error)

	// GetStatus returns the current git status of the repository.
	// Returns information about uncommitted changes, branch, etc.
	GetStatus(ctx context.Context, directory string) (*Status, error)

	// EnsureCloned ensures the repository is cloned based on the clone strategy.
	// For "always": always clones (or re-clones if exists)
	// For "on-demand": only clones if directory doesn't exist
	// TODO: move cloneStrategy up to the caller and remove from here
	EnsureCloned(ctx context.Context, repoURL string, directory string, cloneStrategy string) error

	// IsGitRepository checks if the directory is a valid git repository.
	IsGitRepository(ctx context.Context, directory string) (bool, error)

	// GetCurrentBranch returns the current branch name or empty string if detached.
	GetCurrentBranch(ctx context.Context, directory string) (string, error)

	// IsDetached checks if HEAD is detached (not on any branch).
	IsDetached(ctx context.Context, directory string) (bool, error)

	// GetTrackingBranch returns the upstream tracking branch (e.g., "origin/main").
	// Returns empty string if no tracking branch is set.
	GetTrackingBranch(ctx context.Context, directory string) (string, error)

	// GetDefaultBranch detects the default branch from remote or origin/HEAD.
	// First tries to read from the cloned repository's origin/HEAD, then falls back to querying the remote.
	GetDefaultBranch(ctx context.Context, directory string, repoURL string) (string, error)

	// GetCommitCounts returns (behind, ahead) counts vs tracking branch.
	// Returns (0, 0) if no tracking branch exists.
	GetCommitCounts(ctx context.Context, directory string) (behind, ahead int, err error)

	// DetectState detects the complete state of a repository.
	// This function is fail-safe: it continues collecting state even if some checks fail.
	DetectState(ctx context.Context, directory string, configuredURL string) (*RepositoryState, error)

	// PullFFOnly pulls with fast-forward only to avoid creating merge commits.
	PullFFOnly(ctx context.Context, directory string) error

	// Fetch fetches from origin without merging.
	Fetch(ctx context.Context, directory string) error

	// StashPush stashes uncommitted changes with an optional message.
	StashPush(ctx context.Context, directory string, message string) error
}

Backend defines the interface for low-level git repository operations.

type Client

type Client struct {
}

Client implements Backend using git.

func NewClient

func NewClient() *Client

NewClient creates a new git client.

func (*Client) Clone

func (b *Client) Clone(ctx context.Context, repoURL string, directory string) error

Clone clones the repository to the specified directory.

func (*Client) DetectState

func (b *Client) DetectState(ctx context.Context, directory string, configuredURL string) (*RepositoryState, error)

DetectState detects the complete state of a repository. This function is fail-safe: it continues collecting state even if some checks fail.

func (*Client) EnsureCloned

func (b *Client) EnsureCloned(ctx context.Context, repoURL string, directory string, cloneStrategy string) error

EnsureCloned ensures the repository is cloned based on the clone strategy.

func (*Client) Fetch

func (b *Client) Fetch(ctx context.Context, directory string) error

Fetch fetches from origin without merging.

func (*Client) GetCommitCounts

func (b *Client) GetCommitCounts(ctx context.Context, directory string) (behind, ahead int, err error)

GetCommitCounts returns (behind, ahead) counts vs tracking branch. Returns (0, 0) if no tracking branch exists.

func (*Client) GetCurrentBranch

func (b *Client) GetCurrentBranch(ctx context.Context, directory string) (string, error)

GetCurrentBranch returns the current branch name or empty string if detached.

func (*Client) GetDefaultBranch

func (b *Client) GetDefaultBranch(ctx context.Context, directory string, repoURL string) (string, error)

GetDefaultBranch detects the default branch from remote or origin/HEAD. First tries to read from the cloned repository's origin/HEAD, then falls back to querying the remote.

func (*Client) GetRevision

func (b *Client) GetRevision(ctx context.Context, directory string) (string, error)

GetRevision returns the current git revision (commit SHA).

func (*Client) GetStatus

func (b *Client) GetStatus(ctx context.Context, directory string) (*Status, error)

GetStatus returns the current git status.

func (*Client) GetTrackingBranch

func (b *Client) GetTrackingBranch(ctx context.Context, directory string) (string, error)

GetTrackingBranch returns the upstream tracking branch (e.g., "origin/main"). Returns empty string if no tracking branch is set.

func (*Client) HasUncommittedChanges

func (b *Client) HasUncommittedChanges(ctx context.Context, directory string) (bool, error)

HasUncommittedChanges checks for uncommitted changes (staged or unstaged).

func (*Client) IsDetached

func (b *Client) IsDetached(ctx context.Context, directory string) (bool, error)

IsDetached checks if HEAD is detached (not on any branch).

func (*Client) IsGitRepository

func (b *Client) IsGitRepository(ctx context.Context, directory string) (bool, error)

IsGitRepository checks if the directory is a valid git repository.

func (*Client) Pull

func (b *Client) Pull(ctx context.Context, directory string) error

Pull fetches and merges the latest changes.

func (*Client) PullFFOnly

func (b *Client) PullFFOnly(ctx context.Context, directory string) error

PullFFOnly pulls with fast-forward only to avoid creating merge commits.

func (*Client) StashPush

func (b *Client) StashPush(ctx context.Context, directory string, message string) error

StashPush stashes uncommitted changes.

type RepositoryState

type RepositoryState struct {
	Exists         bool
	IsGitRepo      bool
	CurrentBranch  string // Empty if detached
	IsDetached     bool
	HasUncommitted bool
	HasUnpushed    bool
	TrackingBranch string // e.g., "origin/main"
	DefaultBranch  string // e.g., "main"
	CommitsBehind  int
	CommitsAhead   int
	RemoteURL      string
	RemoteMatches  bool
	LastError      error
}

RepositoryState represents the complete state of a repository.

type Status

type Status struct {
	// Branch is the current branch name.
	Branch string

	// Revision is the current commit SHA.
	Revision string

	// HasUncommittedChanges indicates if there are uncommitted changes.
	HasUncommittedChanges bool

	// RemoteURL is the remote repository URL.
	RemoteURL string
}

Status represents the git status of a repository.

Directories

Path Synopsis
Package git provides auto-generated mocks for the git.Backend interface.
Package git provides auto-generated mocks for the git.Backend interface.

Jump to

Keyboard shortcuts

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