src-d / go-git

Project has been moved to: https://github.com/go-git/go-git
https://github.com/go-git/go-git
Apache License 2.0
4.91k stars 542 forks source link

Question: Use in mem file (*chroot.file) as normal file #1284

Closed pkaramol closed 4 years ago

pkaramol commented 4 years ago

I am using go-git to checkout a repository, and then open a file as in:

    fs := memfs.New()

    repo, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
        URL:           "git@github.com:pkaramol/somerepo.git",
        ReferenceName: "refs/heads/somebranch",
        Depth:         1,
        Auth:          auth,
    })

    if err != nil {
        log.Fatalf(err.Error())
    }

    file, err := fs.Open("somedir/somefile.yaml")

    if err != nil {
        log.Fatalf(err.Error())
    }

    fmt.Println(repo)
    fmt.Printf("%T\n", file)

The last print informs me that what I get by opening an in-memory file is *chroot.file

My question is whether (and if yes, how) can I manipulate this type so as to achieve the same results, had it been a os.File.

e.g. given that in my specific example I am opening a .yaml file, can I use the normal marshalling process etc?

pkaramol commented 4 years ago

*chroot.file (which I confuse with billy.File) implements the io.Reader interface so the rest is easy to figure out. Sorry about this.