git

package module
v0.0.0-...-87e0e4a Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2019 License: Apache-2.0 Imports: 25 Imported by: 0

README

GoDoc License Build Status

go-git

Pure-Go implementation of Git as a library.

Origins

This is a fork of gogits/git (which was originally a fork of speedata/gogit). Unfortunately, gogits/git is no longer maintained and is not accepting PRs, so this is a new extension of that work.

shazow/go-git is not backwards-compatible with gogits/git.

Goals

Short term

go-git's API should not be considered stable until these goals have been achieved.

  • Remove features that depend on shelling out to git.
  • Remove redundant features (in progress).
  • Add key features to support implementing sourcegraph/go-vcs's Repository interface. (See shazow/go-vcs#1)
    • Walk tree filesystem
    • Walk commit ancestry
    • Diff
    • Blame
    • Subrepositories
    • Search
Long term
  • Add a storage driver interface for repositories with virtual/in-memory repository support.
  • Improve test coverage.
  • Improve query performance with caching and bitmap indexes.
  • Rework use of locks (probably get rid of them, require consumer to manage repository locking).

Sponsors

Work on this fork is sponsored by Sourcegraph.

License

  • shazow/go-git changes are released under Apache v2.
  • gogits/git (major rewrite of original MIT code) is licensed under Apache v2.
  • speedata/gogit is licensed under MIT.

Documentation

Index

Constants

View Source
const (
	ObjectCommit ObjectType = 0x10
	ObjectTree              = 0x20
	ObjectBlob              = 0x30
	ObjectTag               = 0x40
)

Variables

View Source
var (
	ErrBranchExisted = errors.New("branch has existed")
)
View Source
var ErrDisjoint = errors.New("commit trees are disjoint")
View Source
var ErrNoPackedRefs = errors.New("no packed-refs")
View Source
var ErrNotExist = os.ErrNotExist
View Source
var IDNotExist = errors.New("object id not exist")
View Source
var (
	SkipCommit = errors.New("skip this commit's ancestry")
)
View Source
var (
	SkipDir = errors.New("skip this directory")
)
View Source
var TreeEntryRe = regexp.MustCompile("^([0-9]+) ([^\x00]+)\x00")

Functions

func CreateBranch

func CreateBranch(repoPath, branchName, id string) error

func CreateRef

func CreateRef(head, repoPath, branchName, id string) error

func CreateTag

func CreateTag(repoPath, tagName, id string) error

func IsBranchExist

func IsBranchExist(repoPath, branchName string) bool

func IsObjectIDHex

func IsObjectIDHex(s string) bool

func ParseModeType

func ParseModeType(modeString string) (EntryMode, ObjectType, error)

func PrependObjectHeader

func PrependObjectHeader(objectType ObjectType, r io.ReadSeeker) (io.Reader, error)

Add '<type> <size>\x00' to beginning of `r`.

func ScanTreeEntry

func ScanTreeEntry(
	data []byte,
	atEOF bool,
) (
	advance int, token []byte, err error,
)

Types

type ArchiveType

type ArchiveType int
const (
	AT_ZIP ArchiveType = iota + 1
	AT_TARGZ
)

type Blob

type Blob struct {
	*TreeEntry
}

func (*Blob) Data

func (b *Blob) Data() ([]byte, error)

type Commit

type Commit struct {
	Tree
	Id            ObjectID // The id of this commit object
	Author        *Signature
	Committer     *Signature
	CommitMessage string
	// contains filtered or unexported fields
}

Commit represents a git commit.

func (*Commit) BehindAhead

func (c *Commit) BehindAhead(commitId string) (behind int, ahead int, treeErr error)

BehindAhead computes the number of commits are behind (missing) and ahead (extra) of commitId. ErrDisjoint is returned when the two commits don't have a common ancestor, and the length of the tree depth for each.

BehindAhead will traverse the entire ancestry of this commit, then traversing the ancestroy of the target commitId until a common ancestor is found.

TODO: Use a bitmap index for reachability (https://github.com/shazow/go-git/issues/4)

func (*Commit) CreateArchive

func (c *Commit) CreateArchive(path string, archiveType ArchiveType) error

func (*Commit) IsAncestor

func (c *Commit) IsAncestor(commitId string) bool

IsAncestor returns whether commitId is an ancestor of this commit. False if it's the same commit. Similar to `git merge-base --is-ancestor`.

IsAncestor will traverse the ancestry of the current commit until it finds the target commitIt.

TODO: Use a bitmap index for reachability (https://github.com/shazow/go-git/issues/4)

func (*Commit) Message

func (c *Commit) Message() string

Return the commit message. Same as retrieving CommitMessage directly.

func (*Commit) Parent

func (c *Commit) Parent(n int) (*Commit, error)

Return parent number n (0-based index)

func (*Commit) ParentCount

func (c *Commit) ParentCount() int

Return the number of parents of the commit. 0 if this is the root commit, otherwise 1,2,...

func (*Commit) ParentId

func (c *Commit) ParentId(n int) (id ObjectID, err error)

Return oid of the parent number n (0-based index). Return nil if no such parent exists.

func (*Commit) ParentIds

func (c *Commit) ParentIds() []ObjectID

Return the parent ids.

func (*Commit) Summary

func (c *Commit) Summary() string

func (*Commit) TreeId

func (c *Commit) TreeId() ObjectID

Return oid of the (root) tree of this commit.

func (*Commit) Walk

func (c *Commit) Walk(walkFn CommitWalkFunc) error

Commit and its' parents will be traversed iteratively in depth-first order, walkFn called on each commit.

type CommitWalkFunc

type CommitWalkFunc func(path []*Commit, current *Commit, err error) error

CommitWalkFunc is similar to path/filepath.WalkFunc, it will traverse all of a commit's ancestry as long as the returned error is nil. If SkipCommit is returned, then it will bypass that commit's subtree.

type Entries

type Entries []*TreeEntry

func (Entries) Len

func (bs Entries) Len() int

func (Entries) Less

func (bs Entries) Less(i, j int) bool

func (Entries) Sort

func (bs Entries) Sort()

func (Entries) Swap

func (bs Entries) Swap(i, j int)

type EntryMode

type EntryMode int
const (
	ModeBlob    EntryMode = 0100644
	ModeExec    EntryMode = 0100755
	ModeSymlink EntryMode = 0120000
	ModeCommit  EntryMode = 0160000
	ModeTree    EntryMode = 0040000
)

There are only a few file modes in Git. They look like unix file modes, but they can only be one of these.

type Object

type Object struct {
	Type ObjectType
	Size uint64
	Data []byte
}

type ObjectID

type ObjectID string

func ObjectIDHex

func ObjectIDHex(s string) ObjectID

func StoreObjectSHA

func StoreObjectSHA(
	objectType ObjectType,
	w io.Writer,
	r io.ReadSeeker,
) (ObjectID, error)

Write an the object contents in `r` in compressed form to `w` and return the hash. Special case: if `w` is `ioutil.Discard`, the data is not compressed, since the data is not consumed anywhere.

func (ObjectID) String

func (id ObjectID) String() string

type ObjectNotFound

type ObjectNotFound ObjectID

ObjectNotFound error returned when a repo query is performed for an ID that does not exist.

func (ObjectNotFound) Error

func (id ObjectNotFound) Error() string

type ObjectType

type ObjectType int

func (ObjectType) String

func (t ObjectType) String() string

type RefNotFound

type RefNotFound string

RefNotFound error returned when a commit is fetched by ref that is not found.

func (RefNotFound) Error

func (err RefNotFound) Error() string

type Repository

type Repository struct {
	Path string
	// contains filtered or unexported fields
}

func OpenRepository

func OpenRepository(path string) (*Repository, error)

func (*Repository) Close

func (r *Repository) Close() (err error)

func (*Repository) CommitsBefore

func (repo *Repository) CommitsBefore(commitId string) (*list.List, error)

func (*Repository) CommitsBetween

func (repo *Repository) CommitsBetween(last *Commit, before *Commit) (*list.List, error)

used only for single tree, (]

func (*Repository) CreateBranch

func (repo *Repository) CreateBranch(branchName, idStr string) error

func (*Repository) CreateTag

func (repo *Repository) CreateTag(tagName, idStr string) error

func (*Repository) GetBranches

func (repo *Repository) GetBranches() ([]string, error)

func (*Repository) GetCommit

func (repo *Repository) GetCommit(commitId string) (*Commit, error)

Find the commit object in the repository.

func (*Repository) GetCommitIdOfBranch

func (repo *Repository) GetCommitIdOfBranch(branchName string) (string, error)

func (*Repository) GetCommitIdOfRef

func (repo *Repository) GetCommitIdOfRef(refpath string) (string, error)

func (*Repository) GetCommitIdOfTag

func (repo *Repository) GetCommitIdOfTag(tagName string) (string, error)

func (*Repository) GetCommitOfBranch

func (repo *Repository) GetCommitOfBranch(branchName string) (*Commit, error)

get branch's last commit or a special commit by id string

func (*Repository) GetCommitOfTag

func (repo *Repository) GetCommitOfTag(tagName string) (*Commit, error)

func (*Repository) GetModules

func (repo *Repository) GetModules() ([]string, error)

GetModules returns the submodule names that are initialized and cached within .git/modules

func (*Repository) GetTag

func (repo *Repository) GetTag(tagName string) (*Tag, error)

func (*Repository) GetTags

func (repo *Repository) GetTags() ([]string, error)

GetTags returns all tags of given repository.

func (*Repository) GetTree

func (repo *Repository) GetTree(idStr string) (*Tree, error)

func (*Repository) IsBranchExist

func (repo *Repository) IsBranchExist(branchName string) bool

func (*Repository) IsTagExist

func (repo *Repository) IsTagExist(tagName string) bool

func (*Repository) Object

func (repo *Repository) Object(id ObjectID) (*Object, error)

func (*Repository) StoreObjectLoose

func (repo *Repository) StoreObjectLoose(
	objectType ObjectType,
	r io.ReadSeeker,
) (ObjectID, error)

Write an object into git's loose database. If the object already exists in the database, it is not overwritten, though the compression is still performed.

func (*Repository) TagPath

func (repo *Repository) TagPath(tagName string) string

type Signature

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

Author and Committer information

type Submodule

type Submodule struct {
	Name string
	Path string
	URL  string
}

Submodule

type Tag

type Tag struct {
	Name string
	Id   ObjectID

	Object     ObjectID // The id of this commit object
	Type       string
	Tagger     *Signature
	TagMessage string
	// contains filtered or unexported fields
}

Tag

func (*Tag) Commit

func (tag *Tag) Commit() (*Commit, error)

type Tree

type Tree struct {
	Id ObjectID
	// contains filtered or unexported fields
}

A tree is a flat directory listing.

func NewTree

func NewTree(repo *Repository, id ObjectID) *Tree

func (*Tree) GetBlobByPath

func (t *Tree) GetBlobByPath(rpath string) (*Blob, error)

func (*Tree) GetSubmodules

func (t *Tree) GetSubmodules() ([]*Submodule, error)

GetSubmodules returns the submodules in this commit tree (parsed from .gitmodules)

func (*Tree) GetTreeEntryByPath

func (t *Tree) GetTreeEntryByPath(rpath string) (*TreeEntry, error)

func (*Tree) ListEntries

func (t *Tree) ListEntries() (Entries, error)

func (*Tree) Scanner

func (t *Tree) Scanner() (*TreeScanner, error)

func (*Tree) SubTree

func (t *Tree) SubTree(rpath string) (*Tree, error)

func (*Tree) Walk

func (t *Tree) Walk(walkFn TreeWalkFunc) error

The tree's directory heirarchy will be traversed recursively in breadth-first order, walkFn will be called once for each entry.

type TreeEntry

type TreeEntry struct {
	Id   ObjectID
	Type ObjectType
	// contains filtered or unexported fields
}

func (*TreeEntry) Blob

func (te *TreeEntry) Blob() *Blob

func (*TreeEntry) EntryMode

func (te *TreeEntry) EntryMode() EntryMode

func (*TreeEntry) IsDir

func (te *TreeEntry) IsDir() bool

func (*TreeEntry) ModTime

func (te *TreeEntry) ModTime() time.Time

func (*TreeEntry) Mode

func (te *TreeEntry) Mode() (mode os.FileMode)

func (*TreeEntry) Name

func (te *TreeEntry) Name() string

func (*TreeEntry) Size

func (te *TreeEntry) Size() int64

func (*TreeEntry) Sys

func (te *TreeEntry) Sys() interface{}

func (*TreeEntry) Tree

func (te *TreeEntry) Tree() *Tree

type TreeScanner

type TreeScanner struct {
	*bufio.Scanner
	// contains filtered or unexported fields
}

func NewTreeScanner

func NewTreeScanner(parent *Tree, r io.Reader) *TreeScanner

func (*TreeScanner) Err

func (t *TreeScanner) Err() error

func (*TreeScanner) Scan

func (t *TreeScanner) Scan() bool

func (*TreeScanner) TreeEntry

func (t *TreeScanner) TreeEntry() *TreeEntry

type TreeWalkFunc

type TreeWalkFunc func(path string, te *TreeEntry, err error) error

TreeWalkFunc is similar to path/filepath.WalkFunc, it will continue as long as the returned error is nil. If SkipDir is returned, then that subtree will be skipped.

Jump to

Keyboard shortcuts

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