Skip to content

Commit

Permalink
Handle nested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
matiit committed May 24, 2023
1 parent f94795c commit c52c924
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions patrol/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ func (r *Repo) detectInternalChangesFrom(revision string, allFiles bool) error {
}

for _, change := range diff {
if !allFiles && !strings.HasSuffix(change.From.Name, ".go") {
goFile := strings.HasSuffix(change.From.Name, ".go")
if !allFiles && !goFile {
// we're only interested in Go files
continue
}
Expand All @@ -259,11 +260,17 @@ func (r *Repo) detectInternalChangesFrom(revision string, allFiles bool) error {

// package is part of our module
if pkgName == "" {
if allFiles {
pkgName = r.ModuleName()
} else {
if goFile {
// go files are always in packages
pkgName = r.ModuleName() + "/" + filepath.Dir(change.From.Name)
}
if allFiles && !goFile {
// Non go files belong to the closest package
pkgName, err = r.closestPackageForFileInModule(change.From.Name)
if err != nil {
return err
}
}
}

r.flagPackageAsChanged(pkgName)
Expand All @@ -272,6 +279,27 @@ func (r *Repo) detectInternalChangesFrom(revision string, allFiles bool) error {
return nil
}

// closestPackageForFileInModule returns the closest go package path for the given file
// it will return the module name if no package is found
func (r *Repo) closestPackageForFileInModule(fileName string) (string, error) {
currentDir := filepath.Dir(fileName)
for currentDir != "." {
files, err := os.ReadDir(r.path + "/" + currentDir)
if err != nil {
return "", err
}

for _, f := range files {
if strings.HasSuffix(f.Name(), ".go") {
return r.ModuleName() + "/" + currentDir, nil
}
}

currentDir = filepath.Dir(currentDir)
}
return r.ModuleName(), nil
}

// detectGoModulesChanges finds differences in dependencies required by
// HEAD:go.mod and {revision}:go.mod and flags as changed any packages
// depending on any of the changed dependencies.
Expand Down

0 comments on commit c52c924

Please sign in to comment.