core

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 34 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package core drives a git repository through lazygit's command layer (github.com/jesseduffield/lazygit/pkg/commands), for a UI to show and act on.

Index

Constants

View Source
const CommitLimit = 300

CommitLimit is how many commits a snapshot loads.

Variables

View Source
var ErrClosed = errors.New("the repository is closed")

ErrClosed is the error of a command of a repository closed.

View Source
var ErrNestedRepository = errors.New("it is a repository of its own, with its history")

ErrNestedRepository is the error of discarding a repository inside the work tree, which DeleteNestedRepository does, with all its history.

View Source
var ErrNotMerged = errors.New("the branch isn't fully merged")

ErrNotMerged is the error of deleting, without forcing, a branch whose commits no other branch has.

View Source
var ErrNotRegular = errors.New("not a regular file of the work tree")

ErrNotRegular is the error of reading or writing, as text, a conflicted path that isn't a regular file of the work tree, as a symlink: a repository's symlink could point anywhere.

View Source
var ErrStale = errors.New("the file changed since its diff was shown: look at it again")

ErrStale is the error of acting on a diff that is no longer the file's.

View Source
var ErrStashKept = errors.New("your local changes are kept in stash@{0}")

ErrStashKept wraps the error of an undo whose local changes, stashed around its reset, didn't apply again: they are in stash@{0}.

View Source
var ErrTooLarge = errors.New("the diff is too large to show here")

ErrTooLarge is the error of a diff larger than maxDiffSize.

Functions

func CommitColumn

func CommitColumn(row []Pipe) int16

CommitColumn is the column of a row's commit.

func GraphRows

func GraphRows(commits []*models.Commit) [][]Pipe

GraphRows lays out the graph of commits, newest first, a row of pipes per commit.

func HandleDaemon

func HandleDaemon() bool

HandleDaemon runs the process as lazygit's daemon when git started it so, as the sequence editor of an interactive rebase, and reports whether it did. main must call it before anything else: lazygit's rebases re-run the app's own binary.

func IsConflict

func IsConflict(err error) bool

IsConflict reports whether err is git stopping on conflicts, as a merge, rebase, pull or cherry-pick does: known from the operation left in progress, or, for a stash applied, from git's words.

Types

type Conflict

type Conflict struct {
	Start, Base, Middle, End int
	// OursLabel and TheirsLabel are what the markers say, as "HEAD" and
	// "feature".
	OursLabel, TheirsLabel string
}

Conflict is a conflict of a file: the indices of its marker lines, as lazygit finds them, Base -1 without the diff3 style.

func (Conflict) BaseLines

func (c Conflict) BaseLines() (int, int)

func (Conflict) Ours

func (c Conflict) Ours() (int, int)

Ours, BaseLines and Theirs are the ranges of the conflict's sides, as [from, to) indices into the file's lines.

func (Conflict) Theirs

func (c Conflict) Theirs() (int, int)

type ConflictedFile

type ConflictedFile struct {
	Lines     []string
	Conflicts []Conflict
}

ConflictedFile is a file with conflict markers, split into lines that keep their line endings.

func ParseConflicts

func ParseConflicts(content string, markerSize int) *ConflictedFile

ParseConflicts finds the conflicts of a file's content, with lazygit's rules: markers of markerSize characters (7 when 0, the conflict-marker-size attribute otherwise), the start, base and end ones with an optional label after a space, the middle one alone.

func (*ConflictedFile) Resolve

func (f *ConflictedFile) Resolve(i int, pick Pick) string

Resolve is the file's content with conflict i resolved by pick, the other conflicts left as they are.

func (*ConflictedFile) ResolveAll

func (f *ConflictedFile) ResolveAll(pick Pick) string

ResolveAll is the file's content with every conflict resolved by pick.

type Credential

type Credential = oscommands.CredentialType

Credential is what a command asks the user for.

type LineAction

type LineAction int

LineAction is what ApplyLines does with the lines chosen of a diff.

const (
	// StageLines puts lines of the unstaged diff into the index.
	StageLines LineAction = iota
	// UnstageLines takes lines of the staged diff out of the index.
	UnstageLines
	// DiscardLines throws lines of the unstaged diff away from the work tree.
	DiscardLines
)

type Options

type Options struct {
	// ConfigDir holds lazygit's config.yml and state.yml for the app; empty
	// keeps lazygit's own lookup (CONFIG_DIR, then lazygit's directory).
	ConfigDir string
	// OnCommand is told of each command about to run, from any goroutine.
	OnCommand func(cmd string, commandLine bool)
	// AskCredential asks for a username, password or passphrase a command
	// waits on; the channel gives the answer. From any goroutine.
	AskCredential func(kind Credential) <-chan string
}

Options are the hooks a UI gives a repository.

type Pick

type Pick int

Pick is the side of a conflict kept.

const (
	PickOurs   Pick = iota // the lines of HEAD, "current"
	PickTheirs             // the lines merged in, "incoming"
	PickBoth               // ours, then theirs
	PickBase               // the common ancestor's, with the diff3 style
)

type Pipe

type Pipe struct {
	From, To int16
	Kind     PipeKind
	// Color is the index of the pipe's color, from the commit it leaves.
	Color uint8
	// contains filtered or unexported fields
}

Pipe is a line of the graph through one row, between columns From (at the row's top, or its commit for Starts) and To (at its bottom, or its commit for Terminates).

func (Pipe) Visible

func (p Pipe) Visible() bool

Visible reports whether a pipe shows: not the one into the first row, nor one from a root commit to the empty tree.

type PipeKind

type PipeKind uint8

PipeKind is how a pipe meets a row.

const (
	// Terminates: the pipe comes from above and ends at the row's commit.
	Terminates PipeKind = iota
	// Starts: the pipe leaves the row's commit for one below.
	Starts
	// Continues: the pipe passes the row, from its top to its bottom.
	Continues
)

type Repo

type Repo struct {

	// Path is the work tree's root.
	Path string
	// contains filtered or unexported fields
}

Repo is an open repository. Its methods run git and block; call them off the UI thread.

Several repositories can be open at once, one per window. lazygit's commands assume one repository per process, though: it changes the process's working directory and git's environment for it, and some of its file operations go by paths relative to the working directory. So the repositories take turns: one command runs at a time, in the whole process, with its repository current (see enter).

func Open

func Open(dir string, opts Options) (r *Repo, err error)

Open opens the repository dir is in. lazygit's constructor changes the working directory and git's environment, so it takes its turn as commands do. It blocks: call it off the UI thread.

func (*Repo) AbortOperation

func (r *Repo) AbortOperation(st models.WorkingTreeState) error

func (*Repo) ApplyLines

func (r *Repo) ApplyLines(f *models.File, diff string, indices []int, action LineAction) error

ApplyLines acts on some of the lines of a file's diff, as lazygit's staging does: diff is the text the lines were chosen in (FileDiff's), and indices are into patch.Parse(diff).Lines(). Lines that are not changes are ignored. Choosing every change acts on the whole file, as staging or unstaging it, so that an added or deleted file is added or deleted rather than emptied.

It acts only on the diff shown: when the file's diff is no longer diff, as after an edit since, it fails with ErrStale rather than act on lines the user hasn't seen.

func (*Repo) ApplyUndo

func (r *Repo) ApplyUndo(s UndoStep, redo bool) error

ApplyUndo undoes (or redoes) a step FindUndo found, marking it in the reflog. A hard reset stashes the work tree's changes around it, as lazygit's does, and gives back only the stash it made; it refuses to overwrite files git doesn't track (untracked or ignored) where the target has files.

func (*Repo) BranchCommits

func (r *Repo) BranchCommits(b *models.Branch, limit int) ([]*models.Commit, [][]Pipe, error)

BranchCommits loads the latest commits of a branch, with their graph.

func (*Repo) Checkout

func (r *Repo) Checkout(name string) error

func (*Repo) CherryPick

func (r *Repo) CherryPick(commits []*models.Commit) error

CherryPick copies commits onto HEAD, given newest first as logs list them; they are applied oldest first.

func (*Repo) Close

func (r *Repo) Close()

Close closes the repository: its commands fail from then on, and its temporary files are removed. It waits for a command running: call it off the UI thread.

func (*Repo) Closing

func (r *Repo) Closing()

Closing marks the repository as about to close, at once: commands that haven't started fail from then on. Close closes it.

func (*Repo) Commit

func (r *Repo) Commit(summary, description string, amend bool) error

Commit commits what is staged. Amending adds it to the last commit, whose message becomes summary and description when summary is given.

func (*Repo) CommitFileDiff

func (r *Repo) CommitFileDiff(c *models.Commit, f *models.CommitFile) (string, error)

CommitFileDiff is the diff of a file a commit changes.

func (*Repo) CommitFiles

func (r *Repo) CommitFiles(c *models.Commit) ([]*models.CommitFile, error)

CommitFiles lists the files a commit changes, against its first parent.

func (*Repo) CommitMessage

func (r *Repo) CommitMessage(c *models.Commit) (string, error)

func (*Repo) ContinueOperation

func (r *Repo) ContinueOperation(st models.WorkingTreeState) error

ContinueOperation continues the merge, rebase, cherry-pick or revert in progress, skipping the editor for a commit's message; SkipOperation skips its current commit, and AbortOperation gives it up.

func (*Repo) DeleteBranch

func (r *Repo) DeleteBranch(name string, force bool) error

DeleteBranch deletes a branch: unless force, only one merged, failing with ErrNotMerged otherwise.

func (*Repo) DeleteNestedRepository

func (r *Repo) DeleteNestedRepository(f *models.File) error

DeleteNestedRepository deletes a repository inside the work tree that isn't a submodule, with all its history.

func (*Repo) Discard

func (r *Repo) Discard(f *models.File) error

func (*Repo) DropCommit

func (r *Repo) DropCommit(commits []*models.Commit, i int) error

func (*Repo) DropCommits added in v0.3.0

func (r *Repo) DropCommits(commits []*models.Commit, lo, hi int) error

DropCommits drops commits lo to hi of a log, newest first, in one interactive rebase. A merge is dropped only by itself: its todo is a merge line, not a pick to drop.

func (*Repo) Fetch

func (r *Repo) Fetch() error

func (*Repo) FileDiff

func (r *Repo) FileDiff(f *models.File, staged bool) (string, error)

FileDiff is the diff of a file of the work tree: its staged changes, or those not staged yet.

func (*Repo) FindUndo

func (r *Repo) FindUndo(redo bool) (UndoStep, error)

FindUndo finds the step undo (or redo) would take, walking the reflog as lazygit does: each undo counts one step back, each redo one forward.

func (*Repo) FixupCommit

func (r *Repo) FixupCommit(commits []*models.Commit, i int) error

FixupCommit squashes a commit into the one below it, keeping that one's message.

func (*Repo) FixupCommits added in v0.3.0

func (r *Repo) FixupCommits(commits []*models.Commit, lo, hi int) error

FixupCommits squashes commits lo to hi of a log, newest first, into the one below them, keeping that one's message, in one interactive rebase.

func (*Repo) Load

func (r *Repo) Load(background bool) (*Snapshot, error)

Load reads the repository's state. background marks a refresh the user didn't ask for, which takes no optional locks.

func (*Repo) MarkResolved

func (r *Repo) MarkResolved(f *models.File) error

MarkResolved stages a file whose conflicts are resolved: one without conflict markers, or a conflict git wrote no markers for.

func (*Repo) Merge

func (r *Repo) Merge(name string) error

func (*Repo) MoveCommit

func (r *Repo) MoveCommit(commits []*models.Commit, i, delta int) error

func (*Repo) MoveCommits added in v0.3.0

func (r *Repo) MoveCommits(commits []*models.Commit, lo, hi, delta int) error

MoveCommits moves commits lo to hi of a log, newest first, one place up (delta -1, newer) or down (delta 1), in one interactive rebase.

func (*Repo) Name

func (r *Repo) Name() string

Name is the work tree's directory name.

func (*Repo) NewBranch

func (r *Repo) NewBranch(name, base string) error

func (*Repo) Pull

func (r *Repo) Pull() error

func (*Repo) Push

func (r *Repo) Push(head *models.Branch, remote string, force bool) error

Push pushes the branch checked out. One with no upstream yet is pushed to remote, which becomes its upstream (see PushRemote).

func (*Repo) PushRemote added in v0.3.0

func (r *Repo) PushRemote(head *models.Branch) (remote string, ask bool, err error)

PushRemote is the remote the first push of head, a branch with no upstream yet, goes to: its branch.<name>.pushRemote, the repository's remote.pushDefault, or its only remote. With several remotes and neither set, ask is true: the user chooses, remote (origin, or the first) being the one to offer. A setting that names no remote fails: git would push to it as a path or a URL, and going to another remote in its place isn't for the app to decide.

func (*Repo) ReadConflicted

func (r *Repo) ReadConflicted(path string) (string, error)

ReadConflicted reads a conflicted file of the work tree, failing with ErrNotRegular for one that isn't a regular file.

func (*Repo) RebaseOnto

func (r *Repo) RebaseOnto(name string) error

func (*Repo) ResolveConflict

func (r *Repo) ResolveConflict(f *models.File, expected, content string) (staged bool, err error)

ResolveConflict writes content, a resolution of the conflicted file f, in place of expected, the content it was resolved from. It fails with ErrStale when the file is no longer expected, as after an edit in another app since, rather than overwrite that. With no conflict left in content, the file is staged, as resolved; staged reports it.

func (*Repo) RewordCommit

func (r *Repo) RewordCommit(commits []*models.Commit, i int, summary, description string) error

func (*Repo) SkipOperation

func (r *Repo) SkipOperation(st models.WorkingTreeState) error

func (*Repo) StashAll

func (r *Repo) StashAll(message string) error

func (*Repo) StashApply

func (r *Repo) StashApply(e *models.StashEntry) error

func (*Repo) StashDrop

func (r *Repo) StashDrop(e *models.StashEntry) error

func (*Repo) StashFileDiff

func (r *Repo) StashFileDiff(e *models.StashEntry, f *models.CommitFile) (string, error)

StashFileDiff is the diff of a file a stash entry changes.

func (*Repo) StashFiles

func (r *Repo) StashFiles(e *models.StashEntry) ([]*models.CommitFile, error)

StashFiles lists the files a stash entry changes.

func (*Repo) StashPop

func (r *Repo) StashPop(e *models.StashEntry) error

StashPop applies a stash entry, then drops it. Applying goes by the entry's commit, which nothing changes; dropping can only go by its index in the stash, which is checked again just before (see stashDo).

func (*Repo) ToggleAllStaged

func (r *Repo) ToggleAllStaged(files []*models.File) error

ToggleAllStaged stages every change, or unstages them all when all are staged already.

func (*Repo) ToggleStaged

func (r *Repo) ToggleStaged(f *models.File) error

func (*Repo) Tr

func (r *Repo) Tr() *i18n.TranslationSet

Tr is lazygit's English strings, as for a working tree state's title.

type Snapshot

type Snapshot struct {
	Files    []*models.File
	Branches []*models.Branch
	Commits  []*models.Commit
	Stash    []*models.StashEntry
	Remotes  []*models.Remote
	// Graph is the commit graph's rows, one per commit.
	Graph [][]Pipe
	// Head is the branch checked out, nil while the head is detached and
	// there are no branches.
	Head *models.Branch
	// State is the operation in progress, as a rebase.
	State models.WorkingTreeState
}

Snapshot is the state of a repository at one time. It is not changed after Load returns it.

type UndoKind

type UndoKind int

UndoKind is what kind of thing a step of the reflog did.

const (
	// UndoCommit is a commit, a reset or a pull: HEAD moved on its branch.
	UndoCommit UndoKind = iota
	// UndoRebase is a whole rebase, from its start to its finish or abort.
	UndoRebase
	// UndoCheckout is a checkout of another branch or commit.
	UndoCheckout
	// UndoMerge is a merge, undone, as a rebase, with a hard reset.
	UndoMerge
)

type UndoStep

type UndoStep struct {
	Kind     UndoKind
	From, To string
}

UndoStep is a step of the reflog that undo or redo would take back or do again: HEAD went From one place To another.

func (UndoStep) Describe

func (s UndoStep) Describe(redo bool) string

Describe says what undoing (or redoing) the step does.

Jump to

Keyboard shortcuts

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