git

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package git provides a shared utility layer for git operations. It wraps system git commands, providing a consistent API for use across workspace preparers and publishers. The design allows for future migration to go-git or a hybrid approach without changing the consumer API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RemoteGetConfig

func RemoteGetConfig(ctx context.Context, key string) (string, error)

RemoteGetConfig retrieves a global/system git configuration value. This function does not use a repository directory and reads from global/system config. Use client.ConfigGet() for repository-specific configuration.

Types

type ApplyOptions

type ApplyOptions struct {
	// PatchPath is the path to the patch file.
	PatchPath string

	// ThreeWay enables 3-way merge.
	ThreeWay bool

	// CheckOnly validates the patch without applying it.
	CheckOnly bool
}

ApplyOptions specifies options for applying a patch.

type Client

type Client struct {
	// Dir is the working directory of the git repository.
	Dir string

	// AuthToken is the optional authentication token for push operations.
	AuthToken string

	// Options provides optional git configuration.
	Options *ClientOptions
}

Client represents a git client for operations on a repository.

func NewClient

func NewClient(dir string) *Client

NewClient creates a new git client for the given directory.

func NewClientWithToken

func NewClientWithToken(dir, token string) *Client

NewClientWithToken creates a new git client with authentication.

func (*Client) Add

func (c *Client) Add(ctx context.Context, args ...string) error

Add stages files for commit.

func (*Client) AddAll

func (c *Client) AddAll(ctx context.Context) error

AddAll stages all changes.

func (*Client) AddWorktree

func (c *Client) AddWorktree(ctx context.Context, path, ref string, detach bool) error

AddWorktree adds a new worktree at the given path, optionally checking out a ref.

func (*Client) Apply

func (c *Client) Apply(ctx context.Context, opts ApplyOptions) error

Apply applies a patch file to the workspace.

func (*Client) ApplyCheck

func (c *Client) ApplyCheck(ctx context.Context, patchPath string, threeWay bool) error

ApplyCheck checks if a patch can be applied without conflicts.

func (*Client) Branch

func (c *Client) Branch(ctx context.Context, name string, create bool) error

Branch creates a new branch or checks out an existing one.

func (*Client) Checkout

func (c *Client) Checkout(ctx context.Context, ref string) error

Checkout checks out a reference (branch, tag, or commit).

func (*Client) Commit

func (c *Client) Commit(ctx context.Context, message string) (string, error)

Commit creates a commit with the given message.

func (*Client) CommitWith

func (c *Client) CommitWith(ctx context.Context, opts CommitOptions) (string, error)

CommitWith creates a commit with options.

func (*Client) ConfigGet

func (c *Client) ConfigGet(ctx context.Context, key string) (string, error)

ConfigGet gets a git configuration value.

func (*Client) ExecCommand

func (c *Client) ExecCommand(ctx context.Context, args ...string) ([]byte, error)

ExecCommand is a safe wrapper to allow callers to run arbitrary git commands.

func (*Client) GetHeadSHA

func (c *Client) GetHeadSHA(ctx context.Context) (string, error)

GetHeadSHA returns the current HEAD SHA.

func (*Client) GetRepositoryInfo

func (c *Client) GetRepositoryInfo(ctx context.Context) (*RepositoryInfo, error)

GetRepositoryInfo returns information about the repository.

func (*Client) HasChanges

func (c *Client) HasChanges(ctx context.Context) bool

HasChanges returns true if there are uncommitted changes.

func (*Client) Init

func (c *Client) Init(ctx context.Context) error

Init initializes a new git repository.

func (*Client) InitRepository

func (c *Client) InitRepository(ctx context.Context) error

InitRepository initializes a git repository with default configuration.

func (*Client) InitSubmodules

func (c *Client) InitSubmodules(ctx context.Context) error

InitSubmodules initializes git submodules.

func (*Client) IsClean

func (c *Client) IsClean(ctx context.Context) bool

IsClean returns true if the working directory has no uncommitted changes.

func (*Client) IsRepo

func (c *Client) IsRepo(ctx context.Context) bool

IsRepo checks if the directory is a git repository.

func (*Client) IsShallowClone

func (c *Client) IsShallowClone(ctx context.Context) (bool, error)

IsShallowClone checks if the repository is a shallow clone.

func (*Client) Push

func (c *Client) Push(ctx context.Context, opts PushOptions) error

Push pushes commits to a remote repository. Note: This requires git credentials to be configured for authentication.

func (*Client) RemoveWorktree

func (c *Client) RemoveWorktree(ctx context.Context, path string, force bool) error

RemoveWorktree removes an existing worktree.

func (*Client) SetConfig

func (c *Client) SetConfig(ctx context.Context, key, value string) error

SetConfig sets a git configuration value.

func (*Client) SetRemote

func (c *Client) SetRemote(ctx context.Context, name, url string) error

SetRemote ensures that a remote with the given name points to the provided URL. If the remote already exists, its URL is updated. If it does not exist, the remote is created with the given URL.

type ClientOptions

type ClientOptions struct {
	// UserName is the git user name for commits.
	UserName string

	// UserEmail is the git user email for commits.
	UserEmail string

	// Quiet suppresses output from git commands.
	Quiet bool

	// DryRun logs commands without executing them.
	DryRun bool
}

ClientOptions holds configuration for git operations.

func DefaultClientOptions

func DefaultClientOptions() *ClientOptions

DefaultClientOptions returns the default client options.

type CloneOptions

type CloneOptions struct {
	// Source is the repository URL or path to clone from.
	Source string

	// Dest is the destination directory.
	Dest string

	// Ref is the reference to checkout after clone (optional).
	Ref string

	// Depth specifies shallow clone depth (0 for full history).
	Depth int

	// Local indicates to use --local for local repositories.
	Local bool

	// Submodules indicates whether to initialize submodules.
	Submodules bool

	// Quiet suppresses output.
	Quiet bool
}

CloneOptions specifies options for cloning a repository.

type CloneResult

type CloneResult struct {
	// HEAD is the checked out commit SHA.
	HEAD string

	// Branch is the checked out branch name.
	Branch string

	// IsShallow indicates if the clone is shallow.
	IsShallow bool
}

CloneResult holds the result of a clone operation.

func Clone

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

Clone clones a repository.

type CommitAuthor

type CommitAuthor struct {
	Name  string
	Email string
	When  time.Time
}

CommitAuthor represents the author of a commit.

type CommitOptions

type CommitOptions struct {
	// Message is the commit message.
	Message string

	// Author is the commit author (defaults to config).
	Author *CommitAuthor

	// AllowEmpty allows creating a commit with no changes.
	AllowEmpty bool
}

CommitOptions specifies options for creating a commit.

type PushOptions

type PushOptions struct {
	// Remote is the remote name (default: "origin").
	Remote string

	// Branch is the branch to push.
	Branch string

	// Force enables force push.
	Force bool

	// SetUpstream sets the upstream branch.
	SetUpstream bool
}

PushOptions specifies options for pushing to a remote.

type RepositoryInfo

type RepositoryInfo struct {
	// HEAD is the current commit SHA.
	HEAD string

	// IsShallow indicates if the repository is a shallow clone.
	IsShallow bool

	// Branch is the current branch name (empty if detached HEAD).
	Branch string

	// Clean indicates if the working directory has no changes.
	Clean bool
}

RepositoryInfo holds information about a git repository.

Jump to

Keyboard shortcuts

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