Categorygg-scm.io/pkg/git
modulepackage
0.11.0
Repository: https://github.com/gg-scm/gg-git.git
Documentation: pkg.go.dev

# README

gg-scm.io/pkg/git

Reference Contributor Covenant

gg-scm.io/pkg/git provides a high-level interface for interacting with a Git subprocess in Go. It was developed for gg, but this library is useful for any program that wishes to interact with a Git repository. gg-scm.io/pkg/git lets your Go program read information from commit history, create new commits, and do anything else you can do from the Git CLI.

Installation

Inside your Go project, run:

go get gg-scm.io/pkg/git

Usage

// Find the Git executable.
g, err := git.New(git.Options{})
if err != nil {
  return err
}

// Write a file and track it with `git add`.
err = ioutil.WriteFile("foo.txt", []byte("Hello, World!\n"), 0666)
if err != nil {
  return err
}
err = g.Add(ctx, []git.Pathspec{git.LiteralPath("foo.txt")}, git.AddOptions{})
if err != nil {
  return err
}

// Create a new commit.
err = g.Commit(ctx, "Added foo.txt with a greeting", git.CommitOptions{})
if err != nil {
  return err
}

See more examples on pkg.go.dev.

This library is tested against Git 2.17.1 and newer. Older versions may work, but are not supported.

Support

If you've found an issue, file it on the issue tracker.

Motivation

As noted in the Git book, shelling out to Git has the benefit of using the canonical implementation of Git and having all of its features. However, as the book notes, trying to interact with a Git subprocess requires "pars[ing] Git's occasionally-changing output format to read progress and result information, which can be inefficient and error-prone." This package handles all those details for you. Common operations like git commit or git rev-parse are wrapped as functions that take in Go data structures as input and return Go data structures as output. These are methods are tested to be robust over many different versions of Git under different scenarios. For less common operations, you can invoke Git with exactly the same command line arguments you would use interactively and gg-scm.io/pkg/git will handle finding Git and collecting error messages for you. All Git subprocess invocations can be logged for easy debugging.

The goals of gg-scm.io/pkg/git are different from those of go-git. go-git aims to be a reimplementation of Git in pure Go. go-git avoids the dependency on the Git CLI, but as a result, go-git does not have the same feature set as upstream Git and introduces the potential for discrepancies in implementation. gg-scm.io/pkg/git is intended for scenarios where the fidelity of interactions with Git matters.

Stability

The following packages are stable and we make a reasonable effort to avoid backward-incompatible changes:

  • gg-scm.io/pkg/git
  • gg-scm.io/pkg/git/githash

The following packages are relatively new and may still make breaking changes:

  • gg-scm.io/pkg/git/object
  • gg-scm.io/pkg/git/packfile
  • gg-scm.io/pkg/git/packfile/client

Because we still have some packages in early development, we have kept the entire repository on major version 0. When all packages are stable, we will start using major version 1.

Contributing

We'd love to accept your patches and contributions to this project. See CONTRIBUTING.md for more details.

If you find this package useful, consider sponsoring @zombiezen, the author and maintainer.

Links

License

Apache 2.0

# Packages

Package githash provides types for Git object identifiers.
Package object provides types for Git objects and functions for parsing and serializing those objects.
Package packfile provides types for operating on Git packfiles.

# Functions

BranchRef returns a ref for the given branch name.
CreateRef returns a RefMutation that creates a ref with the given value, failing if the ref already exists.
Custom creates a new Git context from the given Runner and FileSystem.
DeleteRef returns a RefMutation that unconditionally deletes a ref.
DeleteRefIfMatches returns a RefMutation that attempts to delete a ref, but fails if it has the given value.
JoinPathspecMagic combines a set of magic options and a pattern into a pathspec.
LiteralPath escapes a string from any special characters.
New creates a new Git context that communicates with a local Git subprocess.
NewLocal returns a new local runner with the given options.
ParseHash parses a hex-encoded hash.
ParseURL parses a Git remote URL, including the alternative SCP syntax.
SetRef returns a RefMutation that unconditionally sets a ref to the given value.
SetRefIfMatches returns a RefMutation that sets a ref to newvalue, failing if the ref does not have the given oldvalue.
StartPipe starts a piped Git command on r.
TagRef returns a ref for the given tag name.
URLFromPath converts a filesystem path into a URL.

# Constants

AbortOnFileChange stops the checkout if a file that is modified locally differs between the current HEAD and the target commit.
Diff status codes.
Diff status codes.
Diff status codes.
Diff status codes.
Diff status codes.
Diff status codes.
Diff status codes.
Diff status codes.
Diff status codes.
DiscardLocal uses the target commit's content regardless of local modifications.
FetchHead records the branch which was fetched from a remote repository with the last git fetch invocation.
Head names the commit on which the changes in the working tree are based.
MergeLocal performs a three-way merge on any differing files.
NoPathspec is the special "there is no pathspec" form.

# Structs

AddOptions specifies the command-line options for `git add`.
AmendOptions overrides the previous commit's fields.
BranchOptions specifies options for a new branch.
CheckoutOptions specifies the command-line options for `git checkout`.
CloneOptions specifies the command-line options for `git clone`.
CommitOptions overrides the default metadata for a commit.
Config is a collection of configuration settings.
DeleteBranchOptions specifies options for a new branch.
A DiffStatusEntry describes the state of a single file in a diff.
DiffStatusOptions specifies the command-line arguments for `git diff --status`.
Git is a context for performing Git version control operations.
Invocation holds the parameters for a Git process.
IterateRefsOptions specifies filters for [Git.IterateRefs].
IterateRemoteRefsOptions specifies filters for [Git.IterateRemoteRefs].
ListTreeOptions specifies the command-line options for `git ls-tree`.
Local implements Runner by starting Git subprocesses.
Log is an open handle to a `git cat-file --batch` subprocess.
LogOptions specifies filters and ordering on a log listing.
Options holds the parameters for New and NewLocal.
PathspecMagic specifies all the "magic" pathspec options.
RefIterator is an open handle to a Git subprocess that lists refs.
A RefMutation describes an operation to perform on a ref.
Remote stores the configuration for a remote repository.
RemoveOptions specifies the command-line options for `git add`.
Rev is a parsed reference to a single commit.
A StatusEntry describes the state of a single file in the working copy.
StatusOptions specifies the command-line arguments for `git status`.
A SubmoduleConfig represents a single section in a gitmodules file.
TreeEntry represents a single entry in a Git tree object.

# Interfaces

A FileSystem manipulates paths for a possibly remote filesystem.
A Piper is an optional interface that a Runner can implement for more efficient streaming of long-running outputs.
A Runner executes Git processes.

# Type aliases

CheckoutConflictBehavior specifies the behavior of checkout with local modifications.
DiffStatusCode is a single-letter code from the `git diff --name-status` format.
A FetchRefspec specifies a mapping from remote refs to local refs.
A Hash is the SHA-1 hash of a Git object.
A Pathspec is a Git path pattern.
A Ref is a Git reference to a commit.
A RefPattern is a part of a refspec.
A StatusCode is a two-letter code from the `git status` short format.
A TopPath is a slash-separated path relative to the top level of the repository.