Skip to content

Commit 8e7df42

Browse files
committed
Copy/Delete filters
1 parent ead21be commit 8e7df42

File tree

8 files changed

+98
-51
lines changed

8 files changed

+98
-51
lines changed

pkg/config/defaults.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package config
22

3-
import "github.com/openshift-knative/hack/pkg/dockerfilegen"
3+
import (
4+
"github.com/openshift-knative/deviate/pkg/files"
5+
"github.com/openshift-knative/hack/pkg/dockerfilegen"
6+
)
47

58
// newDefaults creates a new default configuration.
69
func newDefaults(project Project) Config {
@@ -9,7 +12,14 @@ func newDefaults(project Project) Config {
912
releaseSearch = `^release-(\d+)\.(\d+)$`
1013
)
1114
return Config{
12-
GithubWorkflowsRemovalGlob: "knative-*.y?ml",
15+
DeleteFromUpstream: files.Filters{
16+
Include: []string{
17+
".github/workflows/knative-*.y?ml",
18+
},
19+
},
20+
CopyFromMidstream: files.Filters{
21+
Include: []string{"*/*"},
22+
},
1323
Branches: Branches{
1424
Main: "main",
1525
ReleaseNext: "release-next",

pkg/config/git/checkout.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package git
22

3+
import (
4+
"github.com/openshift-knative/deviate/pkg/files"
5+
)
6+
37
type Checkout interface {
48
As(branch string) error
5-
OntoWorkspace() error
9+
OntoWorkspace(filters files.Filters) error
610
}

pkg/config/structure.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
package config
22

3-
import "github.com/openshift-knative/hack/pkg/dockerfilegen"
3+
import (
4+
"github.com/openshift-knative/deviate/pkg/files"
5+
"github.com/openshift-knative/hack/pkg/dockerfilegen"
6+
)
47

58
// Config for a deviate to operate.
69
type Config struct {
7-
Upstream string `json:"upstream" valid:"required"`
8-
Downstream string `json:"downstream" valid:"required"`
9-
DryRun bool `json:"dryRun"`
10-
GithubWorkflowsRemovalGlob string `json:"githubWorkflowsRemovalGlob" valid:"required"`
11-
SyncLabels []string `json:"syncLabels" valid:"required"`
12-
DockerfileGen dockerfilegen.Params `json:"dockerfileGen"`
13-
ResyncReleases `json:"resyncReleases"`
14-
Branches `json:"branches"`
15-
Tags `json:"tags"`
16-
Messages `json:"messages"`
10+
Upstream string `json:"upstream" valid:"required"`
11+
Downstream string `json:"downstream" valid:"required"`
12+
DryRun bool `json:"dryRun"`
13+
CopyFromMidstream files.Filters `json:"copyFromMidstream" valid:"required"`
14+
DeleteFromUpstream files.Filters `json:"deleteFromUpstream" valid:"required"`
15+
SyncLabels []string `json:"syncLabels" valid:"required"`
16+
DockerfileGen dockerfilegen.Params `json:"dockerfileGen"`
17+
ResyncReleases `json:"resyncReleases"`
18+
Branches `json:"branches"`
19+
Tags `json:"tags"`
20+
Messages `json:"messages"`
1721
}
1822

1923
// ResyncReleases holds configuration for resyncing past releases.

pkg/files/filters.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package files
2+
3+
import "path"
4+
5+
// Filters represents what files to include, and which to exclude from copying operations.
6+
type Filters struct {
7+
Include []string `json:"include"`
8+
Exclude []string `json:"exclude"`
9+
}
10+
11+
func (f Filters) Matches(pth string) bool {
12+
result := false
13+
for _, pattern := range f.Include {
14+
matched, _ := path.Match(pattern, pth)
15+
if matched {
16+
result = true
17+
}
18+
}
19+
if !result {
20+
return false
21+
}
22+
for _, pattern := range f.Exclude {
23+
matched, _ := path.Match(pattern, pth)
24+
if matched {
25+
return false
26+
}
27+
}
28+
return true
29+
}

pkg/git/checkout.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/go-git/go-git/v5/storage/memory"
1717
"github.com/openshift-knative/deviate/pkg/config/git"
1818
"github.com/openshift-knative/deviate/pkg/errors"
19+
"github.com/openshift-knative/deviate/pkg/files"
1920
)
2021

2122
func (r Repository) Checkout(remote git.Remote, branch string) git.Checkout { //nolint:ireturn
@@ -86,7 +87,7 @@ func (o onGoingCheckout) As(branch string) error {
8687
return nil
8788
}
8889

89-
func (o onGoingCheckout) OntoWorkspace() error {
90+
func (o onGoingCheckout) OntoWorkspace(filters files.Filters) error {
9091
coOpts := &gitv5.CloneOptions{
9192
URL: "file://" + o.repo.Project.Path,
9293
ReferenceName: plumbing.NewBranchReferenceName(o.branch),
@@ -98,18 +99,21 @@ func (o onGoingCheckout) OntoWorkspace() error {
9899
if err != nil {
99100
return errors.Wrap(err, ErrLocalOperationFailed)
100101
}
101-
return o.applyTree(wt, "/")
102+
return o.applyTree(wt, "/", filters)
102103
}
103104

104-
func (o onGoingCheckout) applyTree(fs billy.Filesystem, dir string) error {
105-
files, err := fs.ReadDir(dir)
105+
func (o onGoingCheckout) applyTree(fs billy.Filesystem, dir string, filters files.Filters) error {
106+
infos, err := fs.ReadDir(dir)
106107
if err != nil {
107108
return errors.Wrap(err, ErrLocalOperationFailed)
108109
}
109-
for _, f := range files {
110+
for _, f := range infos {
110111
fp := path.Join(dir, f.Name())
112+
if !filters.Matches(fp) {
113+
continue
114+
}
111115
if f.IsDir() {
112-
err = o.applyTree(fs, fp)
116+
err = o.applyTree(fs, fp, filters)
113117
if err != nil {
114118
return err
115119
}

pkg/sync/fork_files.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
func (o Operation) addForkFiles(rel release) step {
99
return multiStep([]step{
10-
o.removeGithubWorkflows,
10+
o.removeUnwantedUpstreamFiles,
1111
o.unpackForkOntoWorkspace,
1212
o.commitChanges(o.Config.Messages.ApplyForkFiles),
1313
o.generateImages(rel),
@@ -19,6 +19,6 @@ func (o Operation) unpackForkOntoWorkspace() error {
1919
o.Println("- Add fork's files")
2020
upstream := git.Remote{Name: "upstream", URL: o.Config.Upstream}
2121
err := o.Repository.Checkout(upstream, o.Config.Branches.Main).
22-
OntoWorkspace()
22+
OntoWorkspace(o.Config.CopyFromMidstream)
2323
return errors.Wrap(err, ErrSyncFailed)
2424
}

pkg/sync/remove_github_workflows.go

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package sync
2+
3+
import (
4+
"io/fs"
5+
"os"
6+
"path"
7+
"path/filepath"
8+
9+
"github.com/openshift-knative/deviate/pkg/errors"
10+
)
11+
12+
func (o Operation) removeUnwantedUpstreamFiles() error {
13+
o.Println("- Remove unwanted upstream files")
14+
15+
return errors.Wrap(filepath.WalkDir(o.State.Project.Path, func(pth string, _ fs.DirEntry, err error) error {
16+
if err != nil {
17+
return err
18+
}
19+
if o.Config.DeleteFromUpstream.Matches(pth) {
20+
fp := path.Join(o.State.Project.Path, pth)
21+
return os.RemoveAll(fp)
22+
}
23+
return nil
24+
}), ErrSyncFailed)
25+
}

0 commit comments

Comments
 (0)