git

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package git is a narrow shell-out wrapper around the `git` binary. Every lore subcommand that touches git funnels through here so error handling and exit-code mapping live in one place.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendIgnoreEntry

func AppendIgnoreEntry(path, entry string) error

AppendIgnoreEntry appends entry to path on its own line. Creates the parent directory and the file if either is missing. If the file exists and does not end with a newline, prepends one so entry isn't concatenated onto the previous line.

func CheckIgnore

func CheckIgnore(workdir, path string) (bool, error)

CheckIgnore reports whether path is ignored by gitignore rules as seen from workdir. Lives outside Run because `git check-ignore`'s exit 1 is a normal "not ignored" answer rather than a failure; Run would map that to CodeGitNetwork.

Semantics of `git check-ignore -q <path>`:

  • exit 0 -> path matches a gitignore rule -> (true, nil)
  • exit 1 -> path does not match any gitignore rule -> (false, nil)
  • exit 128 -> workdir is not inside a git repo -> (false, nil)
  • other -> real failure -> (false, *lerr.Error{CodeGitNetwork})

The "not a git repo" case is folded into the not-ignored answer so callers (notably `lore add`'s gitignore precheck) surface the standard CodeRefusedSafety message and the "add it to your global .gitignore" hint, rather than a confusing CodeGitNetwork. Conservatively reporting "not ignored" preserves the safety property: a non-git project is only safe to onboard when its agent dir is globally gitignored anyway, since the user may later `git init` and commit.

A missing git binary maps to CodeSetupMissing for consistency with Run.

func CommitExists

func CommitExists(repo, rev string) (bool, error)

CommitExists reports whether rev resolves to a commit object.

func GlobalExcludesPath

func GlobalExcludesPath() (string, error)

GlobalExcludesPath returns the path of the user's global gitignore. Resolution order:

  1. `core.excludesFile` from git's GLOBAL config scope only. Local repo config is intentionally NOT consulted: slice 4's picker labels this option "machine-wide", so honouring a per-repo `core.excludesFile` override here would mislead the user into writing a rule that doesn't apply elsewhere. System scope is also skipped; the picker is about the user's own global file.
  2. $XDG_CONFIG_HOME/git/ignore, when XDG_CONFIG_HOME is set.
  3. $HOME/.config/git/ignore.

Does not create the file. The caller writes via AppendIgnoreEntry, which creates parents and the file as needed.

func Init

func Init(dir string) error

Init runs `git init` in dir. dir must exist.

func ListTrackedUnder

func ListTrackedUnder(workdir, relPath string) ([]string, error)

ListTrackedUnder returns paths inside relPath that are tracked by the git repo containing workdir. Used by slice 3's safety gate: moving tracked content into central and replacing it with a symlink would silently rewrite git's view of the project, so `lore add` refuses when the agent dir contains any tracked file.

Semantics of `git ls-files -- <relPath>`:

  • exit 0, empty stdout -> nothing tracked under relPath -> ([], nil)
  • exit 0, paths emitted -> tracked files inside relPath -> (paths, nil)
  • exit 128 -> workdir is not inside a git repo -> ([], nil)
  • other -> real failure -> (nil, *lerr.Error{CodeGitNetwork})

The "not a git repo" case folds into an empty result, mirroring CheckIgnore's convention: a non-git project trivially has no tracked files, and the safety gate concerns itself with what git currently tracks rather than what the user might track later.

A missing git binary maps to CodeSetupMissing for consistency with Run and CheckIgnore.

func MergeFile

func MergeFile(project, base, central []byte) ([]byte, bool, error)

MergeFile performs a three-way text merge with stable project and central labels.

func RepoExcludePath

func RepoExcludePath(workdir string) (string, error)

RepoExcludePath returns the absolute path to .git/info/exclude for the repo containing workdir. Uses `git rev-parse --git-path info/exclude` so worktrees (where .git is a file pointing into the main repo's worktrees/ directory) and submodules resolve correctly without us parsing the .git file ourselves.

func RepoToplevel

func RepoToplevel(workdir string) (string, error)

RepoToplevel returns the absolute path to the worktree root that contains workdir. Refuses with CodeRefusedSafety when workdir is not inside a git repo: callers (slice 4's picker, slice 10's link) lean on this as their "is this a git project?" gate so they don't try to land a gitignore entry, an exclude rule, or a check-ignore in a directory that has no git at all.

Used over a bare `git rev-parse --is-inside-work-tree` so the caller also gets the toplevel path (needed by slice 4's TargetRepoGitignore option, which must write to the repo root's .gitignore even when the project is onboarded from a subdirectory of a monorepo).

func Run

func Run(workdir string, args ...string) (string, error)

Run executes `git args...` in workdir. Returns trimmed stdout. Non-zero exits are wrapped as *errors.Error with CodeGitNetwork; the error message includes the git stderr so callers don't need to construct it themselves. It shares its process execution path with RunRaw.

func StatusRaw

func StatusRaw(repo string, paths ...string) ([]byte, error)

StatusRaw returns NUL-delimited porcelain records for paths.

Types

type Result

type Result struct {
	Stdout   []byte
	Stderr   []byte
	ExitCode int
	// contains filtered or unexported fields
}

Result is the unmodified output of a Git process.

func RunRaw

func RunRaw(workdir string, args ...string) (Result, error)

RunRaw executes Git without trimming or converting stdout. It returns non-zero exits to the caller so read APIs can distinguish an absent object from a broken Git invocation.

type TreeEntry

type TreeEntry struct {
	Path       string
	Mode       string
	ObjectType string
	ObjectID   string
	Data       []byte
}

TreeEntry is one file or symlink stored in a Git tree.

func ReadTree

func ReadTree(repo, rev, prefix string) ([]TreeEntry, error)

ReadTree reads every blob under prefix at rev without changing its bytes.

Jump to

Keyboard shortcuts

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