git

package
v0.51.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const EmptyTreeSHA = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"

EmptyTreeSHA is the SHA of an empty tree in git, used for diffing against the root commit or repos with no commits.

Variables

This section is empty.

Functions

func CheckoutBranch added in v0.32.0

func CheckoutBranch(repoPath, branch string) error

CheckoutBranch switches to the given branch in the repository.

func CreateCommit

func CreateCommit(repoPath, message string) (string, error)

CreateCommit stages all changes and creates a commit with the given message Returns the SHA of the new commit

func EnsureAbsoluteHooksPath added in v0.46.1

func EnsureAbsoluteHooksPath(repoPath string) error

EnsureAbsoluteHooksPath checks whether core.hooksPath is set to a relative value and, if so, resolves it to an absolute path and updates the git config. Relative hooks paths break linked worktrees because git resolves them from the worktree root, not the main repo root.

func FormatExcludeArgs added in v0.49.0

func FormatExcludeArgs(patterns []string) []string

FormatExcludeArgs converts user-provided exclude patterns (filenames or globs) into git pathspec arguments. Plain names without path separators get both **/name (file match) and **/name/** (directory subtree) so they work whether the name is a file or directory. Leading-slash patterns (/vendor) are root-anchored — no **/ prefix. Patterns containing "/" are passed through as-is. FormatExcludeArgs converts user-provided exclude patterns into git pathspec arguments suitable for appending after "--".

func GetBranchName

func GetBranchName(repoPath, sha string) string

GetBranchName returns a human-readable branch reference for a commit. Returns something like "main", "feature/foo", or "main~3" depending on where the commit is relative to branch heads. Returns empty string on error or timeout (2 second limit to avoid blocking UI).

func GetCommitsSince

func GetCommitsSince(repoPath, mergeBase string) ([]string, error)

GetCommitsSince returns all commits from mergeBase to HEAD (exclusive of mergeBase) Returns commits in chronological order (oldest first)

func GetCurrentBranch

func GetCurrentBranch(repoPath string) string

GetCurrentBranch returns the current branch name, or empty string if detached HEAD. Uses symbolic-ref (without --short) and strips refs/heads/ directly, because both rev-parse --abbrev-ref and symbolic-ref --short can return "heads/branch" when the name is ambiguous with another ref (remote tracking branch, tag, etc.).

func GetDefaultBranch

func GetDefaultBranch(repoPath string) (string, error)

GetDefaultBranch detects the default branch (from origin/HEAD, or main/master locally)

func GetDiff

func GetDiff(
	repoPath, sha string, extraExcludes ...string,
) (string, error)

GetDiff returns the full diff for a commit, excluding generated files like lock files. Extra exclude patterns (filenames or globs) are appended to the built-in exclusion list.

func GetDiffLimited added in v0.49.0

func GetDiffLimited(
	repoPath, sha string, maxBytes int, extraExcludes ...string,
) (string, bool, error)

GetDiffLimited returns up to maxBytes of a commit diff and reports whether the output was truncated before the full diff was read.

func GetDirtyDiff

func GetDirtyDiff(
	repoPath string, extraExcludes ...string,
) (string, error)

GetDirtyDiff returns a diff of all uncommitted changes including untracked files. The diff includes both tracked file changes (via git diff HEAD) and untracked files formatted as new-file diff entries. Excludes generated files like lock files. Extra exclude patterns (filenames or globs) are appended to the built-in list.

func GetFilesChanged

func GetFilesChanged(
	repoPath, sha string,
) ([]string, error)

GetFilesChanged returns the list of files changed in a commit

func GetHooksPath

func GetHooksPath(repoPath string) (string, error)

GetHooksPath returns the path to the hooks directory, respecting core.hooksPath. Relative paths are resolved against the main repository root (not the worktree root) so that linked worktrees share the same hooks directory.

func GetMainRepoRoot

func GetMainRepoRoot(path string) (string, error)

GetMainRepoRoot returns the main repository root, resolving through worktrees. For a regular repository or submodule, this returns the same as GetRepoRoot. For a worktree, this returns the main repository's root path.

func GetMergeBase

func GetMergeBase(repoPath, ref1, ref2 string) (string, error)

GetMergeBase returns the merge-base (common ancestor) between two refs

func GetParentCommits

func GetParentCommits(repoPath, sha string, count int) ([]string, error)

GetParentCommits returns the N commits before the given commit (not including it) Returns commits in reverse chronological order (most recent parent first)

func GetPatchID added in v0.33.0

func GetPatchID(repoPath, sha string) string

GetPatchID returns the stable patch-id for a commit. Patch-ids are content-based hashes of the diff, so two commits with the same code change (e.g. before and after a rebase) share the same patch-id. Returns "" for merge commits, empty commits, or on any error.

func GetRangeCommits

func GetRangeCommits(repoPath, rangeRef string) ([]string, error)

GetRangeCommits returns all commits in a range (oldest first)

func GetRangeDiff

func GetRangeDiff(
	repoPath, rangeRef string, extraExcludes ...string,
) (string, error)

GetRangeDiff returns the combined diff for a range, excluding generated files like lock files. Extra exclude patterns (filenames or globs) are appended to the built-in exclusion list.

func GetRangeDiffLimited added in v0.49.0

func GetRangeDiffLimited(
	repoPath, rangeRef string, maxBytes int, extraExcludes ...string,
) (string, bool, error)

GetRangeDiffLimited returns up to maxBytes of a range diff and reports whether the output was truncated before the full diff was read.

func GetRangeFilesChanged added in v0.31.0

func GetRangeFilesChanged(
	repoPath, rangeRef string,
) ([]string, error)

GetRangeFilesChanged returns the list of files changed in a range (e.g. "mergeBase..HEAD").

func GetRangeStart

func GetRangeStart(repoPath, rangeRef string) (string, error)

GetRangeStart returns the start commit (first parent before range) for context lookup

func GetRemoteURL

func GetRemoteURL(repoPath, remoteName string) string

GetRemoteURL returns the URL for a git remote. If remoteName is empty, tries "origin" first, then any other remote. Returns empty string if no remotes exist.

func GetRepoRoot

func GetRepoRoot(path string) (string, error)

GetRepoRoot returns the root directory of the git repository

func GetStat

func GetStat(repoPath, sha string) (string, error)

GetStat returns the stat summary for a commit

func HasUncommittedChanges

func HasUncommittedChanges(repoPath string) (bool, error)

HasUncommittedChanges returns true if there are uncommitted changes (staged, unstaged, or untracked files)

func IsAncestor

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

IsAncestor checks if ancestor is an ancestor of descendant. Returns (true, nil) if ancestor is reachable from descendant via the commit graph. Returns (false, nil) if ancestor is not an ancestor (git exits with status 1). Returns (false, error) for git errors (e.g., bad object, repo issues).

func IsRange

func IsRange(ref string) bool

IsRange returns true if the ref is a range (contains "..")

func IsRebaseInProgress

func IsRebaseInProgress(repoPath string) bool

IsRebaseInProgress returns true if a rebase operation is in progress

func IsUnbornHead added in v0.21.0

func IsUnbornHead(repoPath string) bool

IsUnbornHead returns true if the repository has an unborn HEAD (no commits yet). Returns false if HEAD points to a valid commit, if the path is not a git repo, or if HEAD is corrupt (e.g., ref pointing to a missing object).

func IsWorkingTreeClean

func IsWorkingTreeClean(repoPath string) bool

IsWorkingTreeClean returns true if the working tree has no uncommitted or untracked changes

func LocalBranchName

func LocalBranchName(branch string) string

LocalBranchName strips the "origin/" prefix from a branch name if present. This normalizes branch names for comparison since GetDefaultBranch may return "origin/main" while GetCurrentBranch returns "main".

func LooksLikeSHA added in v0.51.0

func LooksLikeSHA(s string) bool

LooksLikeSHA returns true if s looks like a git commit SHA (7-40 hex chars, case-insensitive). The 7-char minimum matches git's default abbreviation length and safely excludes short hex task labels like "dead" or "cafe".

func ParseRange

func ParseRange(ref string) (start, end string, ok bool)

ParseRange splits a range ref into start and end

func ReadFile

func ReadFile(repoPath, sha, filePath string) ([]byte, error)

ReadFile reads a file at a specific commit

func ResetWorkingTree

func ResetWorkingTree(repoPath string) error

ResetWorkingTree discards all uncommitted changes (staged and unstaged)

func ResolveGitDir added in v0.51.0

func ResolveGitDir(repoPath string) (string, error)

ResolveGitDir returns the absolute path to the .git directory for a repository, correctly handling worktrees and MSYS-style paths on Windows.

func ResolveSHA

func ResolveSHA(repoPath, ref string) (string, error)

ResolveSHA resolves a ref (like HEAD) to a full SHA

func ReviewPathspecArgs added in v0.49.0

func ReviewPathspecArgs(extraExcludes ...string) []string

ReviewPathspecArgs returns the git pathspec arguments roborev uses for review diffs: the repo root plus built-in and configured exclusions.

func ShortRef added in v0.34.0

func ShortRef(ref string) string

ShortRef abbreviates a git ref for display. SHA-like tokens (hex strings longer than 7 chars) are truncated to 7 chars. Range refs like "abc123def..xyz789abc" become "abc123d..xyz789a". Non-hex refs (branch names, task labels) pass through unchanged.

func ShortSHA added in v0.34.0

func ShortSHA(sha string) string

ShortSHA returns the first 7 characters of a SHA hash, or the full string if shorter. Matches git's default abbreviation.

func ValidateWorktreeForRepo added in v0.49.0

func ValidateWorktreeForRepo(worktreePath, repoRoot string) bool

ValidateWorktreeForRepo checks that worktreePath is a git checkout whose main repository root matches repoRoot. Returns true if the worktree is valid for the given repo. Returns false (without error) if the path doesn't exist, isn't a git repo, or belongs to a different repository.

func WorktreePathForBranch added in v0.36.0

func WorktreePathForBranch(repoPath, branch string) (string, bool, error)

WorktreePathForBranch returns the worktree directory where branch is checked out. If the branch is checked out in any worktree (including the main repo), returns that path and true. If the branch is not checked out anywhere, returns repoPath and false. Returns a non-nil error if the git command fails. An empty branch always returns repoPath, true, nil.

Types

type CommitError added in v0.32.0

type CommitError struct {
	Phase      string // "add" or "commit"
	HookFailed bool   // true when a hook caused the failure
	Stderr     string
	Err        error
}

CommitError represents a failure during CreateCommit. Phase distinguishes "add" failures (lockfile, permissions) from "commit" failures (hooks, empty commit, identity issues). HookFailed is set by probing whether a hookless commit would succeed — true means a hook (pre-commit, commit-msg, etc.) caused the failure.

func (*CommitError) Error added in v0.32.0

func (e *CommitError) Error() string

func (*CommitError) Unwrap added in v0.32.0

func (e *CommitError) Unwrap() error

type CommitInfo

type CommitInfo struct {
	SHA       string
	Author    string
	Subject   string
	Body      string // Full commit message body (excluding subject)
	Timestamp time.Time
}

CommitInfo holds metadata about a commit

func GetCommitInfo

func GetCommitInfo(repoPath, sha string) (*CommitInfo, error)

GetCommitInfo retrieves commit metadata

Jump to

Keyboard shortcuts

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