git

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: BlueOak-1.0.0 Imports: 9 Imported by: 0

Documentation

Overview

internal/git/git.go

Index

Constants

This section is empty.

Variables

View Source
var ErrDirtyWorkTree = errors.New("working tree has uncommitted changes")

ErrDirtyWorkTree is returned when the working tree has uncommitted changes.

Functions

func AbbrevSHA added in v0.2.0

func AbbrevSHA(sha string) string

AbbrevSHA safely abbreviates a SHA to 7 characters. Returns the full string if it's shorter than 7 characters.

Types

type Commit

type Commit struct {
	Subject string // First line of the commit message
	Body    string // Everything after the first line (may be empty)
}

Commit represents a git commit with its subject and body.

type Git

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

Git provides git operations for a repository.

func New

func New(repoPath string) *Git

New creates a Git instance for the repository at the given path.

func (*Git) BranchExists

func (g *Git) BranchExists(branch string) bool

BranchExists checks if a branch exists.

func (*Git) Checkout

func (g *Git) Checkout(branch string) error

Checkout switches to the specified branch.

func (*Git) Commit

func (g *Git) Commit(message string) error

Commit creates a commit with the given message.

func (*Git) CommitExists

func (g *Git) CommitExists(sha string) bool

CommitExists checks if a commit SHA exists in the repository.

func (*Git) CreateAndCheckout

func (g *Git) CreateAndCheckout(name string) error

CreateAndCheckout creates a new branch and switches to it.

func (*Git) CreateBranch

func (g *Git) CreateBranch(name string) error

CreateBranch creates a new branch at the current HEAD.

func (*Git) CreateBranchAt added in v0.2.0

func (g *Git) CreateBranchAt(name, sha string) error

CreateBranchAt creates a new branch at a specific SHA. This is equivalent to `git branch <name> <sha>`.

func (*Git) CurrentBranch

func (g *Git) CurrentBranch() (string, error)

CurrentBranch returns the name of the current branch.

func (*Git) DeleteBranch

func (g *Git) DeleteBranch(branch string) error

DeleteBranch deletes a local branch.

func (*Git) FastForward

func (g *Git) FastForward(branch string) error

FastForward fast-forwards a branch to its remote tracking branch.

func (*Git) Fetch

func (g *Git) Fetch() error

Fetch fetches from origin.

func (*Git) GetCommits

func (g *Git) GetCommits(base, head string) ([]Commit, error)

GetCommits returns the commits from base..head (commits in head not in base). Returns commits in reverse chronological order (newest first).

func (*Git) GetGitDir

func (g *Git) GetGitDir() string

GetGitDir returns the .git directory path.

func (*Git) GetMergeBase

func (g *Git) GetMergeBase(a, b string) (string, error)

GetMergeBase returns the merge base of two branches.

func (*Git) GetResolvedGitDir added in v0.5.0

func (g *Git) GetResolvedGitDir() (string, error)

GetResolvedGitDir returns the actual git directory for this repository. In a main worktree this is typically <repo>/.git. In a linked worktree this resolves to the per-worktree dir (e.g. <main-repo>/.git/worktrees/<name>), which is where rebase state lives.

func (*Git) GetTip

func (g *Git) GetTip(branch string) (string, error)

GetTip returns the commit SHA at the tip of a branch.

func (*Git) HasStagedChanges

func (g *Git) HasStagedChanges() (bool, error)

HasStagedChanges returns true if there are staged changes.

func (*Git) HasUnmergedCommits

func (g *Git) HasUnmergedCommits(branch, upstream string) (bool, error)

HasUnmergedCommits returns true if the branch has commits not yet in upstream. Uses git cherry to detect by diff content, which works for cherry-picks where the commit SHAs differ but the content is the same. Note: This does NOT detect squash merges where multiple commits are combined. Use IsContentMerged for squash merge detection.

func (*Git) IsContentMerged

func (g *Git) IsContentMerged(branch, upstream string) (bool, error)

IsContentMerged returns true if the branch has no content differences from upstream. This detects squash merges where the tree content is identical even though the commit history differs. Returns true if `git diff upstream branch` is empty.

func (*Git) IsDirty

func (g *Git) IsDirty() (bool, error)

IsDirty returns true if there are uncommitted changes (staged or unstaged).

func (*Git) IsRebaseInProgress

func (g *Git) IsRebaseInProgress() bool

IsRebaseInProgress checks if a rebase is in progress. Uses GetResolvedGitDir so it works in linked worktrees where .git is a file, not a directory.

func (*Git) ListRemoteBranches added in v0.4.1

func (g *Git) ListRemoteBranches() (map[string]bool, error)

ListRemoteBranches returns the set of branch names that exist on origin, based on locally-cached remote-tracking refs (no network call). This is accurate as long as a fetch or push has been done recently.

func (*Git) ListWorktrees added in v0.5.0

func (g *Git) ListWorktrees() (map[string]string, error)

ListWorktrees parses `git worktree list --porcelain` and returns a map of branch name to worktree path for all linked worktrees (the main worktree is excluded).

func (*Git) NeedsRebase

func (g *Git) NeedsRebase(branch, parent string) (bool, error)

NeedsRebase returns true if branch needs to be rebased onto parent.

func (*Git) Push

func (g *Git) Push(branch string, force bool) error

Push force-pushes a branch to origin with lease.

func (*Git) Rebase

func (g *Git) Rebase(onto string) error

Rebase rebases the current branch onto target.

func (*Git) RebaseAbort

func (g *Git) RebaseAbort() error

RebaseAbort aborts an in-progress rebase.

func (*Git) RebaseContinue

func (g *Git) RebaseContinue() error

RebaseContinue continues an in-progress rebase.

func (*Git) RebaseHere added in v0.5.0

func (g *Git) RebaseHere(onto string) error

RebaseHere rebases the already-checked-out HEAD onto the given branch. This is semantically identical to Rebase but named explicitly to clarify that no checkout is needed -- the caller is operating inside a worktree that already has the target branch checked out.

func (*Git) RebaseOnto

func (g *Git) RebaseOnto(newBase, oldBase, branch string) error

RebaseOnto rebases a branch onto a new base, replaying only commits after oldBase. Checks out the branch first, then runs: git rebase --onto <newBase> <oldBase> Useful when a parent branch was squash-merged and we need to replay only the commits unique to the child branch.

func (*Git) RebaseOntoHere added in v0.5.0

func (g *Git) RebaseOntoHere(newBase, oldBase string) error

RebaseOntoHere runs `git rebase --onto <newBase> <oldBase>` on the already-checked-out HEAD. Needed for fork-point rebases in worktrees where we cannot (and don't need to) checkout first.

func (*Git) RemoteBranchExists

func (g *Git) RemoteBranchExists(branch string) bool

RemoteBranchExists checks if a branch exists on the remote (origin).

func (*Git) SetBranchRef added in v0.2.0

func (g *Git) SetBranchRef(branch, sha string) error

SetBranchRef sets a branch ref to point to a specific SHA. This is equivalent to `git branch -f <branch> <sha>`.

func (*Git) Stash added in v0.2.0

func (g *Git) Stash(message string) (string, error)

Stash creates a stash with the given message and returns the stash commit hash. Returns an empty string if there was nothing to stash. Includes untracked files (-u) to capture all working tree changes. The returned hash is stable and can be used to restore this specific stash even if other stashes are created later.

func (*Git) StashList added in v0.2.0

func (g *Git) StashList() (bool, error)

StashList returns true if there are any stash entries.

func (*Git) StashPop added in v0.2.0

func (g *Git) StashPop(ref string) error

StashPop restores a specific stash entry when a reference is provided. If ref is empty, it pops the most recent stash entry (matching `git stash pop`). When a commit hash is provided (from Stash()), uses apply+drop since pop only accepts stash refs. Returns an error if there are conflicts or no matching stash entry.

Jump to

Keyboard shortcuts

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