go-gitGit 的 Go 語(yǔ)言實(shí)現(xiàn)
go-git 是一個(gè) Go 語(yǔ)言實(shí)現(xiàn)的高度可擴(kuò)展的 Git 實(shí)現(xiàn)庫(kù)。可以使用友好的 API 來(lái)管理 Git 的倉(cāng)庫(kù)。支持不同類型的存儲(chǔ),包括內(nèi)存文件系統(tǒng),也可以通過(guò)接口 Storer實(shí)現(xiàn)對(duì)存儲(chǔ)的擴(kuò)展。
該項(xiàng)目從 2015 年開(kāi)始開(kāi)發(fā)。項(xiàng)目旨在兼容 git ,所有的操作實(shí)現(xiàn)與git完全一樣。兩者的兼容比較請(qǐng)閱讀 compatibility documentation.
基本示例
一個(gè)實(shí)現(xiàn) git clone 的最基本示例:
// Clone the given repository to the given directory
Info("git clone https://github.com/src-d/go-git")
_, err := git.PlainClone("/tmp/foo", false, &git.CloneOptions{
URL: "https://github.com/src-d/go-git",
Progress: os.Stdout,
})
CheckIfError(err)
輸出結(jié)果:
Counting objects: 4924, done.
Compressing objects: 100% (1333/1333), done.
Total 4924 (delta 530), reused 6 (delta 6), pack-reused 3533
實(shí)現(xiàn)內(nèi)存存儲(chǔ)的示例
將 git 倉(cāng)庫(kù)克隆到內(nèi)存中,并打印 HEAD 的歷史記錄,類似 git log :
// Clones the given repository in memory, creating the remote, the local
// branches and fetching the objects, exactly as:
Info("git clone https://github.com/src-d/go-siva")
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: "https://github.com/src-d/go-siva",
})
CheckIfError(err)
// Gets the HEAD history from HEAD, just like this command:
Info("git log")
// ... retrieves the branch pointed by HEAD
ref, err := r.Head()
CheckIfError(err)
// ... retrieves the commit history
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
CheckIfError(err)
// ... just iterates over the commits, printing it
err = cIter.ForEach(func(c *object.Commit) error {
fmt.Println(c)
return nil
})
CheckIfError(err)
輸出結(jié)果:
commit ded8054fd0c3994453e9c8aacaf48d118d42991e
Author: Santiago M. Mola <[email protected]>
Date: Sat Nov 12 21:18:41 2016 +0100
index: ReadFrom/WriteTo returns IndexReadError/IndexWriteError. (#9)
commit df707095626f384ce2dc1a83b30f9a21d69b9dfc
Author: Santiago M. Mola <[email protected]>
Date: Fri Nov 11 13:23:22 2016 +0100
readwriter: fix bug when writing index. (#10)
When using ReadWriter on an existing siva file, absolute offset for
index entries was not being calculated correctly.
...
更多的示例請(qǐng)看 examples.
評(píng)論
圖片
表情
