├── node.go ├── git └── git.go ├── LICENSE ├── dir.go ├── file.go ├── README.md └── main.go /node.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bazil.org/fuse/fs" 5 | ) 6 | 7 | type Node interface { 8 | fs.Node 9 | Name() string 10 | Path() string 11 | IsDir() bool 12 | } 13 | -------------------------------------------------------------------------------- /git/git.go: -------------------------------------------------------------------------------- 1 | package git 2 | 3 | import ( 4 | "bytes" 5 | "log" 6 | "os/exec" 7 | "strings" 8 | ) 9 | 10 | func ListFiles(treeish, dir string) (filepaths []string, err error) { 11 | cmd := exec.Command( 12 | "git", 13 | "ls-tree", 14 | "-r", 15 | "--name-only", 16 | treeish, 17 | dir) 18 | 19 | var stdout, stderr bytes.Buffer 20 | cmd.Stdout = &stdout 21 | cmd.Stderr = &stderr 22 | 23 | err = cmd.Run() 24 | if err != nil { 25 | log.Println(stderr.String()) 26 | return nil, err 27 | } 28 | 29 | return strings.Split(strings.TrimSpace(stdout.String()), "\n"), err 30 | } 31 | 32 | func ShowContents(treeish, path string) (content string, err error) { 33 | cmd := exec.Command( 34 | "git", 35 | "show", 36 | treeish+":"+path) 37 | 38 | var stdout, stderr bytes.Buffer 39 | cmd.Stdout = &stdout 40 | cmd.Stderr = &stderr 41 | 42 | err = cmd.Run() 43 | if err != nil { 44 | log.Println(stderr.String()) 45 | return "", err 46 | } 47 | 48 | return stdout.String(), nil 49 | } 50 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 George Shank 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /dir.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | 6 | "bazil.org/fuse" 7 | "bazil.org/fuse/fs" 8 | "golang.org/x/net/context" 9 | ) 10 | 11 | type Dir struct { 12 | nodes []Node 13 | name string 14 | path string 15 | } 16 | 17 | func (d *Dir) IsDir() bool { 18 | return true 19 | } 20 | 21 | func (d *Dir) Name() string { 22 | return d.name 23 | } 24 | 25 | func (d *Dir) Path() string { 26 | return d.path 27 | } 28 | 29 | func (d *Dir) Attr(ctx context.Context, a *fuse.Attr) error { 30 | a.Mode = os.ModeDir | os.ModeDir | 0555 31 | return nil 32 | } 33 | 34 | func (d *Dir) Lookup(ctx context.Context, name string) (fs.Node, error) { 35 | for _, node := range d.nodes { 36 | if node.Name() == name { 37 | return node, nil 38 | } 39 | } 40 | return nil, fuse.ENOENT 41 | } 42 | 43 | func (d *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { 44 | var dirents []fuse.Dirent 45 | for _, node := range d.nodes { 46 | dirent := fuse.Dirent{ 47 | Name: node.Name(), 48 | } 49 | if node.IsDir() { 50 | dirent.Type = fuse.DT_Dir 51 | } else { 52 | dirent.Type = fuse.DT_File 53 | } 54 | dirents = append(dirents, dirent) 55 | } 56 | return dirents, nil 57 | } 58 | 59 | func (d *Dir) AddNode(node Node) { 60 | d.nodes = append(d.nodes, node) 61 | } 62 | -------------------------------------------------------------------------------- /file.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "syscall" 5 | 6 | "bazil.org/fuse" 7 | "bazil.org/fuse/fs" 8 | "bazil.org/fuse/fuseutil" 9 | "golang.org/x/net/context" 10 | "taterbase.me/git-mount/git" 11 | ) 12 | 13 | var ( 14 | _ Node = (*File)(nil) 15 | _ fs.NodeOpener = (*File)(nil) 16 | _ fs.Handle = (*File)(nil) 17 | _ fs.HandleReader = (*File)(nil) 18 | ) 19 | 20 | type File struct { 21 | treeish string 22 | name string 23 | path string 24 | length uint64 25 | } 26 | 27 | func (f *File) IsDir() bool { 28 | return false 29 | } 30 | 31 | func (f *File) Name() string { 32 | return f.name 33 | } 34 | 35 | func (f *File) Path() string { 36 | return f.path 37 | } 38 | 39 | func (f *File) Attr(ctx context.Context, a *fuse.Attr) error { 40 | a.Mode = 0444 41 | a.Size = f.length 42 | return nil 43 | } 44 | 45 | func (f *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) { 46 | if !req.Flags.IsReadOnly() { 47 | return nil, fuse.Errno(syscall.EACCES) 48 | } 49 | resp.Flags |= fuse.OpenKeepCache 50 | return f, nil 51 | } 52 | 53 | func (f *File) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { 54 | content, err := git.ShowContents(f.treeish, f.Path()) 55 | if err != nil { 56 | return err 57 | } 58 | fuseutil.HandleRead(req, resp, []byte(content)) 59 | return nil 60 | } 61 | 62 | type GitFS struct { 63 | root fs.Node 64 | } 65 | 66 | func (gfs *GitFS) Root() (fs.Node, error) { 67 | return gfs.root, nil 68 | } 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #git-mount 2 | 3 | `git-mount` let's you mount your repo as a filesystem based on a revision. 4 | 5 | ##Install 6 | Please note, `git-mount` requires [FUSE](https://en.wikipedia.org/wiki/Filesystem_in_Userspace) 7 | to be installed on your system to work. If you're on OS X you can use 8 | [FUSE for OS X](https://osxfuse.github.io/) 9 | 10 | 11 | You can find the latest binaries on the [releases page](https://github.com/taterbase/git-mount/releases) 12 | or if you have [Go](http://golang.org/) you can build `git-mount` directly from 13 | source. 14 | 15 | ``` 16 | go get taterbase.me/git-mount 17 | go install taterbase.me/git-mount 18 | ``` 19 | 20 | ##Usage 21 | 22 | Change to a directory that is an existing git repo. Once inside you can call 23 | `git-mount` directly 24 | 25 | ``` 26 | git-mount HEAD 27 | ``` 28 | 29 | Or if `git-mount` is on your path you can just call it like an extension 30 | 31 | ``` 32 | git mount 2fdcb3ae 33 | ``` 34 | 35 | If only one argument is passed in `git-mount` treats that argument as a 36 | [treeish](https://schacon.github.io/gitbook/4_git_treeishes.html). Based on 37 | your current location in the repo it will mount all files and folders from that 38 | level and deeper. `git-mount` will only ever descend files, never ascend, so if 39 | you are in folder `foo` and folder `foo` is the top level of th repo the whole repo 40 | will be mounted. If you go into `foo/bar` and call `git-mount {treeish}` then 41 | only `bar` and its descendants will be mounted. 42 | 43 | You can also pass a path to `git-mount` 44 | 45 | ``` 46 | git mount HEAD~2 public/img 47 | ``` 48 | 49 | This will tell `git-mount` to only mount the specified path and its 50 | descendants. 51 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "io/ioutil" 6 | "log" 7 | "os" 8 | "os/signal" 9 | "path/filepath" 10 | "syscall" 11 | 12 | "bazil.org/fuse" 13 | "bazil.org/fuse/fs" 14 | "taterbase.me/git-mount/git" 15 | ) 16 | 17 | var ( 18 | hash_map = make(map[string]*Dir) 19 | root *Dir 20 | 21 | _ fs.FS = (*GitFS)(nil) 22 | _ Node = (*File)(nil) 23 | _ fs.NodeOpener = (*File)(nil) 24 | _ fs.Handle = (*File)(nil) 25 | _ fs.HandleReader = (*File)(nil) 26 | _ Node = (*Dir)(nil) 27 | _ fs.NodeStringLookuper = (*Dir)(nil) 28 | _ fs.HandleReadDirAller = (*Dir)(nil) 29 | 30 | treeish string 31 | ) 32 | 33 | func main() { 34 | flag.Parse() 35 | treeish = flag.Arg(0) 36 | if len(treeish) == 0 { 37 | treeish = "HEAD" 38 | } 39 | 40 | dir := "" 41 | paths, err := git.ListFiles(treeish, dir) 42 | if err != nil { 43 | log.Fatalf("Unable to list files for treeish %s: %v", treeish, 44 | err) 45 | } 46 | 47 | root = &Dir{ 48 | name: "/", 49 | path: ".", 50 | } 51 | 52 | hash_map["."] = root 53 | 54 | for _, file_path := range paths { 55 | dir, file_name := filepath.Split(file_path) 56 | file := &File{treeish: treeish, name: file_name, path: file_path} 57 | content, err := git.ShowContents(treeish, file_path) 58 | if err != nil { 59 | log.Fatal(err) 60 | } 61 | file.length = uint64(len(content)) 62 | parent := getDirent(filepath.Dir(dir)) 63 | parent.AddNode(file) 64 | } 65 | 66 | mountpoint, err := ioutil.TempDir("", "git-mount-") 67 | if err != nil { 68 | log.Fatal(err) 69 | } 70 | 71 | c, err := fuse.Mount( 72 | mountpoint, 73 | fuse.FSName("git-mount"), 74 | fuse.Subtype("git-mountfs"), 75 | fuse.LocalVolume(), 76 | fuse.VolumeName(treeish), 77 | ) 78 | if err != nil { 79 | log.Fatal(err) 80 | } 81 | defer c.Close() 82 | 83 | log.Print("mounted at: ", mountpoint) 84 | 85 | ch := make(chan os.Signal, 1) 86 | signal.Notify(ch, syscall.SIGINT, 87 | syscall.SIGTERM, 88 | syscall.SIGHUP, 89 | syscall.SIGQUIT) 90 | go func() { 91 | <-ch 92 | err := fuse.Unmount(mountpoint) 93 | if err != nil { 94 | log.Print(err) 95 | } 96 | os.Exit(1) 97 | }() 98 | 99 | err = fs.Serve(c, &GitFS{root: root}) 100 | if err != nil { 101 | log.Fatal(err) 102 | } 103 | 104 | // check if the mount process has an error to report 105 | <-c.Ready 106 | if err := c.MountError; err != nil { 107 | log.Fatal(err) 108 | } 109 | } 110 | 111 | func getDirent(path string) *Dir { 112 | dirent, ok := hash_map[path] 113 | if !ok { 114 | name := filepath.Base(path) 115 | parent_dir := filepath.Dir(path) 116 | 117 | parent := getDirent(parent_dir) 118 | dirent = &Dir{ 119 | name: name, 120 | path: path, 121 | } 122 | parent.AddNode(dirent) 123 | hash_map[path] = dirent 124 | } 125 | return dirent 126 | } 127 | --------------------------------------------------------------------------------