Skip to content

Commit 3637bc4

Browse files
authored
Merge pull request #7 from utilitywarehouse/all-files-flag
Add support for checking for all files.
2 parents b0cc3c9 + 664f79c commit 3637bc4

File tree

13 files changed

+113
-13
lines changed

13 files changed

+113
-13
lines changed

main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ func main() {
1212
revision := flag.String("from", "", "revision that should be used to detected "+
1313
"changes in HEAD.\nE.g.: -from=a0e002f951f56d53d552f9427b3331b11ea66e92")
1414

15+
allFiles := flag.Bool("all-files", false, "detect changes in all files, not just go files")
16+
1517
flag.Parse()
1618

1719
args := flag.Args()
@@ -34,7 +36,7 @@ func main() {
3436
os.Exit(1)
3537
}
3638

37-
changes, err := repo.ChangesFrom(*revision)
39+
changes, err := repo.ChangesFrom(*revision, *allFiles)
3840
if err != nil {
3941
fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
4042
os.Exit(1)

patrol/patrol_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"testing"
1212
"time"
1313

14-
"github.com/go-git/go-git/v5"
14+
git "github.com/go-git/go-git/v5"
1515
"github.com/go-git/go-git/v5/plumbing/object"
1616
"github.com/stretchr/testify/assert"
1717
"github.com/stretchr/testify/require"
@@ -28,6 +28,9 @@ type RepoTest struct {
2828

2929
// description of the test, what are trying to assess?
3030
Description string
31+
32+
// is this test for go files only or for all files?
33+
AllFiles bool
3134
}
3235

3336
func (test *RepoTest) Run(t *testing.T) {
@@ -74,7 +77,7 @@ func (test *RepoTest) Run(t *testing.T) {
7477
r, err := patrol.NewRepo(tmp)
7578
require.NoError(t, err)
7679

77-
changes, err := r.ChangesFrom(previousCommit)
80+
changes, err := r.ChangesFrom(previousCommit, test.AllFiles)
7881
require.NoError(t, err)
7982
assert.ElementsMatch(t, expected, changes, test.Name+": expected changes do not match")
8083
}

patrol/repo.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"path/filepath"
1010
"strings"
1111

12-
"github.com/go-git/go-git/v5"
12+
git "github.com/go-git/go-git/v5"
1313
"github.com/go-git/go-git/v5/plumbing"
1414
"golang.org/x/mod/modfile"
1515
)
@@ -93,9 +93,9 @@ func NewRepo(path string) (*Repo, error) {
9393
// packages in vendor/) that changed since the given revision. A package will
9494
// be flagged as change if any file within the package itself changed or if any
9595
// packages it imports (whether local, vendored or external modules) changed
96-
// since the given revision.
97-
func (r *Repo) ChangesFrom(revision string) ([]string, error) {
98-
err := r.detectInternalChangesFrom(revision)
96+
// since the given revision. If allChanges is false it will be only concerned about changes in .go files.
97+
func (r *Repo) ChangesFrom(revision string, allChanges bool) ([]string, error) {
98+
err := r.detectInternalChangesFrom(revision, allChanges)
9999
if err != nil {
100100
return nil, err
101101
}
@@ -194,9 +194,11 @@ func (r *Repo) addDependant(dependant *Package, dependencyName string) {
194194
}
195195

196196
// detectInternalChangesFrom will run a git diff (revision...HEAD) and flag as
197-
// changed any packages (part of the module in repo or vendored packages) that
198-
// have *.go files that are part of the that diff and packages that depend on them
199-
func (r *Repo) detectInternalChangesFrom(revision string) error {
197+
// changed any packages (part of the module in the repo or vendored packages) that
198+
// have files that are part of that diff and packages that depend on them. If allFiles
199+
// is set to true, it checks for changes in all file types. If false, it only checks for
200+
// changes in *.go files.
201+
func (r *Repo) detectInternalChangesFrom(revision string, allFiles bool) error {
200202
repo, err := git.PlainOpen(r.path)
201203
if err != nil {
202204
return err
@@ -243,8 +245,8 @@ func (r *Repo) detectInternalChangesFrom(revision string) error {
243245
}
244246

245247
for _, change := range diff {
246-
// we're only interested in Go files
247-
if !strings.HasSuffix(change.From.Name, ".go") {
248+
if !allFiles && !strings.HasSuffix(change.From.Name, ".go") {
249+
// we're only interested in Go files
248250
continue
249251
}
250252

@@ -257,7 +259,11 @@ func (r *Repo) detectInternalChangesFrom(revision string) error {
257259

258260
// package is part of our module
259261
if pkgName == "" {
260-
pkgName = r.ModuleName() + "/" + filepath.Dir(change.From.Name)
262+
if allFiles {
263+
pkgName = r.ModuleName()
264+
} else {
265+
pkgName = r.ModuleName() + "/" + filepath.Dir(change.From.Name)
266+
}
261267
}
262268

263269
r.flagPackageAsChanged(pkgName)

patrol/repo_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,49 @@ func TestRepo(t *testing.T) {
99
Name: "change within module",
1010
Description: "A change to a package within the same module\n" +
1111
"should flag depending packages as changed",
12+
AllFiles: false,
1213
},
1314
RepoTest{
1415
TestdataFolder: "modules",
1516
Name: "change in go modules dependency",
1617
Description: "A change to a go modules dependency\n" +
1718
"should flag depending packages as changed",
19+
AllFiles: false,
1820
},
1921
RepoTest{
2022
TestdataFolder: "vendoring",
2123
Name: "change in vendored dependencies",
2224
Description: "A change to a vendored dependency\n" +
2325
"should flag depending packages as changed",
26+
AllFiles: false,
2427
},
2528
RepoTest{
2629
TestdataFolder: "exportedtesting",
2730
Name: "change in a package with packagename_test test package",
2831
Description: "A change to package x that is being tested " +
2932
"using x_test package should not result in a stack overflow :D",
33+
AllFiles: false,
3034
},
3135
RepoTest{
3236
TestdataFolder: "submodules",
3337
Name: "change in go modules dependency sub package",
3438
Description: "A change to a go modules dependency\n" +
3539
"should flag depending packages as changed",
40+
AllFiles: false,
3641
},
3742
RepoTest{
3843
TestdataFolder: "alias",
3944
Name: "change in go modules dependency that was aliased",
4045
Description: "A change to a go modules dependency\n" +
4146
"should flag depending packages as changed",
47+
AllFiles: false,
48+
},
49+
RepoTest{
50+
TestdataFolder: "assets",
51+
Name: "change in files that are not go source files",
52+
Description: "A change to a file that is not a go source file\n" +
53+
"should flag a package as changed",
54+
AllFiles: true,
4255
},
4356
}
4457

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/utilitywarehouse/modules
2+
3+
go 1.17
4+
5+
require github.com/sirupsen/logrus v1.8.1
6+
7+
require (
8+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
9+
)
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g=
4+
github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/sirupsen/logrus v1.8.0 h1:nfhvjKcUMhBMVqbKHJlk5RPrrfYr/NMo3692g0dwfWU=
8+
github.com/sirupsen/logrus v1.8.0/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A=
9+
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
10+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
11+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
12+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
"github.com/sirupsen/logrus"
5+
)
6+
7+
func main() {
8+
logrus.Debug("hey")
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE users (
2+
id SERIAL PRIMARY KEY,
3+
username VARCHAR(255) UNIQUE NOT NULL,
4+
password VARCHAR(255) NOT NULL,
5+
email VARCHAR(255) UNIQUE NOT NULL,
6+
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github.com/utilitywarehouse/modules
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/utilitywarehouse/modules
2+
3+
go 1.17
4+
5+
require github.com/sirupsen/logrus v1.8.1
6+
7+
require (
8+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
9+
)
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g=
4+
github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/sirupsen/logrus v1.8.0 h1:nfhvjKcUMhBMVqbKHJlk5RPrrfYr/NMo3692g0dwfWU=
8+
github.com/sirupsen/logrus v1.8.0/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A=
9+
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
10+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
11+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
12+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
"github.com/sirupsen/logrus"
5+
)
6+
7+
func main() {
8+
logrus.Debug("hey")
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TABLE users (
2+
id SERIAL PRIMARY KEY,
3+
username VARCHAR(255) UNIQUE NOT NULL,
4+
password VARCHAR(255) NOT NULL,
5+
email VARCHAR(255) UNIQUE NOT NULL,
6+
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
7+
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
8+
);

0 commit comments

Comments
 (0)