@@ -11,6 +11,7 @@ import (
1111 "os"
1212 "path/filepath"
1313 "regexp"
14+ "slices"
1415 "strings"
1516
1617 "github.com/agext/levenshtein"
@@ -33,8 +34,7 @@ type ScanResult struct {
3334// displayPath mimics diff(1) output for relative paths.
3435func displayPath (base , path string ) string {
3536 if filepath .IsAbs (path ) {
36- rel , err := filepath .Rel (base , path )
37- if err == nil {
37+ if rel , err := filepath .Rel (base , path ); err == nil {
3838 return rel
3939 }
4040 }
@@ -43,9 +43,11 @@ func displayPath(base, path string) string {
4343
4444// relPath returns the cleanest possible relative path between a source path and files within said path.
4545func relPath (from string , fr * malcontent.FileReport , isArchive bool , isImage bool ) (string , string , error ) {
46- var base string
47- var err error
48- var rel string
46+ var (
47+ base , rel string
48+ err error
49+ )
50+
4951 switch {
5052 case isArchive :
5153 fromRoot := fr .ArchiveRoot
@@ -136,10 +138,16 @@ func relFileReport(ctx context.Context, c malcontent.Config, fromPath string, is
136138 }
137139
138140 fromRelPath := map [string ]* malcontent.FileReport {}
139- var base string
140- var rangeErr error
141+
142+ var (
143+ base string
144+ rangeErr error
145+ )
141146
142147 fromReport .Files .Range (func (key , value any ) bool {
148+ if ctx .Err () != nil {
149+ return false
150+ }
143151 if key == nil || value == nil {
144152 return true
145153 }
@@ -171,29 +179,21 @@ func relFileReport(ctx context.Context, c malcontent.Config, fromPath string, is
171179
172180// scoreFile returns a boolean to determine how individual files are stored in a diff report.
173181func scoreFile (fr , tr * malcontent.FileReport ) bool {
174- scoreSrc := false
175- scoreDest := false
176-
177182 patterns := []string {
178183 `^[\w.-]+\.so$` ,
179184 `^.+-.*-r\d+\.spdx\.json$` ,
180185 }
181186
182187 for _ , pattern := range patterns {
183188 re := regexp .MustCompile (pattern )
184- if re . MatchString ( fr . Path ) {
185- scoreSrc = true
186- }
187- if re .MatchString (tr .Path ) {
188- scoreDest = true
189+
190+ // If both files match patterns, reeturn true to indicate that `inferMoves` should be used
191+ // Otherwise, indicate that `handleFile` should be used
192+ if re .MatchString (fr . Path ) && re . MatchString ( tr .Path ) {
193+ return true
189194 }
190195 }
191196
192- // If both files match patterns, reeturn true to indicate that `inferMoves` should be used
193- // Otherwise, indicate that `handleFile` should be used
194- if scoreSrc && scoreDest {
195- return true
196- }
197197 return false
198198}
199199
@@ -206,13 +206,15 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
206206 return nil , fmt .Errorf ("diff mode requires 2 paths, you passed in %d path(s)" , len (c .ScanPaths ))
207207 }
208208
209- srcPath := c .ScanPaths [0 ]
210- destPath := c .ScanPaths [1 ]
209+ srcPath , destPath := c .ScanPaths [0 ], c .ScanPaths [1 ]
211210
212211 // If diffing images, use their temporary directories as scan paths
213212 // Flip c.OCI to false when finished to block other image code paths
214- var isImage bool
215- var err error
213+ var (
214+ err error
215+ isImage bool
216+ )
217+
216218 if c .OCI {
217219 srcPath , err = archive .OCI (ctx , srcPath )
218220 if err != nil {
@@ -222,24 +224,20 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
222224 if err != nil {
223225 return nil , fmt .Errorf ("failed to prepare scan path: %w" , err )
224226 }
225- isImage = true
226- c .OCI = false
227+ isImage , c .OCI = true , false
227228 }
228229
229230 var g errgroup.Group
230231
231- srcCh := make (chan ScanResult , 1 )
232- destCh := make (chan ScanResult , 1 )
232+ srcCh , destCh := make (chan ScanResult , 1 ), make (chan ScanResult , 1 )
233233
234- srcIsArchive := programkind .IsSupportedArchive (srcPath )
235- destIsArchive := programkind .IsSupportedArchive (destPath )
234+ srcIsArchive , destIsArchive := programkind .IsSupportedArchive (srcPath ), programkind .IsSupportedArchive (destPath )
236235
237236 g .Go (func () error {
238237 files , base , err := relFileReport (ctx , c , srcPath , isImage )
239238 res := ScanResult {files : files , base : base , err : err }
240239 if isImage {
241- res .imageURI = c .ScanPaths [0 ]
242- res .tmpRoot = srcPath
240+ res .imageURI , res .tmpRoot = c .ScanPaths [0 ], srcPath
243241 }
244242 srcCh <- res
245243 return err
@@ -249,8 +247,7 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
249247 files , base , err := relFileReport (ctx , c , destPath , isImage )
250248 res := ScanResult {files : files , base : base , err : err }
251249 if isImage {
252- res .imageURI = c .ScanPaths [1 ]
253- res .tmpRoot = destPath
250+ res .imageURI , res .tmpRoot = c .ScanPaths [1 ], destPath
254251 }
255252 destCh <- res
256253 return err
@@ -293,7 +290,9 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
293290 // and employ add/delete for files that are not the same
294291 // When scanning two files, do a 1:1 comparison and
295292 // consider the source -> destination as a change rather than an add/delete
296- if ((srcInfo .IsDir () && destInfo .IsDir ()) || (srcIsArchive && destIsArchive )) || isImage {
293+ shouldHandleDir := ((srcInfo .IsDir () && destInfo .IsDir ()) || (srcIsArchive && destIsArchive )) || isImage
294+
295+ if shouldHandleDir {
297296 handleDir (ctx , c , srcResult , destResult , d , isImage )
298297 } else {
299298 var srcFile , destFile * malcontent.FileReport
@@ -330,8 +329,7 @@ func handleDir(ctx context.Context, c malcontent.Config, src, dest ScanResult, d
330329 return
331330 }
332331
333- srcFiles := make (map [string ]* malcontent.FileReport )
334- destFiles := make (map [string ]* malcontent.FileReport )
332+ srcFiles , destFiles := make (map [string ]* malcontent.FileReport ), make (map [string ]* malcontent.FileReport )
335333
336334 for path , fr := range src .files {
337335 base := filepath .Base (path )
0 commit comments