git

package
v0.52.4 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: BSD-2-Clause Imports: 18 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommitAndRelease

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

CommitAndRelease performs the full git workflow: 1. git add -A 2. Read CHANGELOG.md and rename ## Unreleased to version 3. Bump version (patch or minor) 4. git commit 5. git tag 6. git push + push tag

func CommitCompletedFile

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

CommitCompletedFile stages and commits a completed prompt file. This is called after MoveToCompleted() to ensure the moved file is committed. Does nothing if the file is already staged or committed.

func GetNextVersion

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

GetNextVersion determines the next version based on the bump type.

func MoveFile

func 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 ResolveGitRoot added in v0.52.3

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.

Types

type BitbucketRemoteCoords added in v0.52.3

type BitbucketRemoteCoords struct {
	// Project is the Bitbucket project key (uppercased, e.g. "BRO").
	Project string
	// Repo is the repository slug (lowercased, e.g. "sentinel").
	Repo string
}

BitbucketRemoteCoords holds the project key and repo slug parsed from a Bitbucket Server remote URL.

func ParseBitbucketRemoteFromGit added in v0.52.3

func ParseBitbucketRemoteFromGit(
	ctx context.Context,
	remoteName string,
) (*BitbucketRemoteCoords, error)

ParseBitbucketRemoteFromGit reads the git remote URL for the given remote name and parses it. Uses `git remote get-url <remoteName>` to fetch the URL.

func ParseBitbucketRemoteURL added in v0.52.3

func ParseBitbucketRemoteURL(
	ctx context.Context,
	remoteURL string,
) (*BitbucketRemoteCoords, error)

ParseBitbucketRemoteURL parses a Bitbucket Server git remote URL and returns project key and repo slug.

Supported formats:

The project key is uppercased and the repo slug is lowercased (matching Bitbucket Server conventions).

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)
	MergeToDefault(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.52.3

type BrancherOption func(*brancher)

BrancherOption is a functional option for configuring a brancher.

func WithDefaultBranch added in v0.52.3

func WithDefaultBranch(branch string) BrancherOption

WithDefaultBranch sets a configured default branch on the brancher. When set, DefaultBranch() returns this value directly without calling gh. Passing an empty string is a no-op (gh CLI fallback is used).

type Cloner added in v0.52.3

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.52.3

func NewCloner() Cloner

NewCloner creates a new Cloner.

type CollaboratorFetcher added in v0.52.3

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

CollaboratorFetcher fetches GitHub repository collaborators.

func NewBitbucketCollaboratorFetcher added in v0.52.3

func NewBitbucketCollaboratorFetcher(
	baseURL string,
	token string,
	project string,
	repo string,
	defaultBranch string,
	currentUser string,
) CollaboratorFetcher

NewBitbucketCollaboratorFetcher creates a CollaboratorFetcher backed by the Bitbucket Server default-reviewers plugin. currentUser is excluded from the result to avoid self-review.

func NewCollaboratorFetcher added in v0.52.3

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

NewCollaboratorFetcher creates a CollaboratorFetcher. If useCollaborators is false or allowedReviewers is non-empty, collaborators are not fetched from GitHub.

type CollaboratorLister added in v0.52.3

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

CollaboratorLister lists collaborator logins for a GitHub repository.

func NewGHCollaboratorLister added in v0.52.3

func NewGHCollaboratorLister(ghToken string) CollaboratorLister

NewGHCollaboratorLister creates a CollaboratorLister that uses gh CLI.

type CommandOutputFn added in v0.52.3

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

CommandOutputFn executes a command and returns its output.

type PRCreator added in v0.9.0

type PRCreator interface {
	Create(ctx context.Context, title string, body 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 NewBitbucketPRCreator added in v0.52.3

func NewBitbucketPRCreator(
	baseURL string,
	token string,
	project string,
	repo string,
	defaultBranch string,
	reviewers []string,
) PRCreator

NewBitbucketPRCreator creates a PRCreator backed by the Bitbucket Server REST API.

func NewPRCreator added in v0.9.0

func NewPRCreator(ghToken string) PRCreator

NewPRCreator creates a new PRCreator.

func NewPRCreatorWithCommandOutput added in v0.52.3

func NewPRCreatorWithCommandOutput(ghToken string, fn CommandOutputFn) PRCreator

NewPRCreatorWithCommandOutput creates a new PRCreator with a custom command output function.

type PRMerger added in v0.52.3

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

PRMerger watches a PR until mergeable and merges it.

func NewBitbucketPRMerger added in v0.52.3

func NewBitbucketPRMerger(
	baseURL string,
	token string,
	project string,
	repo string,
) PRMerger

NewBitbucketPRMerger creates a PRMerger backed by the Bitbucket Server REST API.

func NewPRMerger added in v0.52.3

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

NewPRMerger creates a new PRMerger.

type Releaser

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
}

Releaser handles git commit, tag, and push operations.

func NewReleaser

func NewReleaser() Releaser

NewReleaser creates a new Releaser.

type RepoNameFetcher added in v0.52.3

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

RepoNameFetcher fetches the current GitHub repository name with owner.

func NewGHRepoNameFetcher added in v0.52.3

func NewGHRepoNameFetcher(ghToken string) RepoNameFetcher

NewGHRepoNameFetcher creates a RepoNameFetcher that uses gh CLI.

type ReviewFetcher added in v0.52.3

type ReviewFetcher interface {
	// FetchLatestReview returns the latest review from a trusted reviewer.
	// Returns ReviewVerdictNone if no trusted review exists yet.
	FetchLatestReview(
		ctx context.Context,
		prURL string,
		allowedReviewers []string,
	) (*ReviewResult, error)
	// FetchPRState returns the raw PR state string: "OPEN", "MERGED", "CLOSED".
	FetchPRState(ctx context.Context, prURL string) (string, error)
}

ReviewFetcher polls a GitHub PR for reviews from trusted reviewers.

func NewBitbucketReviewFetcher added in v0.52.3

func NewBitbucketReviewFetcher(
	baseURL string,
	token string,
	project string,
	repo string,
) ReviewFetcher

NewBitbucketReviewFetcher creates a ReviewFetcher backed by the Bitbucket Server REST API.

func NewReviewFetcher added in v0.52.3

func NewReviewFetcher(ghToken string) ReviewFetcher

NewReviewFetcher creates a new ReviewFetcher.

type ReviewResult added in v0.52.3

type ReviewResult struct {
	Verdict ReviewVerdict
	Body    string // full review body text
}

ReviewResult holds the latest review from a trusted reviewer.

type ReviewVerdict added in v0.52.3

type ReviewVerdict string

ReviewVerdict represents the outcome of a PR review.

const (
	ReviewVerdictNone             ReviewVerdict = ""
	ReviewVerdictApproved         ReviewVerdict = "approved"
	ReviewVerdictChangesRequested ReviewVerdict = "changes_requested"
)

type SemanticVersionNumber

type SemanticVersionNumber struct {
	Major int
	Minor int
	Patch int
}

SemanticVersionNumber represents a parsed semantic version.

func ParseSemanticVersionNumber

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

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

func (SemanticVersionNumber) BumpPatch

BumpPatch returns a new version with patch incremented.

func (SemanticVersionNumber) Less

Less returns true if v is lower than other.

func (SemanticVersionNumber) String

func (v SemanticVersionNumber) String() string

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

type VersionBump

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.

Jump to

Keyboard shortcuts

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