git

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BranchExists

func BranchExists(dir, branch string) bool

BranchExists checks if a branch exists in the repository.

func Checkout

func Checkout(dir, branch string) error

Checkout checks out a branch.

func CommitExists

func CommitExists(dir, rev string) bool

CommitExists returns whether rev resolves to a commit object.

func CommitsBehind

func CommitsBehind(dir, baseCommit, targetRef string) (int, error)

CommitsBehind returns how many commits targetRef is ahead of baseCommit. Equivalent to: git rev-list --count <baseCommit>..<targetRef>

func CreateWorktree

func CreateWorktree(repoDir, worktreePath, branch string) error

CreateWorktree creates a git worktree.

func CurrentBranch

func CurrentBranch(dir string) (string, error)

CurrentBranch returns the current branch name.

func DiffFile

func DiffFile(dir, baseRef, path string) (string, error)

DiffFile returns the unified diff for a single file path compared to baseRef.

func DiffFileRange

func DiffFileRange(dir, baseRef, branchRef, path string) (string, error)

DiffFileRange returns the unified diff for a single file path for base..branch.

func DiffNameStatus

func DiffNameStatus(dir, baseRef string) (map[string]string, error)

DiffNameStatus returns per-file status codes compared to baseRef. Includes both committed changes on the current branch and uncommitted changes.

Output format: - <status>\t<path> - Renames/copies: R<score>\t<old>\t<new> (we return status "R" for <new>)

func DiffNameStatusRange

func DiffNameStatusRange(dir, baseRef, branchRef string) (map[string]string, error)

DiffNameStatusRange returns per-file status codes for base..branch.

func DiffStat

func DiffStat(dir, baseRef string) (added, removed int, err error)

DiffStat returns lines added and removed compared to baseRef. Includes both committed changes on the current branch and uncommitted changes.

func DiffStatRange added in v0.2.0

func DiffStatRange(dir, baseRef, branchRef string) (added, removed int, err error)

DiffStatRange returns summed added/removed lines for base..branch.

This is committed-history only (does not include uncommitted workspace changes).

func EffectiveTarget

func EffectiveTarget(dir, target string) string

EffectiveTarget returns target or origin/target if origin is ahead.

Note: This is used for *observation/detection* (e.g. noticing merges that happened upstream after a fetch), not for preferring remotes during user-facing operations.

func Fetch

func Fetch(dir string) error

Fetch fetches from origin.

func FilterLineEndingWarnings

func FilterLineEndingWarnings(stderr string) string

FilterLineEndingWarnings removes common git line-ending conversion warnings.

func GetCommitSubjects

func GetCommitSubjects(dir, base string) ([]string, error)

GetCommitSubjects returns commit subjects from base..HEAD.

func HasRemote

func HasRemote(dir string) bool

HasRemote checks if the repository has a remote named "origin".

func IsAncestor added in v0.2.0

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

IsAncestor reports whether ancestor is reachable from descendant.

This wraps `git merge-base --is-ancestor`: - returns (true, nil) if ancestor is an ancestor of descendant - returns (false, nil) if ancestor is NOT an ancestor of descendant - returns (false, err) for other git errors (missing commits, not a repo, etc.)

func IsClean

func IsClean(dir string) (bool, error)

IsClean checks if the working directory is clean.

func IsPushed

func IsPushed(dir string) (bool, error)

IsPushed checks if the current branch is pushed to remote.

func ListRefs

func ListRefs(dir string, patterns ...string) (map[string]string, error)

ListRefs returns a map of full refname -> object SHA for the given ref namespaces/patterns.

Example patterns: - "refs/heads" - "refs/remotes/origin"

It runs a single `git for-each-ref` command and is intended to be fast.

func LocalPush

func LocalPush(dir, targetBranch string) error

LocalPush fast-forwards targetBranch to current HEAD.

If targetBranch is checked out in another worktree (e.g. the user's main worktree), update it using a fast-forward merge so local uncommitted changes are preserved when they don't overlap (git-like behavior).

If targetBranch is not checked out anywhere, update only the ref.

func MergeBase

func MergeBase(dir, ref1, ref2 string) (string, error)

MergeBase returns the merge-base commit between two refs.

func MergeBaseForkPoint added in v0.2.0

func MergeBaseForkPoint(dir, upstream, commit string) (string, error)

MergeBaseForkPoint returns the fork-point merge-base between upstream and commit.

This is useful for finding a stable "PR base" even if the branch tip is already reachable from upstream (e.g., fast-forward merged), as long as upstream's reflog still contains the previous base tip.

func MergeConflictFiles

func MergeConflictFiles(dir, targetRef, headRef string) ([]string, error)

MergeConflictFiles returns the list of files that would conflict when merging headRef into targetRef.

This is a non-mutating check intended for preflight/status displays. It uses `git merge-tree` to simulate the merge and only reports files when git reports a conflict.

func Output

func Output(dir string, args ...string) (string, error)

Output runs a git command and returns its output.

func OverlappingFiles

func OverlappingFiles(dir, baseCommit, workerHead, targetRef string) ([]string, error)

OverlappingFiles returns files changed in both: - baseCommit..workerHead (worker changes) - baseCommit..targetRef (base branch changes)

This is a heuristic for potential merge conflicts.

func RebaseOnto

func RebaseOnto(dir, target string) error

RebaseOnto rebases current branch onto target. Aborts and returns error on conflict. On conflict, returns extracted CONFLICT lines (if any).

func ResetHard

func ResetHard(dir string) error

ResetHard resets the worktree to HEAD, discarding all changes.

func ResolveDiffBase

func ResolveDiffBase(dir string, branchRef, baseBranch string) (string, error)

ResolveDiffBase returns the base commit to diff against.

Always compute merge-base between branchRef (e.g. "HEAD" or the task branch) and the local baseBranch.

func RevListCount added in v0.2.0

func RevListCount(dir, baseCommit, headRef string) (int, error)

RevListCount returns how many commits are reachable from headRef but not baseCommit. Equivalent to: git rev-list --count <baseCommit>..<headRef>

func Run

func Run(dir string, args ...string) error

Run runs a git command in the specified directory.

func RunQuiet

func RunQuiet(dir string, args ...string) error

RunQuiet runs a git command without printing output.

func RunSilent

func RunSilent(dir string, args ...string) error

RunSilent runs a git command, capturing output and only showing it on error.

func RunWithStderrFilter

func RunWithStderrFilter(dir string, stderrFilter func(string) string, args ...string) error

RunWithStderrFilter runs a git command, streaming stdout and filtering stderr (on success only). This is useful for removing known-benign warnings that would otherwise pollute CLI output.

If the command fails, stderr is not filtered.

func SetupBranch

func SetupBranch(dir string, taskBranch string, baseBranch string, baseCommit string) error

SetupBranch sets up the git branch for a task (local-first).

func ShowDiffStat

func ShowDiffStat(dir, commit string) (added, removed int, ok bool, err error)

ShowDiffStat returns lines added/removed for a single commit (like `git show --numstat`). If the commit does not exist, ok is false and err is nil.

func SquashCommits

func SquashCommits(dir, mergeBase, message string) error

SquashCommits soft-resets to mergeBase and commits with message. All changes since mergeBase are staged and committed as a single commit.

func Switch

func Switch(dir, branch, startPoint string) error

Switch creates and switches to a new branch from a start point.

Types

type CommitMeta added in v0.2.0

type CommitMeta struct {
	SHA         string
	Subject     string
	AuthorName  string
	AuthorEmail string
	AuthoredAt  int64 // unix seconds
}

func ListCommitsRange added in v0.2.0

func ListCommitsRange(dir, from, to string) ([]CommitMeta, error)

ListCommitsRange returns commits reachable from to but not from from (from..to), ordered from oldest to newest.

type DiffFileStat

type DiffFileStat struct {
	Path    string
	Added   int
	Removed int
	Binary  bool
	Status  string // from `git diff --name-status` (e.g. A/M/D/R/C/T/U)
}

func DiffNumstat

func DiffNumstat(dir, baseRef string) ([]DiffFileStat, error)

DiffNumstat returns per-file diff stats compared to baseRef. Includes both committed changes on the current branch and uncommitted changes.

func DiffNumstatRange

func DiffNumstatRange(dir, baseRef, branchRef string) ([]DiffFileStat, error)

DiffNumstatRange returns per-file diff stats for base..branch.

type Error added in v0.2.0

type Error struct {
	Dir    string
	Args   []string
	Stderr string
	Cause  error
}

Error is a structured git execution error that avoids leaking raw "exit status N" strings.

func (*Error) Error added in v0.2.0

func (e *Error) Error() string

func (*Error) Unwrap added in v0.2.0

func (e *Error) Unwrap() error

type IntegrationReason

type IntegrationReason string

IntegrationReason describes why a branch is considered integrated into target.

const (
	IntegratedSameCommit       IntegrationReason = "same_commit"        // Branch HEAD == target HEAD
	IntegratedAncestor         IntegrationReason = "ancestor"           // Branch is ancestor of target
	IntegratedNoAddedChanges   IntegrationReason = "no_changes"         // Three-dot diff is empty
	IntegratedTreesMatch       IntegrationReason = "trees_match"        // Same file tree, different history
	IntegratedMergeAddsNothing IntegrationReason = "merge_adds_nothing" // Merge simulation produces same tree
)

func IsIntegrated

func IsIntegrated(dir, branch, target string) IntegrationReason

IsIntegrated checks if branch's changes are integrated into target. Returns the reason if integrated, empty string if not. Checks are ordered by cost (cheapest first).

Jump to

Keyboard shortcuts

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