Documentation ¶
Overview ¶
Package git is a low level and highly extensible git client library for reading repositories from git servers. It is written in Go from scratch, without any C dependencies.
We have been following the open/close principle in its design to facilitate extensions.
Small example extracting the commits from a repository:
func ExampleBasic_printCommits() { r := git.NewMemoryRepository() o := &git.CloneOptions{ URL: "https://github.com/src-d/go-git", } if err := r.Clone(o); err != nil { panic(err) } iter, err := r.Commits() if err != nil { panic(err) } defer iter.Close() for { commit, err := iter.Next() if err != nil { if err == io.EOF { break } panic(err) } fmt.Println(commit) } }
Index ¶
- Constants
- Variables
- func References(c *object.Commit, path string) ([]*object.Commit, error)
- type BlameResult
- type CloneOptions
- type FetchOptions
- type Line
- type PullOptions
- type PushOptions
- type Remote
- type Repository
- func (r *Repository) Blob(h plumbing.Hash) (*object.Blob, error)
- func (r *Repository) Blobs() (*object.BlobIter, error)
- func (r *Repository) Clone(o *CloneOptions) error
- func (r *Repository) Commit(h plumbing.Hash) (*object.Commit, error)
- func (r *Repository) Commits() (*object.CommitIter, error)
- func (r *Repository) Config() (*config.Config, error)
- func (r *Repository) CreateRemote(c *config.RemoteConfig) (*Remote, error)
- func (r *Repository) DeleteRemote(name string) error
- func (r *Repository) Fetch(o *FetchOptions) error
- func (r *Repository) Head() (*plumbing.Reference, error)
- func (r *Repository) IsEmpty() (bool, error)
- func (r *Repository) Object(t plumbing.ObjectType, h plumbing.Hash) (object.Object, error)
- func (r *Repository) Objects() (*object.ObjectIter, error)
- func (r *Repository) Pull(o *PullOptions) error
- func (r *Repository) Push(o *PushOptions) error
- func (r *Repository) Reference(name plumbing.ReferenceName, resolved bool) (*plumbing.Reference, error)
- func (r *Repository) References() (storer.ReferenceIter, error)
- func (r *Repository) Remote(name string) (*Remote, error)
- func (r *Repository) Remotes() ([]*Remote, error)
- func (r *Repository) Tag(h plumbing.Hash) (*object.Tag, error)
- func (r *Repository) Tags() (*object.TagIter, error)
- func (r *Repository) Tree(h plumbing.Hash) (*object.Tree, error)
- func (r *Repository) Trees() (*object.TreeIter, error)
- type Storer
Constants ¶
const (
// DefaultRemoteName name of the default Remote, just like git command
DefaultRemoteName = "origin"
)
Variables ¶
var ( ErrMissingURL = errors.New("URL field is required") ErrInvalidRefSpec = errors.New("invalid refspec") )
var ( ErrObjectNotFound = errors.New("object not found") ErrInvalidReference = errors.New("invalid reference, should be a tag or a branch") ErrRepositoryNonEmpty = errors.New("repository non empty") ErrRemoteNotFound = errors.New("remote not found") ErrRemoteExists = errors.New("remote already exists") )
Functions ¶
func References ¶
References returns a References for the file at "path", the commits are sorted in commit order. It stops searching a branch for a file upon reaching the commit were the file was created.
Caveats:
- Moves and copies are not currently supported.
- Cherry-picks are not detected unless there are no commits between them and therefore can appear repeated in the list. (see git path-id for hints on how to fix this).
Types ¶
type BlameResult ¶
func Blame ¶
func Blame(c *object.Commit, path string) (*BlameResult, error)
Blame returns the last commit that modified each line of a file in a repository.
The file to blame is identified by the input arguments: repo, commit and path. The output is a slice of commits, one for each line in the file.
Blaming a file is a two step process:
1. Create a linear history of the commits affecting a file. We use revlist.New for that.
2. Then build a graph with a node for every line in every file in the history of the file.
Each node (line) holds the commit where it was introduced or last modified. To achieve that we use the FORWARD algorithm described in Zimmermann, et al. "Mining Version Archives for Co-changed Lines", in proceedings of the Mining Software Repositories workshop, Shanghai, May 22-23, 2006.
Each node is assigned a commit: Start by the nodes in the first commit. Assign that commit as the creator of all its lines.
Then jump to the nodes in the next commit, and calculate the diff between the two files. Newly created lines get assigned the new commit as its origin. Modified lines also get this new commit. Untouched lines retain the old commit.
All this work is done in the assignOrigin function which holds all the internal relevant data in a "blame" struct, that is not exported.
TODO: ways to improve the efficiency of this function:
1. Improve revlist
2. Improve how to traverse the history (example a backward traversal will be much more efficient)
TODO: ways to improve the function in general:
1. Add memoization between revlist and assign.
2. It is using much more memory than needed, see the TODOs below.
type CloneOptions ¶
type CloneOptions struct { // The (possibly remote) repository URL to clone from URL string // Auth credentials, if required, to use with the remote repository Auth transport.AuthMethod // Name of the remote to be added, by default `origin` RemoteName string // Remote branch to clone ReferenceName plumbing.ReferenceName // Fetch only ReferenceName if true SingleBranch bool // Limit fetching to the specified number of commits Depth int }
CloneOptions describe how a clone should be perform
func (*CloneOptions) Validate ¶
func (o *CloneOptions) Validate() error
Validate validate the fields and set the default values
type FetchOptions ¶
type FetchOptions struct { // Name of the remote to fetch from. Defaults to origin. RemoteName string RefSpecs []config.RefSpec // Depth limit fetching to the specified number of commits from the tip of // each remote branch history. Depth int // Auth credentials, if required, to use with the remote repository Auth transport.AuthMethod }
FetchOptions describe how a fetch should be perform
func (*FetchOptions) Validate ¶
func (o *FetchOptions) Validate() error
Validate validate the fields and set the default values
type Line ¶
type Line struct { Author string // email address of the author of the line. Text string // original text of the line. }
Line values represent the contents and author of a line in BlamedResult values.
type PullOptions ¶
type PullOptions struct { // Name of the remote to be pulled. If empty, uses the default. RemoteName string // Remote branch to clone. If empty, uses HEAD. ReferenceName plumbing.ReferenceName // Fetch only ReferenceName if true. SingleBranch bool // Limit fetching to the specified number of commits. Depth int // Auth credentials, if required, to use with the remote repository Auth transport.AuthMethod }
PullOptions describe how a pull should be perform.
func (*PullOptions) Validate ¶
func (o *PullOptions) Validate() error
Validate validate the fields and set the default values.
type PushOptions ¶
type PushOptions struct { // RemoteName is the name of the remote to be pushed to. RemoteName string // RefSpecs specify what destination ref to update with what source // object. A refspec with empty src can be used to delete a reference. RefSpecs []config.RefSpec // Auth credentials, if required, to use with the remote repository Auth transport.AuthMethod }
PushOptions describe how a push should be performed.
func (*PushOptions) Validate ¶
func (o *PushOptions) Validate() error
Validate validate the fields and set the default values
type Remote ¶
type Remote struct {
// contains filtered or unexported fields
}
Remote represents a connection to a remote repository
func (*Remote) Fetch ¶
func (r *Remote) Fetch(o *FetchOptions) error
Fetch fetches references from the remote to the local repository. Returns nil if the operation is successful, NoErrAlreadyUpToDate if there are no changes to be fetched, or an error.
func (*Remote) Push ¶
func (r *Remote) Push(o *PushOptions) (err error)
Push performs a push to the remote. Returns NoErrAlreadyUpToDate if the remote was already up-to-date.
TODO: Support deletes. TODO: Support pushing tags. TODO: Check if force update is given, otherwise reject non-fast forward.
type Repository ¶
type Repository struct { // Progress is where the human readable information sent by the server is // stored, if nil nothing is stored and the capability (if supported) // no-progress, is sent to the server to avoid send this information Progress sideband.Progress // contains filtered or unexported fields }
Repository giturl string, auth common.AuthMethod repository struct
func NewFilesystemRepository ¶
func NewFilesystemRepository(path string) (*Repository, error)
NewFilesystemRepository creates a new repository, backed by a filesystem.Storage based on a fs.OS, if you want to use a custom one you need to use the function NewRepository and build you filesystem.Storage
func NewMemoryRepository ¶
func NewMemoryRepository() *Repository
NewMemoryRepository creates a new repository, backed by a memory.Storage
func NewRepository ¶
func NewRepository(s Storer) (*Repository, error)
NewRepository creates a new repository with the given Storage
func (*Repository) Blob ¶
Blob returns the blob with the given hash
func (*Repository) Blobs ¶
func (r *Repository) Blobs() (*object.BlobIter, error)
Blobs decodes the objects into blobs
func (*Repository) Clone ¶
func (r *Repository) Clone(o *CloneOptions) error
Clone clones a remote repository
func (*Repository) Commit ¶
object.Commit return the commit with the given hash
func (*Repository) Commits ¶
func (r *Repository) Commits() (*object.CommitIter, error)
Commits decode the objects into commits
func (*Repository) Config ¶
func (r *Repository) Config() (*config.Config, error)
Config return the repository config
func (*Repository) CreateRemote ¶
func (r *Repository) CreateRemote(c *config.RemoteConfig) (*Remote, error)
CreateRemote creates a new remote
func (*Repository) DeleteRemote ¶
func (r *Repository) DeleteRemote(name string) error
DeleteRemote delete a remote from the repository and delete the config
func (*Repository) Fetch ¶
func (r *Repository) Fetch(o *FetchOptions) error
Fetch fetches changes from a remote repository. Returns nil if the operation is successful, NoErrAlreadyUpToDate if there are no changes to be fetched, or an error.
func (*Repository) Head ¶
func (r *Repository) Head() (*plumbing.Reference, error)
Head returns the reference where HEAD is pointing to.
func (*Repository) IsEmpty ¶
func (r *Repository) IsEmpty() (bool, error)
IsEmpty returns true if the repository is empty
func (*Repository) Object ¶
func (r *Repository) Object(t plumbing.ObjectType, h plumbing.Hash) (object.Object, error)
Object returns an object with the given hash.
func (*Repository) Objects ¶
func (r *Repository) Objects() (*object.ObjectIter, error)
Objects returns an object.ObjectIter that can step through all of the annotated tags in the repository.
func (*Repository) Pull ¶
func (r *Repository) Pull(o *PullOptions) error
Pull incorporates changes from a remote repository into the current branch. Returns nil if the operation is successful, NoErrAlreadyUpToDate if there are no changes to be fetched, or an error.
func (*Repository) Push ¶
func (r *Repository) Push(o *PushOptions) error
Push pushes changes to a remote.
func (*Repository) Reference ¶
func (r *Repository) Reference(name plumbing.ReferenceName, resolved bool) ( *plumbing.Reference, error)
Reference returns the reference for a given reference name. If resolved is true, any symbolic reference will be resolved.
func (*Repository) References ¶
func (r *Repository) References() (storer.ReferenceIter, error)
References returns a ReferenceIter for all references.
func (*Repository) Remote ¶
func (r *Repository) Remote(name string) (*Remote, error)
Remote return a remote if exists
func (*Repository) Remotes ¶
func (r *Repository) Remotes() ([]*Remote, error)
Remotes return all the remotes
func (*Repository) Tag ¶
Tag returns a tag with the given hash.
func (*Repository) Tags ¶
func (r *Repository) Tags() (*object.TagIter, error)
Tags returns a object.TagIter that can step through all of the annotated tags in the repository.
func (*Repository) Tree ¶
Tree return the tree with the given hash
func (*Repository) Trees ¶
func (r *Repository) Trees() (*object.TreeIter, error)
Trees decodes the objects into trees
type Storer ¶
type Storer interface { storer.EncodedObjectStorer storer.ReferenceStorer storer.ShallowStorer config.ConfigStorer }
Storer is a generic storage of objects, references and any information related to a particular repository. Some Storer implementations persist the information in an system directory (such as `.git`) and others implementations are in memmory being ephemeral
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cli
|
|
Package config storage is the implementation of git config for go-git
|
Package config storage is the implementation of git config for go-git |
+build ignore
|
+build ignore |
package plumbing implement the core interfaces and structs used by go-git
|
package plumbing implement the core interfaces and structs used by go-git |
format/config
Package config implements decoding/encoding of git config files.
|
Package config implements decoding/encoding of git config files. |
format/idxfile
Package idxfile implements a encoder/decoder of idx files
|
Package idxfile implements a encoder/decoder of idx files |
format/index
Package index implements a encoder/decoder of index format files
|
Package index implements a encoder/decoder of index format files |
format/packfile
Package packfile implements a encoder/decoder of packfile format
|
Package packfile implements a encoder/decoder of packfile format |
format/pktline
Package pktline implements reading payloads form pkt-lines and encoding pkt-lines from payloads.
|
Package pktline implements reading payloads form pkt-lines and encoding pkt-lines from payloads. |
protocol/packp/sideband
Package sideband implements a sideband mutiplex/demultiplexer
|
Package sideband implements a sideband mutiplex/demultiplexer |
revlist
Package revlist implements functions to walk the objects referenced by a commit history.
|
Package revlist implements functions to walk the objects referenced by a commit history. |
transport
Package transport includes the implementation for different transport protocols.
|
Package transport includes the implementation for different transport protocols. |
transport/http
Package http implements a HTTP client for go-git.
|
Package http implements a HTTP client for go-git. |
transport/internal/common
Package common implements the git pack protocol with a pluggable transport.
|
Package common implements the git pack protocol with a pluggable transport. |
transport/server
Package server implements the git server protocol.
|
Package server implements the git server protocol. |
transport/test
Package test implements common test suite for different transport implementations.
|
Package test implements common test suite for different transport implementations. |
storage
|
|
filesystem
Package filesystem is a storage backend base on filesystems
|
Package filesystem is a storage backend base on filesystems |
filesystem/internal/dotgit
https://github.com/git/git/blob/master/Documentation/gitrepository-layout.txt
|
https://github.com/git/git/blob/master/Documentation/gitrepository-layout.txt |
memory
Package memory is a storage backend base on memory
|
Package memory is a storage backend base on memory |
utils
|
|
binary
Package binary implements sintax-sugar functions on top of the standard library binary package
|
Package binary implements sintax-sugar functions on top of the standard library binary package |
diff
Package diff implements line oriented diffs, similar to the ancient Unix diff command.
|
Package diff implements line oriented diffs, similar to the ancient Unix diff command. |