@@ -33,8 +33,7 @@ type ScanResult struct {
3333// displayPath mimics diff(1) output for relative paths.
3434func displayPath (base , path string ) string {
3535 if filepath .IsAbs (path ) {
36- rel , err := filepath .Rel (base , path )
37- if err == nil {
36+ if rel , err := filepath .Rel (base , path ); err == nil {
3837 return rel
3938 }
4039 }
@@ -43,9 +42,11 @@ func displayPath(base, path string) string {
4342
4443// relPath returns the cleanest possible relative path between a source path and files within said path.
4544func 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
45+ var (
46+ base , rel string
47+ err error
48+ )
49+
4950 switch {
5051 case isArchive :
5152 fromRoot := fr .ArchiveRoot
@@ -136,10 +137,16 @@ func relFileReport(ctx context.Context, c malcontent.Config, fromPath string, is
136137 }
137138
138139 fromRelPath := map [string ]* malcontent.FileReport {}
139- var base string
140- var rangeErr error
140+
141+ var (
142+ base string
143+ rangeErr error
144+ )
141145
142146 fromReport .Files .Range (func (key , value any ) bool {
147+ if ctx .Err () != nil {
148+ return false
149+ }
143150 if key == nil || value == nil {
144151 return true
145152 }
@@ -171,29 +178,21 @@ func relFileReport(ctx context.Context, c malcontent.Config, fromPath string, is
171178
172179// scoreFile returns a boolean to determine how individual files are stored in a diff report.
173180func scoreFile (fr , tr * malcontent.FileReport ) bool {
174- scoreSrc := false
175- scoreDest := false
176-
177181 patterns := []string {
178182 `^[\w.-]+\.so$` ,
179183 `^.+-.*-r\d+\.spdx\.json$` ,
180184 }
181185
182186 for _ , pattern := range patterns {
183187 re := regexp .MustCompile (pattern )
184- if re . MatchString ( fr . Path ) {
185- scoreSrc = true
186- }
187- if re .MatchString (tr .Path ) {
188- scoreDest = true
188+
189+ // If both files match patterns, return true to indicate that `inferMoves` should be used
190+ // Otherwise, indicate that `handleFile` should be used
191+ if re .MatchString (fr . Path ) && re . MatchString ( tr .Path ) {
192+ return true
189193 }
190194 }
191195
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- }
197196 return false
198197}
199198
@@ -206,13 +205,15 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
206205 return nil , fmt .Errorf ("diff mode requires 2 paths, you passed in %d path(s)" , len (c .ScanPaths ))
207206 }
208207
209- srcPath := c .ScanPaths [0 ]
210- destPath := c .ScanPaths [1 ]
208+ srcPath , destPath := c .ScanPaths [0 ], c .ScanPaths [1 ]
211209
212210 // If diffing images, use their temporary directories as scan paths
213211 // Flip c.OCI to false when finished to block other image code paths
214- var isImage bool
215- var err error
212+ var (
213+ err error
214+ isImage bool
215+ )
216+
216217 if c .OCI {
217218 srcPath , err = archive .OCI (ctx , srcPath )
218219 if err != nil {
@@ -222,24 +223,20 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
222223 if err != nil {
223224 return nil , fmt .Errorf ("failed to prepare scan path: %w" , err )
224225 }
225- isImage = true
226- c .OCI = false
226+ isImage , c .OCI = true , false
227227 }
228228
229229 var g errgroup.Group
230230
231- srcCh := make (chan ScanResult , 1 )
232- destCh := make (chan ScanResult , 1 )
231+ srcCh , destCh := make (chan ScanResult , 1 ), make (chan ScanResult , 1 )
233232
234- srcIsArchive := programkind .IsSupportedArchive (srcPath )
235- destIsArchive := programkind .IsSupportedArchive (destPath )
233+ srcIsArchive , destIsArchive := programkind .IsSupportedArchive (srcPath ), programkind .IsSupportedArchive (destPath )
236234
237235 g .Go (func () error {
238236 files , base , err := relFileReport (ctx , c , srcPath , isImage )
239237 res := ScanResult {files : files , base : base , err : err }
240238 if isImage {
241- res .imageURI = c .ScanPaths [0 ]
242- res .tmpRoot = srcPath
239+ res .imageURI , res .tmpRoot = c .ScanPaths [0 ], srcPath
243240 }
244241 srcCh <- res
245242 return err
@@ -249,8 +246,7 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
249246 files , base , err := relFileReport (ctx , c , destPath , isImage )
250247 res := ScanResult {files : files , base : base , err : err }
251248 if isImage {
252- res .imageURI = c .ScanPaths [1 ]
253- res .tmpRoot = destPath
249+ res .imageURI , res .tmpRoot = c .ScanPaths [1 ], destPath
254250 }
255251 destCh <- res
256252 return err
@@ -293,7 +289,9 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
293289 // and employ add/delete for files that are not the same
294290 // When scanning two files, do a 1:1 comparison and
295291 // consider the source -> destination as a change rather than an add/delete
296- if ((srcInfo .IsDir () && destInfo .IsDir ()) || (srcIsArchive && destIsArchive )) || isImage {
292+ shouldHandleDir := ((srcInfo .IsDir () && destInfo .IsDir ()) || (srcIsArchive && destIsArchive )) || isImage
293+
294+ if shouldHandleDir {
297295 handleDir (ctx , c , srcResult , destResult , d , isImage )
298296 } else {
299297 var srcFile , destFile * malcontent.FileReport
@@ -330,8 +328,7 @@ func handleDir(ctx context.Context, c malcontent.Config, src, dest ScanResult, d
330328 return
331329 }
332330
333- srcFiles := make (map [string ]* malcontent.FileReport )
334- destFiles := make (map [string ]* malcontent.FileReport )
331+ srcFiles , destFiles := make (map [string ]* malcontent.FileReport ), make (map [string ]* malcontent.FileReport )
335332
336333 for path , fr := range src .files {
337334 base := filepath .Base (path )
0 commit comments