git

package
v0.48.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: MIT Imports: 9 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 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 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 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 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 ResolveSHA

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

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

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