简体   繁体   English

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

[英]Read a file after cloning a repository with go-git

I want to be able to do run-time git manipulations with go .我希望能够使用go进行运行时git操作。

I recently discovered the go-git package which comes in very handy to this end.我最近发现了go-git包,它为此非常方便。

I was also able to perform pull operations, more or less as follows:我也能够执行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"
})

My question is, assuming I am using in-memory checkout of the repo as above), how will I be able to read in (my go program) a file from the repository?我的问题是,假设我使用上述存储库的内存结帐),我将如何从存储库中读取(我的go程序)文件? ie assuming the file即假设文件

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

Would it be preferable (in case I need just this particular file) to perform a git clone (still in mem) of only the particular file?难道是最好(如果我需要这个特定的文件)到(仍处于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 .使用给定的选项将存储库克隆到给定的 Storer 和工作树文件系统中,如果工作树为零,则创建一个裸存储库

Don't pass a nil filesystem if you want access to the worktree.如果您想访问工作树,请不要传递 nil 文件系统。 Use something like gopkg.in/src-d/go-billy.v4/memfs.Memory :使用类似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")
}

You cannot clone a single file (that's not how git works), but you can limit the number of commits that are downloaded using CloneOptions.Depth .您不能克隆单个文件(这不是 git 的工作方式),但您可以使用CloneOptions.Depth限制下载的提交数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM