-
Notifications
You must be signed in to change notification settings - Fork 2
🎁 Copy/Delete filters #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-bot
merged 9 commits into
openshift-knative:main
from
cardil:feature/upstream-midstream-filters
Jun 9, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8e7df42
Copy/Delete filters
cardil ebce0fc
Tests for filters
cardil e1eaeb8
Merge remote-tracking branch 'upstream/main' into feature/upstream-mi…
cardil 668e488
Extract all files
cardil d21b0ff
Filter only files, not dirs
cardil 8c5c473
Merge branch 'main' into feature/upstream-midstream-filters
cardil 2958087
Checkout test
cardil 3bbd6e6
Delete files by filters
cardil 96eb35c
Start checkout with empty dir
cardil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,10 @@ | ||
| package git | ||
|
|
||
| import ( | ||
| "github.com/openshift-knative/deviate/pkg/files" | ||
| ) | ||
|
|
||
| type Checkout interface { | ||
| As(branch string) error | ||
| OntoWorkspace() error | ||
| OntoWorkspace(filters files.Filters) error | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package files | ||
|
|
||
| import ( | ||
| "io/fs" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/openshift-knative/deviate/pkg/errors" | ||
| ) | ||
|
|
||
| // ErrCantDeleteFiles when cannot delete files. | ||
| var ErrCantDeleteFiles = errors.New("cannot delete files") | ||
|
|
||
| // DeleteFiles will delete all matching files starting at given root directory. | ||
| func (f Filters) DeleteFiles(root string) error { | ||
| matcher := f.Matcher() | ||
| err := filepath.WalkDir(root, func(pth string, de fs.DirEntry, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if de.IsDir() { | ||
| // continue | ||
| return nil | ||
| } | ||
| pth = filepath.ToSlash(pth) | ||
| relPath := strings.TrimPrefix(pth, root+"/") | ||
| if matcher.Matches(relPath) { | ||
| return os.Remove(pth) | ||
| } | ||
| return nil | ||
| }) | ||
| return errors.Wrap(err, ErrCantDeleteFiles) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package files_test | ||
|
|
||
| import ( | ||
| "os" | ||
| "path" | ||
| "slices" | ||
| "testing" | ||
|
|
||
| gitv5 "github.com/go-git/go-git/v5" | ||
| "github.com/openshift-knative/deviate/pkg/files" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestDeleteByFilters(t *testing.T) { | ||
| root := t.TempDir() | ||
| paths := []string{ | ||
| "a.txt", | ||
| "a/b.txt", | ||
| "a/b/c.md", | ||
| "a/b/c.txt", | ||
| "a/b/d.md", | ||
| "a/b/d.txt", | ||
| "a/b/e/f.md", | ||
| "a/b/e/f.txt", | ||
| "a/c.txt", | ||
| "c.txt", | ||
| } | ||
| for _, file := range paths { | ||
| fp := path.Join(root, file) | ||
| dir := path.Dir(fp) | ||
| require.NoError(t, os.MkdirAll(dir, 0o755)) | ||
| require.NoError(t, os.WriteFile(fp, []byte("test"), 0o600)) | ||
| } | ||
|
|
||
| filters := files.Filters{ | ||
| Include: []string{"a/b/**"}, | ||
| Exclude: []string{"**.txt"}, | ||
| } | ||
| require.NoError(t, filters.DeleteFiles(root)) | ||
|
|
||
| repo, err := gitv5.PlainInit(root, false) | ||
| require.NoError(t, err) | ||
| wt, err := repo.Worktree() | ||
| require.NoError(t, err) | ||
|
|
||
| st, serr := wt.Status() | ||
| require.NoError(t, serr) | ||
| got := make([]string, 0, len(st)) | ||
| for f := range st { | ||
| got = append(got, f) | ||
| } | ||
| slices.Sort(got) | ||
| want := []string{ | ||
| "a.txt", | ||
| "a/b.txt", | ||
| "a/b/c.txt", | ||
| "a/b/d.txt", | ||
| "a/b/e/f.txt", | ||
| "a/c.txt", | ||
| "c.txt", | ||
| } | ||
| assert.Equal(t, want, got) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package files | ||
|
|
||
| import "github.com/gobwas/glob" | ||
|
|
||
| // Filters represents what files to include, and which to exclude from copying operations. | ||
| type Filters struct { | ||
| Include []string `json:"include"` | ||
| Exclude []string `json:"exclude"` | ||
| } | ||
|
|
||
| func (f Filters) Matcher() Matcher { | ||
| m := Matcher{ | ||
| Include: make([]glob.Glob, 0, len(f.Include)), | ||
| Exclude: make([]glob.Glob, 0, len(f.Exclude)), | ||
| } | ||
| separators := []rune{'/'} | ||
| for _, p := range f.Include { | ||
| g := glob.MustCompile(p, separators...) | ||
| m.Include = append(m.Include, g) | ||
| } | ||
| for _, p := range f.Exclude { | ||
| g := glob.MustCompile(p, separators...) | ||
| m.Exclude = append(m.Exclude, g) | ||
| } | ||
| return m | ||
| } | ||
|
|
||
| type Matcher struct { | ||
| Include []glob.Glob | ||
| Exclude []glob.Glob | ||
| } | ||
|
|
||
| func (m Matcher) Matches(pth string) bool { | ||
| result := false | ||
| for _, pattern := range m.Include { | ||
| if pattern.Match(pth) { | ||
| result = true | ||
| break | ||
| } | ||
| } | ||
| if !result { | ||
| return false | ||
| } | ||
| for _, pattern := range m.Exclude { | ||
| if pattern.Match(pth) { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package files_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/openshift-knative/deviate/pkg/files" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestFilters_Match(t *testing.T) { | ||
| filelist := []string{ | ||
| "a/b/c.txt", | ||
| "a/d.txt", | ||
| "a/b/d.md", | ||
| "a/b/d.txt", | ||
| "a.txt", | ||
| "b.md", | ||
| } | ||
| tcs := []testFiltersMatchCases{{ | ||
| name: "all md files", | ||
| Filters: files.Filters{ | ||
| Include: []string{"**/*.md", "*.md"}, | ||
| }, | ||
| want: []string{ | ||
| "a/b/d.md", | ||
| "b.md", | ||
| }, | ||
| }, { | ||
| name: "root level md files", | ||
| Filters: files.Filters{ | ||
| Include: []string{"*.md"}, | ||
| }, | ||
| want: []string{ | ||
| "b.md", | ||
| }, | ||
| }, { | ||
| "just one txt", | ||
| files.Filters{ | ||
| Include: []string{"**.txt"}, | ||
| Exclude: []string{"a/b**", "a.txt"}, | ||
| }, | ||
| []string{ | ||
| "a/d.txt", | ||
| }, | ||
| }} | ||
| for _, tc := range tcs { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| matcher := tc.Matcher() | ||
| got := make([]string, 0, len(tc.want)) | ||
| for _, f := range filelist { | ||
| if matcher.Matches(f) { | ||
| got = append(got, f) | ||
| } | ||
| } | ||
|
|
||
| assert.Equal(t, tc.want, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| type testFiltersMatchCases struct { | ||
| name string | ||
| files.Filters | ||
| want []string | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.