git

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BatchDeleteRemoteBranches

func BatchDeleteRemoteBranches(ctx context.Context, repoPath string, names []string) error

BatchDeleteRemoteBranches deletes all named remote branches in a single "git push origin --delete" call, which is much faster than one call per branch. Returns an error if the push fails; on failure the caller should fall back to individual deletions to identify which specific branches could not be removed.

func CloneForAnalysis

func CloneForAnalysis(ctx context.Context, url, destDir string) error

CloneForAnalysis does a treeless bare clone of url into destDir. --filter=tree:0 downloads only commit objects (no trees, no blobs) which is sufficient for all ancestry queries this tool performs (branch --contains, merge-base --is-ancestor, log --format). Much faster than blobless on large repos.

func CurrentBranch

func CurrentBranch(ctx context.Context, repoPath string) string

CurrentBranch returns the name of the currently checked-out branch.

func DeleteLocalBranch

func DeleteLocalBranch(ctx context.Context, repoPath string, b Branch) error

DeleteLocalBranch deletes a local branch using -d (safe delete). Git's own merge check provides a second safety net on top of VerifyMerged.

func EnrichAuthors

func EnrichAuthors(ctx context.Context, repoPath string, branches []Branch)

EnrichAuthors populates the Author field for each branch using the first commit that is unique to that branch (via --ancestry-path from the merge-base with MergedInto). Runs in parallel using the same worker pool pattern as MergedBranchesAnywhere.

func RemoteURL

func RemoteURL(ctx context.Context, repoPath string) string

RemoteURL returns the fetch URL for the origin remote, or "" if not set.

func ValidateRepo

func ValidateRepo(ctx context.Context, repoPath string) error

ValidateRepo checks that repoPath (or the cwd if empty) is inside a git repository.

func ValidateSortField

func ValidateSortField(s string) error

ValidateSortField returns an error if s is not a recognised SortField.

func VerifyMerged

func VerifyMerged(ctx context.Context, repoPath string, b Branch, fpc *FirstParentCache) error

VerifyMerged checks that b is still merged (into b.MergedInto, or anywhere if empty) without deleting it. Use this to pre-validate remote branches before a batch push.

Types

type Branch

type Branch struct {
	Name        string
	IsRemote    bool
	LastCommit  time.Time
	AgeDays     int
	RelativeAge string
	MergedInto  string // the branch this was found merged into; used for deletion verification
	SHA         string // full 40-char tip commit hash
	Author      string // author of the first commit unique to this branch (optional, requires --authors)
}

Branch represents a git branch with metadata.

func FilterByAge

func FilterByAge(branches []Branch, olderThanDays int) []Branch

FilterByAge returns only branches older than the given number of days.

func FilterByExcludeMergedInto

func FilterByExcludeMergedInto(branches []Branch, patterns []string) ([]Branch, error)

FilterByExcludeMergedInto removes branches whose MergedInto field matches any of the given regex patterns. Returns an error if any pattern is invalid. Example patterns: "release/26\.0\.0", "release/.*", "hotfix/.*"

func FilterByTierHierarchy

func FilterByTierHierarchy(branches []Branch, tiers [][]string) []Branch

FilterByTierHierarchy enforces a branch protection hierarchy. Each element of tiers is a slice of glob patterns at that tier level (0 = highest).

  • Tier 0 branches are never prunable.
  • Tier N (N>0) branches are prunable only when merged into a branch at tier < N.
  • Branches not in any tier are treated as an implicit lowest tier: prunable only if merged into a branch that IS in an explicit tier (any tier level). This prevents e.g. task/foo merged into task/bar from being prunable until the work reaches a named tier branch like main or release/*.

func FilterProtected

func FilterProtected(branches []Branch, protected []string) []Branch

FilterProtected removes protected branch names from the list. Supports glob patterns (e.g. "release/*") for consistency with --tier.

func MergedBranches

func MergedBranches(ctx context.Context, repoPath, targetBranch string, includeRemote bool, sortBy SortField) ([]Branch, error)

MergedBranches returns branches that have been merged into targetBranch. Each returned branch has MergedInto set to targetBranch.

func MergedBranchesAnywhere

func MergedBranchesAnywhere(ctx context.Context, repoPath string, includeRemote bool, sortBy SortField) ([]Branch, error)

MergedBranchesAnywhere returns branches whose tip commit is reachable from at least one OTHER existing branch, regardless of which branch that is. This correctly handles repos with multiple long-lived branches (hotfix/*, release/*, etc.) where feature branches may be merged back into any of them.

type FirstParentCache

type FirstParentCache = firstParentCache

FirstParentCache is an opaque cache for first-parent ancestry queries. Create one with NewFirstParentCache and pass it to VerifyMerged to amortise expensive rev-list calls across many deletions.

func NewFirstParentCache

func NewFirstParentCache() *FirstParentCache

NewFirstParentCache returns an empty, ready-to-use FirstParentCache.

type SortField

type SortField string

SortField controls how branches are sorted.

const (
	SortByAge  SortField = "age"
	SortByName SortField = "name"
)

Jump to

Keyboard shortcuts

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