@@ -245,7 +245,8 @@ func (r *Repo) detectInternalChangesFrom(revision string, allFiles bool) error {
245
245
}
246
246
247
247
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 {
249
250
// we're only interested in Go files
250
251
continue
251
252
}
@@ -259,11 +260,17 @@ func (r *Repo) detectInternalChangesFrom(revision string, allFiles bool) error {
259
260
260
261
// package is part of our module
261
262
if pkgName == "" {
262
- if allFiles {
263
- pkgName = r .ModuleName ()
264
- } else {
263
+ if goFile {
264
+ // go files are always in packages
265
265
pkgName = r .ModuleName () + "/" + filepath .Dir (change .From .Name )
266
266
}
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
+ }
267
274
}
268
275
269
276
r .flagPackageAsChanged (pkgName )
@@ -272,6 +279,27 @@ func (r *Repo) detectInternalChangesFrom(revision string, allFiles bool) error {
272
279
return nil
273
280
}
274
281
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
+
275
303
// detectGoModulesChanges finds differences in dependencies required by
276
304
// HEAD:go.mod and {revision}:go.mod and flags as changed any packages
277
305
// depending on any of the changed dependencies.
0 commit comments