Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Adds support for .gitignore at repository root #420

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion utils/merkletrie/filesystem/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"path/filepath"

"github.com/monochromegane/go-gitignore"

"gopkg.in/src-d/go-billy.v2"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
Expand All @@ -25,6 +27,7 @@ type node struct {
submodules map[string]plumbing.Hash

path string
matcher gitignore.IgnoreMatcher
hash []byte
children []noder.Noder
isDir bool
Expand All @@ -39,7 +42,11 @@ func NewRootNode(
fs billy.Filesystem,
submodules map[string]plumbing.Hash,
) noder.Noder {
return &node{fs: fs, submodules: submodules, isDir: true}
n := &node{fs: fs, submodules: submodules, isDir: true}
if f, err := n.fs.Open(filepath.Join(n.path, ".gitignore")); err == nil {
n.matcher = gitignore.NewGitIgnoreFromReader(n.path, f)
}
return n
}

// Hash the hash of a filesystem is the result of concatenating the computed
Expand Down Expand Up @@ -95,6 +102,10 @@ func (n *node) calculateChildren() error {
continue
}

if n.matcher != nil && n.matcher.Match(filepath.Join(n.path, file.Name()), file.IsDir()) {
continue
}

c, err := n.newChildNode(file)
if err != nil {
return err
Expand All @@ -119,6 +130,7 @@ func (n *node) newChildNode(file billy.FileInfo) (*node, error) {
submodules: n.submodules,

path: path,
matcher: n.matcher,
hash: hash,
isDir: file.IsDir(),
}
Expand Down
23 changes: 23 additions & 0 deletions utils/merkletrie/filesystem/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ func (s *NoderSuite) TestDiff(c *C) {
c.Assert(ch, HasLen, 0)
}

func (s *NoderSuite) TestGitignore(c *C) {
fsA := memfs.New()
WriteFile(fsA, "foo", []byte("foo"), 0644)
WriteFile(fsA, ".gitignore", []byte("bar"), 0644)
WriteFile(fsA, "qux/bar", []byte("somevalue"), 0644)
WriteFile(fsA, "qux/qux", []byte("foo"), 0644)

fsB := memfs.New()
WriteFile(fsB, "foo", []byte("foo"), 0644)
WriteFile(fsB, ".gitignore", []byte("bar"), 0644)
WriteFile(fsB, "qux/bar", []byte("mismatch"), 0644)
WriteFile(fsB, "qux/qux", []byte("foo"), 0644)

ch, err := merkletrie.DiffTree(
NewRootNode(fsA, nil),
NewRootNode(fsB, nil),
IsEquals,
)

c.Assert(err, IsNil)
c.Assert(ch, HasLen, 0)
}

func (s *NoderSuite) TestDiffChangeContent(c *C) {
fsA := memfs.New()
WriteFile(fsA, "foo", []byte("foo"), 0644)
Expand Down