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
- func FormatGitAuthor(name, email string) string
- func GetGlobalConfig(key string) stringdeprecated
- func GetHostGitConfig(key string) stringdeprecated
- func GetUserEmail(opts ConfigOptions) stringdeprecated
- func GetUserInfo() (string, string)deprecated
- func GetUserName(opts ConfigOptions) stringdeprecated
- func ParseGitAuthor(author string) (name, email string)
- func RemoteGetConfig(ctx context.Context, key string) (string, error)
- type ApplyOptions
- type Client
- func (c *Client) Add(ctx context.Context, args ...string) error
- func (c *Client) AddAll(ctx context.Context) error
- func (c *Client) AddWorktree(ctx context.Context, path, ref string, detach bool) error
- func (c *Client) Apply(ctx context.Context, opts ApplyOptions) error
- func (c *Client) ApplyCheck(ctx context.Context, patchPath string, threeWay bool) error
- func (c *Client) Branch(ctx context.Context, name string, create bool) error
- func (c *Client) Checkout(ctx context.Context, ref string) error
- func (c *Client) Commit(ctx context.Context, message string) (string, error)
- func (c *Client) CommitWith(ctx context.Context, opts CommitOptions) (string, error)
- func (c *Client) ConfigGet(ctx context.Context, key string) (string, error)
- func (c *Client) DiagnoseWorkingTree(ctx context.Context) string
- func (c *Client) ExecCommand(ctx context.Context, args ...string) ([]byte, error)
- func (c *Client) GetHeadSHA(ctx context.Context) (string, error)
- func (c *Client) GetRepositoryInfo(ctx context.Context) (*RepositoryInfo, error)
- func (c *Client) GetWorkingTreeStatus(ctx context.Context) ([]FileStatus, error)
- func (c *Client) HasChanges(ctx context.Context) bool
- func (c *Client) Init(ctx context.Context) error
- func (c *Client) InitRepository(ctx context.Context) error
- func (c *Client) InitSubmodules(ctx context.Context) error
- func (c *Client) IsClean(ctx context.Context) bool
- func (c *Client) IsRepo(ctx context.Context) bool
- func (c *Client) IsShallowClone(ctx context.Context) (bool, error)
- func (c *Client) Push(ctx context.Context, opts PushOptions) error
- func (c *Client) RemoveWorktree(ctx context.Context, path string, force bool) error
- func (c *Client) SetConfig(ctx context.Context, key, value string) error
- func (c *Client) SetRemote(ctx context.Context, name, url string) error
- type ClientOptions
- type CloneOptions
- type CloneResult
- type CommitAuthor
- type CommitOptions
- type Config
- type ConfigOptions
- type FileStatus
- type PushOptions
- type RepositoryInfo
Constants ¶
const DefaultAuthorEmail = "bot@holon.run"
DefaultAuthorEmail is the default git author email when no other config is available.
const DefaultAuthorName = "Holon 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
FormatGitAuthor formats a git author string in the format "Name <email>".
func GetGlobalConfig
deprecated
added in
v0.6.0
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
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
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
ParseGitAuthor parses a git author string in the format "Name <email>". Returns the name and email separately.
func RemoteGetConfig ¶
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 NewClientWithToken ¶
NewClientWithToken creates a new git client with authentication.
func (*Client) AddWorktree ¶
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 ¶
ApplyCheck checks if a patch can be applied without conflicts.
func (*Client) CommitWith ¶
CommitWith creates a commit with options.
func (*Client) DiagnoseWorkingTree ¶ added in v0.6.0
DiagnoseWorkingTree returns detailed diagnostic information about the working tree state. It returns a formatted string with file-by-file status information.
func (*Client) ExecCommand ¶
ExecCommand is a safe wrapper to allow callers to run arbitrary git commands.
func (*Client) GetHeadSHA ¶
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 ¶
HasChanges returns true if there are uncommitted changes.
func (*Client) InitRepository ¶
InitRepository initializes a git repository with default configuration.
func (*Client) InitSubmodules ¶
InitSubmodules initializes git submodules.
func (*Client) IsShallowClone ¶
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 ¶
RemoveWorktree removes an existing worktree.
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 ¶
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 ("Holon Bot <bot@holon.run>")
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 ("Holon Bot <bot@holon.run>")
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.