gitutil

package
v0.7.156 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotRegularFile reports a changed path that is neither a regular file
	// nor absent — most often a dirty submodule, whose state lives in another
	// repository and so cannot be hashed from here.
	ErrNotRegularFile = errors.New("changed path is not a regular file")
	// ErrDigestBudget reports that the changed files total more than the digest
	// budget, the point past which hashing them costs more than whatever the
	// caller would have skipped.
	ErrDigestBudget = errors.New("changed files exceed the digest budget")
)

Errors reported by Fingerprint. They name the condition that stopped it, because a caller that silently stops caching for one repo and not another is otherwise impossible to explain: each of these means "this tree cannot be fingerprinted at all", not "nothing has changed".

View Source
var ErrNoOriginHEAD = errors.New("origin/HEAD is not set")

ErrNoOriginHEAD is returned by MergeBase when the upstream tracking branch is set but origin/HEAD is not — a common state after git init + push without fetch.

Functions

func CreateBundle added in v0.7.122

func CreateBundle(base, cwd string) ([]byte, error)

CreateBundle creates a git bundle from base..HEAD in the repo at cwd and returns the raw bytes. If base is empty, the full history up to HEAD is bundled.

func CurrentBranch

func CurrentBranch() (string, error)

CurrentBranch returns the current git branch name. Returns an error if in detached HEAD state or not in a git repo.

func GeneratePatch

func GeneratePatch(base string) (string, error)

GeneratePatch generates a binary diff from the given base commit, including untracked files. It temporarily stages untracked files with git add -N and resets them after generating the diff.

func HeadRef added in v0.7.122

func HeadRef(cwd string) (string, error)

HeadRef returns the SHA of the current HEAD commit in the repo at cwd.

func HeadRefCtx added in v0.7.138

func HeadRefCtx(ctx context.Context, cwd string) (string, error)

HeadRefCtx returns the SHA of the current HEAD commit in the repo at cwd, honouring ctx for cancellation/timeout.

func IsBranchPushed added in v0.7.1

func IsBranchPushed() bool

IsBranchPushed returns true if the current branch exists on the remote (i.e. refs/remotes/origin/<branch> is present locally).

func MergeBase

func MergeBase() (string, error)

MergeBase returns a commit SHA that the remote is guaranteed to have. Tries merge-base between upstream and origin/HEAD first, falls back to origin/HEAD.

func RepoRoot

func RepoRoot(from string) (string, error)

RepoRoot returns the root directory of the current git repository by walking up from the given directory looking for .git/.

func TopLevelCtx added in v0.7.138

func TopLevelCtx(ctx context.Context, dir string) string

TopLevelCtx returns the git repository root for dir, or "" if not in a git repo, honouring ctx for cancellation/timeout.

Types

type Worktree added in v0.7.144

type Worktree struct {
	// Head is the SHA of the current HEAD commit.
	Head string
	// Digest hashes every path git reports as changed, contents included.
	Digest string
	// Clean reports whether git sees no change at all relative to HEAD: nothing
	// modified, staged, or untracked.
	Clean bool
}

Worktree fingerprints the state of a git working tree at a point in time. Two Worktrees with equal Head and Digest describe trees with identical content, so callers can compare one against a previously recorded value to decide whether anything has changed since.

The zero Worktree describes no tree at all. It reports the tree as not clean and is refused as a cache key, so a caller holding one falls back to doing whatever work it might otherwise have skipped. Fingerprint returns it alongside an error saying which condition made the tree unfingerprintable.

func Fingerprint added in v0.7.144

func Fingerprint(dir string) (Worktree, error)

Fingerprint captures the state of the working tree at dir: the HEAD commit plus a digest of the porcelain status entries — which record adds, deletions, renames and mode changes — and the current contents of every changed path.

Hashing contents is what makes the fingerprint sensitive to repeated edits. Porcelain output for a modified file is byte-identical before and after a further edit (both report " M path"), so status alone would call the tree unchanged across that edit — exactly the loop a Stop hook runs in.

The error is non-nil when the tree's state cannot be established: dir is not a repo, the repo has no commits yet, a changed path cannot be read, or the tree exceeds the digest budget. The returned Worktree is then the zero value, which is safe to pass on: no caller can mistake it for a real tree.

Jump to

Keyboard shortcuts

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