Skip to content

Commit c620a62

Browse files
authored
Make remote URL metadata optional for git scanning (#313)
* Make remote URL metadata optional for git scanning * Use helper function in ScanUnstaged
1 parent f9a23fa commit c620a62

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

pkg/sources/git/git.go

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ func (s *Git) ScanCommits(repo *git.Repository, path string, scanOptions *ScanOp
267267
if err != nil {
268268
return errors.WrapPrefix(err, "could not open repo path", 0)
269269
}
270+
271+
// get the URL metadata for reporting (may be empty)
272+
urlMetadata := getSafeRemoteURL(repo, "origin")
273+
270274
var depth int64
271275
for file := range fileChan {
272276
if scanOptions.MaxDepth > 0 && depth >= scanOptions.MaxDepth {
@@ -297,15 +301,6 @@ func (s *Git) ScanCommits(repo *git.Repository, path string, scanOptions *ScanOp
297301
when = file.PatchHeader.AuthorDate.String()
298302
}
299303

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-
309304
for _, frag := range file.TextFragments {
310305
newLines := bytes.Buffer{}
311306
newLineNumber := frag.NewPosition
@@ -314,7 +309,7 @@ func (s *Git) ScanCommits(repo *git.Repository, path string, scanOptions *ScanOp
314309
newLines.WriteString(strings.ReplaceAll(line.String(), "\n", " ") + "\n")
315310
}
316311
}
317-
metadata := s.sourceMetadataFunc(fileName, email, hash, when, safeRepo, newLineNumber)
312+
metadata := s.sourceMetadataFunc(fileName, email, hash, when, urlMetadata, newLineNumber)
318313
chunksChan <- &sources.Chunk{
319314
SourceName: s.sourceName,
320315
SourceID: s.sourceID,
@@ -329,17 +324,11 @@ func (s *Git) ScanCommits(repo *git.Repository, path string, scanOptions *ScanOp
329324
}
330325

331326
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")
340329

341330
// Also scan any unstaged changes in the working tree of the repo
342-
_, err = repo.Head()
331+
_, err := repo.Head()
343332
if err == nil || err == plumbing.ErrReferenceNotFound {
344333
wt, err := repo.Worktree()
345334
if err != nil {
@@ -357,7 +346,7 @@ func (s *Git) ScanUnstaged(repo *git.Repository, scanOptions *ScanOptions, chunk
357346
continue
358347
}
359348
metadata := s.sourceMetadataFunc(
360-
fh, "unstaged", "unstaged", time.Now().String(), safeRepo, 0,
349+
fh, "unstaged", "unstaged", time.Now().String(), urlMetadata, 0,
361350
)
362351

363352
fileBuf := bytes.NewBuffer(nil)
@@ -491,3 +480,26 @@ func PrepareRepo(uriString string) (string, bool, error) {
491480
log.Debugf("Git repo local path: %s", path)
492481
return path, remote, nil
493482
}
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

Comments
 (0)