go-git 是一个Go语言实现的高度可扩展的Git实现库。可以使用友好的API来管理Git的仓库。支持不同类型的存储,包括内存文件系统,也可以通过接口Storer实现对存储的扩展。
该项目从2015年开始开发。项目旨在兼容git,所有的操作实现与git完全一样。两者的兼容比较请阅读 compatibilitydocumetatio.
基本示例一个实现gitcloe的最基本示例:
//CloethegiverepositorytothegivedirectoryIfo("gitcloehttps://github.com/src-d/go-git")_,err:=git.PlaiCloe("/tmp/foo",false,&git.CloeOptios{URL:"https://github.com/src-d/go-git",Progress:os.Stdout,})CheckIfError(err)输出结果:
Coutigobjects:4924,doe.Compressigobjects:100%(1333/1333),doe.Total4924(delta530),reused6(delta6),pack-reused3533实现内存存储的示例将git仓库克隆到内存中,并打印HEAD的历史记录,类似gitlog:
//Cloesthegiverepositoryimemory,creatigtheremote,thelocal//brachesadfetchigtheobjects,exactlyas:Ifo("gitcloehttps://github.com/src-d/go-siva")r,err:=git.Cloe(memory.NewStorage(),il,&git.CloeOptios{URL:"https://github.com/src-d/go-siva",})CheckIfError(err)//GetstheHEADhistoryfromHEAD,justlikethiscommad:Ifo("gitlog")//...retrievesthebrachpoitedbyHEADref,err:=r.Head()CheckIfError(err)//...retrievesthecommithistorycIter,err:=r.Log(&git.LogOptios{From:ref.Hash()})CheckIfError(err)//...justiteratesoverthecommits,pritigiterr=cIter.ForEach(fuc(c*object.Commit)error{fmt.Pritl(c)returil})CheckIfError(err)输出结果:
commitded8054fd0c3994453e9c8aacaf48d118d42991eAuthor:SatiagoM.Mola<sati@mola.io>Date:SatNov1221:18:412016+0100idex:ReadFrom/WriteToretursIdexReadError/IdexWriteError.(#9)commitdf707095626f384ce2dc1a83b30f9a21d69b9dfcAuthor:SatiagoM.Mola<sati@mola.io>Date:FriNov1113:23:222016+0100readwriter:fixbugwhewritigidex.(#10)WheusigReadWriteroaexistigsivafile,absoluteoffsetforidexetrieswasotbeigcalculatedcorrectly....更多的示例请看 examples.










评论