git

package
v0.0.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BranchExists

func BranchExists(name string) bool

BranchExists returns whether a local branch with the given name exists.

func CheckoutBranch

func CheckoutBranch(name string) error

CheckoutBranch switches to the specified branch.

func Commit

func Commit(message string) (string, error)

Commit creates a commit with the given message and returns the new HEAD SHA.

func CommitInteractive

func CommitInteractive() (string, error)

CommitInteractive launches the user's configured editor for the commit message, equivalent to running `git commit` without `-m`.

func ConflictedFiles

func ConflictedFiles() ([]string, error)

ConflictedFiles returns the list of files that have merge conflicts.

func CreateBranch

func CreateBranch(name, base string) error

CreateBranch creates a new branch from the given base.

func CurrentBranch

func CurrentBranch() (string, error)

CurrentBranch returns the name of the current branch.

func DefaultBranch

func DefaultBranch() (string, error)

DefaultBranch returns the HEAD branch from origin.

func DeleteBranch

func DeleteBranch(name string, force bool) error

DeleteBranch deletes a local branch.

func DeleteRemoteBranch

func DeleteRemoteBranch(remote, branch string) error

DeleteRemoteBranch deletes a branch on the remote.

func DiffStatRange

func DiffStatRange(base, head string) (additions, deletions int, err error)

DiffStatRange returns the total additions and deletions between two refs.

func EnableRerere

func EnableRerere() error

EnableRerere enables git rerere (reuse recorded resolution) and rerere.autoupdate (auto-stage resolved files) for the repository.

func Fetch

func Fetch(remote string) error

Fetch fetches from the given remote.

func GitDir

func GitDir() (string, error)

GitDir returns the path to the .git directory.

func HasStagedChanges

func HasStagedChanges() bool

HasStagedChanges returns true if there are staged changes ready to commit.

func IsAncestor

func IsAncestor(ancestor, descendant string) (bool, error)

IsAncestor returns whether ancestor is an ancestor of descendant. This is useful to check if a fast-forward merge is possible.

func IsRebaseInProgress

func IsRebaseInProgress() bool

IsRebaseInProgress checks whether a rebase is currently in progress.

func IsRerereDeclined

func IsRerereDeclined() (bool, error)

IsRerereDeclined returns whether the user previously declined the rerere prompt.

func IsRerereEnabled

func IsRerereEnabled() (bool, error)

IsRerereEnabled returns whether rerere.enabled is set to "true" in git config.

func MergeBase

func MergeBase(a, b string) (string, error)

MergeBase returns the best common ancestor commit between two refs.

func MergeFF

func MergeFF(target string) error

MergeFF fast-forwards the currently checked-out branch using a merge.

func Push

func Push(remote string, branches []string, force, atomic bool) error

Push pushes branches to a remote with optional force and atomic flags.

func Rebase

func Rebase(base string) error

Rebase rebases the current branch onto the given base. If rerere resolves all conflicts automatically, the rebase continues without user intervention.

func RebaseAbort

func RebaseAbort() error

RebaseAbort aborts an in-progress rebase.

func RebaseContinue

func RebaseContinue() error

RebaseContinue continues an in-progress rebase. It sets GIT_EDITOR=true to prevent git from opening an interactive editor for the commit message, which would cause the command to hang. If rerere resolves subsequent conflicts automatically, the rebase continues without user intervention.

func RebaseOnto

func RebaseOnto(newBase, oldBase, branch string) error

RebaseOnto rebases a branch using the three-argument form:

git rebase --onto <newBase> <oldBase> <branch>

This replays commits after oldBase from branch onto newBase. It is used when a prior branch was squash-merged and the normal rebase cannot detect which commits have already been applied. If rerere resolves all conflicts automatically, the rebase continues without user intervention.

func ResetHard

func ResetHard(ref string) error

ResetHard resets the current branch to the given ref.

func ResolveRemote

func ResolveRemote(branch string) (string, error)

ResolveRemote determines the remote for pushing a branch. Checks git config in priority order, falls back to listing remotes. Returns *ErrMultipleRemotes if multiple remotes exist with no configured default.

func RevParse

func RevParse(ref string) (string, error)

RevParse resolves a ref to its full SHA via git rev-parse.

func RevParseMap

func RevParseMap(refs []string) (map[string]string, error)

RevParseMap resolves multiple refs and returns a ref→SHA map.

func RevParseMulti

func RevParseMulti(refs []string) ([]string, error)

RevParseMulti resolves multiple refs to their full SHAs in a single git rev-parse invocation. Returns SHAs in the same order as the input refs.

func SaveRerereDeclined

func SaveRerereDeclined() error

SaveRerereDeclined records that the user declined the rerere prompt.

func SetOps

func SetOps(o Ops) func()

SetOps replaces the git operations implementation. Returns a restore function.

func SetUpstreamTracking

func SetUpstreamTracking(branch, remote string) error

SetUpstreamTracking sets the upstream tracking branch.

func StageAll

func StageAll() error

StageAll stages all changes including untracked files (git add -A).

func StageTracked

func StageTracked() error

StageTracked stages changes to tracked files only (git add -u).

func UpdateBranchRef

func UpdateBranchRef(branch, sha string) error

UpdateBranchRef moves a branch pointer to a new commit (for branches not currently checked out).

func ValidateRefName

func ValidateRefName(name string) error

ValidateRefName checks whether name is a valid git branch name.

Types

type CommitInfo

type CommitInfo struct {
	SHA     string
	Subject string
	Body    string
	Time    time.Time
}

CommitInfo holds metadata about a single commit.

func Log

func Log(ref string, maxCount int) ([]CommitInfo, error)

Log returns recent commits for the given branch.

func LogRange

func LogRange(base, head string) ([]CommitInfo, error)

LogRange returns commits in the range base..head (commits reachable from head but not from base). This is useful for seeing all commits unique to a branch.

type ConflictMarkerInfo

type ConflictMarkerInfo struct {
	File     string
	Sections []ConflictSection
}

ConflictMarkerInfo holds the location of conflict markers in a file.

func FindConflictMarkers

func FindConflictMarkers(filePath string) (*ConflictMarkerInfo, error)

FindConflictMarkers scans a file for conflict markers and returns their locations.

type ConflictSection

type ConflictSection struct {
	StartLine int // line number of <<<<<<<
	EndLine   int // line number of >>>>>>>
}

ConflictSection represents a single conflict hunk in a file.

type ErrMultipleRemotes

type ErrMultipleRemotes struct {
	Remotes []string
}

ErrMultipleRemotes is returned by ResolveRemote when multiple remotes are configured and none is designated as the push target.

func (*ErrMultipleRemotes) Error

func (e *ErrMultipleRemotes) Error() string

type FileDiffStat

type FileDiffStat struct {
	Path      string
	Additions int
	Deletions int
}

FileDiffStat holds per-file diff statistics.

func DiffStatFiles

func DiffStatFiles(base, head string) ([]FileDiffStat, error)

DiffStatFiles returns per-file additions and deletions between two refs.

type MockOps

type MockOps struct {
	GitDirFn              func() (string, error)
	CurrentBranchFn       func() (string, error)
	BranchExistsFn        func(string) bool
	CheckoutBranchFn      func(string) error
	FetchFn               func(string) error
	DefaultBranchFn       func() (string, error)
	CreateBranchFn        func(string, string) error
	PushFn                func(string, []string, bool, bool) error
	ResolveRemoteFn       func(string) (string, error)
	RebaseFn              func(string) error
	EnableRerereFn        func() error
	IsRerereEnabledFn     func() (bool, error)
	IsRerereDeclinedFn    func() (bool, error)
	SaveRerereDeclinedFn  func() error
	RebaseOntoFn          func(string, string, string) error
	RebaseContinueFn      func() error
	RebaseAbortFn         func() error
	IsRebaseInProgressFn  func() bool
	ConflictedFilesFn     func() ([]string, error)
	FindConflictMarkersFn func(string) (*ConflictMarkerInfo, error)
	IsAncestorFn          func(string, string) (bool, error)
	RevParseFn            func(string) (string, error)
	RevParseMultiFn       func([]string) ([]string, error)
	MergeBaseFn           func(string, string) (string, error)
	LogFn                 func(string, int) ([]CommitInfo, error)
	LogRangeFn            func(string, string) ([]CommitInfo, error)
	DiffStatRangeFn       func(string, string) (int, int, error)
	DiffStatFilesFn       func(string, string) ([]FileDiffStat, error)
	DeleteBranchFn        func(string, bool) error
	DeleteRemoteBranchFn  func(string, string) error
	ResetHardFn           func(string) error
	SetUpstreamTrackingFn func(string, string) error
	MergeFFFn             func(string) error
	UpdateBranchRefFn     func(string, string) error
	StageAllFn            func() error
	StageTrackedFn        func() error
	HasStagedChangesFn    func() bool
	CommitFn              func(string) (string, error)
	CommitInteractiveFn   func() (string, error)
	ValidateRefNameFn     func(string) error
}

MockOps is a test double for git operations. Each field is an optional function that, when set, handles the corresponding Ops method call. When nil, a reasonable default is returned.

func (*MockOps) BranchExists

func (m *MockOps) BranchExists(name string) bool

func (*MockOps) CheckoutBranch

func (m *MockOps) CheckoutBranch(name string) error

func (*MockOps) Commit

func (m *MockOps) Commit(message string) (string, error)

func (*MockOps) CommitInteractive

func (m *MockOps) CommitInteractive() (string, error)

func (*MockOps) ConflictedFiles

func (m *MockOps) ConflictedFiles() ([]string, error)

func (*MockOps) CreateBranch

func (m *MockOps) CreateBranch(name, base string) error

func (*MockOps) CurrentBranch

func (m *MockOps) CurrentBranch() (string, error)

func (*MockOps) DefaultBranch

func (m *MockOps) DefaultBranch() (string, error)

func (*MockOps) DeleteBranch

func (m *MockOps) DeleteBranch(name string, force bool) error

func (*MockOps) DeleteRemoteBranch

func (m *MockOps) DeleteRemoteBranch(remote, branch string) error

func (*MockOps) DiffStatFiles

func (m *MockOps) DiffStatFiles(base, head string) ([]FileDiffStat, error)

func (*MockOps) DiffStatRange

func (m *MockOps) DiffStatRange(base, head string) (int, int, error)

func (*MockOps) EnableRerere

func (m *MockOps) EnableRerere() error

func (*MockOps) Fetch

func (m *MockOps) Fetch(remote string) error

func (*MockOps) FindConflictMarkers

func (m *MockOps) FindConflictMarkers(filePath string) (*ConflictMarkerInfo, error)

func (*MockOps) GitDir

func (m *MockOps) GitDir() (string, error)

func (*MockOps) HasStagedChanges

func (m *MockOps) HasStagedChanges() bool

func (*MockOps) IsAncestor

func (m *MockOps) IsAncestor(ancestor, descendant string) (bool, error)

func (*MockOps) IsRebaseInProgress

func (m *MockOps) IsRebaseInProgress() bool

func (*MockOps) IsRerereDeclined

func (m *MockOps) IsRerereDeclined() (bool, error)

func (*MockOps) IsRerereEnabled

func (m *MockOps) IsRerereEnabled() (bool, error)

func (*MockOps) Log

func (m *MockOps) Log(ref string, maxCount int) ([]CommitInfo, error)

func (*MockOps) LogRange

func (m *MockOps) LogRange(base, head string) ([]CommitInfo, error)

func (*MockOps) MergeBase

func (m *MockOps) MergeBase(a, b string) (string, error)

func (*MockOps) MergeFF

func (m *MockOps) MergeFF(target string) error

func (*MockOps) Push

func (m *MockOps) Push(remote string, branches []string, force, atomic bool) error

func (*MockOps) Rebase

func (m *MockOps) Rebase(base string) error

func (*MockOps) RebaseAbort

func (m *MockOps) RebaseAbort() error

func (*MockOps) RebaseContinue

func (m *MockOps) RebaseContinue() error

func (*MockOps) RebaseOnto

func (m *MockOps) RebaseOnto(newBase, oldBase, branch string) error

func (*MockOps) ResetHard

func (m *MockOps) ResetHard(ref string) error

func (*MockOps) ResolveRemote

func (m *MockOps) ResolveRemote(branch string) (string, error)

func (*MockOps) RevParse

func (m *MockOps) RevParse(ref string) (string, error)

func (*MockOps) RevParseMulti

func (m *MockOps) RevParseMulti(refs []string) ([]string, error)

func (*MockOps) SaveRerereDeclined

func (m *MockOps) SaveRerereDeclined() error

func (*MockOps) SetUpstreamTracking

func (m *MockOps) SetUpstreamTracking(branch, remote string) error

func (*MockOps) StageAll

func (m *MockOps) StageAll() error

func (*MockOps) StageTracked

func (m *MockOps) StageTracked() error

func (*MockOps) UpdateBranchRef

func (m *MockOps) UpdateBranchRef(branch, sha string) error

func (*MockOps) ValidateRefName

func (m *MockOps) ValidateRefName(name string) error

type Ops

type Ops interface {
	GitDir() (string, error)
	CurrentBranch() (string, error)
	BranchExists(name string) bool
	CheckoutBranch(name string) error
	Fetch(remote string) error
	DefaultBranch() (string, error)
	CreateBranch(name, base string) error
	Push(remote string, branches []string, force, atomic bool) error
	ResolveRemote(branch string) (string, error)
	Rebase(base string) error
	EnableRerere() error
	IsRerereEnabled() (bool, error)
	IsRerereDeclined() (bool, error)
	SaveRerereDeclined() error
	RebaseOnto(newBase, oldBase, branch string) error
	RebaseContinue() error
	RebaseAbort() error
	IsRebaseInProgress() bool
	ConflictedFiles() ([]string, error)
	FindConflictMarkers(filePath string) (*ConflictMarkerInfo, error)
	IsAncestor(ancestor, descendant string) (bool, error)
	RevParse(ref string) (string, error)
	RevParseMulti(refs []string) ([]string, error)
	MergeBase(a, b string) (string, error)
	Log(ref string, maxCount int) ([]CommitInfo, error)
	LogRange(base, head string) ([]CommitInfo, error)
	DiffStatRange(base, head string) (additions, deletions int, err error)
	DiffStatFiles(base, head string) ([]FileDiffStat, error)
	DeleteBranch(name string, force bool) error
	DeleteRemoteBranch(remote, branch string) error
	ResetHard(ref string) error
	SetUpstreamTracking(branch, remote string) error
	MergeFF(target string) error
	UpdateBranchRef(branch, sha string) error
	StageAll() error
	StageTracked() error
	HasStagedChanges() bool
	Commit(message string) (string, error)
	CommitInteractive() (string, error)
	ValidateRefName(name string) error
}

Ops defines the interface for git operations used by commands. The package-level functions are the default production implementation. Tests can substitute a mock via SetOps().

func CurrentOps

func CurrentOps() Ops

CurrentOps returns the current Ops implementation.

Jump to

Keyboard shortcuts

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