Documentation
¶
Overview ¶
Package git provides git utility functions for repository introspection, branch management, and pushing.
Index ¶
- Constants
- func DiscoverCheckouts(root string, maxDepth int) ([]string, error)
- func GenerateCommitMsg(ctx context.Context, p genai.Provider, metadata, diff string, ...) (string, error)
- func RemoteToHTTPS(raw string) string
- type Checkout
- func (c *Checkout) CheckoutBranch(ctx context.Context, name string) error
- func (c *Checkout) CreateBranch(ctx context.Context, name, startPoint string, track bool) error
- func (c *Checkout) CurrentBranch(ctx context.Context) (string, error)
- func (c *Checkout) DefaultBranch(ctx context.Context, remote string) (string, error)
- func (c *Checkout) DefaultRemote(ctx context.Context) (string, error)
- func (c *Checkout) Fetch(ctx context.Context) error
- func (c *Checkout) FindModuleDirs(ctx context.Context) ([]string, error)
- func (c *Checkout) IsReachable(ctx context.Context, commit string) (bool, error)
- func (c *Checkout) ListBranches(ctx context.Context, remote string) ([][2]string, error)
- func (c *Checkout) ListSubmodules(ctx context.Context) ([]Submodule, error)
- func (c *Checkout) MergeBase(ctx context.Context, baseRef string) string
- func (c *Checkout) ObjectStats(ctx context.Context) (ObjectStats, error)
- func (c *Checkout) PushRef(ctx context.Context, remote, sourceRef, destinationBranch string, force bool) error
- func (c *Checkout) RefExists(ctx context.Context, ref string) (bool, error)
- func (c *Checkout) RemoteOriginURL(ctx context.Context) string
- func (c *Checkout) Remotes(ctx context.Context) ([]string, error)
- func (c *Checkout) RevParse(ctx context.Context, ref string) (string, error)
- func (c *Checkout) RunGit(ctx context.Context, args ...string) (string, error)
- func (c *Checkout) SquashOnto(ctx context.Context, sourceRef, targetBranch, message string) error
- type CommitMsgOptions
- type Logger
- type ObjectStats
- type Submodule
Constants ¶
const ( // DefaultCommitMessageTokens is the assumed model context window. DefaultCommitMessageTokens = 64_000 // MinCommitMessageTokens is the smallest supported model context window. MinCommitMessageTokens = 8_000 )
Variables ¶
This section is empty.
Functions ¶
func DiscoverCheckouts ¶
DiscoverCheckouts recursively walks root up to maxDepth levels, returning absolute paths of git repositories.
Both regular repos (containing a .git subdirectory) and bare repos (containing HEAD, objects/, and refs/ directly) are detected. Hidden directories (prefix ".") are skipped. Recursion stops once a repo is found, so submodules are not found.
func GenerateCommitMsg ¶
func GenerateCommitMsg(ctx context.Context, p genai.Provider, metadata, diff string, opts *CommitMsgOptions) (string, error)
GenerateCommitMsg generates a commit message from Git metadata and a unified diff.
The input budget is derived from opts.ContextTokens or GIT_DESC_TOKENS and defaults to 64000. Large changes are reduced, split, summarized in parallel, and merged in rounds so every provider request remains within the budget. opts.Filters controls the progressive file-body omissions; nil uses the defaults.
func RemoteToHTTPS ¶
RemoteToHTTPS converts a git remote URL to an HTTPS browse URL. SSH (git@host:owner/repo.git), ssh:// and https:// with .git suffix are normalised. Unrecognised formats are returned as-is.
Types ¶
type Checkout ¶
Checkout provides git operations scoped to the repository at Root, logging via Logger.
func RootDir ¶
RootDir discovers the git repository root for the given working directory and returns a Git scoped to it.
func (*Checkout) CheckoutBranch ¶
CheckoutBranch switches to an existing branch.
func (*Checkout) CreateBranch ¶
CreateBranch creates a new branch from startPoint without touching the working tree or index.
func (*Checkout) CurrentBranch ¶
CurrentBranch returns the current branch name for Root.
func (*Checkout) DefaultBranch ¶
DefaultBranch returns the default branch name (e.g. "main" or "master") for the given remote in Root.
func (*Checkout) DefaultRemote ¶
DefaultRemote returns the default remote for Root. If there is exactly one remote, it is returned. Otherwise "origin" is used.
func (*Checkout) FindModuleDirs ¶
FindModuleDirs returns relative paths (from <Root>/.git/modules/) for every bare module repository found under that directory.
Bare repos are detected by the presence of a HEAD file, an objects/ directory, and a refs/ directory. Nested module repos (submodules of submodules) stored under <module>/modules/ are included recursively. Returns nil if the modules directory does not exist.
func (*Checkout) IsReachable ¶
IsReachable reports whether commit is an ancestor of (or equal to) any ref in refs/heads/ or refs/remotes/origin/. Container remote-tracking refs (refs/remotes/<container>/*) are excluded by construction.
func (*Checkout) ListBranches ¶
ListBranches returns branches sorted alphabetically. It always runs git directly with no caching so the result is always fresh even when branches are created or deleted frequently.
If remote is empty, local branches (refs/heads/) are listed and each entry's first element is the short branch name. If remote is non-empty, remote tracking branches for that remote (refs/remotes/<remote>/) are listed and HEAD is excluded.
func (*Checkout) ListSubmodules ¶
ListSubmodules returns the submodules declared in .gitmodules for Root. Returns nil if no .gitmodules file exists or it has no entries.
func (*Checkout) MergeBase ¶
MergeBase returns the merge-base between HEAD and baseRef, falling back to baseRef itself if merge-base fails (e.g. unrelated histories).
func (*Checkout) ObjectStats ¶ added in v0.18.5
func (c *Checkout) ObjectStats(ctx context.Context) (ObjectStats, error)
ObjectStats reads loose-object count and object-store sizes from Git. Git reports size and size-pack in KiB; the returned sizes are bytes.
func (*Checkout) PushRef ¶
func (c *Checkout) PushRef(ctx context.Context, remote, sourceRef, destinationBranch string, force bool) error
PushRef pushes sourceRef to destinationBranch on the named remote.
sourceRef can be HEAD, a commit ID, or a local or remote-tracking ref. destinationBranch is a branch name, without the refs/heads/ prefix. When force is true, --force is passed.
func (*Checkout) RefExists ¶ added in v0.17.0
RefExists reports whether the fully-qualified ref (e.g. "refs/heads/foo") exists in Root.
func (*Checkout) RemoteOriginURL ¶
RemoteOriginURL returns the URL of the "origin" remote, or "" if unavailable.
func (*Checkout) Remotes ¶ added in v0.17.0
Remotes returns the configured remote names sorted alphabetically.
func (*Checkout) SquashOnto ¶
SquashOnto creates a single squash commit of sourceRef's tree on top of origin/<targetBranch> and pushes it.
Uses plumbing commands only, no active branch checkout needed. The push is non-force so it fails with a non-fast-forward error if origin/<targetBranch> has advanced since fetch.
type CommitMsgOptions ¶ added in v0.18.1
type CommitMsgOptions struct {
// BriefMetadata replaces metadata in requests that summarize one diff part.
// When empty, GenerateCommitMsg removes the recent-commit section from metadata.
BriefMetadata string
// ContextTokens is the model context window. Zero reads GIT_DESC_TOKENS and
// then defaults to [DefaultCommitMessageTokens].
ContextTokens int
// Filters are applied progressively to omit low-value file bodies. Nil uses
// the default test and structured-data filters.
Filters []func(string) bool
// Progress receives notices when a diff is reduced, split, or merged.
Progress io.Writer
}
CommitMsgOptions configures GenerateCommitMsg.
type ObjectStats ¶ added in v0.18.5
ObjectStats describes the loose and packed objects in a Git checkout.