From 387dc435ef569dfe9e8072d35effe438ba992d6d Mon Sep 17 00:00:00 2001 From: tdstein Date: Thu, 7 Sep 2023 12:15:33 -0400 Subject: [PATCH 1/3] Fixes ignore matching on directories --- internal/bundles/gitignore/gitignore.go | 3 ++- internal/bundles/gitignore/gitignore_test.go | 13 +++++++++++++ test/sample-content/fastapi-simple/.gitignore | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 test/sample-content/fastapi-simple/.gitignore diff --git a/internal/bundles/gitignore/gitignore.go b/internal/bundles/gitignore/gitignore.go index f76b3c7f4..3aaa9f9df 100644 --- a/internal/bundles/gitignore/gitignore.go +++ b/internal/bundles/gitignore/gitignore.go @@ -319,7 +319,8 @@ func (ign *GitIgnoreList) match(path string, info os.FileInfo) *Match { // Match returns whether any of the globs in the ignore list match the // specified path. Uses the same matching rules as .gitignore files. func (ign *GitIgnoreList) Match(path string) *Match { - return ign.match(path, nil) + stat, _ := ign.fs.Stat(path) + return ign.match(path, stat) } // Walk walks the file tree with the specified root and calls fn on each file diff --git a/internal/bundles/gitignore/gitignore_test.go b/internal/bundles/gitignore/gitignore_test.go index d0b247d15..5311edd96 100644 --- a/internal/bundles/gitignore/gitignore_test.go +++ b/internal/bundles/gitignore/gitignore_test.go @@ -75,4 +75,17 @@ func (s *GitIgnoreSuite) TestMatch() { s.Equal("*.bak", m.Pattern) s.Equal("", m.FilePath) s.Equal(0, m.Line) + + ignoredir := s.cwd.Join("ignoredir") + err = ignoredir.MkdirAll(0700) + s.NoError(err) + err = ign.AppendGlobs([]string{"ignoredir/"}, MatchSourceUser) + s.NoError(err) + + m = ign.Match(ignoredir.Path()) + s.NotNil(m) + s.Equal(MatchSourceUser, m.Source) + s.Equal("ignoredir/", m.Pattern) + s.Equal("", m.FilePath) + s.Equal(0, m.Line) } diff --git a/test/sample-content/fastapi-simple/.gitignore b/test/sample-content/fastapi-simple/.gitignore new file mode 100644 index 000000000..1d17dae13 --- /dev/null +++ b/test/sample-content/fastapi-simple/.gitignore @@ -0,0 +1 @@ +.venv From 6be8d6ed9b59028934f3e70540ae9d657e9d43c1 Mon Sep 17 00:00:00 2001 From: tdstein Date: Thu, 7 Sep 2023 12:56:22 -0400 Subject: [PATCH 2/3] Modifies IgnoreList interface --- internal/bundles/gitignore/gitignore.go | 11 ++++++++--- internal/bundles/gitignore/gitignore_test.go | 12 ++++++++---- internal/bundles/gitignore/interface.go | 8 +++++--- internal/services/api/files/files.go | 6 +++++- internal/services/api/files/services.go | 6 +++++- 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/internal/bundles/gitignore/gitignore.go b/internal/bundles/gitignore/gitignore.go index 3aaa9f9df..db7fc4d05 100644 --- a/internal/bundles/gitignore/gitignore.go +++ b/internal/bundles/gitignore/gitignore.go @@ -318,9 +318,14 @@ func (ign *GitIgnoreList) match(path string, info os.FileInfo) *Match { // Match returns whether any of the globs in the ignore list match the // specified path. Uses the same matching rules as .gitignore files. -func (ign *GitIgnoreList) Match(path string) *Match { - stat, _ := ign.fs.Stat(path) - return ign.match(path, stat) +func (ign *GitIgnoreList) Match(path string) (*Match, error) { + stat, err := ign.fs.Stat(path) + if err != nil { + if !os.IsNotExist(err) { + return nil, err + } + } + return ign.match(path, stat), nil } // Walk walks the file tree with the specified root and calls fn on each file diff --git a/internal/bundles/gitignore/gitignore_test.go b/internal/bundles/gitignore/gitignore_test.go index 5311edd96..9e8a212d8 100644 --- a/internal/bundles/gitignore/gitignore_test.go +++ b/internal/bundles/gitignore/gitignore_test.go @@ -57,11 +57,13 @@ func (s *GitIgnoreSuite) TestMatch() { s.NoError(err) // Match returns nil if no match - m := ign.Match("app.py") + m, err := ign.Match("app.py") + s.Nil(err) s.Nil(m) // File matches include file info - m = ign.Match(".Rhistory") + m, err = ign.Match(".Rhistory") + s.Nil(err) s.NotNil(m) s.Equal(MatchSourceFile, m.Source) s.Equal(".Rhistory", m.Pattern) @@ -69,7 +71,8 @@ func (s *GitIgnoreSuite) TestMatch() { s.Equal(1, m.Line) // Non-file matches don't include file info - m = ign.Match("app.py.bak") + m, err = ign.Match("app.py.bak") + s.Nil(err) s.NotNil(m) s.Equal(MatchSourceUser, m.Source) s.Equal("*.bak", m.Pattern) @@ -82,7 +85,8 @@ func (s *GitIgnoreSuite) TestMatch() { err = ign.AppendGlobs([]string{"ignoredir/"}, MatchSourceUser) s.NoError(err) - m = ign.Match(ignoredir.Path()) + m, err = ign.Match(ignoredir.Path()) + s.Nil(err) s.NotNil(m) s.Equal(MatchSourceUser, m.Source) s.Equal("ignoredir/", m.Pattern) diff --git a/internal/bundles/gitignore/interface.go b/internal/bundles/gitignore/interface.go index 0ab74568d..0a9721be8 100644 --- a/internal/bundles/gitignore/interface.go +++ b/internal/bundles/gitignore/interface.go @@ -12,7 +12,7 @@ type IgnoreList interface { Append(path util.Path) error AppendGlobs(patterns []string, source MatchSource) error AppendGit() error - Match(path string) *Match + Match(path string) (*Match, error) Walk(root util.Path, fn util.WalkFunc) error } @@ -38,9 +38,9 @@ func (m *MockGitIgnoreList) AppendGit() error { return args.Error(0) } -func (m *MockGitIgnoreList) Match(path string) bool { +func (m *MockGitIgnoreList) Match(path string) (*Match, error) { args := m.Called(path) - return args.Bool(0) + return args.Get(0).(*Match), args.Error(1) } func (m *MockGitIgnoreList) Walk(root util.Path, fn util.WalkFunc) error { @@ -48,6 +48,8 @@ func (m *MockGitIgnoreList) Walk(root util.Path, fn util.WalkFunc) error { return args.Error(0) } +var _ IgnoreList = &MockGitIgnoreList{} + // Maintain a reference to the original gitignore so it // and its license remain in our vendor directory. type _ gitignore.IgnoreList diff --git a/internal/services/api/files/files.go b/internal/services/api/files/files.go index eee18ad19..4770d2bc9 100644 --- a/internal/services/api/files/files.go +++ b/internal/services/api/files/files.go @@ -86,7 +86,11 @@ func (f *File) insert(root util.Path, path util.Path, ignore gitignore.IgnoreLis } // otherwise, create it - exclusion := ignore.Match(path.Path()) + exclusion, err := ignore.Match(path.Path()) + if err != nil { + return nil, err + } + child, err := CreateFile(root, path, exclusion) if err != nil { return nil, err diff --git a/internal/services/api/files/services.go b/internal/services/api/files/services.go index 71a698900..59ef7b3b1 100644 --- a/internal/services/api/files/services.go +++ b/internal/services/api/files/services.go @@ -38,7 +38,11 @@ type filesService struct { func (s filesService) GetFile(p util.Path) (*File, error) { p = p.Clean() - m := s.ignore.Match(p.String()) + m, err := s.ignore.Match(p.String()) + if err != nil { + return nil, err + } + file, err := CreateFile(s.root, p, m) if err != nil { return nil, err From 5be263e7e7e8a14f75f6b602e7bf252c22ac90a8 Mon Sep 17 00:00:00 2001 From: tdstein Date: Thu, 7 Sep 2023 13:03:34 -0400 Subject: [PATCH 3/3] Changes error assertions to NoError --- internal/bundles/gitignore/gitignore_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/bundles/gitignore/gitignore_test.go b/internal/bundles/gitignore/gitignore_test.go index 9e8a212d8..9463a0f3a 100644 --- a/internal/bundles/gitignore/gitignore_test.go +++ b/internal/bundles/gitignore/gitignore_test.go @@ -58,12 +58,12 @@ func (s *GitIgnoreSuite) TestMatch() { // Match returns nil if no match m, err := ign.Match("app.py") - s.Nil(err) + s.NoError(err) s.Nil(m) // File matches include file info m, err = ign.Match(".Rhistory") - s.Nil(err) + s.NoError(err) s.NotNil(m) s.Equal(MatchSourceFile, m.Source) s.Equal(".Rhistory", m.Pattern) @@ -72,7 +72,7 @@ func (s *GitIgnoreSuite) TestMatch() { // Non-file matches don't include file info m, err = ign.Match("app.py.bak") - s.Nil(err) + s.NoError(err) s.NotNil(m) s.Equal(MatchSourceUser, m.Source) s.Equal("*.bak", m.Pattern) @@ -86,7 +86,7 @@ func (s *GitIgnoreSuite) TestMatch() { s.NoError(err) m, err = ign.Match(ignoredir.Path()) - s.Nil(err) + s.NoError(err) s.NotNil(m) s.Equal(MatchSourceUser, m.Source) s.Equal("ignoredir/", m.Pattern)