7c891bd5f2
message: Initial commit, can create master commit and verify previous master commits change_hash: ADgeVBdfi1hA0TTDrBIkYHaQQYoxZaInZz1p/BAH35Ng credentials: - type: pgp_signature pub_key_id: 95C46FA6A41148AC body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5IbRgACgkQlcRvpqQRSKzWjg/+P0a3einWQ8wFUe05qXUbmMQ4K86Oa4I85pF6kubZlFy/UbcjiPnTPRMKAhmGZi4WCz1sW1F2al4qKvtq3nvn6+hZY8dj0SjPgGG2lkMMLEVy1hjsO7d9S9ZEfUv0cHOcvkphgVQk+InkegBXvFS45mwKQLDOiW5tPcTFDHTHBmC/nlCV/sKCrZEmQGU7KaELJKOf26LSY2zXe6fbVCa8njpIycYS7Wulu2OODcI5n6Ye2U6DvxN6MvuNvziyX7VMePS1xEdJYpltsNMhSkMMGLU7dovxbrhD617uwOsm1847YX9HTJ3Ixs+M0yobHmz8ob4OBcZx8r3AoiyDo+HNMmAZ96ue8pPHmI+2O9jEmbmbH61yq4crhUVAP8PncSTdq0tiYKj/zaSTJ8CT2W0uicX/3v9EtIFn0thqe/qZzHh6upixvpXDpNjZZ5SxiVm8MITnWzInQRbo9yvFsfgd7LqMGKZeGv5q5rgNTRM4fwGrJDuslwj8V2B4uw1ofPncL+LHmXArXWiewvvJFU2uRpfvsl+u4is2dl2SGVpe7ixm+a088gllOQCMRgLbuaN8dQ/eqdkfdxUg+SYQlx6vykrdJOSQrs9zaX/JuxnaNBTi/yLY1FqFXaXBGID6qX1cnPilw+J6vEZYt1MBtzXX+UEjHyVowIhMRsnts6Wq3Z8= account: mediocregopher
101 lines
2.4 KiB
Go
101 lines
2.4 KiB
Go
// Package fs implements abstractions for interacting with a filesystem, either
|
|
// via a git tree, a staged index, or directly.
|
|
package fs
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
|
|
"gopkg.in/src-d/go-billy.v4"
|
|
"gopkg.in/src-d/go-git.v4"
|
|
"gopkg.in/src-d/go-git.v4/plumbing/object"
|
|
)
|
|
|
|
// FS is a simple interface for reading off a snapshot of a filesystem.
|
|
type FS interface {
|
|
Open(path string) (io.ReadCloser, error)
|
|
}
|
|
|
|
type treeFS struct {
|
|
tree *object.Tree
|
|
}
|
|
|
|
// FromTree wraps a git tree object to implement the FS interface. All paths
|
|
// will be relative to the root of the tree.
|
|
func FromTree(t *object.Tree) FS {
|
|
return treeFS{tree: t}
|
|
}
|
|
|
|
func (gt treeFS) Open(path string) (io.ReadCloser, error) {
|
|
f, err := gt.tree.File(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return f.Blob.Reader()
|
|
}
|
|
|
|
type billyFS struct {
|
|
fs billy.Filesystem
|
|
}
|
|
|
|
// FromBillyFilesystem wraps a billy.Filesystem to implement the FS interface.
|
|
// All paths will be relative to the filesystem's root.
|
|
func FromBillyFilesystem(bfs billy.Filesystem) FS {
|
|
return billyFS{fs: bfs}
|
|
}
|
|
|
|
func (bfs billyFS) Open(path string) (io.ReadCloser, error) {
|
|
return bfs.fs.Open(path)
|
|
}
|
|
|
|
// FromStagedChangesTree processes the current set of staged changes into a tree
|
|
// object, and returns an FS for that tree. All paths will be relative to the
|
|
// root of the git repo.
|
|
func FromStagedChangesTree(repo *git.Repository) (FS, *object.Tree, error) {
|
|
w, err := repo.Worktree()
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("could not open git worktree: %w", err)
|
|
}
|
|
|
|
storer := repo.Storer
|
|
idx, err := storer.Index()
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("could not open git staging index: %w", err)
|
|
}
|
|
|
|
th := &buildTreeHelper{
|
|
fs: w.Filesystem,
|
|
s: storer,
|
|
}
|
|
|
|
treeHash, err := th.BuildTree(idx)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("could not build staging index tree: %w", err)
|
|
}
|
|
|
|
tree, err := repo.TreeObject(treeHash)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("could not get staged tree object (%q): %w", treeHash, err)
|
|
}
|
|
|
|
return FromTree(tree), tree, nil
|
|
}
|
|
|
|
// Stub is an implementation of FS based on a map of paths to the file contents
|
|
// at that path. Paths should be "clean" or they will not match with anything.
|
|
type Stub map[string][]byte
|
|
|
|
// Open implements the method for the FS interface.
|
|
func (s Stub) Open(path string) (io.ReadCloser, error) {
|
|
body, ok := s[path]
|
|
if !ok {
|
|
return nil, os.ErrNotExist
|
|
}
|
|
|
|
return ioutil.NopCloser(bytes.NewReader(body)), nil
|
|
}
|