gitx

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 12 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 gitx is the only package that runs the system git binary, with argument slices, validated refs and hardened flags and environment.

Index

Constants

View Source
const DefaultMaxBytes = 10 << 20

DefaultMaxBytes is the per-file limit used when none is given (10 MiB).

Variables

View Source
var (
	// ErrNotFound: the git binary is not on PATH.
	ErrNotFound = errors.New("gitx: git not found")
	// ErrNotRepo: the directory is not inside a git work tree.
	ErrNotRepo = errors.New("gitx: not a git repository")
	// ErrBadRef: a ref is not acceptable or does not resolve to a commit.
	ErrBadRef = errors.New("gitx: invalid ref")
	// ErrGit: git ran and failed.
	ErrGit = errors.New("gitx: git failed")
)

Errors returned by gitx. They carry no git output (AD-3).

Functions

func Available

func Available() bool

Available reports whether a git binary is on PATH.

Types

type Config

type Config struct {
	// HTTPSProxy becomes https_proxy.
	HTTPSProxy string
	// NoProxy becomes no_proxy.
	NoProxy string
	// CAFile is a PEM file path that becomes GIT_SSL_CAINFO.
	CAFile string
}

Config is the network configuration handed to git, mapped from the public network options. Empty fields set nothing.

type Entry

type Entry struct {
	// Commit, Author and Date describe the commit; they are empty for staged entries.
	// Author is the name only, never the email. Date is UTC.
	Commit string
	Author string
	Date   time.Time
	// Path is the post-image path with forward slashes, relative to the scan directory.
	Path string
	// Data is the post-image of the file with every line that was not added replaced by
	// an empty line, so line k of Data is line k of the file. Nil when TooLarge.
	Data []byte
	// TooLarge is set when Data would exceed the size limit.
	TooLarge bool
}

Entry is the added lines of one file in one commit, or in the index.

type Git

type Git struct {
	// contains filtered or unexported fields
}

Git runs hardened git commands. The zero value is not usable; call New.

func New

func New(cfg Config) *Git

New returns a runner with cfg.

func (*Git) HookInfo added in v0.3.0

func (g *Git) HookInfo(ctx context.Context, dir string) (HookInfo, error)

HookInfo reads the top level, the common git directory and the effective core.hooksPath of the repository containing dir. It runs no hook and reads config only. Outside a work tree it returns ErrNotRepo; without git, ErrNotFound.

func (*Git) InRepo

func (g *Git) InRepo(ctx context.Context, dir string) (bool, error)

InRepo reports whether dir is inside a git work tree. A directory that is not, or that git refuses to read (for example because of its owner), reports false.

func (*Git) ListFiles

func (g *Git) ListFiles(ctx context.Context, dir string) ([]string, error)

ListFiles lists what git would commit from dir: tracked files plus untracked files that are not ignored, relative to dir with forward slashes, without duplicates. A path that is absolute or climbs out of dir is dropped.

func (*Git) StreamLog

func (g *Git) StreamLog(ctx context.Context, dir string, opt LogOptions, fn func(Entry) bool) error

StreamLog streams the added lines of the whole history of dir, one Entry per file per commit, through fn, which returns false to stop. It reads git's output through a bufio.Reader and never holds more than one file of one commit.

func (*Git) StreamStaged

func (g *Git) StreamStaged(ctx context.Context, dir string, maxBytes int64, fn func(Entry) bool) error

StreamStaged streams the added lines of the index against HEAD, one Entry per file, through fn, which returns false to stop. Deleted and binary files give no Entry. GIT_INDEX_FILE, when set, selects the index (as git sets it for a hook).

func (*Git) ValidateRef

func (g *Git) ValidateRef(ctx context.Context, dir, ref string) (string, error)

ValidateRef checks ref for --since and resolves it to a full commit id. The ref must be non-empty, must not start with "-" or hold NUL, whitespace or control characters, and must be 4 to 64 hex characters or pass git check-ref-format --allow-onelevel. It then must resolve to a commit. Any failure is ErrBadRef.

type HookInfo added in v0.3.0

type HookInfo struct {
	// TopLevel is the absolute top-level directory of the work tree.
	TopLevel string
	// CommonDir is the absolute common git directory (shared by linked worktrees);
	// its hooks/ directory holds the hooks when core.hooksPath is not set.
	CommonDir string
	// HooksPath is the effective core.hooksPath read with the user's real config
	// (system, global, repository), "~" expanded; empty when it is not set.
	HooksPath string
}

HookInfo is where the git hooks of a repository live, for installing one.

type LogOptions

type LogOptions struct {
	// Since is a full commit id (from ValidateRef): history is limited to commits
	// reachable from Until but not from it. Empty means every ref (--all), unless
	// Until or ExcludeRemotes is set.
	Since string
	// Until is a full commit id (from ValidateRef), the tip the history ends at.
	// Empty means HEAD when Since or ExcludeRemotes is set, else every ref.
	Until string
	// ExcludeRemotes also leaves out every commit reachable from a remote-tracking
	// ref (refs/remotes/*). With an empty Since the walk is Until (or HEAD) minus
	// the remotes, instead of every ref.
	ExcludeRemotes bool
	// MaxBytes is the largest post-image one file of one commit may take; larger
	// ones come back with TooLarge set. 0 or less means 10 MiB.
	MaxBytes int64
	// OnCommit, when set, is called once per commit the log walks, including commits
	// that add no lines (and so give no Entry).
	OnCommit func()
}

LogOptions configures StreamLog.

Directories

Path Synopsis
Package gitxtest builds throwaway git repositories for tests.
Package gitxtest builds throwaway git repositories for tests.

Jump to

Keyboard shortcuts

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