我希望能够使用git
进行运行时go
操纵。
我最近发现了 go-git
软件包,可以非常方便地完成此任务。
我也能够执行pull
操作,大致如下:
import {
git "gopkg.in/src-d/go-git.v4"
}
repo, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: "https://github.com/pkaramol/myrepo",
})
err := repo.Pull(&git.PullOptions{
RemoteName: "origin"
})
我的问题是,假设我正在使用上述存储库的内存中 checkout ),如何从存储库中读取文件(我的
go
程序)?即假设文件https://github.com/pkaramol/myrepo/someConfig.yaml
(如果我需要仅此特定文件)执行仅特定文件的git克隆(仍存储在mem中),会更好吗?
最佳答案
Clone a repository into the given Storer and worktree Filesystem with the given options, if worktree is nil a bare repository is created.
如果要访问工作树,请不要传递nil文件系统。使用类似gopkg.in/src-d/go-billy.v4/memfs.Memory的东西:
package main
import (
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/storage/memory"
"gopkg.in/src-d/go-billy.v4/memfs"
)
func main() {
fs := memfs.New()
repo, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
URL: "https://github.com/pkaramol/myrepo",
})
file, err := fs.Open("someConfig.yaml")
}
您无法克隆单个文件(这不是git的工作原理),但是您可以限制使用CloneOptions.Depth下载的提交数量。
关于git - 使用go-git克隆存储库后读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60080923/