Skip to content
This repository has been archived by the owner on Aug 22, 2020. It is now read-only.

Commit

Permalink
gh-19: support more urls (#27)
Browse files Browse the repository at this point in the history
* gh-19: abstracted out the parsing logic and started adding support points for svn and mercurial repositories

* gh-19: reordered imports
  • Loading branch information
Mya Pitzeruse committed Jan 21, 2019
1 parent ba98602 commit 4bf2f50
Show file tree
Hide file tree
Showing 16 changed files with 287 additions and 89 deletions.
2 changes: 1 addition & 1 deletion cmd/gitfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"fmt"
"github.com/sirupsen/logrus"
"os"

"github.com/mjpitz/gitfs/cmd"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down
37 changes: 21 additions & 16 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package cmd
import (
"os"
"os/user"
"path"
"strconv"
"strings"

"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/mjpitz/gitfs/pkg/clone"
"github.com/mjpitz/gitfs/pkg/config"
"github.com/mjpitz/gitfs/pkg/filesystem"
"github.com/mjpitz/gitfs/pkg/remotes"
"github.com/mjpitz/gitfs/pkg/tree"
"github.com/mjpitz/gitfs/pkg/urls"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -54,23 +55,27 @@ var StartCommand = &cobra.Command{

logrus.Info("[main] parsing repositories into a directory structure")
for _, repository := range repositories {
start := 4
separator := strings.Index(repository, ":")
end := len(repository) - 4
url, err := urls.ParseUrl(repository)
if err != nil {
logrus.Warnf("[main] failed to parse url: %v", err)
continue
}

// git@code.corp.indeed.com:squall/yellowhat-snapshots-prod.git
// git@<<HOST>>:<<PATH>>.git
host := repository[start:separator]
path := repository[separator+1 : end]
host := url.URL.Host
fullPath := url.URL.Path

// expand into directory structure
parts := strings.Split(path, "/")
// remove suffix
ext := path.Ext(fullPath)
fullPath = strings.TrimSuffix(fullPath, ext)
fullPath = strings.TrimPrefix(fullPath, "/")

parts := strings.Split(fullPath, "/")

var fullPathParts []string
fullPathParts = append(fullPathParts, host)
fullPathParts = append(fullPathParts, parts...)

tree.Insert(fullPathParts, repository)
tree.Insert(fullPathParts, url)
}

logrus.Infof("[main] attempting to mount %s", mountpoint)
Expand All @@ -83,13 +88,13 @@ var StartCommand = &cobra.Command{
uid, _ := strconv.Atoi(current.Uid)
gid, _ := strconv.Atoi(current.Gid)

cloner := clone.NewCloner(cfg.Clone)
cloner := urls.NewFileSystemAdapter(cfg.Clone)

filesys := &filesystem.FileSystem{
Uid: uint32(uid),
Gid: uint32(gid),
Tree: tree,
Cloner: cloner,
Uid: uint32(uid),
Gid: uint32(gid),
Tree: tree,
FSA: cloner,
}

logrus.Info("[main] now serving file system")
Expand Down
32 changes: 16 additions & 16 deletions pkg/filesystem/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ import (

"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/mjpitz/gitfs/pkg/clone"
"github.com/mjpitz/gitfs/pkg/tree"
"github.com/mjpitz/gitfs/pkg/urls"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
)

func NewDirectory(uid, gid uint32, tree *gitfstree.TreeNode, cloner *clone.Cloner) fs.Node {
func NewDirectory(uid, gid uint32, tree *gitfstree.TreeNode, cloner *urls.FileSystemAdapter) fs.Node {
return &Directory{
uid: uid,
gid: gid,
tree: tree,
cloner: cloner,
lock: &sync.Mutex{},
uid: uid,
gid: gid,
tree: tree,
fsa: cloner,
lock: &sync.Mutex{},
}
}

type Directory struct {
uid uint32
gid uint32
tree *gitfstree.TreeNode
cloner *clone.Cloner
lock *sync.Mutex
uid uint32
gid uint32
tree *gitfstree.TreeNode
fsa *urls.FileSystemAdapter
lock *sync.Mutex
}

func (d *Directory) Lookup(ctx context.Context, name string) (fs.Node, error) {
Expand All @@ -41,15 +41,15 @@ func (d *Directory) Lookup(ctx context.Context, name string) (fs.Node, error) {

var directory fs.Node

if len(node.URL) > 0 {
cloned, err := d.cloner.Clone(node.URL)
if node.URL != nil {
cloned, err := d.fsa.Clone(node.URL)
if err != nil {
logrus.Errorf("[filesystem.directory] failed to clone url: %s, %v", node.URL, err)
return nil, fuse.ENOENT
}

directory = &BillyNode{
repourl: node.URL,
repourl: node.URL.String(),
fs: cloned,
path: "",
target: "",
Expand All @@ -63,7 +63,7 @@ func (d *Directory) Lookup(ctx context.Context, name string) (fs.Node, error) {
mu: &sync.Mutex{},
}
} else {
directory = NewDirectory(d.uid, d.gid, node, d.cloner)
directory = NewDirectory(d.uid, d.gid, node, d.fsa)
}

return directory, nil
Expand Down
12 changes: 6 additions & 6 deletions pkg/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package filesystem

import (
"bazil.org/fuse/fs"
"github.com/mjpitz/gitfs/pkg/clone"
"github.com/mjpitz/gitfs/pkg/tree"
"github.com/mjpitz/gitfs/pkg/urls"
)

type FileSystem struct {
Uid uint32
Gid uint32
Tree *gitfstree.TreeNode
Cloner *clone.Cloner
Uid uint32
Gid uint32
Tree *gitfstree.TreeNode
FSA *urls.FileSystemAdapter
}

var _ fs.FS = (*FileSystem)(nil)

func (f *FileSystem) Root() (fs.Node, error) {
return NewDirectory(f.Uid, f.Gid, f.Tree, f.Cloner), nil
return NewDirectory(f.Uid, f.Gid, f.Tree, f.FSA), nil
}
5 changes: 3 additions & 2 deletions pkg/remotes/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package remotes
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/mjpitz/gitfs/pkg/config"
"github.com/nytlabs/gojee"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
)

func NewGenericRemote(cfg *config.Generic) Remote {
Expand Down
2 changes: 1 addition & 1 deletion pkg/remotes/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package remotes

import (
"context"
"github.com/sirupsen/logrus"
"net/http"

"github.com/google/go-github/v20/github"
"github.com/mjpitz/gitfs/pkg/config"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2"
)

Expand Down
1 change: 1 addition & 0 deletions pkg/remotes/remotes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package remotes

import (
"fmt"

"github.com/mjpitz/gitfs/pkg/config"
)

Expand Down
10 changes: 7 additions & 3 deletions pkg/tree/tree.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package gitfstree

import "reflect"
import (
"reflect"

"github.com/mjpitz/gitfs/pkg/urls"
)

func NewTreeNode() *TreeNode {
return &TreeNode{
Expand All @@ -9,11 +13,11 @@ func NewTreeNode() *TreeNode {
}

type TreeNode struct {
URL string
URL *urls.URL
children map[string]*TreeNode
}

func (t *TreeNode) Insert(path []string, url string) {
func (t *TreeNode) Insert(path []string, url *urls.URL) {
ptr := t

for _, part := range path {
Expand Down
9 changes: 9 additions & 0 deletions pkg/urls/cloner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package urls

import (
"gopkg.in/src-d/go-billy.v4"
)

type Cloner interface {
Clone(url *URL, depth int, fs billy.Filesystem) error
}
33 changes: 33 additions & 0 deletions pkg/urls/compositecloner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package urls

import (
"fmt"

"gopkg.in/src-d/go-billy.v4"
)

func NewCloner() Cloner {
cloners := make(map[VCS]Cloner)
cloners[GIT] = &gitcloner{}

return &compositecloner{
cloners: cloners,
}
}

var _ Cloner = &compositecloner{}

type compositecloner struct {
cloners map[VCS]Cloner
}

func (cc *compositecloner) Clone(url *URL, depth int, fs billy.Filesystem) error {
cloner, ok := cc.cloners[url.VCS]
if !ok {
return fmt.Errorf("unsupported vcs: %s", url.VCS)
}

return cloner.Clone(url, depth, fs)
}


Loading

0 comments on commit 4bf2f50

Please sign in to comment.