gitutil

package
v0.7.4 Latest Latest
Warning

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

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

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckoutBranch

func CheckoutBranch(ctx context.Context, dir, name string) error

CheckoutBranch switches to an existing branch.

func CreateBranch

func CreateBranch(ctx context.Context, dir, name, startPoint string) error

CreateBranch creates a new branch from startPoint and checks it out.

func CreateBranchAt

func CreateBranchAt(ctx context.Context, dir, name, commit string) error

CreateBranchAt creates a local branch pointing at commit without checking it out. This does not touch the working tree or index.

func CurrentBranch

func CurrentBranch(ctx context.Context, wd string) (string, error)

CurrentBranch returns the current branch name for the given working directory.

func DefaultBranch

func DefaultBranch(ctx context.Context, wd, remote string) (string, error)

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

func DefaultRemote

func DefaultRemote(ctx context.Context, wd string) (string, error)

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

func DiscoverRepos

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

DiscoverRepos recursively walks root up to maxDepth levels, returning absolute paths of directories containing a .git subdirectory. Hidden directories (prefix ".") are skipped. Recursion stops once .git is found.

func Fetch

func Fetch(ctx context.Context, dir string) error

Fetch fetches the latest refs from origin.

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 IsReachable

func IsReachable(ctx context.Context, dir, 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 MergeBase

func MergeBase(ctx context.Context, dir, 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 PushRef

func PushRef(ctx context.Context, dir, 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 RemoteOriginURL

func RemoteOriginURL(ctx context.Context, dir string) string

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

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.

func RevParse

func RevParse(ctx context.Context, dir, ref string) (string, error)

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

func RootDir

func RootDir(ctx context.Context, wd string) (string, error)

RootDir returns the git repository root for the given working directory.

func RunGit

func RunGit(ctx context.Context, dir string, args ...string) (string, error)

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

func SquashOnto

func SquashOnto(ctx context.Context, dir, 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 working-tree checkout needed. The push is non-force so it fails with a non-fast-forward error if origin/<targetBranch> has advanced since fetch.

Types

This section is empty.

Jump to

Keyboard shortcuts

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