git

package
v0.19.2 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package git provides git utility functions for repository introspection, branch management, and pushing.

Index

Constants

View Source
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

func DiscoverCheckouts(root string, maxDepth int) ([]string, error)

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

func RemoteToHTTPS(raw string) string

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

type Checkout struct {
	Root   string
	Logger Logger
	// contains filtered or unexported fields
}

Checkout provides git operations scoped to the repository at Root, logging via Logger.

func RootDir

func RootDir(ctx context.Context, wd string, logger Logger) (*Checkout, error)

RootDir discovers the git repository root for the given working directory and returns a Git scoped to it.

func (*Checkout) CheckoutBranch

func (c *Checkout) CheckoutBranch(ctx context.Context, name string) error

CheckoutBranch switches to an existing branch.

func (*Checkout) CreateBranch

func (c *Checkout) CreateBranch(ctx context.Context, name, startPoint string, track bool) error

CreateBranch creates a new branch from startPoint without touching the working tree or index.

func (*Checkout) CurrentBranch

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

CurrentBranch returns the current branch name for Root.

func (*Checkout) DefaultBranch

func (c *Checkout) DefaultBranch(ctx context.Context, remote string) (string, error)

DefaultBranch returns the default branch name (e.g. "main" or "master") for the given remote in Root.

func (*Checkout) DefaultRemote

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

DefaultRemote returns the default remote for Root. If there is exactly one remote, it is returned. Otherwise "origin" is used.

func (*Checkout) Fetch

func (c *Checkout) Fetch(ctx context.Context) error

Fetch fetches the latest refs from origin.

func (*Checkout) FindModuleDirs

func (c *Checkout) FindModuleDirs(ctx context.Context) ([]string, error)

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

func (c *Checkout) IsReachable(ctx context.Context, commit string) (bool, error)

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

func (c *Checkout) ListBranches(ctx context.Context, remote string) ([][2]string, error)

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

func (c *Checkout) ListSubmodules(ctx context.Context) ([]Submodule, error)

ListSubmodules returns the submodules declared in .gitmodules for Root. Returns nil if no .gitmodules file exists or it has no entries.

func (*Checkout) MergeBase

func (c *Checkout) MergeBase(ctx context.Context, baseRef string) string

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

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

RefExists reports whether the fully-qualified ref (e.g. "refs/heads/foo") exists in Root.

func (*Checkout) RemoteOriginURL

func (c *Checkout) RemoteOriginURL(ctx context.Context) string

RemoteOriginURL returns the URL of the "origin" remote, or "" if unavailable.

func (*Checkout) Remotes added in v0.17.0

func (c *Checkout) Remotes(ctx context.Context) ([]string, error)

Remotes returns the configured remote names sorted alphabetically.

func (*Checkout) RevParse

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

RevParse resolves a git ref to its full SHA-1 hash.

func (*Checkout) RunGit

func (c *Checkout) RunGit(ctx context.Context, args ...string) (string, error)

RunGit executes a git command in Root and returns captured stdout.

func (*Checkout) SquashOnto

func (c *Checkout) SquashOnto(ctx context.Context, sourceRef, targetBranch, message string) error

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 Logger

type Logger interface {
	Log(ctx context.Context, level slog.Level, msg string, args ...any)
}

Logger receives structured log records.

type ObjectStats added in v0.18.5

type ObjectStats struct {
	LooseCount int64
	LooseBytes int64
	PackBytes  int64
}

ObjectStats describes the loose and packed objects in a Git checkout.

type Submodule

type Submodule struct {
	Name string // submodule name (key in .gitmodules, usually equals Path)
	Path string // relative path in the worktree
}

Submodule represents a git submodule declaration from .gitmodules.

Jump to

Keyboard shortcuts

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