Documentation
¶
Overview ¶
Package git wraps the git CLI for gitree.
Index ¶
- Constants
- Variables
- func GoneError(upstream string) error
- func SanitizeLine(s string) string
- type ActionError
- type AutostashConflictError
- type AutostashKeptError
- type CheckoutKind
- type CheckoutRestoredError
- type CheckoutResult
- type CheckoutTarget
- type CmdError
- type Commit
- type CommitMeta
- type DiffTarget
- type FileChange
- type GoneUpstreamError
- type HookFailedError
- type PushKind
- type PushPlan
- type Ref
- type RefInfo
- type RefKind
- type Repo
- func (r *Repo) BranchExists(name string) bool
- func (r *Repo) Checkout(t CheckoutTarget) error
- func (r *Repo) CheckoutBranch(name string) error
- func (r *Repo) CheckoutDetached(hash string) error
- func (r *Repo) CheckoutRemote(remoteBranch string) error
- func (r *Repo) CheckoutWithStash(t CheckoutTarget) (CheckoutResult, error)
- func (r *Repo) CherryPick(hash string) error
- func (r *Repo) CommitFiles(hash string) ([]FileChange, error)
- func (r *Repo) CommitMeta(hash string) (CommitMeta, error)
- func (r *Repo) CreateBranch(name, at string) error
- func (r *Repo) CurrentBranch() (string, error)
- func (r *Repo) CurrentBranchRaw() (string, error)
- func (r *Repo) Diff(t DiffTarget, path string, width int) (string, error)
- func (r *Repo) Fetch(ctx context.Context) error
- func (r *Repo) Fingerprint() (string, error)
- func (r *Repo) GitreeStashes() ([]StashEntry, error)
- func (r *Repo) HasTrackedChanges() (bool, error)
- func (r *Repo) HeadHash() (string, error)
- func (r *Repo) InProgress() (bool, error)
- func (r *Repo) ListRefs() ([]RefInfo, error)
- func (r *Repo) LocalBranchFor(remoteBranch string) (name string, exists bool, err error)
- func (r *Repo) Log(scope Scope, skip, limit int) ([]Commit, error)
- func (r *Repo) Merge(branch string) error
- func (r *Repo) Message(hash string) (string, error)
- func (r *Repo) Pull(ctx context.Context) (int, error)
- func (r *Repo) PullAutostash(ctx context.Context) (int, error)
- func (r *Repo) Push(ctx context.Context, want PushPlan) error
- func (r *Repo) PushPlan() (PushPlan, error)
- func (r *Repo) Root() string
- func (r *Repo) ShowHeader(hash string) (string, error)
- func (r *Repo) TrackedChanges() ([]FileChange, error)
- func (r *Repo) Tracking() (map[string]Track, error)
- func (r *Repo) ValidBranchName(name string) bool
- func (r *Repo) WorkingFiles() ([]FileChange, error)
- type Scope
- type StashConflictError
- type StashEntry
- type StashKeptError
- type Track
Constants ¶
const NetworkTimeout = 60 * time.Second
NetworkTimeout bounds one pull or push (and its ssh / remote helpers).
Variables ¶
var ( // ErrDetached is returned when pull or push is asked for without a branch. ErrDetached = errors.New("HEAD is detached: check out a branch first") // ErrDiverged means the branch and its upstream both have commits: a // fast-forward pull is impossible and nothing was changed. ErrDiverged = errors.New("branches diverged: pull with lazygit (L)") // ErrRejected is a push the remote refused because it has new commits. ErrRejected = errors.New("push rejected: remote has new commits — pull first (p)") // ErrNothingToPush is returned, without running git, when the branch has // no commits its upstream lacks. ErrNothingToPush = errors.New("nothing to push") // ErrNoUpstream is returned by Pull for a branch that tracks nothing. ErrNoUpstream = errors.New("no upstream: push first with P") // ErrBranchChanged is returned by Push when the branch or its push // destination is no longer the one that was confirmed. ErrBranchChanged = errors.New("branch changed — try again") )
var DeltaArgs = []string{
"--dark", "--paging=never", "--line-numbers", "--syntax-theme=gruvbox-dark",
`--plus-style=syntax "#34381b"`, `--plus-emph-style=syntax "#4b5a26"`,
`--minus-style=syntax "#402120"`, `--minus-emph-style=syntax "#6a2e2b"`,
"--line-numbers-plus-style=#a9b665", "--line-numbers-minus-style=#ea6962",
"--line-numbers-zero-style=#7c6f64",
"--hunk-header-style=file line-number syntax", "--hunk-header-decoration-style=#665c54 box",
"--file-style=#d8a657 bold", "--file-decoration-style=#665c54 ul",
}
DeltaArgs are the delta flags (from the lazygit config), one argv element each.
var ErrControlName = errors.New("branch name contains control characters: use git directly")
ErrControlName is returned when an action would involve a branch whose real name contains control characters. Names are sanitized for display, so the name shown could be another branch's; gitree refuses instead of guessing.
var ErrDirty = errors.New("there are uncommitted changes in tracked files")
ErrDirty is returned, without running git, when tracked files have uncommitted changes. gitree never stashes automatically.
var ErrInProgress = errors.New("a merge, cherry-pick, revert or rebase is in progress")
ErrInProgress is returned, without running git, when a merge, cherry-pick, revert or rebase is in progress: it has to be finished (in lazygit) first.
var ErrNotRepo = errors.New("not a git repository")
ErrNotRepo is returned by Open when the path is not inside a git repository.
var LookPath = exec.LookPath
LookPath locates executables; overridable in tests.
Functions ¶
func SanitizeLine ¶
SanitizeLine removes control characters and terminal escape sequences from text that came from the repository (subjects, names, ref names), so it can never reach the terminal raw. Tabs become spaces; everything else that is a control character (C0, DEL, C1) is dropped, along with the rest of any ESC / CSI / OSC sequence it starts.
Types ¶
type ActionError ¶
type ActionError struct {
Op string
Output string
Conflict bool
InProgress bool
Blocked []string
BlockedUntracked bool
}
ActionError is a failed repo-mutating git command. When Conflict is true the repo is deliberately left in the conflicted state for the user to resolve. InProgress means the failed merge/cherry-pick left an operation in progress (always with Conflict; alone e.g. for an empty cherry-pick). It is never aborted automatically.
Blocked lists the files git refused to overwrite (nothing was changed): untracked files when BlockedUntracked, otherwise local changes.
func (*ActionError) Error ¶
func (e *ActionError) Error() string
type AutostashConflictError ¶
type AutostashConflictError struct{ Commits int }
AutostashConflictError is returned by PullAutostash when the pull (Commits commits) was applied but the stashed local changes conflicted when git restored them: git keeps them in the stash and leaves the conflicts in the working tree for the user to resolve.
func (*AutostashConflictError) Error ¶
func (e *AutostashConflictError) Error() string
type AutostashKeptError ¶
type AutostashKeptError struct{ Cause error }
AutostashKeptError is returned by PullAutostash when the pull did not finish (Cause: timed out, killed, …) after git had stashed the local changes: gitree moved them into the stash (`git merge --quit`), where they wait to be restored.
func (*AutostashKeptError) Error ¶
func (e *AutostashKeptError) Error() string
func (*AutostashKeptError) Unwrap ¶
func (e *AutostashKeptError) Unwrap() error
type CheckoutKind ¶
type CheckoutKind int
CheckoutKind is what a CheckoutTarget names.
const ( // CheckoutLocal switches to the local branch Name. CheckoutLocal CheckoutKind = iota // CheckoutTrack creates a local branch tracking the remote branch Name // ("origin/x") and switches to it. CheckoutTrack // CheckoutDetached checks out commit Name (a hash) with a detached HEAD. CheckoutDetached )
type CheckoutRestoredError ¶
CheckoutRestoredError is a checkout that failed after gitree stashed the local changes: HEAD did not move and they were restored on the original branch. Err is git's refusal (often an *ActionError). Kept means the stash entry could not be dropped safely and is still in the stash.
func (*CheckoutRestoredError) Error ¶
func (e *CheckoutRestoredError) Error() string
func (*CheckoutRestoredError) Unwrap ¶
func (e *CheckoutRestoredError) Unwrap() error
type CheckoutResult ¶
CheckoutResult is a successful CheckoutWithStash: Unstaged when the staged state could not be restored (the changes came back unstaged), Hook the failed post-checkout hook's first error line, Kept when the stash entry was restored but left in the stash (the stash changed meanwhile).
type CheckoutTarget ¶
type CheckoutTarget struct {
Kind CheckoutKind
Name string
}
CheckoutTarget is what a checkout switches to.
type CmdError ¶
type CmdError struct {
Args []string
Stdout string // what git printed before failing (e.g. "CONFLICT (content): …")
Stderr string
ExitCode int
}
CmdError describes a failed git invocation.
type Commit ¶
type Commit struct {
Hash string
Parents []string
Refs []Ref
AuthorName string
AuthorEmail string
Time time.Time
Subject string
}
Commit is one entry of the log.
type CommitMeta ¶
type CommitMeta struct {
Hash string
AuthorName, AuthorEmail string
AuthorDate time.Time
CommitterName, CommitterEmail string
Subject string
Body string // the full raw message (unsanitized: never print it as is)
CoAuthors []string // names from Co-authored-by trailers
}
CommitMeta is the metadata the preview header shows for a commit.
func (CommitMeta) CommitterDiffers ¶
func (c CommitMeta) CommitterDiffers() bool
CommitterDiffers reports whether someone other than the author committed.
type DiffTarget ¶
type DiffTarget struct{ Hash, OldPath string }
DiffTarget selects what to diff. Hash == "" means working tree vs HEAD. OldPath is the pre-rename path of the file being diffed (optional); with it both paths are passed so git can pair them and show a rename, not an add.
type FileChange ¶
type FileChange struct {
Status byte
Path string
Staged bool // some of the change is in the index (TrackedChanges only)
OldPath string
Added int
Deleted int
}
FileChange is one changed file. Status is one of 'M','A','D','R','?'.
type GoneUpstreamError ¶
type GoneUpstreamError struct{ Upstream string }
GoneUpstreamError reports an upstream branch that no longer exists.
func (*GoneUpstreamError) Error ¶
func (e *GoneUpstreamError) Error() string
type HookFailedError ¶
type HookFailedError struct{ Line string }
HookFailedError is a checkout that happened (HEAD moved) but whose post-checkout hook failed; Line is the hook's first error line.
func (*HookFailedError) Error ¶
func (e *HookFailedError) Error() string
type PushKind ¶
type PushKind int
PushKind says where a push goes relative to the branch's upstream.
const ( // PushUpstream pushes to the existing, same-named upstream branch. PushUpstream PushKind = iota // PushNewBranch pushes to a same-named branch on origin and makes it the // upstream: used without an upstream, or when the upstream is a local // branch or has a different name (pushing there would publish the // commits onto another branch, which push.default=simple refuses too). PushNewBranch // PushRecreate pushes to the upstream branch, which was deleted on the // remote, recreating it. PushRecreate )
type PushPlan ¶
PushPlan is what a push will do: Branch (local) goes to Target on Remote. Ahead is the number of commits to push for PushUpstream.
type Ref ¶
Ref is a branch, tag or HEAD pointing at a commit. Name is "develop", "origin/develop", "v1.0" or "HEAD". Remote is the remote of a RemoteBranch ("origin"), "" otherwise.
func ParseDecorations ¶
ParseDecorations parses git's %D output. A name is a remote branch when it is prefixed by "<remote>/" for one of remotes; remote HEAD symrefs are dropped. Control characters are stripped from the names.
type RefInfo ¶
RefInfo is one branch or tag of the repo for the branch picker. Name is short ("develop", "origin/develop", "v1.0"); Remote is the remote of a RemoteBranch ("origin"), "" otherwise; Hash is the commit it points at (annotated tags are peeled).
type Repo ¶
type Repo struct {
// contains filtered or unexported fields
}
Repo is an opened git repository.
func (*Repo) BranchExists ¶
BranchExists reports whether the local branch name exists.
func (*Repo) Checkout ¶
func (r *Repo) Checkout(t CheckoutTarget) error
Checkout switches to t, refusing with ErrInProgress or ErrDirty first. A checkout that moved HEAD but whose post-checkout hook failed is a *HookFailedError (the checkout happened).
func (*Repo) CheckoutBranch ¶
CheckoutBranch switches to a local branch.
func (*Repo) CheckoutDetached ¶
CheckoutDetached checks out a commit in detached HEAD state.
func (*Repo) CheckoutRemote ¶
CheckoutRemote creates a local branch tracking remoteBranch ("origin/x") and switches to it.
func (*Repo) CheckoutWithStash ¶
func (r *Repo) CheckoutWithStash(t CheckoutTarget) (CheckoutResult, error)
CheckoutWithStash checks out t with uncommitted changes in tracked files: it stashes them (`git stash push`, untracked files stay where they are), checks out t and restores them (`git stash apply --index`, then drops the entry while it is still stash@{0}). It is only run after the user explicitly confirmed it.
When the checkout fails without moving HEAD the changes are restored on the original branch and the error is a *CheckoutRestoredError. When HEAD moved (a failing post-checkout hook) the changes are restored on the new branch and the hook is reported. When restoring conflicts it is a *StashConflictError (the entry is kept). When the staged state cannot be restored the changes come back unstaged. A stash entry is never dropped on a failure path.
func (*Repo) CherryPick ¶
CherryPick applies commit hash onto the current branch.
func (*Repo) CommitFiles ¶
func (r *Repo) CommitFiles(hash string) ([]FileChange, error)
CommitFiles lists files changed by a commit versus its first parent (or the empty tree for a root commit).
func (*Repo) CommitMeta ¶
func (r *Repo) CommitMeta(hash string) (CommitMeta, error)
CommitMeta reads the author, committer, date and message of a commit with a single git call.
func (*Repo) CreateBranch ¶
CreateBranch creates branch name at the given commit/ref without switching.
func (*Repo) CurrentBranch ¶
CurrentBranch returns the checked-out branch name, or "" when detached.
func (*Repo) CurrentBranchRaw ¶
CurrentBranchRaw returns the checked-out branch name exactly as git reports it, "" when detached. Use it for comparisons and git arguments, never for display.
func (*Repo) Diff ¶
Diff returns the colored diff for a commit or the working tree, piped through delta when available. path "" means the whole commit/working tree.
func (*Repo) Fetch ¶
Fetch updates all remotes and prunes deleted branches. It never prompts: git gets GIT_TERMINAL_PROMPT=0 (runCtx), credential helpers are told not to be interactive, and runCtx detaches git from the controlling terminal so ssh cannot ask for a passphrase either. The user's own ssh configuration (core.sshCommand, GIT_SSH_COMMAND, ...) is left untouched. ctx bounds the whole operation, children included.
func (*Repo) Fingerprint ¶ added in v0.2.0
Fingerprint summarizes HEAD and every ref (branches, remote branches, tags, the stash) with their commits. Two equal fingerprints mean the log, labels and current branch would load the same, so polling it is a cheap way to notice changes made outside gitree. The index and working tree are not part of it (git status refreshes the index on its own).
func (*Repo) GitreeStashes ¶
func (r *Repo) GitreeStashes() ([]StashEntry, error)
GitreeStashes lists the stash entries gitree made for a checkout (their message starts with "gitree: checkout "), wherever they are in the stash.
func (*Repo) HasTrackedChanges ¶
HasTrackedChanges reports whether any tracked file differs from HEAD.
func (*Repo) InProgress ¶
InProgress reports whether a merge, cherry-pick, revert or rebase is in progress.
func (*Repo) ListRefs ¶
ListRefs returns the local branches, remote branches (without the <remote>/HEAD symrefs) and tags, ordered local → remote → tag and alphabetically (ignoring case) within each group. Tags that do not lead to a commit (a tree or blob) are left out; a tag of a tag is peeled to its commit with rev-parse.
func (*Repo) LocalBranchFor ¶
LocalBranchFor finds the local copy of remoteBranch ("origin/x"): the local branch whose upstream it is, else a local branch with the name after the remote. exists is false when there is none; name is then the branch `git checkout --track` would create.
func (*Repo) Pull ¶
Pull fast-forwards the current branch to its upstream (`git pull --ff-only`) and returns how many commits it brought (0: already up to date). It never merges, rebases or stashes: uncommitted changes in tracked files are ErrDirty.
func (*Repo) PullAutostash ¶
PullAutostash is Pull for a dirty tree: git stashes the tracked changes, fast-forwards and restores them (`--autostash`). It is only run after the user explicitly confirmed it. When restoring conflicts the pull stays applied and the error is an *AutostashConflictError.
func (*Repo) Push ¶
Push pushes the current branch as want (a plan the user confirmed). The plan is made again first: if the branch or its destination changed meanwhile it fails with ErrBranchChanged and pushes nothing. Branches always go to a same-named remote branch, with explicit refspecs. It never forces: a rejected push is ErrRejected.
func (*Repo) ShowHeader ¶
ShowHeader returns the full hash, author, date and full message of a commit.
func (*Repo) TrackedChanges ¶
func (r *Repo) TrackedChanges() ([]FileChange, error)
TrackedChanges lists the tracked files that differ from HEAD (staged or not), without line counts: what ErrDirty is about.
func (*Repo) Tracking ¶
Tracking returns upstream info keyed by local branch name. Branches without an upstream are omitted.
func (*Repo) ValidBranchName ¶
ValidBranchName reports whether name is acceptable as a branch name (git check-ref-format --branch). Names starting with '-' are rejected.
func (*Repo) WorkingFiles ¶
func (r *Repo) WorkingFiles() ([]FileChange, error)
WorkingFiles lists tracked changes (staged+unstaged vs HEAD) plus untracked files as '?'.
type StashConflictError ¶
StashConflictError means the checkout of Target succeeded but the stashed changes conflicted when restored: the entry (Entry, commit Hash) stays in the stash and the conflicts are left in the working tree. Hook is set when the post-checkout hook failed too.
func (*StashConflictError) Error ¶
func (e *StashConflictError) Error() string
type StashEntry ¶
StashEntry is one stash entry: Ref ("stash@{1}"), its commit and message.
type StashKeptError ¶
StashKeptError means the stashed changes could not be restored (Cause): they are still in the stash entry Entry (commit Hash), untouched.
func (*StashKeptError) Error ¶
func (e *StashKeptError) Error() string
func (*StashKeptError) Unwrap ¶
func (e *StashKeptError) Unwrap() error
type Track ¶
type Track struct {
Upstream string
Remote string
UpstreamBranch string
Ahead int
Behind int
Gone bool
}
Track describes how a local branch relates to its upstream. Upstream is the short name ("origin/feature/x"); Remote is the upstream's remote ("origin", or "." for a local branch); UpstreamBranch is the branch name on that remote ("feature/x"). Gone means the upstream branch no longer exists (deleted on the remote and pruned).