Skip to content

Commit 3bbd6e6

Browse files
committed
Delete files by filters
1 parent 2958087 commit 3bbd6e6

File tree

4 files changed

+102
-16
lines changed

4 files changed

+102
-16
lines changed

pkg/files/delete_by_filters.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package files
2+
3+
import (
4+
"io/fs"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
9+
"github.com/openshift-knative/deviate/pkg/errors"
10+
)
11+
12+
// ErrCantDeleteFiles when cannot delete files.
13+
var ErrCantDeleteFiles = errors.New("cannot delete files")
14+
15+
// DeleteFiles will delete all matching files starting at given root directory.
16+
func (f Filters) DeleteFiles(root string) error {
17+
matcher := f.Matcher()
18+
err := filepath.WalkDir(root, func(pth string, de fs.DirEntry, err error) error {
19+
if err != nil {
20+
return err
21+
}
22+
if de.IsDir() {
23+
// continue
24+
return nil
25+
}
26+
pth = filepath.ToSlash(pth)
27+
relPath := strings.TrimPrefix(pth, root+"/")
28+
if matcher.Matches(relPath) {
29+
return os.Remove(pth)
30+
}
31+
return nil
32+
})
33+
return errors.Wrap(err, ErrCantDeleteFiles)
34+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package files_test
2+
3+
import (
4+
"os"
5+
"path"
6+
"slices"
7+
"testing"
8+
9+
gitv5 "github.com/go-git/go-git/v5"
10+
"github.com/openshift-knative/deviate/pkg/files"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestDeleteByFilters(t *testing.T) {
16+
root := t.TempDir()
17+
paths := []string{
18+
"a.txt",
19+
"a/b.txt",
20+
"a/b/c.md",
21+
"a/b/c.txt",
22+
"a/b/d.md",
23+
"a/b/d.txt",
24+
"a/b/e/f.md",
25+
"a/b/e/f.txt",
26+
"a/c.txt",
27+
"c.txt",
28+
}
29+
for _, file := range paths {
30+
fp := path.Join(root, file)
31+
dir := path.Dir(fp)
32+
require.NoError(t, os.MkdirAll(dir, 0o755))
33+
require.NoError(t, os.WriteFile(fp, []byte("test"), 0o600))
34+
}
35+
36+
filters := files.Filters{
37+
Include: []string{"a/b/**"},
38+
Exclude: []string{"**.txt"},
39+
}
40+
require.NoError(t, filters.DeleteFiles(root))
41+
42+
repo, err := gitv5.PlainInit(root, false)
43+
require.NoError(t, err)
44+
wt, err := repo.Worktree()
45+
require.NoError(t, err)
46+
47+
st, serr := wt.Status()
48+
require.NoError(t, serr)
49+
got := make([]string, 0, len(st))
50+
for f := range st {
51+
got = append(got, f)
52+
}
53+
slices.Sort(got)
54+
want := []string{
55+
"a.txt",
56+
"a/b.txt",
57+
"a/b/c.txt",
58+
"a/b/d.txt",
59+
"a/b/e/f.txt",
60+
"a/c.txt",
61+
"c.txt",
62+
}
63+
assert.Equal(t, want, got)
64+
}

pkg/files/filters.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func (m Matcher) Matches(pth string) bool {
3535
for _, pattern := range m.Include {
3636
if pattern.Match(pth) {
3737
result = true
38+
break
3839
}
3940
}
4041
if !result {
Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
package sync
22

33
import (
4-
"io/fs"
5-
"os"
6-
"path"
7-
"path/filepath"
8-
94
"github.com/openshift-knative/deviate/pkg/errors"
105
)
116

127
func (o Operation) removeUnwantedUpstreamFiles() error {
138
o.Println("- Remove unwanted upstream files")
14-
matcher := o.Config.DeleteFromUpstream.Matcher()
159

16-
return errors.Wrap(filepath.WalkDir(o.State.Project.Path, func(pth string, _ fs.DirEntry, err error) error {
17-
if err != nil {
18-
return err
19-
}
20-
if matcher.Matches(pth) {
21-
fp := path.Join(o.State.Project.Path, pth)
22-
return os.RemoveAll(fp)
23-
}
24-
return nil
25-
}), ErrSyncFailed)
10+
return errors.Wrap(
11+
o.Config.DeleteFromUpstream.DeleteFiles(o.State.Project.Path),
12+
ErrSyncFailed)
2613
}

0 commit comments

Comments
 (0)