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 541 forks source link

Is it possible to clone from an in-memory filesystem? #1236

Open hairyhenderson opened 4 years ago

hairyhenderson commented 4 years ago

I'm wondering there's any way to clone from a repo stored in an in-memory filesystem?

I have a use-case where I need to clone a repo from a filesystem into memory so that I can work with it there. I can't read directly from the actual on-disk repo because I don't want to pick up local changes or affect the state of the local repo.

From reading the code my impression is that this isn't possible, but that I may be able to implement something with client.InstallProtocol. I was hoping to piggyback on the plumbing/transport/file code, but it seems that just execs out to the git-upload-pack git-receive-pack binaries, which won't help at all 😂

Has this been done before? Am I missing something obvious?

hairyhenderson commented 4 years ago

I've had some partial success on this. I'd missed the server.NewFilesystemLoader function, and I'm having some success with:

fs := memfs.New()
l := server.NewFilesystemLoader(fs)
c := server.NewClient(l)
client.InstallProtocol("mem", c)

I'll follow up if I have issues, but I think it'd still be useful to call this out in the examples since this can be a useful pattern especially in testing...