From ddd9c4d1c340d98b5a75e38c3d8bd8e3be987a0c Mon Sep 17 00:00:00 2001 From: Mateusz Tracz Date: Fri, 19 May 2023 12:17:34 +0100 Subject: [PATCH 1/3] Add support for checking for all files. --- .idea/.gitignore | 8 +++++++ .idea/modules.xml | 8 +++++++ .idea/patrol.iml | 9 +++++++ .idea/vcs.xml | 6 +++++ main.go | 4 +++- patrol/patrol_test.go | 5 +++- patrol/repo.go | 24 ++++++++++++------- patrol/repo_test.go | 13 ++++++++++ patrol/testdata/assets/commits/1/go.mod | 9 +++++++ patrol/testdata/assets/commits/1/go.sum | 12 ++++++++++ patrol/testdata/assets/commits/1/main.go | 9 +++++++ .../assets/commits/1/migrations/001.sql | 7 ++++++ .../testdata/assets/commits/2/changes.patrol | 1 + patrol/testdata/assets/commits/2/go.mod | 9 +++++++ patrol/testdata/assets/commits/2/go.sum | 12 ++++++++++ patrol/testdata/assets/commits/2/main.go | 9 +++++++ .../assets/commits/2/migrations/001.sql | 8 +++++++ 17 files changed, 142 insertions(+), 11 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/patrol.iml create mode 100644 .idea/vcs.xml create mode 100644 patrol/testdata/assets/commits/1/go.mod create mode 100644 patrol/testdata/assets/commits/1/go.sum create mode 100644 patrol/testdata/assets/commits/1/main.go create mode 100644 patrol/testdata/assets/commits/1/migrations/001.sql create mode 100644 patrol/testdata/assets/commits/2/changes.patrol create mode 100644 patrol/testdata/assets/commits/2/go.mod create mode 100644 patrol/testdata/assets/commits/2/go.sum create mode 100644 patrol/testdata/assets/commits/2/main.go create mode 100644 patrol/testdata/assets/commits/2/migrations/001.sql diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d7d8c00 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/patrol.iml b/.idea/patrol.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/patrol.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/main.go b/main.go index e1cff17..af87c64 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,8 @@ func main() { revision := flag.String("from", "", "revision that should be used to detected "+ "changes in HEAD.\nE.g.: -from=a0e002f951f56d53d552f9427b3331b11ea66e92") + allFiles := flag.Bool("all-files", false, "detect changes in all files, not just go files") + flag.Parse() args := flag.Args() @@ -34,7 +36,7 @@ func main() { os.Exit(1) } - changes, err := repo.ChangesFrom(*revision) + changes, err := repo.ChangesFrom(*revision, *allFiles) if err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err.Error()) os.Exit(1) diff --git a/patrol/patrol_test.go b/patrol/patrol_test.go index ba0a6db..157ef94 100644 --- a/patrol/patrol_test.go +++ b/patrol/patrol_test.go @@ -28,6 +28,9 @@ type RepoTest struct { // description of the test, what are trying to assess? Description string + + // is this test for go files only or for all files? + AllFiles bool } func (test *RepoTest) Run(t *testing.T) { @@ -74,7 +77,7 @@ func (test *RepoTest) Run(t *testing.T) { r, err := patrol.NewRepo(tmp) require.NoError(t, err) - changes, err := r.ChangesFrom(previousCommit) + changes, err := r.ChangesFrom(previousCommit, test.AllFiles) require.NoError(t, err) assert.ElementsMatch(t, expected, changes, test.Name+": expected changes do not match") } diff --git a/patrol/repo.go b/patrol/repo.go index 2618a07..00a4a1a 100644 --- a/patrol/repo.go +++ b/patrol/repo.go @@ -93,9 +93,9 @@ func NewRepo(path string) (*Repo, error) { // packages in vendor/) that changed since the given revision. A package will // be flagged as change if any file within the package itself changed or if any // packages it imports (whether local, vendored or external modules) changed -// since the given revision. -func (r *Repo) ChangesFrom(revision string) ([]string, error) { - err := r.detectInternalChangesFrom(revision) +// since the given revision. If allChanges is false it will be only concerned about changes in .go files. +func (r *Repo) ChangesFrom(revision string, allChanges bool) ([]string, error) { + err := r.detectInternalChangesFrom(revision, allChanges) if err != nil { return nil, err } @@ -194,9 +194,11 @@ func (r *Repo) addDependant(dependant *Package, dependencyName string) { } // detectInternalChangesFrom will run a git diff (revision...HEAD) and flag as -// changed any packages (part of the module in repo or vendored packages) that -// have *.go files that are part of the that diff and packages that depend on them -func (r *Repo) detectInternalChangesFrom(revision string) error { +// changed any packages (part of the module in the repo or vendored packages) that +// have files that are part of that diff and packages that depend on them. If allFiles +// is set to true, it checks for changes in all file types. If false, it only checks for +// changes in *.go files. +func (r *Repo) detectInternalChangesFrom(revision string, allFiles bool) error { repo, err := git.PlainOpen(r.path) if err != nil { return err @@ -243,8 +245,8 @@ func (r *Repo) detectInternalChangesFrom(revision string) error { } for _, change := range diff { - // we're only interested in Go files - if !strings.HasSuffix(change.From.Name, ".go") { + if !allFiles && !strings.HasSuffix(change.From.Name, ".go") { + // we're only interested in Go files continue } @@ -257,7 +259,11 @@ func (r *Repo) detectInternalChangesFrom(revision string) error { // package is part of our module if pkgName == "" { - pkgName = r.ModuleName() + "/" + filepath.Dir(change.From.Name) + if allFiles { + pkgName = r.ModuleName() + } else { + pkgName = r.ModuleName() + "/" + filepath.Dir(change.From.Name) + } } r.flagPackageAsChanged(pkgName) diff --git a/patrol/repo_test.go b/patrol/repo_test.go index 98ce6c5..9ab2553 100644 --- a/patrol/repo_test.go +++ b/patrol/repo_test.go @@ -9,36 +9,49 @@ func TestRepo(t *testing.T) { Name: "change within module", Description: "A change to a package within the same module\n" + "should flag depending packages as changed", + AllFiles: false, }, RepoTest{ TestdataFolder: "modules", Name: "change in go modules dependency", Description: "A change to a go modules dependency\n" + "should flag depending packages as changed", + AllFiles: false, }, RepoTest{ TestdataFolder: "vendoring", Name: "change in vendored dependencies", Description: "A change to a vendored dependency\n" + "should flag depending packages as changed", + AllFiles: false, }, RepoTest{ TestdataFolder: "exportedtesting", Name: "change in a package with packagename_test test package", Description: "A change to package x that is being tested " + "using x_test package should not result in a stack overflow :D", + AllFiles: false, }, RepoTest{ TestdataFolder: "submodules", Name: "change in go modules dependency sub package", Description: "A change to a go modules dependency\n" + "should flag depending packages as changed", + AllFiles: false, }, RepoTest{ TestdataFolder: "alias", Name: "change in go modules dependency that was aliased", Description: "A change to a go modules dependency\n" + "should flag depending packages as changed", + AllFiles: false, + }, + RepoTest{ + TestdataFolder: "assets", + Name: "change in files that are not go source files", + Description: "A change to a file that is not a go source file\n" + + "should flag a package as changed", + AllFiles: true, }, } diff --git a/patrol/testdata/assets/commits/1/go.mod b/patrol/testdata/assets/commits/1/go.mod new file mode 100644 index 0000000..e5cb8b4 --- /dev/null +++ b/patrol/testdata/assets/commits/1/go.mod @@ -0,0 +1,9 @@ +module github.com/utilitywarehouse/modules + +go 1.17 + +require github.com/sirupsen/logrus v1.8.1 + +require ( + golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect +) diff --git a/patrol/testdata/assets/commits/1/go.sum b/patrol/testdata/assets/commits/1/go.sum new file mode 100644 index 0000000..a42fced --- /dev/null +++ b/patrol/testdata/assets/commits/1/go.sum @@ -0,0 +1,12 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g= +github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.8.0 h1:nfhvjKcUMhBMVqbKHJlk5RPrrfYr/NMo3692g0dwfWU= +github.com/sirupsen/logrus v1.8.0/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/patrol/testdata/assets/commits/1/main.go b/patrol/testdata/assets/commits/1/main.go new file mode 100644 index 0000000..4bc4063 --- /dev/null +++ b/patrol/testdata/assets/commits/1/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "github.com/sirupsen/logrus" +) + +func main() { + logrus.Debug("hey") +} diff --git a/patrol/testdata/assets/commits/1/migrations/001.sql b/patrol/testdata/assets/commits/1/migrations/001.sql new file mode 100644 index 0000000..59f18f3 --- /dev/null +++ b/patrol/testdata/assets/commits/1/migrations/001.sql @@ -0,0 +1,7 @@ +CREATE TABLE users ( + id SERIAL PRIMARY KEY, + username VARCHAR(255) UNIQUE NOT NULL, + password VARCHAR(255) NOT NULL, + email VARCHAR(255) UNIQUE NOT NULL, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); \ No newline at end of file diff --git a/patrol/testdata/assets/commits/2/changes.patrol b/patrol/testdata/assets/commits/2/changes.patrol new file mode 100644 index 0000000..8943f4a --- /dev/null +++ b/patrol/testdata/assets/commits/2/changes.patrol @@ -0,0 +1 @@ +github.com/utilitywarehouse/modules diff --git a/patrol/testdata/assets/commits/2/go.mod b/patrol/testdata/assets/commits/2/go.mod new file mode 100644 index 0000000..e5cb8b4 --- /dev/null +++ b/patrol/testdata/assets/commits/2/go.mod @@ -0,0 +1,9 @@ +module github.com/utilitywarehouse/modules + +go 1.17 + +require github.com/sirupsen/logrus v1.8.1 + +require ( + golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect +) diff --git a/patrol/testdata/assets/commits/2/go.sum b/patrol/testdata/assets/commits/2/go.sum new file mode 100644 index 0000000..a42fced --- /dev/null +++ b/patrol/testdata/assets/commits/2/go.sum @@ -0,0 +1,12 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g= +github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.8.0 h1:nfhvjKcUMhBMVqbKHJlk5RPrrfYr/NMo3692g0dwfWU= +github.com/sirupsen/logrus v1.8.0/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/patrol/testdata/assets/commits/2/main.go b/patrol/testdata/assets/commits/2/main.go new file mode 100644 index 0000000..4bc4063 --- /dev/null +++ b/patrol/testdata/assets/commits/2/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "github.com/sirupsen/logrus" +) + +func main() { + logrus.Debug("hey") +} diff --git a/patrol/testdata/assets/commits/2/migrations/001.sql b/patrol/testdata/assets/commits/2/migrations/001.sql new file mode 100644 index 0000000..9318892 --- /dev/null +++ b/patrol/testdata/assets/commits/2/migrations/001.sql @@ -0,0 +1,8 @@ +CREATE TABLE users ( + id SERIAL PRIMARY KEY, + username VARCHAR(255) UNIQUE NOT NULL, + password VARCHAR(255) NOT NULL, + email VARCHAR(255) UNIQUE NOT NULL, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); \ No newline at end of file From b04ab0d9572ea32cd1703908d179dd0f7950a056 Mon Sep 17 00:00:00 2001 From: Mateusz Tracz Date: Fri, 19 May 2023 14:36:59 +0100 Subject: [PATCH 2/3] named import --- patrol/patrol_test.go | 2 +- patrol/repo.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/patrol/patrol_test.go b/patrol/patrol_test.go index 157ef94..c206b9d 100644 --- a/patrol/patrol_test.go +++ b/patrol/patrol_test.go @@ -11,7 +11,7 @@ import ( "testing" "time" - "github.com/go-git/go-git/v5" + git "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing/object" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/patrol/repo.go b/patrol/repo.go index 00a4a1a..ed1865e 100644 --- a/patrol/repo.go +++ b/patrol/repo.go @@ -9,7 +9,7 @@ import ( "path/filepath" "strings" - "github.com/go-git/go-git/v5" + git "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "golang.org/x/mod/modfile" ) From 2fb3b70e3ed6a86ee75ce03c5b5766336346a5ef Mon Sep 17 00:00:00 2001 From: Mateusz Tracz Date: Mon, 22 May 2023 12:13:50 +0100 Subject: [PATCH 3/3] Not adding .idea --- .idea/.gitignore | 8 -------- .idea/modules.xml | 8 -------- .idea/patrol.iml | 9 --------- .idea/vcs.xml | 6 ------ 4 files changed, 31 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/modules.xml delete mode 100644 .idea/patrol.iml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index d7d8c00..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/patrol.iml b/.idea/patrol.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/patrol.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file