Skip to content

Commit

Permalink
Cloning remote repositories into memory
Browse files Browse the repository at this point in the history
I think it will better than cloning into temp directory. For big repositories you should use manual clone into local directory and mount it.
  • Loading branch information
dsxack committed Sep 24, 2023
1 parent 084fdd2 commit 5922292
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 81 deletions.
22 changes: 12 additions & 10 deletions cmd/gitfs/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package main
import (
"fmt"
"github.com/dsxack/gitfs/nodes"
"github.com/fjl/memsize"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/storage/filesystem"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -78,7 +80,7 @@ var mountCmd = &cobra.Command{
if err != nil {
return fmt.Errorf("failed to mount filesystem: %w", err)
}
cmd.Printf("Filesystem mounted successfully into directory: %s\n", mountPoint)
cmd.Printf("Filesystem successfully mounted into directory: %s\n", mountPoint)

go server.Wait()

Expand Down Expand Up @@ -117,19 +119,19 @@ func newRepository(cmd *cobra.Command, repositoryURL string) (*git.Repository, e

_, err := os.Stat(repositoryURL)
if os.IsNotExist(err) {
clonePath, err := os.MkdirTemp("", "gitfs")
if err != nil {
return nil, fmt.Errorf("failed to create temporary directory: %w", err), dummyCleanup
}
cmd.Printf("Cloning repository into temporary directory: %s\n", clonePath)
repository, err := git.PlainClone(clonePath, false, &git.CloneOptions{
URL: repositoryURL,
cmd.Printf("Cloning repository %s into memory\n", repositoryURL)
storage := memory.NewStorage()
r, err := git.Clone(storage, nil, &git.CloneOptions{
NoCheckout: true,
URL: repositoryURL,
Progress: cmd.OutOrStderr(),
})
if err != nil {
return nil, fmt.Errorf("failed to clone repository: %w", err), dummyCleanup
}
cmd.Println("Repository cloned successfully")
return repository, nil, dummyCleanup
size := memsize.Scan(storage)
cmd.Printf("Repository size: %s\n", memsize.HumanSize(size.Total))
return r, nil, dummyCleanup
}
workDirFS := osfs.New(repositoryURL)
if _, err := workDirFS.Stat(git.GitDirName); err == nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/dsxack/gitfs
go 1.21

require (
github.com/fjl/memsize v0.0.2
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.9.0
github.com/hanwen/go-fuse/v2 v2.4.0
Expand All @@ -23,7 +24,6 @@ require (
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
Expand Down
Loading

0 comments on commit 5922292

Please sign in to comment.