Skip to content

Commit

Permalink
Merge pull request #7 from utilitywarehouse/all-files-flag
Browse files Browse the repository at this point in the history
Add support for checking for all files.
  • Loading branch information
matiit committed May 22, 2023
2 parents b0cc3c9 + 664f79c commit 3637bc4
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 13 deletions.
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions patrol/patrol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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")
}
Expand Down
26 changes: 16 additions & 10 deletions patrol/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions patrol/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down
9 changes: 9 additions & 0 deletions patrol/testdata/assets/commits/1/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
12 changes: 12 additions & 0 deletions patrol/testdata/assets/commits/1/go.sum
Original file line number Diff line number Diff line change
@@ -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=
9 changes: 9 additions & 0 deletions patrol/testdata/assets/commits/1/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"github.com/sirupsen/logrus"
)

func main() {
logrus.Debug("hey")
}
7 changes: 7 additions & 0 deletions patrol/testdata/assets/commits/1/migrations/001.sql
Original file line number Diff line number Diff line change
@@ -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
);
1 change: 1 addition & 0 deletions patrol/testdata/assets/commits/2/changes.patrol
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github.com/utilitywarehouse/modules
9 changes: 9 additions & 0 deletions patrol/testdata/assets/commits/2/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
12 changes: 12 additions & 0 deletions patrol/testdata/assets/commits/2/go.sum
Original file line number Diff line number Diff line change
@@ -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=
9 changes: 9 additions & 0 deletions patrol/testdata/assets/commits/2/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"github.com/sirupsen/logrus"
)

func main() {
logrus.Debug("hey")
}
8 changes: 8 additions & 0 deletions patrol/testdata/assets/commits/2/migrations/001.sql
Original file line number Diff line number Diff line change
@@ -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
);

0 comments on commit 3637bc4

Please sign in to comment.