Skip to content

Commit c52c924

Browse files
committed
Handle nested changes
1 parent f94795c commit c52c924

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

patrol/repo.go

+32-4
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ func (r *Repo) detectInternalChangesFrom(revision string, allFiles bool) error {
245245
}
246246

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

260261
// package is part of our module
261262
if pkgName == "" {
262-
if allFiles {
263-
pkgName = r.ModuleName()
264-
} else {
263+
if goFile {
264+
// go files are always in packages
265265
pkgName = r.ModuleName() + "/" + filepath.Dir(change.From.Name)
266266
}
267+
if allFiles && !goFile {
268+
// Non go files belong to the closest package
269+
pkgName, err = r.closestPackageForFileInModule(change.From.Name)
270+
if err != nil {
271+
return err
272+
}
273+
}
267274
}
268275

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

282+
// closestPackageForFileInModule returns the closest go package path for the given file
283+
// it will return the module name if no package is found
284+
func (r *Repo) closestPackageForFileInModule(fileName string) (string, error) {
285+
currentDir := filepath.Dir(fileName)
286+
for currentDir != "." {
287+
files, err := os.ReadDir(r.path + "/" + currentDir)
288+
if err != nil {
289+
return "", err
290+
}
291+
292+
for _, f := range files {
293+
if strings.HasSuffix(f.Name(), ".go") {
294+
return r.ModuleName() + "/" + currentDir, nil
295+
}
296+
}
297+
298+
currentDir = filepath.Dir(currentDir)
299+
}
300+
return r.ModuleName(), nil
301+
}
302+
275303
// detectGoModulesChanges finds differences in dependencies required by
276304
// HEAD:go.mod and {revision}:go.mod and flags as changed any packages
277305
// depending on any of the changed dependencies.

0 commit comments

Comments
 (0)