go4git

package module
v0.0.0-...-8476655 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2016 License: MIT Imports: 17 Imported by: 0

README ¶

go4git

Git library in pure GO (Under construction)

import "github.com/ashaniray/go4git"

💀

Usage

Open an existing repository.
repo := go4git.NewRepository("path/to/my/repository")
Initialize a new empty repository
go4git.InitAt("/path/to/repo", false)
Initialize a new bare repository
go4git.InitAt("/path/to/repo.git", true)
Accessing a Repository
// Returns `true` if the given SHA1 exist in this repository
repo.Exists("07b44cbda23b726e5d54e2ef383495922c024202")
Query repository state
repo.IsBare()
repo.IsEmpty()
repo.IsHeadUnborn()
repo.IsHeadDetached()
Path accessors
repo.Path()
repo.Workdir()
The HEAD of the repository.
ref := repo.Head()
Properties of ref
ref.Name()
ref.Target()
Reading an object
obj := repo.Read("a0ae5566e3c8a3bddffab21022056f0b5e03ef07")
obj.Length()
obj.Data()
obj.Type()
Writing to a Repository
sha := repo.Write([]byte("some content."), go4git.Blob)
Commit Objects
commit := repo.LookupCommit('a0ae5566e3c8a3bddffab21022056f0b5e03ef07')

commit.Message()
commit.Time()
commit.Author()
commit.Tree()
commit.Parents()
Tag Objects
tag, err:= repo.LookupTag('a0ae5566e3c8a3bddffab21022056f0b5e03ef07')

tag.Target()
tag.Target().Oid
tag.Target().Type // can be one of go4git.Commit, go4git.Tag, go4git.Blob
tag.Name()
tag.Message()
tag.Tagger()
Tree Objects
tree, err:= repo.LookupTree('779fbb1e17e666832773a9825875300ea736c2da')

tree.Count()
tree[0]
Blob Objects
blob := repo.LookupBlob('e1253910439ea902cf49be8a9f02f3c08d89ac73')
blob.Content() // Gives the content of the blob.
Manipulating git index
index := go4git.NewIndex(path)


index.Reload()          // Reload the index file from disk.
count = index.Count()   // Get the count of index entries.
index.Entries()         // Get the collection of index entries.

// Iterating over index entries.
for i := range index {
  fmt.Println(i)
}

index.Entry(path)         // Get a particular entry in the index.
index.Remove(path)        // Remove from staging
index.Add(entry)          // Add to staging. Also updates existing entry if there is one.
index.AddFromPath(path)   // Add to staging. Creates entry from file in path, updates the index.

References
ref := repo.Reference("refs/heads/master")

ref.Target().Id // SHA1 hash
ref.Type()      // go4git.Direct
ref.Name()      // "refs/heads/master"

// Iterate over all references:

refs := repo.References()

for ref := range refs {
  fmt.Println(ref)
}



// Iterate only over references that match the given pattern (glob):

refs := repo.ReferencesByGlob("refs/tags/*")

for ref := range refs {
  fmt.Println(ref)
}
Create, update, rename or delete a reference
ref := repo.CreateReference("refs/heads/unit_test", some_commit_sha)

repo.UpdateReference(ref, newSha)
repo.UpdateReferenceByName("refs/heads/unit_test", newSha)

repo.RenameReference(ref, "refs/heads/blead") 
repo.RenameReferenceByName("refs/heads/unit_test", "refs/heads/blead")

repo.DeleteReference(ref)
repo.DeleteReferenceByName("refs/heads/unit_test")
Access the reflog for any branch:
ref      := repo.Reference("refs/heads/master")
reflog   := ref.Log()
entry    := reflog[0]      // Get the first entry

entry.OldId
entry.NewId
entry.Message
entry.Committer
Branches
Iterate over all branches:
branches := repo.LocalBranches()

for branch := range branches {       // ["master"]
  fmt.Println(branch.Name())
}

branches := repo.RemoteBranches()

for branch := range branches {       // ["origin/HEAD", "origin/master", "origin/packed"]
  fmt.Println(branch.Name())
}
Look up branches and get attributes
branch = repo.Branch("master")
branch.Name()           // "master"
branch.CanonicalName()  // "refs/heads/master"
Look up the id for the target of a branch:

branch.Target().Id // "36060c58702ed4c2a40832c51758d5344201d89a"

Create and delete branch
branch = repo.CreateBranch("test_branch", "HEAD")

repo.branches.RenameBranchByName("test_branch", "new_branch")
repo.RenameBranchByCName("refs/heads/test_branch", "new_branch")
repo.RenameBranch(ref, "new_branch")

repo.DeleteBranchByName("test_branch")
repo.DeleteBranchByCName("refs/heads/test_branch")
repo.DeleteBranch(ref)

Documentation ¶

Index ¶

Constants ¶

View Source
const (
	COMMIT
	TREE
	BLOB
	TAG

	OFS_DELTA
	REF_DELTA
)

Variables ¶

View Source
var BLOB_DATA = []byte{}/* 114 elements not displayed */
View Source
var COMMIT_DATA = []byte{}/* 232 elements not displayed */
View Source
var COMMIT_DATA_WP = []byte{}/* 328 elements not displayed */
View Source
var PACK_DATA = []byte{}/* 9266 elements not displayed */
View Source
var PACK_IDX_DATA = []byte{}/* 3172 elements not displayed */
View Source
var TAG_DATA = []byte{}/* 232 elements not displayed */
View Source
var TREE_DATA = []byte{}/* 136 elements not displayed */

Functions ¶

func Byte2String ¶

func Byte2String(b []byte) string

func GenSHA1 ¶

func GenSHA1(in io.Reader, objType string) ([]byte, error)

func GetArgInputFile ¶

func GetArgInputFile() (*os.File, error)

func GetTotalCount ¶

func GetTotalCount(in io.ReadSeeker) (uint, error)

func InitAt ¶

func InitAt(root string, bare bool) error

func ReadObjectType ¶

func ReadObjectType(in *os.File) (int, string, error)

func ReadUint32 ¶

func ReadUint32(in io.Reader) (uint32, error)

func SplitTwoAndTrim ¶

func SplitTwoAndTrim(s string, sep string) (string, string)

func Unzlib ¶

func Unzlib(in io.Reader, out io.Writer) error

func UnzlibToBuffer ¶

func UnzlibToBuffer(in io.Reader) ([]byte, error)

func Zlib ¶

func Zlib(in io.Reader, out io.Writer) error

Types ¶

type ByOffset ¶

type ByOffset []PackIndex

func (ByOffset) Len ¶

func (a ByOffset) Len() int

func (ByOffset) Less ¶

func (a ByOffset) Less(i, j int) bool

func (ByOffset) Swap ¶

func (a ByOffset) Swap(i, j int)

type Commit ¶

type Commit struct {
	// contains filtered or unexported fields
}

func ParseCommit ¶

func ParseCommit(in io.Reader) (*Commit, error)

func (*Commit) Author ¶

func (c *Commit) Author() *Person

func (*Commit) Committer ¶

func (c *Commit) Committer() *Person

func (*Commit) HasParent ¶

func (c *Commit) HasParent() bool

func (*Commit) Id ¶

func (c *Commit) Id() string

func (*Commit) Message ¶

func (c *Commit) Message() string

func (*Commit) Parent ¶

func (c *Commit) Parent() string

func (*Commit) String ¶

func (c *Commit) String() string

func (*Commit) Tree ¶

func (c *Commit) Tree() string

type CommitFields ¶

type CommitFields map[string]string

func (CommitFields) ToCommit ¶

func (cf CommitFields) ToCommit() *Commit

type Index ¶

type Index struct {
	Header  IndexHeader
	Entries []IndexEntry
}

func ParseIndex ¶

func ParseIndex(in io.ReadSeeker) (Index, error)

func (Index) String ¶

func (idx Index) String() string

type IndexEntry ¶

type IndexEntry struct {
	CtimeSecs      uint32
	CtimeNanoSecs  uint32
	MtimeSecs      uint32
	MtimeNanoSecs  uint32
	Dev            uint32
	Ino            uint32
	ObjectType     int
	UnixPermission int
	Uid            uint32
	Gid            uint32
	FileSize       int
	Hash           []byte
	Flags          []byte
	AdditionalFlag []byte
	EntryPathName  string
	Padding        []byte
}

func (IndexEntry) String ¶

func (e IndexEntry) String() string

type IndexHeader ¶

type IndexHeader struct {
	Signature    string
	Version      uint32
	CountEntries int
}

func (IndexHeader) String ¶

func (hdr IndexHeader) String() string

type Object ¶

type Object struct {
	// contains filtered or unexported fields
}

type ObjectType ¶

type ObjectType int

func (ObjectType) String ¶

func (t ObjectType) String() string

type PackIndex ¶

type PackIndex struct {
	Hash   []byte
	CRC    []byte
	Offset int
}

func GetAllPackedIndex ¶

func GetAllPackedIndex(in io.ReadSeeker) ([]PackIndex, error)

func GetObjectForHash ¶

func GetObjectForHash(hash string, in io.ReadSeeker) (PackIndex, error)

func ReadPackIndexAt ¶

func ReadPackIndexAt(indexAt int, in io.ReadSeeker) (PackIndex, error)

func (PackIndex) CRCAsString ¶

func (idx PackIndex) CRCAsString() string

func (PackIndex) HashAsString ¶

func (idx PackIndex) HashAsString() string

func (PackIndex) String ¶

func (idx PackIndex) String() string

type PackedObject ¶

type PackedObject struct {
	Type        ObjectType
	Data        []byte
	HashOfRef   []byte
	RefOffset   int64
	Size        int64
	StartOffset int64
	DeltaData   []byte
	ActualType  ObjectType
	Hash        []byte
	RefLevel    int
	BaseHash    []byte
}

func ReadPackedObjectAtOffset ¶

func ReadPackedObjectAtOffset(offset int64, in io.ReadSeeker, inIndex io.ReadSeeker) (PackedObject, error)

func (PackedObject) String ¶

func (o PackedObject) String() string

type Person ¶

type Person struct {
	Name  string
	Email string
	Time  time.Time
}

func (*Person) String ¶

func (p *Person) String() string

type Reference ¶

type Reference struct {
	// contains filtered or unexported fields
}

func (Reference) HasLog ¶

func (r Reference) HasLog() bool

func (Reference) IsBranch ¶

func (r Reference) IsBranch() bool

func (Reference) IsRemote ¶

func (r Reference) IsRemote() bool

func (Reference) IsTag ¶

func (r Reference) IsTag() bool

func (Reference) Name ¶

func (r Reference) Name() string

func (Reference) String ¶

func (r Reference) String() string

func (Reference) Type ¶

func (r Reference) Type() string

type Repository ¶

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

func NewRepository ¶

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

func (*Repository) IsBare ¶

func (r *Repository) IsBare() bool

func (*Repository) LookupCommit ¶

func (r *Repository) LookupCommit(sha string) (*Commit, error)

func (*Repository) LooseObjPath ¶

func (r *Repository) LooseObjPath(sha string) string

func (*Repository) LooseObjects ¶

func (r *Repository) LooseObjects() ([]string, error)

func (*Repository) Path ¶

func (r *Repository) Path() string

func (*Repository) References ¶

func (r *Repository) References() ([]Reference, error)

func (*Repository) WorkDir ¶

func (r *Repository) WorkDir() string

type Tag ¶

type Tag struct {
	// contains filtered or unexported fields
}

func ParseTag ¶

func ParseTag(in io.Reader) (*Tag, error)

func (Tag) Message ¶

func (t Tag) Message() string

func (Tag) Name ¶

func (t Tag) Name() string

func (*Tag) String ¶

func (t *Tag) String() string

func (Tag) Tagger ¶

func (t Tag) Tagger() *Person

func (Tag) TargetId ¶

func (t Tag) TargetId() string

func (Tag) TargetType ¶

func (t Tag) TargetType() string

type TagFields ¶

type TagFields map[string]string

func (TagFields) ToTag ¶

func (tf TagFields) ToTag() *Tag

type Tree ¶

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

func ParseTree ¶

func ParseTree(in *os.File) (Tree, error)

func (Tree) String ¶

func (tree Tree) String() string

type TreeItem ¶

type TreeItem struct {
	// contains filtered or unexported fields
}

func (TreeItem) String ¶

func (item TreeItem) String() string

Directories ¶

Path Synopsis

Jump to

Keyboard shortcuts

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