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

标签 git go go-git

我希望能够使用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中),会更好吗?

最佳答案

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下载的提交数量。

关于git - 使用go-git克隆存储库后读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60080923/

相关文章:

go - 如何使用环境变量 GODEBUG

go - 使用 config.toml 文件部署 Golang 应用程序(api)

git - 为什么我通过1970年的go-git提交内容?

git - 是否有可能 git diff 等同于 go-git

google-app-engine - 通过Regex Google数据存储区查询实体

git - 使用 go-git 检查分支是否已经被推送到远程

git - 在 Git 中提交时使用 emacsclient -t

linux - 使用 GIT 在本地服务器和实时服务器之间同步网站文件?

ios - 无法推送、 pull 或 merge git。 "Working copy has uncommited changes"

git删除了所有东西,如何恢复文件和文件夹