{"modulePath":"github.com/contextvibes/cli","version":"v0.6.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package git\n\nPackage git provides a high-level client for interacting with Git repositories programmatically from Go applications. It abstracts the direct execution of 'git' command-line operations, offering a more Go-idiomatic API.\n\nThe primary entry point for using this package is the GitClient type, which is instantiated via the NewClient function. The client requires a working directory to determine the repository context and can be configured using GitClientConfig.\n\nKey features include:\n\n  - Repository context detection (finding .git and top-level directories).\n  - Execution of common Git commands (status, commit, add, branch, etc.) through structured methods.\n  - Abstraction over command execution, allowing for custom executors (primarily for testing).\n  - Integration with structured logging via the slog package.\n\nUsage Example:\n\n\t// Assume globalLogger is an initialized *slog.Logger\n\tctx := context.Background()\n\tworkDir, _ := os.Getwd()\n\n\tgitCfg := git.GitClientConfig{\n\t\tLogger: globalLogger,\n\t\t// Other configurations can be set here\n\t}\n\n\tclient, err := git.NewClient(ctx, workDir, gitCfg)\n\tif err != nil {\n\t\tlog.Fatalf(\"Failed to create Git client: %v\", err)\n\t}\n\n\tbranch, err := client.GetCurrentBranchName(ctx)\n\tif err != nil {\n\t\tlog.Printf(\"Error getting branch: %v\", err)\n\t} else {\n\t\tlog.Printf(\"Current branch: %s\", branch)\n\t}\n\nError Handling:\n\nMethods on the GitClient typically return an error as their last argument if an operation fails. Errors can originate from underlying git command failures, invalid input, or issues with the repository state. It's important for callers to check these errors.\n\nLogging:\n\nThe GitClient uses an slog.Logger instance provided via GitClientConfig. This allows for consistent, structured logging of its operations, which can be directed to various outputs (e.g., console, files, AI-consumable streams) by configuring the logger's handlers at the application level.\n\nTesting:\n\nThe GitClient is designed with testability in mind. The GitClientConfig.Executor field allows injecting a mock 'executor' interface, enabling unit tests for client methods without relying on an actual 'git' executable or a live repository.\n\n## Types\n\n```go\ntype GitClient struct {\n\t// contains filtered or unexported fields\n}\n```\nGitClient provides methods for interacting with a Git repository.\n\n```go\nfunc NewClient(ctx context.Context, workDir string, config GitClientConfig) (*GitClient, error)\n```\nNewClient creates and initializes a new GitClient. The GitClientConfig's Executor field should be pre-populated, or validateAndSetDefaults will create a default OSCommandExecutor.\n\n```go\nfunc (c *GitClient) AddAll(ctx context.Context) error\n```\nAddAll stages all changes.\n\n```go\nfunc (c *GitClient) Commit(ctx context.Context, message string) error\n```\nCommit commits staged changes with a message.\n\n```go\nfunc (c *GitClient) CreateAndSwitchBranch(\n\tctx context.Context,\n\tnewBranchName string,\n\tbaseBranch string,\n) error\n```\nCreateAndSwitchBranch creates a new branch and switches to it.\n\n```go\nfunc (c *GitClient) GetCurrentBranchName(ctx context.Context) (string, error)\n```\nGetCurrentBranchName returns the name of the current branch.\n\n```go\nfunc (c *GitClient) GetDiffCached(ctx context.Context) (string, string, error)\n```\nGetDiffCached returns the cached diff.\n\n```go\nfunc (c *GitClient) GetDiffUnstaged(ctx context.Context) (string, string, error)\n```\nGetDiffUnstaged returns the unstaged diff.\n\n```go\nfunc (c *GitClient) GetLogAndDiffFromMergeBase(\n\tctx context.Context,\n\tbaseBranchRef string,\n) (log, diff string, err error)\n```\nGetLogAndDiffFromMergeBase finds the common ancestor with a branch and returns the log and diff since that point.\n\n```go\nfunc (c *GitClient) GetRemoteURL(ctx context.Context, remoteName string) (string, error)\n```\nGetRemoteURL retrieves the URL for a given remote name.\n\n```go\nfunc (c *GitClient) GetStatusShort(ctx context.Context) (string, string, error)\n```\nGetStatusShort returns the short status of the repository.\n\n```go\nfunc (c *GitClient) GitDir() string\n```\nGitDir returns the .git directory path.\n\n```go\nfunc (c *GitClient) HasStagedChanges(ctx context.Context) (bool, error)\n```\nHasStagedChanges checks if there are staged changes.\n\n```go\nfunc (c *GitClient) IsBranchAhead(ctx context.Context) (bool, error)\n```\nIsBranchAhead checks if the local branch is ahead of the remote.\n\n```go\nfunc (c *GitClient) IsWorkingDirClean(ctx context.Context) (bool, error)\n```\nIsWorkingDirClean checks if the working directory is clean.\n\n```go\nfunc (c *GitClient) ListTrackedAndCachedFiles(ctx context.Context) (string, string, error)\n```\nListTrackedAndCachedFiles returns a list of tracked and cached files.\n\n```go\nfunc (c *GitClient) ListUntrackedFiles(ctx context.Context) (string, string, error)\n```\nListUntrackedFiles returns a list of untracked files.\n\n```go\nfunc (c *GitClient) LocalBranchExists(ctx context.Context, branchName string) (bool, error)\n```\nLocalBranchExists checks if a local branch exists.\n\n```go\nfunc (c *GitClient) Logger() *slog.Logger\n```\nLogger returns the logger.\n\n```go\nfunc (c *GitClient) MainBranchName() string\n```\nMainBranchName returns the configured main branch name.\n\n```go\nfunc (c *GitClient) Path() string\n```\nPath returns the repository path.\n\n```go\nfunc (c *GitClient) PullRebase(ctx context.Context, branch string) error\n```\nPullRebase pulls changes from the remote and rebases.\n\n```go\nfunc (c *GitClient) Push(ctx context.Context, branch string) error\n```\nPush pushes changes to the remote.\n\n```go\nfunc (c *GitClient) PushAndSetUpstream(ctx context.Context, branchName string) error\n```\nPushAndSetUpstream pushes the branch and sets the upstream.\n\n```go\nfunc (c *GitClient) RemoteName() string\n```\nRemoteName returns the configured remote name.\n\n```go\nfunc (c *GitClient) StashPush(ctx context.Context) error\n```\nStashPush saves the current state of the working directory and the index, but leaves the working directory clean.\n\n```go\nfunc (c *GitClient) SwitchBranch(ctx context.Context, branchName string) error\n```\nSwitchBranch switches to a branch.\n\n```go\ntype GitClientConfig struct {\n\tGitExecutable         string\n\tDefaultRemoteName     string               // Will be set by cmd layer from LoadedAppConfig\n\tDefaultMainBranchName string               // Will be set by cmd layer from LoadedAppConfig\n\tLogger                *slog.Logger         // Logger for GitClient's own operations\n\tExecutor              exec.CommandExecutor // Use the new CommandExecutor interface from internal/exec\n}\n```\nGitClientConfig configures the GitClient.\n\n","path":"github.com/contextvibes/cli/internal/git","name":"git","synopsis":"Package git provides a high-level client for interacting with Git repositories programmatically from Go applications.","isRedistributable":true}
