Fixed relative path in git repo scan

This commit is contained in:
shm12
2022-06-09 16:42:03 +03:00
parent 7bb124b6fe
commit 74c81e2270
2 changed files with 22 additions and 0 deletions
+9
View File
@@ -116,3 +116,12 @@ func (g *LocalGitRepository) GetFileLastCommit(filePath string) (*apis.Commit, e
Files: []apis.Files{},
}, nil
}
func (g *LocalGitRepository) GetRootDir() (string, error) {
wt, err := g.repo.Worktree()
if err != nil {
return "", fmt.Errorf("failed to get repo root")
}
return wt.Filesystem.Root(), nil
}
+13
View File
@@ -65,8 +65,21 @@ func (fileHandler *FileResourceHandler) GetResources(sessionObj *cautils.OPASess
if err != nil {
return nil, allResources, nil, err
}
// Get repo root
repoRoot := ""
giRepo, err := cautils.NewLocalGitRepository(path)
if err == nil {
repoRoot, _ = giRepo.GetRootDir()
}
for source, ws := range sourceToWorkloads {
workloads = append(workloads, ws...)
relSource, err := filepath.Rel(repoRoot, source)
if err == nil {
source = relSource
}
for i := range ws {
workloadIDToSource[ws[i].GetID()] = reporthandling.Source{RelativePath: source}
}