git

package
v0.16.2 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

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, filters []func(string) bool) (string, error)

GenerateCommitMsg applies a progressive reduction pipeline to fit the diff under the LLM context limit, then calls the LLM to produce a commit message.

metadata should contain git context (branch name, file stats, recent commit messages). diff should be a unified diff of the changes to describe. filters is an ordered list of file predicates applied progressively to reduce the diff size. Pass nil to use defaultDiffFilters.

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) PushRef

func (c *Checkout) PushRef(ctx context.Context, ref, branch string, force bool) error

PushRef pushes a local ref to the origin remote as the given branch. ref can be a remote-tracking ref (e.g. "container/branch"), a branch name, or any valid git ref. When force is true, --force is passed.

func (*Checkout) RemoteOriginURL

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

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

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 Logger

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

Logger receives structured log records.

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