go-git 是一个Go语言实现的高度可扩展的Git实现库。可以使用友好的API来管理Git的仓库。支持不同类型的存储,包括内存文件系统,也可以通过接口Storer实现对存储的扩展。
该项目从2015年开始开发。项目旨在兼容git,所有的操作实现与git完全一样。两者的兼容比较请阅读 compatibilitydocumentation.
基本示例一个实现gitclone的最基本示例:
//ClonethegivenrepositorytothegivendirectoryInfo("gitclonehttps://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)输出结果:
Countingobjects:4924,done.Compressingobjects:100%(1333/1333),done.Total4924(delta530),reused6(delta6),pack-reused3533实现内存存储的示例将git仓库克隆到内存中,并打印HEAD的历史记录,类似gitlog:
//Clonesthegivenrepositoryinmemory,creatingtheremote,thelocal//branchesandfetchingtheobjects,exactlyas:Info("gitclonehttps://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)//GetstheHEADhistoryfromHEAD,justlikethiscommand:Info("gitlog")//...retrievesthebranchpointedbyHEADref,err:=r.Head()CheckIfError(err)//...retrievesthecommithistorycIter,err:=r.Log(&git.LogOptions{From:ref.Hash()})CheckIfError(err)//...justiteratesoverthecommits,printingiterr=cIter.ForEach(func(c*object.Commit)error{fmt.Println(c)returnnil})CheckIfError(err)输出结果:
commitded8054fd0c3994453e9c8aacaf48d118d42991eAuthor:SantiagoM.Mola<santi@mola.io>Date:SatNov1221:18:412016+0100index:ReadFrom/WriteToreturnsIndexReadError/IndexWriteError.(#9)commitdf707095626f384ce2dc1a83b30f9a21d69b9dfcAuthor:SantiagoM.Mola<santi@mola.io>Date:FriNov1113:23:222016+0100readwriter:fixbugwhenwritingindex.(#10)WhenusingReadWriteronanexistingsivafile,absoluteoffsetforindexentrieswasnotbeingcalculatedcorrectly....更多的示例请看 examples.
评论