diff --git a/internal/bundles/gitignore/gitignore.go b/internal/bundles/gitignore/gitignore.go index f76b3c7f4..db7fc4d05 100644 --- a/internal/bundles/gitignore/gitignore.go +++ b/internal/bundles/gitignore/gitignore.go @@ -318,8 +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 { - return ign.match(path, nil) +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 d0b247d15..9463a0f3a 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.NoError(err) s.Nil(m) // File matches include file info - m = ign.Match(".Rhistory") + m, err = ign.Match(".Rhistory") + s.NoError(err) s.NotNil(m) s.Equal(MatchSourceFile, m.Source) s.Equal(".Rhistory", m.Pattern) @@ -69,10 +71,25 @@ 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.NoError(err) s.NotNil(m) s.Equal(MatchSourceUser, m.Source) 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, err = ign.Match(ignoredir.Path()) + s.NoError(err) + 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/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 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