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 ReverseSortCommits(l []*Commit)
- func SortCommits(l []*Commit)
- func WalkCommitHistory(c *Commit, cb func(*Commit) error) error
- type Action
- type Blame
- type Blob
- type BlobIter
- type Change
- type ChangeEntry
- type Changes
- type CloneOptions
- type Commit
- func (c *Commit) Blame(path string) (*Blame, error)
- func (c *Commit) Decode(o core.Object) (err error)
- func (b *Commit) Encode(o core.Object) error
- func (c *Commit) File(path string) (*File, error)
- func (c *Commit) Files() (*FileIter, error)
- func (c *Commit) History() ([]*Commit, error)
- func (c *Commit) ID() core.Hash
- func (c *Commit) NumParents() int
- func (c *Commit) Parents() *CommitIter
- func (c *Commit) References(path string) ([]*Commit, error)
- func (c *Commit) String() string
- func (c *Commit) Tree() (*Tree, error)
- func (c *Commit) Type() core.ObjectType
- type CommitIter
- type FetchOptions
- type File
- type FileIter
- type Hash
- type Object
- type ObjectIter
- type PullOptions
- type Remote
- func (r *Remote) Capabilities() *packp.Capabilities
- func (r *Remote) Config() *config.RemoteConfig
- func (r *Remote) Connect() error
- func (r *Remote) Disconnect() error
- func (r *Remote) Fetch(o *FetchOptions) (err error)
- func (r *Remote) Head() *core.Reference
- func (r *Remote) Info() *common.GitUploadPackInfo
- func (r *Remote) Ref(name core.ReferenceName, resolved bool) (*core.Reference, error)
- func (r *Remote) Refs() (core.ReferenceIter, error)
- func (r *Remote) String() string
- type Repository
- func (r *Repository) Blob(h core.Hash) (*Blob, error)
- func (r *Repository) Blobs() (*BlobIter, error)
- func (r *Repository) Clone(o *CloneOptions) error
- func (r *Repository) Commit(h core.Hash) (*Commit, error)
- func (r *Repository) Commits() (*CommitIter, error)
- func (r *Repository) CreateRemote(c *config.RemoteConfig) (*Remote, error)
- func (r *Repository) DeleteRemote(name string) error
- func (r *Repository) Head() (*core.Reference, error)
- func (r *Repository) IsEmpty() (bool, error)
- func (r *Repository) Object(t core.ObjectType, h core.Hash) (Object, error)
- func (r *Repository) Objects() (*ObjectIter, error)
- func (r *Repository) Pull(o *PullOptions) error
- func (r *Repository) Ref(name core.ReferenceName, resolved bool) (*core.Reference, error)
- func (r *Repository) Refs() (core.ReferenceIter, error)
- func (r *Repository) Remote(name string) (*Remote, error)
- func (r *Repository) Remotes() ([]*Remote, error)
- func (r *Repository) Tag(h core.Hash) (*Tag, error)
- func (r *Repository) Tags() (*TagIter, error)
- func (r *Repository) Tree(h core.Hash) (*Tree, error)
- func (r *Repository) Trees() (*TreeIter, error)
- type Signature
- type Storage
- type Tag
- func (t *Tag) Blob() (*Blob, error)
- func (t *Tag) Commit() (*Commit, error)
- func (t *Tag) Decode(o core.Object) (err error)
- func (t *Tag) Encode(o core.Object) error
- func (t *Tag) ID() core.Hash
- func (t *Tag) Object() (Object, error)
- func (t *Tag) String() string
- func (t *Tag) Tree() (*Tree, error)
- func (t *Tag) Type() core.ObjectType
- type TagIter
- type Tree
- type TreeEntry
- type TreeIter
- type TreeWalker
Constants ¶
const DateFormat = "Mon Jan 02 15:04:05 2006 -0700"
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") )
var ( ErrMaxTreeDepth = errors.New("maximum tree depth exceeded") ErrFileNotFound = errors.New("file not found") )
New errors defined by this package.
var ErrUnsupportedObject = errors.New("unsupported object type")
ErrUnsupportedObject trigger when a non-supported object is being decoded.
var NoErrAlreadyUpToDate = errors.New("already up-to-date")
Functions ¶
func ReverseSortCommits ¶
func ReverseSortCommits(l []*Commit)
ReverseSortCommits sort a commit list by commit date, from newer to older.
func SortCommits ¶
func SortCommits(l []*Commit)
SortCommits sort a commit list by commit date, from older to newer.
Types ¶
type Blob ¶
Blob is used to store file data - it is generally a file.
func (*Blob) ID ¶
ID returns the object ID of the blob. The returned value will always match the current value of Blob.Hash.
ID is present to fulfill the Object interface.
func (*Blob) Reader ¶
func (b *Blob) Reader() (core.ObjectReader, error)
Reader returns a reader allow the access to the content of the blob
func (*Blob) Type ¶
func (b *Blob) Type() core.ObjectType
Type returns the type of object. It always returns core.BlobObject.
Type is present to fulfill the Object interface.
type BlobIter ¶
type BlobIter struct { core.ObjectIter // contains filtered or unexported fields }
BlobIter provides an iterator for a set of blobs.
func NewBlobIter ¶
func NewBlobIter(r *Repository, iter core.ObjectIter) *BlobIter
NewBlobIter returns a CommitIter for the given repository and underlying object iterator.
The returned BlobIter will automatically skip over non-blob objects.
type Change ¶
type Change struct { Action From ChangeEntry To ChangeEntry }
type ChangeEntry ¶
type CloneOptions ¶
type CloneOptions struct { // The (possibly remote) repository URL to clone from URL string // Auth credentials, if required, to uses with the remote repository Auth common.AuthMethod // Name of the remote to be added, by default `origin` RemoteName string // Remote branch to clone ReferenceName core.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 Commit ¶
type Commit struct { Hash core.Hash Author Signature Committer Signature Message string // contains filtered or unexported fields }
Commit points to a single tree, marking it as what the project looked like at a certain point in time. It contains meta-information about that point in time, such as a timestamp, the author of the changes since the last commit, a pointer to the previous commit(s), etc. http://schacon.github.io/gitbook/1_the_git_object_model.html
func (*Commit) Blame ¶
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.
func (*Commit) File ¶
File returns the file with the specified "path" in the commit and a nil error if the file exists. If the file does not exist, it returns a nil file and the ErrFileNotFound error.
func (*Commit) History ¶
History return a slice with the previous commits in the history of this commit
func (*Commit) ID ¶
ID returns the object ID of the commit. The returned value will always match the current value of Commit.Hash.
ID is present to fulfill the Object interface.
func (*Commit) NumParents ¶
NumParents returns the number of parents in a commit.
func (*Commit) Parents ¶
func (c *Commit) Parents() *CommitIter
Parents return a CommitIter to the parent Commits
func (*Commit) 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).
func (*Commit) Type ¶
func (c *Commit) Type() core.ObjectType
Type returns the type of object. It always returns core.CommitObject.
Type is present to fulfill the Object interface.
type CommitIter ¶
type CommitIter struct { core.ObjectIter // contains filtered or unexported fields }
CommitIter provides an iterator for a set of commits.
func NewCommitIter ¶
func NewCommitIter(r *Repository, iter core.ObjectIter) *CommitIter
NewCommitIter returns a CommitIter for the given repository and underlying object iterator.
The returned CommitIter will automatically skip over non-commit objects.
func (*CommitIter) ForEach ¶
func (iter *CommitIter) ForEach(cb func(*Commit) error) error
ForEach call the cb function for each commit contained on this iter until an error happends or the end of the iter is reached. If ErrStop is sent the iteration is stop but no error is returned. The iterator is closed.
func (*CommitIter) Next ¶
func (iter *CommitIter) Next() (*Commit, error)
Next moves the iterator to the next commit and returns a pointer to it. If it has reached the end of the set it will return io.EOF.
type FetchOptions ¶
type FetchOptions struct { RefSpecs []config.RefSpec // Depth limit fetching to the specified number of commits from the tip of // each remote branch history. Depth int }
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 File ¶
File represents git file objects.
type FileIter ¶
type FileIter struct {
// contains filtered or unexported fields
}
func NewFileIter ¶
func NewFileIter(r *Repository, t *Tree) *FileIter
type Object ¶
type Object interface { ID() core.Hash Type() core.ObjectType Decode(core.Object) error Encode(core.Object) error }
Object is a generic representation of any git object. It is implemented by Commit, Tree, Blob and Tag, and includes the functions that are common to them.
Object is returned when an object could of any type. It is frequently used with a type cast to acquire the specific type of object:
func process(obj Object) { switch o := obj.(type) { case *Commit: // o is a Commit case *Tree: // o is a Tree case *Blob: // o is a Blob case *Tag: // o is a Tag } }
This interface is intentionally different from core.Object, which is a lower level interface used by storage implementations to read and write objects.
type ObjectIter ¶
type ObjectIter struct { core.ObjectIter // contains filtered or unexported fields }
ObjectIter provides an iterator for a set of objects.
func NewObjectIter ¶
func NewObjectIter(r *Repository, iter core.ObjectIter) *ObjectIter
NewObjectIter returns a ObjectIter for the given repository and underlying object iterator.
func (*ObjectIter) ForEach ¶
func (iter *ObjectIter) ForEach(cb func(Object) error) error
ForEach call the cb function for each object contained on this iter until an error happens or the end of the iter is reached. If ErrStop is sent the iteration is stop but no error is returned. The iterator is closed.
func (*ObjectIter) Next ¶
func (iter *ObjectIter) Next() (Object, error)
Next moves the iterator to the next object and returns a pointer to it. If it has reached the end of the set it will return io.EOF.
type PullOptions ¶
type PullOptions struct { // Name of the remote to be pulled RemoteName string // Remote branch to clone ReferenceName core.ReferenceName // Fetch only ReferenceName if true SingleBranch bool // Limit fetching to the specified number of commits Depth int }
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 Remote ¶
type Remote struct {
// contains filtered or unexported fields
}
Remote represents a connection to a remote repository
func (*Remote) Capabilities ¶
func (r *Remote) Capabilities() *packp.Capabilities
Capabilities returns the remote capabilities
func (*Remote) Disconnect ¶
Disconnect from the remote and save the config
func (*Remote) Fetch ¶
func (r *Remote) Fetch(o *FetchOptions) (err error)
Fetch returns a reader using the request
func (*Remote) Info ¶
func (r *Remote) Info() *common.GitUploadPackInfo
Info returns the git-upload-pack info
type Repository ¶
type Repository struct {
// 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 Storage) (*Repository, error)
NewRepository creates a new repository with the given Storage
func (*Repository) Blob ¶
func (r *Repository) Blob(h core.Hash) (*Blob, error)
Blob returns the blob with the given hash
func (*Repository) Blobs ¶
func (r *Repository) Blobs() (*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 ¶
func (r *Repository) Commit(h core.Hash) (*Commit, error)
Commit return the commit with the given hash
func (*Repository) Commits ¶
func (r *Repository) Commits() (*CommitIter, error)
Commits decode the objects into commits
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) Head ¶
func (r *Repository) Head() (*core.Reference, error)
Head returns the reference where HEAD is pointing
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 core.ObjectType, h core.Hash) (Object, error)
Object returns an object with the given hash.
func (*Repository) Objects ¶
func (r *Repository) Objects() (*ObjectIter, error)
Objects returns an 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
func (*Repository) Ref ¶
func (r *Repository) Ref(name core.ReferenceName, resolved bool) (*core.Reference, error)
Ref returns the Hash pointing the given refName
func (*Repository) Refs ¶
func (r *Repository) Refs() (core.ReferenceIter, error)
Refs returns a map with all the 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 ¶
func (r *Repository) Tag(h core.Hash) (*Tag, error)
Tag returns a tag with the given hash.
func (*Repository) Tags ¶
func (r *Repository) Tags() (*TagIter, error)
Tags returns a TagIter that can step through all of the annotated tags in the repository.
func (*Repository) Tree ¶
func (r *Repository) Tree(h core.Hash) (*Tree, error)
Tree return the tree with the given hash
func (*Repository) Trees ¶
func (r *Repository) Trees() (*TreeIter, error)
Trees decodes the objects into trees
type Signature ¶
Signature represents an action signed by a person
type Storage ¶
type Storage interface { ConfigStorage() config.ConfigStorage ObjectStorage() core.ObjectStorage ReferenceStorage() core.ReferenceStorage }
Storage storage of objects and references
type Tag ¶
type Tag struct { Hash core.Hash Name string Tagger Signature Message string TargetType core.ObjectType Target core.Hash // contains filtered or unexported fields }
Tag represents an annotated tag object. It points to a single git object of any type, but tags typically are applied to commit or blob objects. It provides a reference that associates the target with a tag name. It also contains meta-information about the tag, including the tagger, tag date and message.
https://git-scm.com/book/en/v2/Git-Internals-Git-References#Tags
func (*Tag) Blob ¶
Blob returns the blob pointed to by the tag. If the tag points to a different type of object ErrUnsupportedObject will be returned.
func (*Tag) Commit ¶
Commit returns the commit pointed to by the tag. If the tag points to a different type of object ErrUnsupportedObject will be returned.
func (*Tag) ID ¶
ID returns the object ID of the tag, not the object that the tag references. The returned value will always match the current value of Tag.Hash.
ID is present to fulfill the Object interface.
func (*Tag) String ¶
String returns the meta information contained in the tag as a formatted string.
func (*Tag) Tree ¶
Tree returns the tree pointed to by the tag. If the tag points to a commit object the tree of that commit will be returned. If the tag does not point to a commit or tree object ErrUnsupportedObject will be returned.
func (*Tag) Type ¶
func (t *Tag) Type() core.ObjectType
Type returns the type of object. It always returns core.TagObject.
Type is present to fulfill the Object interface.
type TagIter ¶
type TagIter struct { core.ObjectIter // contains filtered or unexported fields }
TagIter provides an iterator for a set of tags.
func NewTagIter ¶
func NewTagIter(r *Repository, iter core.ObjectIter) *TagIter
NewTagIter returns a TagIter for the given repository and underlying object iterator.
The returned TagIter will automatically skip over non-tag objects.
type Tree ¶
Tree is basically like a directory - it references a bunch of other trees and/or blobs (i.e. files and sub-directories)
func (*Tree) File ¶
File returns the hash of the file identified by the `path` argument. The path is interpreted as relative to the tree receiver.
func (*Tree) ID ¶
ID returns the object ID of the tree. The returned value will always match the current value of Tree.Hash.
ID is present to fulfill the Object interface.
func (*Tree) Type ¶
func (t *Tree) Type() core.ObjectType
Type returns the type of object. It always returns core.TreeObject.
type TreeIter ¶
type TreeIter struct { core.ObjectIter // contains filtered or unexported fields }
TreeIter provides an iterator for a set of trees.
func NewTreeIter ¶
func NewTreeIter(r *Repository, iter core.ObjectIter) *TreeIter
NewTreeIter returns a TreeIter for the given repository and underlying object iterator.
The returned TreeIter will automatically skip over non-tree objects.
type TreeWalker ¶
type TreeWalker struct {
// contains filtered or unexported fields
}
TreeWalker provides a means of walking through all of the entries in a Tree.
func NewTreeWalker ¶
func NewTreeWalker(r *Repository, t *Tree, recursive bool) *TreeWalker
NewTreeWalker returns a new TreeWalker for the given repository and tree.
It is the caller's responsibility to call Close() when finished with the tree walker.
func (*TreeWalker) Close ¶
func (w *TreeWalker) Close()
Close releases any resources used by the TreeWalker.
func (*TreeWalker) Next ¶
func (w *TreeWalker) Next() (name string, entry TreeEntry, err error)
Next returns the next object from the tree. Objects are returned in order and subtrees are included. After the last object has been returned further calls to Next() will return io.EOF.
In the current implementation any objects which cannot be found in the underlying repository will be skipped automatically. It is possible that this may change in future versions.
func (*TreeWalker) Tree ¶
func (w *TreeWalker) Tree() *Tree
Tree returns the tree that the tree walker most recently operated on.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package clients includes the implementation for diferent transport protocols go-git needs the packfile and the refs of the repo.
|
Package clients includes the implementation for diferent transport protocols go-git needs the packfile and the refs of the repo. |
common
Package common contains interfaces and non-specific protocol entities
|
Package common contains interfaces and non-specific protocol entities |
http
Package http implements a HTTP client for go-git.
|
Package http implements a HTTP client for go-git. |
ssh
Package ssh implements a ssh client for go-git.
|
Package ssh implements a ssh client for go-git. |
Package config storage is the implementation of git config for go-git
|
Package config storage is the implementation of git config for go-git |
Package core implement the core interfaces and structs used by go-git
|
Package core implement the core interfaces and structs used by go-git |
+build ignore +build ignore +build ignore +build ignore +build ignore +build ignore +build ignore +build ignore +build ignore
|
+build ignore +build ignore +build ignore +build ignore +build ignore +build ignore +build ignore +build ignore +build ignore |
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. |
examples
|
|
formats
|
|
config
Package config implements decoding, encoding and manipulation of git config files.
|
Package config implements decoding, encoding and manipulation of git config files. |
idxfile
Package idxfile implements a encoder/decoder of idx files
|
Package idxfile implements a encoder/decoder of idx files |
index
Package index implements a encoder/decoder of index format files
|
Package index implements a encoder/decoder of index format files |
packfile
Package packfile implements a encoder/decoder of packfile format
|
Package packfile implements a encoder/decoder of packfile format |
packp/advrefs
Package advrefs implements encoding and decoding advertised-refs messages from a git-upload-pack command.
|
Package advrefs implements encoding and decoding advertised-refs messages from a git-upload-pack command. |
packp/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. |
packp/ulreq
Package ulreq implements encoding and decoding upload-request messages from a git-upload-pack command.
|
Package ulreq implements encoding and decoding upload-request messages from a git-upload-pack command. |
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 |
fs
Package fs interace and implementations used by storage/filesystem
|
Package fs interace and implementations used by storage/filesystem |