Skip to content

Commit

Permalink
Merge pull request #204 from rstudio/tdstein/103
Browse files Browse the repository at this point in the history
Fixes ignore matching on directories
  • Loading branch information
tdstein authored Sep 13, 2023
2 parents 398b48b + b3da2c0 commit d1395ff
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
10 changes: 8 additions & 2 deletions internal/bundles/gitignore/gitignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 20 additions & 3 deletions internal/bundles/gitignore/gitignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,39 @@ 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)
s.Equal(gitignorePath.Path(), m.FilePath)
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)
}
8 changes: 5 additions & 3 deletions internal/bundles/gitignore/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -38,16 +38,18 @@ 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 {
args := m.Called(root, fn)
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
6 changes: 5 additions & 1 deletion internal/services/api/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion internal/services/api/files/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/sample-content/fastapi-simple/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.venv

0 comments on commit d1395ff

Please sign in to comment.