git

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 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.

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

View Source
const DefaultAuthorEmail = "250454749+holonbot[bot]@users.noreply.github.com"

DefaultAuthorEmail is the default git author email when no other config is available.

View Source
const DefaultAuthorName = "holonbot[bot]"

DefaultAuthorName is the default git author name when no other config is available.

Variables

This section is empty.

Functions

func FormatGitAuthor added in v0.7.0

func FormatGitAuthor(name, email string) string

FormatGitAuthor formats a git author string in the format "Name <email>".

func GetGlobalConfig deprecated added in v0.6.0

func GetGlobalConfig(key string) string

GetGlobalConfig reads a global git configuration value. Returns empty string if the configuration is not set. This function is useful for getting the host's git user.name and user.email to use as fallback when project config doesn't specify them.

Deprecated: Use ResolveConfig() instead which provides proper priority handling. This function is kept for backward compatibility.

func GetHostGitConfig deprecated added in v0.7.0

func GetHostGitConfig(key string) string

GetHostGitConfig reads a git configuration value from the host system. This is a public wrapper around getHostGitConfig for external use. Returns empty string if the configuration is not set.

Deprecated: Use ResolveConfig() instead which provides proper priority handling. This function is kept for backward compatibility.

func GetUserEmail deprecated added in v0.7.0

func GetUserEmail(opts ConfigOptions) string

GetUserEmail returns the resolved git user email. This is a convenience function that resolves the full config and returns just the email.

Deprecated: Use ResolveConfig() instead.

func GetUserInfo deprecated added in v0.7.0

func GetUserInfo() (string, string)

GetUserInfo returns both user name and email from host git config. This is used by various parts of the system that need git identity.

Deprecated: Use ResolveConfig() instead which provides proper priority handling. This function is kept for backward compatibility.

func GetUserName deprecated added in v0.7.0

func GetUserName(opts ConfigOptions) string

GetUserName returns the resolved git user name. This is a convenience function that resolves the full config and returns just the name.

Deprecated: Use ResolveConfig() instead.

func ParseGitAuthor added in v0.7.0

func ParseGitAuthor(author string) (name, email string)

ParseGitAuthor parses a git author string in the format "Name <email>". Returns the name and email separately.

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) DiagnoseWorkingTree added in v0.6.0

func (c *Client) DiagnoseWorkingTree(ctx context.Context) string

DiagnoseWorkingTree returns detailed diagnostic information about the working tree state. It returns a formatted string with file-by-file status information.

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) GetWorkingTreeStatus added in v0.6.0

func (c *Client) GetWorkingTreeStatus(ctx context.Context) ([]FileStatus, error)

GetWorkingTreeStatus returns detailed status information about the working tree. It parses git status --porcelain output to return file-level status.

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 Config added in v0.7.0

type Config struct {
	// AuthorName is the git user.name for commits.
	AuthorName string

	// AuthorEmail is the git user.email for commits.
	AuthorEmail string
}

Config holds resolved git configuration.

func ResolveConfig added in v0.7.0

func ResolveConfig(opts ConfigOptions) Config

ResolveConfig resolves git configuration with the following priority: 1. Explicit overrides (ExplicitAuthorName/Email) - highest priority for user-specified values 2. Host git config (local > global > system) 3. Environment variables (GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL) 4. ProjectConfig (.holon/config.yaml git.author_*) 5. Defaults ("holonbot[bot] <250454749+holonbot[bot]@users.noreply.github.com>")

This function consolidates all git config resolution logic into one place. It reads host git config with proper scope awareness (local > global > system).

The returned Config contains the resolved author name and email that should be used for all git operations (run, publish, runtime).

Usage:

cfg := git.ResolveConfig(git.ConfigOptions{
    ProjectAuthorName: projectConfig.Git.AuthorName,
    ProjectAuthorEmail: projectConfig.Git.AuthorEmail,
})
fmt.Printf("Git author: %s <%s>", cfg.AuthorName, cfg.AuthorEmail)

func ResolveConfigForWorkspace added in v0.7.0

func ResolveConfigForWorkspace(ctx context.Context, workspaceDir string, opts ConfigOptions) (Config, error)

ResolveConfigForWorkspace resolves git configuration for a specific workspace directory. This is useful when you need workspace-specific git config (e.g., for publishers).

Priority: 1. Workspace local git config 2. Host git config (global > system) 3. Environment variables (GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL) 4. ProjectConfig (.holon/config.yaml git.author_*) 5. Defaults ("holonbot[bot] <250454749+holonbot[bot]@users.noreply.github.com>")

func ResolveConfigFromEnv added in v0.7.0

func ResolveConfigFromEnv() Config

ResolveConfigFromEnv resolves git configuration from environment variables only. This is useful for testing and scenarios where only env vars should be considered.

type ConfigOptions added in v0.7.0

type ConfigOptions struct {
	// ExplicitAuthorName is an explicitly set author name that should override all other sources.
	// This has the highest priority and is used for user-specified overrides.
	ExplicitAuthorName string

	// ExplicitAuthorEmail is an explicitly set author email that should override all other sources.
	// This has the highest priority and is used for user-specified overrides.
	ExplicitAuthorEmail string

	// ProjectAuthorName is the project-level git author name from .holon/config.yaml.
	ProjectAuthorName string

	// ProjectAuthorEmail is the project-level git author email from .holon/config.yaml.
	ProjectAuthorEmail string

	// EnvAuthorName is the author name from environment variables (GIT_AUTHOR_NAME).
	EnvAuthorName string

	// EnvAuthorEmail is the author email from environment variables (GIT_AUTHOR_EMAIL).
	EnvAuthorEmail string
}

ConfigOptions holds options for resolving git configuration.

type FileStatus added in v0.6.0

type FileStatus struct {
	// Path is the file path.
	Path string

	// Status is the human-readable status (e.g., "modified", "added", "deleted").
	Status string

	// StatusCode is the raw status code from git status.
	StatusCode string
}

FileStatus represents the status of a single file in the working tree.

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