Help us shape the future of Go! Take the 2026 Go Developer Survey

gitx

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package gitx runs the git CLI. It knows nothing about features, forges, or Ghostbranch's rules; it only makes git calls predictable.

Index

Constants

View Source
const AttributesOverride = "* -filter -ident -text -working-tree-encoding merge=text -diff\n"

AttributesOverride is written to every engine mirror's info/attributes, which outranks in-tree .gitattributes files. No tracked file can then name a filter, a merge driver, or an eol or encoding conversion, so blobs pass through a rebase byte for byte. merge=text is git's own 3-way merge.

Variables

This section is empty.

Functions

func Cleanup

func Cleanup() error

Cleanup removes the isolation directory, if this process made one. Call it once no git command is running: main calls it after the command returns, and test binaries call it from TestMain (testutil.Main). A git call after Cleanup makes a new directory.

func Version

func Version(ctx context.Context) (string, error)

Version is `git version`'s output, trimmed: "git version 2.47.1" and the like. It reads no repository.

Types

type ConflictError

type ConflictError struct {
	Files []string
	// Commit is the commit that stopped a cherry-pick replay; empty for a rebase.
	Commit string
}

ConflictError means a rebase or cherry-pick replay stopped on conflicts. It has been aborted; Files lists the conflicting paths.

func (*ConflictError) Error

func (e *ConflictError) Error() string

type Error

type Error struct {
	Args     []string
	ExitCode int
	Stdout   string
	Stderr   string
}

Error is a failed git invocation. Args never contain secrets: credentials travel in the environment.

func (*Error) Error

func (e *Error) Error() string

type Git

type Git struct {
	Dir    string
	Config [][2]string
	Env    []string
}

Git runs git against one repository directory (bare repo or worktree). Config entries are passed through GIT_CONFIG_* environment variables, so values such as auth headers never appear in a process listing.

func Init

func Init(ctx context.Context, dir string, bare bool) (*Git, error)

Init creates a repository at dir, which may not exist yet. A mirror holds unreleased shadow commits (F-28), so the repo is private whatever the umask: its directory and every parent Init creates are 0700, the files git init wrote lose their group and other bits, and core.sharedRepository=0600 makes git write every later file 0600 and directory 0700. The key is set after init rather than with --shared, which would also set receive.denyNonFastForwards and so change how a bare repo made here takes pushes.

func New

func New(dir string) *Git

New returns a Git for dir.

func (*Git) AddRemote

func (g *Git) AddRemote(ctx context.Context, name, url string) error

AddRemote adds a remote, or updates its URL if it already exists.

func (*Git) CherryPick

func (g *Git) CherryPick(ctx context.Context, commits []string, committer Identity) ([]string, error)

CherryPick applies commits, in order, onto this worktree's HEAD, keeping each commit's author and message and committing as committer. A commit that becomes empty is kept. It returns the new SHA for each input commit. On conflict it aborts and returns *ConflictError naming that commit; any other failure names the commit and aborts nothing that wasn't started.

func (*Git) CommitAuthor

func (g *Git) CommitAuthor(ctx context.Context, sha string) (Identity, error)

CommitAuthor reads a commit's author from the raw object, with its time in UTC.

func (*Git) CommitIdentities

func (g *Git) CommitIdentities(ctx context.Context, sha string) (author, committer string, err error)

CommitIdentities returns a commit's author and committer as "Name <email>", read from the raw object.

func (*Git) CommitMessage

func (g *Git) CommitMessage(ctx context.Context, sha string) (string, error)

CommitMessage returns a commit's message exactly as stored: the raw object after the header's blank line. Unlike log --format=%B, no config (such as i18n.logOutputEncoding) can re-encode it.

func (*Git) CommitTree

func (g *Git) CommitTree(ctx context.Context, tree string, parents []string, message string, author, committer Identity) (string, error)

CommitTree writes a commit object. The message is stored with exactly one trailing newline.

func (*Git) CommitsBetween

func (g *Git) CommitsBetween(ctx context.Context, base, head string) ([]string, error)

CommitsBetween lists the commits in head but not base along head's first-parent line, oldest first; never nil.

func (*Git) DeleteRef

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

DeleteRef deletes ref in this repository.

func (*Git) DiffNames

func (g *Git) DiffNames(ctx context.Context, a, b string) ([]string, error)

DiffNames lists paths that differ between two commits or trees, sorted.

func (*Git) Fetch

func (g *Git) Fetch(ctx context.Context, remote string, refspecs ...string) error

Fetch fetches refspecs from remote, pruning refs that disappeared. Tags are fetched only when a refspec names them.

func (*Git) HashObject

func (g *Git) HashObject(ctx context.Context, content []byte) (string, error)

HashObject stores content as a blob, byte for byte.

func (*Git) IsAncestor

func (g *Git) IsAncestor(ctx context.Context, a, b string) (bool, error)

IsAncestor reports whether a is an ancestor of (or equal to) b.

func (*Git) ListTree

func (g *Git) ListTree(ctx context.Context, rev string) ([]TreeEntry, error)

ListTree flattens rev's tree into its files and submodules, sorted by path.

func (*Git) LsRemote

func (g *Git) LsRemote(ctx context.Context, remote string) (map[string]string, error)

LsRemote maps every ref the remote advertises to its SHA.

func (*Git) MakeTree

func (g *Git) MakeTree(ctx context.Context, entries []TreeEntry) (string, error)

MakeTree writes a tree from flattened entries and returns its SHA.

func (*Git) MergeTreeCheck

func (g *Git) MergeTreeCheck(ctx context.Context, base, head string) (bool, []string, error)

MergeTreeCheck reports whether head merges cleanly onto base, without a worktree: git merge-tree --write-tree exits 0 when clean and 1 when it conflicts, listing the conflicted files after the tree line. Any other failure (a bad ref, say) is an error.

func (*Git) MustResolve

func (g *Git) MustResolve(ctx context.Context, rev string) (string, error)

MustResolve resolves rev and fails if it does not exist.

func (*Git) PatchIDs

func (g *Git) PatchIDs(ctx context.Context, commits []string) ([]string, error)

PatchIDs returns each commit's stable patch-id against its first parent, in order (git patch-id --stable): the same change on another base, or with another message, has the same patch-id. A commit that changes nothing has "".

func (*Git) ProtectAttributes

func (g *Git) ProtectAttributes(ctx context.Context) error

ProtectAttributes writes AttributesOverride to the repository's info/attributes, which its worktrees share.

func (*Git) Push

func (g *Git) Push(ctx context.Context, remote string, opt PushOptions, refspecs ...string) error

Push pushes refspecs to remote. Refspec sources may be SHAs; destinations must be full ref names.

func (*Git) ReadBlobs

func (g *Git) ReadBlobs(ctx context.Context, shas []string) (map[string][]byte, error)

ReadBlobs returns the content of each blob, keyed by SHA.

func (*Git) Refs

func (g *Git) Refs(ctx context.Context, prefix string) (map[string]string, error)

Refs maps every ref under prefix (e.g. "refs/heads") to its SHA.

func (*Git) Resolve

func (g *Git) Resolve(ctx context.Context, rev string) (string, bool, error)

Resolve returns the object rev names, or ok=false if it does not exist.

func (*Git) Run

func (g *Git) Run(ctx context.Context, args ...string) (string, error)

Run runs git and returns stdout without trailing newlines.

func (*Git) RunInput

func (g *Git) RunInput(ctx context.Context, stdin io.Reader, args ...string) (string, error)

RunInput runs git with stdin and returns stdout without trailing newlines.

func (*Git) ShowFile

func (g *Git) ShowFile(ctx context.Context, rev, path string) ([]byte, bool, error)

ShowFile returns the content of path at rev, or ok=false if it is absent.

func (*Git) SourceCommits

func (g *Git) SourceCommits(ctx context.Context, base, head string) ([]string, error)

SourceCommits lists the commits replaying head onto base applies, oldest first: those in head but not base, without merges, and without commits whose patch base already has (as git rebase leaves them out).

func (*Git) TreeOf

func (g *Git) TreeOf(ctx context.Context, rev string) (string, error)

TreeOf returns the tree SHA of a commit or tree.

func (*Git) UpdateRef

func (g *Git) UpdateRef(ctx context.Context, ref, sha string) error

UpdateRef points ref at sha in this repository.

func (*Git) With

func (g *Git) With(dir string) *Git

With returns a copy of g that runs in dir, keeping config and env.

func (*Git) WorktreeAdd

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

WorktreeAdd checks out rev, detached, into a new worktree at dir and returns a Git for it.

func (*Git) WorktreeRemove

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

WorktreeRemove deletes the worktree at dir and prunes its metadata.

type Identity

type Identity struct {
	Name  string
	Email string
	When  time.Time
}

Identity is an author or committer with a fixed time, so commits made from the same inputs always get the same SHA.

type Lease

type Lease struct {
	Ref    string
	Expect string
}

Lease guards a remote ref during push. Expect "" means the ref must not exist yet.

type PermissionError

type PermissionError struct{ Detail string }

PermissionError means the remote refused the push because the credentials may not write to it (GitHub answers 403).

func (*PermissionError) Error

func (e *PermissionError) Error() string

type PushOptions

type PushOptions struct {
	Atomic bool
	Leases []Lease
}

PushOptions controls a push.

type RefusedError

type RefusedError struct {
	Refs   []string
	Reason string // the server's reason, e.g. text inside "[remote rejected] (...)"
}

RefusedError means the remote refused at least one ref for a reason of its own (permissions, a ruleset, a hook); retrying won't help.

func (*RefusedError) Error

func (e *RefusedError) Error() string

type RejectedError

type RejectedError struct {
	Refs   []string
	Detail string
}

RejectedError means the remote refused at least one ref, for example because a lease did not match.

func (*RejectedError) Error

func (e *RejectedError) Error() string

type TreeEntry

type TreeEntry struct {
	Mode string
	Type string
	SHA  string
	Path string
}

TreeEntry is one file (blob) or submodule (commit) in a flattened tree.

Jump to

Keyboard shortcuts

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