@@ -543,7 +543,7 @@ func getGitDir(path string, options *ScanOptions) string {
543
543
544
544
func (s * Git ) ScanCommits (ctx context.Context , repo * git.Repository , path string , scanOptions * ScanOptions , reporter sources.ChunkReporter ) error {
545
545
// Get the remote URL for reporting (may be empty)
546
- remoteURL := getSafeRemoteURL (repo , "origin" )
546
+ remoteURL := GetSafeRemoteURL (repo , "origin" )
547
547
var repoCtx context.Context
548
548
549
549
if ctx .Value ("repo" ) == nil {
@@ -642,6 +642,15 @@ func (s *Git) ScanCommits(ctx context.Context, repo *git.Repository, path string
642
642
643
643
// Handle binary files by reading the entire file rather than using the diff.
644
644
if diff .IsBinary {
645
+ commitHash := plumbing .NewHash (fullHash )
646
+
647
+ if s .skipBinaries || feature .ForceSkipBinaries .Load () {
648
+ logger .V (5 ).Info ("skipping binary file" ,
649
+ "commit" , commitHash .String ()[:7 ],
650
+ "path" , path )
651
+ continue
652
+ }
653
+
645
654
metadata := s .sourceMetadataFunc (fileName , email , fullHash , when , remoteURL , 0 )
646
655
chunkSkel := & sources.Chunk {
647
656
SourceName : s .sourceName ,
@@ -652,8 +661,7 @@ func (s *Git) ScanCommits(ctx context.Context, repo *git.Repository, path string
652
661
Verify : s .verify ,
653
662
}
654
663
655
- commitHash := plumbing .NewHash (fullHash )
656
- if err := s .handleBinary (ctx , gitDir , reporter , chunkSkel , commitHash , fileName ); err != nil {
664
+ if err := HandleBinary (ctx , gitDir , reporter , chunkSkel , commitHash , fileName , s .skipArchives ); err != nil {
657
665
logger .Error (
658
666
err ,
659
667
"error handling binary file" ,
@@ -793,7 +801,7 @@ func (s *Git) gitChunk(ctx context.Context, diff *gitparse.Diff, fileName, email
793
801
// ScanStaged chunks staged changes.
794
802
func (s * Git ) ScanStaged (ctx context.Context , repo * git.Repository , path string , scanOptions * ScanOptions , reporter sources.ChunkReporter ) error {
795
803
// Get the URL metadata for reporting (may be empty).
796
- urlMetadata := getSafeRemoteURL (repo , "origin" )
804
+ urlMetadata := GetSafeRemoteURL (repo , "origin" )
797
805
798
806
diffChan , err := s .parser .Staged (ctx , path )
799
807
if err != nil {
@@ -864,6 +872,14 @@ func (s *Git) ScanStaged(ctx context.Context, repo *git.Repository, path string,
864
872
// Handle binary files by reading the entire file rather than using the diff.
865
873
if diff .IsBinary {
866
874
commitHash := plumbing .NewHash (fullHash )
875
+
876
+ if s .skipBinaries || feature .ForceSkipBinaries .Load () {
877
+ logger .V (5 ).Info ("skipping binary file" ,
878
+ "commit" , commitHash .String ()[:7 ],
879
+ "path" , path )
880
+ continue
881
+ }
882
+
867
883
metadata := s .sourceMetadataFunc (fileName , email , "Staged" , when , urlMetadata , 0 )
868
884
chunkSkel := & sources.Chunk {
869
885
SourceName : s .sourceName ,
@@ -873,7 +889,7 @@ func (s *Git) ScanStaged(ctx context.Context, repo *git.Repository, path string,
873
889
SourceMetadata : metadata ,
874
890
Verify : s .verify ,
875
891
}
876
- if err := s . handleBinary (ctx , gitDir , reporter , chunkSkel , commitHash , fileName ); err != nil {
892
+ if err := HandleBinary (ctx , gitDir , reporter , chunkSkel , commitHash , fileName , s . skipArchives ); err != nil {
877
893
logger .Error (err , "error handling binary file" )
878
894
}
879
895
continue
@@ -938,7 +954,7 @@ func (s *Git) ScanRepo(ctx context.Context, repo *git.Repository, repoPath strin
938
954
remotes , _ := repo .Remotes ()
939
955
repoURL := "Could not get remote for repo"
940
956
if len (remotes ) != 0 {
941
- repoURL = getSafeRemoteURL (repo , remotes [0 ].Config ().Name )
957
+ repoURL = GetSafeRemoteURL (repo , remotes [0 ].Config ().Name )
942
958
}
943
959
logger = logger .WithValues ("repo" , repoURL )
944
960
}
@@ -1190,10 +1206,10 @@ func PrepareRepo(ctx context.Context, uriString string) (string, bool, error) {
1190
1206
return path , remote , nil
1191
1207
}
1192
1208
1193
- // getSafeRemoteURL is a helper function that will attempt to get a safe URL first
1209
+ // GetSafeRemoteURL is a helper function that will attempt to get a safe URL first
1194
1210
// from the preferred remote name, falling back to the first remote name
1195
1211
// available, or an empty string if there are no remotes.
1196
- func getSafeRemoteURL (repo * git.Repository , preferred string ) string {
1212
+ func GetSafeRemoteURL (repo * git.Repository , preferred string ) string {
1197
1213
remote , err := repo .Remote (preferred )
1198
1214
if err != nil {
1199
1215
var remotes []* git.Remote
@@ -1213,13 +1229,14 @@ func getSafeRemoteURL(repo *git.Repository, preferred string) string {
1213
1229
return safeURL
1214
1230
}
1215
1231
1216
- func ( s * Git ) handleBinary (
1232
+ func HandleBinary (
1217
1233
ctx context.Context ,
1218
1234
gitDir string ,
1219
1235
reporter sources.ChunkReporter ,
1220
1236
chunkSkel * sources.Chunk ,
1221
1237
commitHash plumbing.Hash ,
1222
1238
path string ,
1239
+ skipArchives bool ,
1223
1240
) (err error ) {
1224
1241
fileCtx := context .WithValues (ctx , "commit" , commitHash .String ()[:7 ], "path" , path )
1225
1242
fileCtx .Logger ().V (5 ).Info ("handling binary file" )
@@ -1229,11 +1246,6 @@ func (s *Git) handleBinary(
1229
1246
return nil
1230
1247
}
1231
1248
1232
- if s .skipBinaries || feature .ForceSkipBinaries .Load () {
1233
- fileCtx .Logger ().V (5 ).Info ("skipping binary file" , "path" , path )
1234
- return nil
1235
- }
1236
-
1237
1249
const (
1238
1250
cmdTimeout = 60 * time .Second
1239
1251
waitDelay = 5 * time .Second
@@ -1293,7 +1305,7 @@ func (s *Git) handleBinary(
1293
1305
err = errors .Join (err , copyErr , waitErr )
1294
1306
}()
1295
1307
1296
- return handlers .HandleFile (catFileCtx , stdout , chunkSkel , reporter , handlers .WithSkipArchives (s . skipArchives ))
1308
+ return handlers .HandleFile (catFileCtx , stdout , chunkSkel , reporter , handlers .WithSkipArchives (skipArchives ))
1297
1309
}
1298
1310
1299
1311
func (s * Source ) Enumerate (ctx context.Context , reporter sources.UnitReporter ) error {
0 commit comments