git

package
v0.195.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: BSD-2-Clause Imports: 17 Imported by: 0

Documentation

Overview

Package git provides version control operations for commits, tags, branches, and PRs.

Index

Constants

This section is empty.

Variables

View Source
var DefaultCommitBackoff = run.Backoff{
	Delay:   2 * time.Second,
	Factor:  1.0,
	Retries: 3,
}

DefaultCommitBackoff defines the default retry backoff for git commit operations. 3 retries with exponential backoff: ~2s, ~4s, ~8s.

Functions

func CommitAll added in v0.124.0

func CommitAll(ctx context.Context, message string) error

CommitAll stages all changes and commits with the given message. Used during committing recovery to commit work files left from a prior run.

func CommitAndRelease

func CommitAndRelease(ctx context.Context, bump VersionBump) error

CommitAndRelease performs the full git workflow (package-level wrapper for tests and scripts).

func CommitCompletedFile added in v0.2.27

func CommitCompletedFile(ctx context.Context, path string) error

CommitCompletedFile stages and commits a completed prompt file (package-level wrapper).

func CommitWithRetry added in v0.124.0

func CommitWithRetry(
	ctx context.Context,
	backoff run.Backoff,
	fn func(context.Context) error,
) error

CommitWithRetry runs fn with retry logic using the given backoff configuration. Logs WARN on each retry attempt. Pass DefaultCommitBackoff for production use; tests can pass a Backoff with Delay: 0 and small Retries.

func GetNextVersion added in v0.2.0

func GetNextVersion(ctx context.Context, bump VersionBump) (string, error)

GetNextVersion determines the next version (package-level wrapper).

func HasDirtyFiles added in v0.124.0

func HasDirtyFiles(ctx context.Context) (bool, error)

HasDirtyFiles returns true if there are any uncommitted changes in the working tree.

func MoveFile added in v0.2.36

func MoveFile(ctx context.Context, oldPath string, newPath string) error

MoveFile moves a file using git mv (package-level wrapper).

func ResolveGitRoot added in v0.32.0

func ResolveGitRoot(ctx context.Context) (string, error)

ResolveGitRoot returns the absolute path to the root of the current git repository by running `git rev-parse --show-toplevel`. Returns an error if not inside a git repo.

func StderrFromError added in v0.185.0

func StderrFromError(err error) string

StderrFromError extracts and truncates the child stderr captured in a wrapped *exec.ExitError. Exported so sibling packages (e.g. pkg/gitprovider/bitbucket) can use it without duplicating the logic.

func TruncateStderr added in v0.184.0

func TruncateStderr(s string) string

TruncateStderr returns s unchanged if len(s) <= maxStderrBytes. If s exceeds the limit, the first maxStderrBytes bytes are returned followed by the literal string " (truncated)". Exported so sibling git-provider packages (e.g. pkg/gitprovider/bitbucket) can use it without duplicating.

func ValidateBranchName added in v0.54.0

func ValidateBranchName(ctx context.Context, name string) error

ValidateBranchName returns an error if the branch name contains characters that could be used for argument injection in git commands.

func ValidatePRTitle added in v0.54.0

func ValidatePRTitle(ctx context.Context, title string) error

ValidatePRTitle returns an error if the PR title is empty or starts with a dash, which could be interpreted as a flag by the gh CLI.

func ValidatePRURL added in v0.106.8

func ValidatePRURL(ctx context.Context, prURL string) error

ValidatePRURL returns an error if the given PR URL is not a valid GitHub pull request URL.

Types

type Brancher added in v0.9.0

type Brancher interface {
	CreateAndSwitch(ctx context.Context, name string) error
	Push(ctx context.Context, name string) error
	Switch(ctx context.Context, name string) error
	CurrentBranch(ctx context.Context) (string, error)
	Fetch(ctx context.Context) error
	FetchAndVerifyBranch(ctx context.Context, branch string) error
	DefaultBranch(ctx context.Context) (string, error)
	Pull(ctx context.Context) error
	MergeOriginDefault(ctx context.Context) error
	IsClean(ctx context.Context) (bool, error)
	// IsCleanIgnoring returns the dirty paths that are NOT covered by any of
	// the given ignorePrefixes. An empty slice means the tree is effectively
	// clean for the caller's purposes. The prefix comparison is
	// strings.HasPrefix(path, prefix) on the path as reported by
	// git status --porcelain (relative, no leading slash).
	// A non-nil error means the git command itself failed.
	IsCleanIgnoring(ctx context.Context, ignorePrefixes []string) ([]string, error)
	// DiscardUncommittedInPaths restores each path prefix to its HEAD state using
	// git checkout HEAD -- <prefix>. Prefixes not covered by any uncommitted change
	// are silently skipped. An empty prefixes slice is a no-op.
	DiscardUncommittedInPaths(ctx context.Context, prefixes []string) error
	MergeToDefault(ctx context.Context, branch string) error
	CommitsAhead(ctx context.Context, branch string) (int, error)
	// FetchBranch fetches the named branch from origin as a local branch
	// (git fetch origin <branch>:<branch>).
	FetchBranch(ctx context.Context, branch string) error
}

Brancher handles git branch operations.

func NewBrancher added in v0.9.0

func NewBrancher(opts ...BrancherOption) Brancher

NewBrancher creates a new Brancher.

type BrancherOption added in v0.33.0

type BrancherOption func(*brancher)

BrancherOption is a functional option for configuring a brancher.

func WithDefaultBranch added in v0.33.0

func WithDefaultBranch(branch string) BrancherOption

WithDefaultBranch sets a configured default branch on the brancher.

type Cloner added in v0.30.4

type Cloner interface {
	Clone(ctx context.Context, srcDir string, destDir string, branch string) error
	Remove(ctx context.Context, path string) error
}

Cloner handles local git clone operations.

func NewCloner added in v0.30.4

func NewCloner() Cloner

NewCloner creates a new Cloner.

type CollaboratorFetcher added in v0.30.12

type CollaboratorFetcher interface {
	Fetch(ctx context.Context) []string
}

CollaboratorFetcher fetches GitHub repository collaborators.

func NewCollaboratorFetcher added in v0.30.12

func NewCollaboratorFetcher(
	repoNameFetcher RepoNameFetcher,
	collaboratorLister CollaboratorLister,
	useCollaborators bool,
	allowedReviewers []string,
) CollaboratorFetcher

NewCollaboratorFetcher creates a CollaboratorFetcher.

type CollaboratorLister added in v0.30.15

type CollaboratorLister interface {
	List(ctx context.Context, repoName string) ([]string, error)
}

CollaboratorLister lists collaborator logins for a GitHub repository.

func NewGHCollaboratorLister added in v0.30.15

func NewGHCollaboratorLister(ghToken string) CollaboratorLister

NewGHCollaboratorLister creates a CollaboratorLister that uses gh CLI.

type CommandOutputFn added in v0.44.1

type CommandOutputFn func(cmd *exec.Cmd) ([]byte, error)

CommandOutputFn executes a command and returns its output. Kept for backward compatibility; new code should use the runner-based constructors.

type Helpers added in v0.185.0

type Helpers struct {
	// contains filtered or unexported fields
}

Helpers groups git CLI free functions onto a runner-bearing struct so production wiring injects a runner once and tests inject a fake.

func NewHelpers added in v0.185.0

func NewHelpers() *Helpers

NewHelpers wires a Helpers with the default production runner.

func NewHelpersWithRunner added in v0.185.0

func NewHelpersWithRunner(r subproc.Runner) *Helpers

NewHelpersWithRunner is the test seam.

func (*Helpers) CommitAll added in v0.185.0

func (h *Helpers) CommitAll(ctx context.Context, message string) error

CommitAll stages all changes and commits with the given message.

func (*Helpers) CommitAndRelease added in v0.185.0

func (h *Helpers) CommitAndRelease(ctx context.Context, bump VersionBump) error

CommitAndRelease performs the full git workflow.

func (*Helpers) CommitCompletedFile added in v0.185.0

func (h *Helpers) CommitCompletedFile(ctx context.Context, path string) error

CommitCompletedFile stages and commits a completed prompt file.

func (*Helpers) HasDirtyFiles added in v0.185.0

func (h *Helpers) HasDirtyFiles(ctx context.Context) (bool, error)

HasDirtyFiles returns true if there are any uncommitted changes in the working tree.

func (*Helpers) MoveFile added in v0.185.0

func (h *Helpers) MoveFile(ctx context.Context, oldPath string, newPath string) error

MoveFile moves a file using git mv to preserve history. Falls back to os.Rename if git operations fail or not in a git repo.

func (*Helpers) ResolveGitRoot added in v0.185.0

func (h *Helpers) ResolveGitRoot(ctx context.Context) (string, error)

ResolveGitRoot returns the absolute path to the root of the current git repository.

type PRCreator added in v0.9.0

type PRCreator interface {
	// Create creates a pull request on the given branch and returns the PR URL.
	Create(ctx context.Context, title string, body string, branch string) (string, error)
	// FindOpenPR returns the URL of an open PR for the given branch, or "" if none exists.
	FindOpenPR(ctx context.Context, branch string) (string, error)
}

PRCreator handles GitHub pull request creation.

func NewPRCreator added in v0.9.0

func NewPRCreator(ghToken string) PRCreator

NewPRCreator creates a new PRCreator.

func NewPRCreatorWithCommandOutput added in v0.44.1

func NewPRCreatorWithCommandOutput(ghToken string, _ CommandOutputFn) PRCreator

NewPRCreatorWithCommandOutput creates a PRCreator. The CommandOutputFn is accepted for backward compatibility but the spawn goes through the runner.

func NewPRCreatorWithRunner added in v0.185.0

func NewPRCreatorWithRunner(ghToken string, r subproc.Runner) PRCreator

NewPRCreatorWithRunner creates a PRCreator with an injected runner (for tests).

type PRMerger added in v0.17.6

type PRMerger interface {
	WaitAndMerge(ctx context.Context, prURL string) error
}

PRMerger watches a PR until mergeable and merges it.

func NewPRMerger added in v0.17.6

func NewPRMerger(ghToken string, currentDateTimeGetter libtime.CurrentDateTimeGetter) PRMerger

NewPRMerger creates a new PRMerger.

type Releaser added in v0.2.26

type Releaser interface {
	GetNextVersion(ctx context.Context, bump VersionBump) (string, error)
	CommitAndRelease(ctx context.Context, bump VersionBump) error
	CommitCompletedFile(ctx context.Context, path string) error
	CommitOnly(ctx context.Context, message string) error
	HasChangelog(ctx context.Context) bool
	MoveFile(ctx context.Context, oldPath string, newPath string) error
	PushBranch(ctx context.Context) error
	// DetermineBump inspects CHANGELOG.md in the current directory and returns
	// the appropriate version bump. PatchBump if missing or no `- feat:` entry.
	DetermineBump(ctx context.Context) VersionBump
	// CommitWithRetry runs fn with the default retry backoff and lock-aware
	// logging. Application-layer code uses this seam instead of the package-
	// level git.CommitWithRetry so processor stays mockable.
	CommitWithRetry(ctx context.Context, fn func(context.Context) error) error
}

Releaser handles git commit, tag, and push operations.

func NewReleaser added in v0.2.26

func NewReleaser() Releaser

NewReleaser creates a new Releaser.

type RepoNameFetcher added in v0.30.15

type RepoNameFetcher interface {
	Fetch(ctx context.Context) (string, error)
}

RepoNameFetcher fetches the current GitHub repository name with owner.

func NewGHRepoNameFetcher added in v0.30.15

func NewGHRepoNameFetcher(ghToken string) RepoNameFetcher

NewGHRepoNameFetcher creates a RepoNameFetcher that uses gh CLI.

type SemanticVersionNumber added in v0.2.33

type SemanticVersionNumber struct {
	Major int
	Minor int
	Patch int
}

SemanticVersionNumber represents a parsed semantic version.

func ParseSemanticVersionNumber added in v0.2.33

func ParseSemanticVersionNumber(ctx context.Context, tag string) (SemanticVersionNumber, error)

ParseSemanticVersionNumber parses "vX.Y.Z" into a SemanticVersionNumber. Returns error if format is invalid.

func (SemanticVersionNumber) BumpMinor added in v0.2.33

BumpMinor returns a new version with minor incremented and patch reset to 0.

func (SemanticVersionNumber) BumpPatch added in v0.2.33

BumpPatch returns a new version with patch incremented.

func (SemanticVersionNumber) Less added in v0.2.33

Less returns true if v is lower than other.

func (SemanticVersionNumber) String added in v0.2.33

func (v SemanticVersionNumber) String() string

String returns the "vX.Y.Z" representation.

type VersionBump added in v0.2.30

type VersionBump int

VersionBump specifies the type of version bump to perform.

const (
	// PatchBump increments the patch version (vX.Y.Z -> vX.Y.Z+1)
	PatchBump VersionBump = iota
	// MinorBump increments the minor version (vX.Y.Z -> vX.Y+1.0)
	MinorBump
)

func DetermineBumpFromChangelog added in v0.52.3

func DetermineBumpFromChangelog(ctx context.Context, dir string) VersionBump

DetermineBumpFromChangelog reads CHANGELOG.md from the given directory and returns MinorBump if any ## Unreleased entry starts with "- feat:", PatchBump otherwise. Returns PatchBump when CHANGELOG.md is missing or has no ## Unreleased section.

type Worktreer added in v0.113.0

type Worktreer interface {
	// Add creates a linked worktree at worktreePath on branch.
	Add(ctx context.Context, worktreePath string, branch string) error
	// Remove removes the linked worktree at worktreePath.
	Remove(ctx context.Context, worktreePath string) error
}

Worktreer handles git worktree operations.

func NewWorktreer added in v0.113.0

func NewWorktreer() Worktreer

NewWorktreer creates a new Worktreer.

Jump to

Keyboard shortcuts

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