使用 go-git 克隆存储库后读取文件

Read a file after cloning a repository with go-git

我希望能够对 go 进行 运行 次 git 操作。

我最近发现了 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"
})

我的问题是,假设我正在使用上述存储库的内存检出),我将如何从存储库中读入(我的 go 程序)文件?即假设文件

https://github.com/pkaramol/myrepo/someConfig.yaml

如果我只需要 这个特定文件,是否最好只执行 的 git 克隆(仍在内存中) 特定文件?

From the docs:

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.

限制下载的提交数量