git

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultContextLines = 3

DefaultContextLines is the default number of context lines in unified diffs.

Variables

This section is empty.

Functions

func CurrentBranchName

func CurrentBranchName() string

CurrentBranchName returns the name of the currently checked out branch, or "" if in detached HEAD state.

func CurrentRef

func CurrentRef() (string, error)

CurrentRef returns the current HEAD commit hash.

func DefaultBranch

func DefaultBranch() (string, error)

DefaultBranch detects the default branch of the repository.

func DiscardFile

func DiscardFile(path string, status FileStatus, staged bool) error

DiscardFile discards all changes to a file. For untracked files, removes the file. If staged is true, unstages first, then reverts the working tree.

func DiscardHunk

func DiscardHunk(path string, status FileStatus, h Hunk) error

DiscardHunk discards a hunk by reverting it from the working tree. For staged hunks, it first unstages, then reverts the working tree.

func EnableUntrackedCache added in v0.4.0

func EnableUntrackedCache() error

EnableUntrackedCache sets core.untrackedCache=true in the current repo's local config. Local scope, not --global — we never modify the user's global git config implicitly.

func Format added in v0.1.3

func Format(d *Diff) string

Format reconstructs unified diff output from a parsed Diff.

func FormatGutter added in v0.4.0

func FormatGutter(l Line) string

FormatGutter returns the 6-char line-number gutter shown next to each diff line: 5-char right-aligned number + 1 space, or 6 spaces for non-content lines (hunk headers, blank separators). Removed lines use OldNum; added and context lines use NewNum.

func FormatHunks added in v0.4.0

func FormatHunks(d *Diff) string

FormatHunks renders a Diff in the same shape the TUI shows on screen — file path header, hunk header with source label + function context, then each line prefixed by a 6-char line-number gutter and a +/-/space marker. No ANSI colors. Useful for non-interactive debugging via `revise diff --hunks`.

func FormatLine

func FormatLine(l Line) string

FormatLine returns a formatted string for a line with gutter.

func GitDir added in v0.4.0

func GitDir() (string, error)

GitDir returns the absolute path to the git directory for the current working tree. In a worktree this is `<main-repo>/.git/worktrees/<name>/`, not the worktree's `.git` file. Used by fswatch to monitor index/HEAD changes (per-worktree, not the main repo's).

func HasCommits

func HasCommits() bool

HasCommits checks if the repository has any commits.

func HunkContextText added in v0.4.0

func HunkContextText(header string) string

HunkContextText extracts the trailing context from a unified-diff hunk header (`@@ -x,y +a,b @@ trailing context`). Returns "" if the header has no trailing context.

func HunkHeaderText added in v0.4.0

func HunkHeaderText(h Hunk) string

HunkHeaderText composes the hunk header the way it appears in the TUI: optional `[source]` tag (branch/staged/unstaged) + the function-context trailer from the `@@ -x,y +a,b @@ context` line. Returns "" when neither is available (e.g. file review mode).

func HunkPatch

func HunkPatch(path string, status FileStatus, h Hunk) string

HunkPatch reconstructs a unified diff patch for a single hunk, suitable for piping to `git apply`.

func HunkSourceLabel added in v0.4.0

func HunkSourceLabel(s HunkSource) string

HunkSourceLabel returns the lowercase label for a HunkSource ("branch", "staged", "unstaged"), or "" for the zero value.

func InvalidateUntrackedCache

func InvalidateUntrackedCache()

InvalidateUntrackedCache forces the next UntrackedFiles call to re-read files.

func IsGitRepo

func IsGitRepo() bool

IsGitRepo checks if the current directory is inside a git repository.

func IsOnDefaultBranch

func IsOnDefaultBranch() (bool, error)

IsOnDefaultBranch returns true if the current HEAD is at the merge-base with the default branch AND up to date with the remote tracking branch. Returns false when on a feature branch (merge-base != HEAD) or when on the default branch but the remote has diverged (useful for reviewing incoming/outgoing changes via branch mode).

func MergeBase

func MergeBase(branch string) (string, error)

MergeBase returns the merge base between the current HEAD and the given branch.

func RawDiff

func RawDiff(mergeBase string, contextLines int) (string, error)

RawDiff returns the raw unified diff from the merge-base to HEAD.

func RawDiffBetween

func RawDiffBetween(from, to string, contextLines int) (string, error)

RawDiffBetween returns the raw unified diff between two refs.

func RawDiffBetweenIgnoreWhitespace

func RawDiffBetweenIgnoreWhitespace(from, to string, contextLines int) (string, error)

RawDiffBetweenIgnoreWhitespace returns the raw unified diff between two refs, ignoring whitespace.

func RemoteName

func RemoteName() string

RemoteName returns the name of the tracking remote. It prefers "origin" if present, otherwise returns the first configured remote. Returns an empty string if there are no remotes.

func RepoRoot

func RepoRoot() (string, error)

RepoRoot returns the absolute path to the top-level directory of the git repository.

func StageFile

func StageFile(path string) error

StageFile stages an entire file.

func StageHunk

func StageHunk(path string, status FileStatus, h Hunk) error

StageHunk stages a single hunk by applying the patch to the index. Untracked files don't exist in the index, so git apply --cached fails; fall back to git add for those.

func StagedDiff

func StagedDiff(contextLines int) (string, error)

StagedDiff returns the raw diff of staged changes.

func StagedDiffIgnoreWhitespace

func StagedDiffIgnoreWhitespace(contextLines int) (string, error)

StagedDiffIgnoreWhitespace returns the raw diff of staged changes, ignoring whitespace.

func StatusFingerprint

func StatusFingerprint() (string, error)

StatusFingerprint returns a string that changes whenever the working tree or index changes. It combines `git status --porcelain` (file-level status including untracked) with mtimes of dirty working tree files for content-level sensitivity.

func TestUntrackedCacheSupport added in v0.4.0

func TestUntrackedCacheSupport() error

TestUntrackedCacheSupport runs git's built-in filesystem self-test. Returns nil if the filesystem reliably updates directory mtimes (required for the untracked cache), or an error otherwise. The test has small, transient filesystem side effects and should be treated as a one-shot check.

func UnstageFile

func UnstageFile(path string) error

UnstageFile unstages an entire file (keeps working tree changes).

func UnstageHunk

func UnstageHunk(path string, status FileStatus, h Hunk) error

UnstageHunk unstages a single hunk by reverse-applying the patch from the index.

func UnstagedDiff

func UnstagedDiff(contextLines int) (string, error)

UnstagedDiff returns the raw diff of unstaged changes.

func UnstagedDiffIgnoreWhitespace

func UnstagedDiffIgnoreWhitespace(contextLines int) (string, error)

UnstagedDiffIgnoreWhitespace returns the raw diff of unstaged changes, ignoring whitespace.

func UntrackedCacheEnabled added in v0.4.0

func UntrackedCacheEnabled() bool

UntrackedCacheEnabled reports whether git's core.untrackedCache is enabled for the current repo. A missing config key is treated as not enabled. The untracked cache speeds up `git status` by caching directory mtimes — enabling it makes revise's polling loop noticeably faster on large repos.

Types

type Diff

type Diff struct {
	Files []FileDiff
}

Diff represents the complete diff output.

func BranchDiff

func BranchDiff(contextLines int) (*Diff, error)

BranchDiff returns the merge-base diff merged with all working tree changes. This is the broadest view — committed + staged + unstaged + untracked. On the default branch behind the remote, it shows the remote's changes. The working tree diff runs concurrently with the branch diff computation.

func BranchDiffOptions

func BranchDiffOptions(contextLines int, hideWhitespace bool) (*Diff, error)

BranchDiffOptions returns BranchDiff with optional whitespace ignoring. The working tree diff runs concurrently with the branch diff computation.

func GetDiff

func GetDiff() (*Diff, error)

GetDiff returns a parsed Diff for the current branch vs the default branch. If on the default branch, it shows working tree changes (staged + unstaged). Otherwise it shows committed changes vs merge-base plus working tree changes. In a repo with no commits, IsOnDefaultBranch returns true and the working tree diff still surfaces staged + untracked files via the empty-tree implicit base used by `git diff --cached`.

func Parse

func Parse(raw string) *Diff

Parse parses a unified diff string into a Diff struct.

func StagedOnlyDiff

func StagedOnlyDiff(contextLines int) (*Diff, error)

StagedOnlyDiff returns only staged changes.

func StagedOnlyDiffOptions

func StagedOnlyDiffOptions(contextLines int, hideWhitespace bool) (*Diff, error)

StagedOnlyDiffOptions returns only staged changes, optionally ignoring whitespace.

func UnstagedOnlyDiff

func UnstagedOnlyDiff(contextLines int) (*Diff, error)

UnstagedOnlyDiff returns unstaged changes + untracked files.

func UnstagedOnlyDiffOptions

func UnstagedOnlyDiffOptions(contextLines int, hideWhitespace bool) (*Diff, error)

UnstagedOnlyDiffOptions returns unstaged changes + untracked files, optionally ignoring whitespace.

func WorkingTreeDiff

func WorkingTreeDiff(contextLines int) (*Diff, error)

WorkingTreeDiff returns staged + unstaged + untracked changes.

func WorkingTreeDiffOptions

func WorkingTreeDiffOptions(contextLines int, hideWhitespace bool) (*Diff, error)

WorkingTreeDiffOptions returns staged + unstaged + untracked changes, optionally ignoring whitespace.

type FileDiff

type FileDiff struct {
	Path     string
	OldPath  string // for renames
	Status   FileStatus
	IsBinary bool
	Hunks    []Hunk
}

FileDiff represents the diff for a single file.

func UntrackedFiles

func UntrackedFiles() ([]FileDiff, error)

UntrackedFiles returns synthetic FileDiff entries for untracked files. Results are cached to avoid re-reading file contents when only context lines change (the bottleneck identified in issue #35).

type FileStatus

type FileStatus string

FileStatus represents the type of change.

const (
	StatusModified  FileStatus = "M"
	StatusAdded     FileStatus = "A"
	StatusDeleted   FileStatus = "D"
	StatusRenamed   FileStatus = "R"
	StatusUntracked FileStatus = "?"
)

type Hunk

type Hunk struct {
	OldStart int
	OldCount int
	NewStart int
	NewCount int
	Header   string // the @@ line
	Source   HunkSource
	Lines    []Line
}

Hunk represents a single diff hunk.

type HunkSource

type HunkSource string

HunkSource identifies where a hunk came from.

const (
	SourceBranch   HunkSource = "Branch"
	SourceStaged   HunkSource = "Staged"
	SourceUnstaged HunkSource = "Unstaged"
)

type Line

type Line struct {
	Type    LineType
	Content string
	OldNum  int // 0 if not applicable
	NewNum  int // 0 if not applicable
}

Line represents a single line in a diff.

type LineType

type LineType int

LineType represents the type of a diff line.

const (
	LineContext LineType = iota
	LineAdded
	LineRemoved
)

Jump to

Keyboard shortcuts

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