Git library in pure GO (Under construction)
import "github.com/ashaniray/go4git"
repo := go4git.NewRepository("path/to/my/repository")
go4git.InitAt("/path/to/repo", false)
go4git.InitAt("/path/to/repo.git", true)
// Returns `true` if the given SHA1 exist in this repository
repo.Exists("07b44cbda23b726e5d54e2ef383495922c024202")
repo.IsBare()
repo.IsEmpty()
repo.IsHeadUnborn()
repo.IsHeadDetached()
repo.Path()
repo.Workdir()
ref := repo.Head()
ref.Name()
ref.Target()
obj := repo.Read("a0ae5566e3c8a3bddffab21022056f0b5e03ef07")
obj.Length()
obj.Data()
obj.Type()
sha := repo.Write([]byte("some content."), go4git.Blob)
commit := repo.LookupCommit('a0ae5566e3c8a3bddffab21022056f0b5e03ef07')
commit.Message()
commit.Time()
commit.Author()
commit.Tree()
commit.Parents()
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, err:= repo.LookupTree('779fbb1e17e666832773a9825875300ea736c2da')
tree.Count()
tree[0]
blob := repo.LookupBlob('e1253910439ea902cf49be8a9f02f3c08d89ac73')
blob.Content() // Gives the content of the blob.
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.
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)
}
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")
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 := 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())
}
branch = repo.Branch("master")
branch.Name() // "master"
branch.CanonicalName() // "refs/heads/master"
branch.Target().Id // "36060c58702ed4c2a40832c51758d5344201d89a"
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)