@@ -267,6 +267,10 @@ func (s *Git) ScanCommits(repo *git.Repository, path string, scanOptions *ScanOp
267
267
if err != nil {
268
268
return errors .WrapPrefix (err , "could not open repo path" , 0 )
269
269
}
270
+
271
+ // get the URL metadata for reporting (may be empty)
272
+ urlMetadata := getSafeRemoteURL (repo , "origin" )
273
+
270
274
var depth int64
271
275
for file := range fileChan {
272
276
if scanOptions .MaxDepth > 0 && depth >= scanOptions .MaxDepth {
@@ -297,15 +301,6 @@ func (s *Git) ScanCommits(repo *git.Repository, path string, scanOptions *ScanOp
297
301
when = file .PatchHeader .AuthorDate .String ()
298
302
}
299
303
300
- remote , err := repo .Remote ("origin" )
301
- if err != nil {
302
- return errors .WrapPrefix (err , "error getting repo remote origin" , 0 )
303
- }
304
- safeRepo , err := stripPassword (remote .Config ().URLs [0 ])
305
- if err != nil {
306
- return errors .WrapPrefix (err , "couldn't get repo name" , 0 )
307
- }
308
-
309
304
for _ , frag := range file .TextFragments {
310
305
newLines := bytes.Buffer {}
311
306
newLineNumber := frag .NewPosition
@@ -314,7 +309,7 @@ func (s *Git) ScanCommits(repo *git.Repository, path string, scanOptions *ScanOp
314
309
newLines .WriteString (strings .ReplaceAll (line .String (), "\n " , " " ) + "\n " )
315
310
}
316
311
}
317
- metadata := s .sourceMetadataFunc (fileName , email , hash , when , safeRepo , newLineNumber )
312
+ metadata := s .sourceMetadataFunc (fileName , email , hash , when , urlMetadata , newLineNumber )
318
313
chunksChan <- & sources.Chunk {
319
314
SourceName : s .sourceName ,
320
315
SourceID : s .sourceID ,
@@ -329,17 +324,11 @@ func (s *Git) ScanCommits(repo *git.Repository, path string, scanOptions *ScanOp
329
324
}
330
325
331
326
func (s * Git ) ScanUnstaged (repo * git.Repository , scanOptions * ScanOptions , chunksChan chan * sources.Chunk ) error {
332
- remote , err := repo .Remote ("origin" )
333
- if err != nil {
334
- return errors .New (err )
335
- }
336
- safeRepo , err := stripPassword (remote .Config ().URLs [0 ])
337
- if err != nil {
338
- return errors .New (err )
339
- }
327
+ // get the URL metadata for reporting (may be empty)
328
+ urlMetadata := getSafeRemoteURL (repo , "origin" )
340
329
341
330
// Also scan any unstaged changes in the working tree of the repo
342
- _ , err = repo .Head ()
331
+ _ , err : = repo .Head ()
343
332
if err == nil || err == plumbing .ErrReferenceNotFound {
344
333
wt , err := repo .Worktree ()
345
334
if err != nil {
@@ -357,7 +346,7 @@ func (s *Git) ScanUnstaged(repo *git.Repository, scanOptions *ScanOptions, chunk
357
346
continue
358
347
}
359
348
metadata := s .sourceMetadataFunc (
360
- fh , "unstaged" , "unstaged" , time .Now ().String (), safeRepo , 0 ,
349
+ fh , "unstaged" , "unstaged" , time .Now ().String (), urlMetadata , 0 ,
361
350
)
362
351
363
352
fileBuf := bytes .NewBuffer (nil )
@@ -491,3 +480,26 @@ func PrepareRepo(uriString string) (string, bool, error) {
491
480
log .Debugf ("Git repo local path: %s" , path )
492
481
return path , remote , nil
493
482
}
483
+
484
+ // getSafeRemoteURL is a helper function that will attempt to get a safe URL first
485
+ // from the preferred remote name, falling back to the first remote name
486
+ // available, or an empty string if there are no remotes.
487
+ func getSafeRemoteURL (repo * git.Repository , preferred string ) string {
488
+ remote , err := repo .Remote (preferred )
489
+ if err != nil {
490
+ var remotes []* git.Remote
491
+ if remotes , err = repo .Remotes (); err != nil {
492
+ return ""
493
+ }
494
+ if len (remotes ) == 0 {
495
+ return ""
496
+ }
497
+ remote = remotes [0 ]
498
+ }
499
+ // URLs is guaranteed to be non-empty
500
+ safeURL , err := stripPassword (remote .Config ().URLs [0 ])
501
+ if err != nil {
502
+ return ""
503
+ }
504
+ return safeURL
505
+ }
0 commit comments