From 7c85e39c6b003021e733a01883a8fd18cded9af5 Mon Sep 17 00:00:00 2001 From: williamfzc <178894043@qq.com> Date: Thu, 13 Jul 2023 23:41:08 +0800 Subject: [PATCH] feat(#43): line level impact --- cmd/srctx/diff/core.go | 2 +- cmd/srctx/diff/core_file.go | 17 ++++++++++++----- cmd/srctx/diff/core_func.go | 21 +++++++++++++++++---- cmd/srctx/diff/objects.go | 27 ++++++++++++++------------- object/stat.go | 17 +++++++---------- 5 files changed, 51 insertions(+), 33 deletions(-) diff --git a/cmd/srctx/diff/core.go b/cmd/srctx/diff/core.go index 8259b08..4e48f7f 100644 --- a/cmd/srctx/diff/core.go +++ b/cmd/srctx/diff/core.go @@ -45,7 +45,7 @@ func MainDiff(opts *Options) error { return err } case nodeLevelFile: - err = fileLevelMain(opts, lineMap) + err = fileLevelMain(opts, lineMap, totalLineCountMap) if err != nil { return err } diff --git a/cmd/srctx/diff/core_file.go b/cmd/srctx/diff/core_file.go index 284d026..b92ea13 100644 --- a/cmd/srctx/diff/core_file.go +++ b/cmd/srctx/diff/core_file.go @@ -6,15 +6,13 @@ import ( "os" "github.com/gocarina/gocsv" - "github.com/williamfzc/srctx/object" - "github.com/opensibyl/sibyl2/pkg/core" log "github.com/sirupsen/logrus" "github.com/williamfzc/srctx/diff" "github.com/williamfzc/srctx/graph/file" ) -func fileLevelMain(opts *Options, lineMap diff.AffectedLineMap) error { +func fileLevelMain(opts *Options, lineMap diff.AffectedLineMap, totalLineCountMap map[string]int) error { log.Infof("file level main entry") fileGraph, err := createFileGraph(opts) if err != nil { @@ -28,10 +26,19 @@ func fileLevelMain(opts *Options, lineMap diff.AffectedLineMap) error { startPoints = append(startPoints, pv) } // start scan - stats := make([]*object.ImpactUnit, 0) + stats := make([]*ImpactUnitWithFile, 0) for _, eachPtr := range startPoints { eachStat := fileGraph.Stat(eachPtr) - stats = append(stats, eachStat) + wrappedStat := WrapImpactUnitWithFile(eachStat) + + // fill with file info + if totalLineCount, ok := totalLineCountMap[eachStat.FileName]; ok { + wrappedStat.TotalLineCount = totalLineCount + } + if affectedLineCount, ok := lineMap[eachStat.FileName]; ok { + wrappedStat.AffectedLineCount = len(affectedLineCount) + } + stats = append(stats, wrappedStat) log.Infof("start point: %v, refed: %d, ref: %d", eachPtr.Id(), len(eachStat.ReferencedIds), len(eachStat.ReferenceIds)) } log.Infof("diff finished.") diff --git a/cmd/srctx/diff/core_func.go b/cmd/srctx/diff/core_func.go index 173a780..34a4ea2 100644 --- a/cmd/srctx/diff/core_func.go +++ b/cmd/srctx/diff/core_func.go @@ -5,8 +5,6 @@ import ( "errors" "os" - "github.com/williamfzc/srctx/object" - "github.com/gocarina/gocsv" "github.com/opensibyl/sibyl2/pkg/core" log "github.com/sirupsen/logrus" @@ -35,10 +33,25 @@ func funcLevelMain(opts *Options, lineMap diff.AffectedLineMap, totalLineCountMa } // start scan - stats := make([]*object.ImpactUnit, 0) + stats := make([]*ImpactUnitWithFile, 0) for _, eachPtr := range startPoints { eachStat := funcGraph.Stat(eachPtr) - stats = append(stats, eachStat) + wrappedStat := WrapImpactUnitWithFile(eachStat) + + totalLineCount := len(eachPtr.GetSpan().Lines()) + affectedLineCount := 0 + + if lines, ok := lineMap[eachPtr.Path]; ok { + for _, eachLine := range lines { + if eachPtr.GetSpan().ContainLine(eachLine) { + affectedLineCount++ + } + } + } + wrappedStat.TotalLineCount = totalLineCount + wrappedStat.AffectedLineCount = affectedLineCount + + stats = append(stats, wrappedStat) log.Infof("start point: %v, refed: %d, ref: %d", eachPtr.Id(), len(eachStat.ReferencedIds), len(eachStat.ReferenceIds)) } log.Infof("diff finished.") diff --git a/cmd/srctx/diff/objects.go b/cmd/srctx/diff/objects.go index 0567729..23e69e3 100644 --- a/cmd/srctx/diff/objects.go +++ b/cmd/srctx/diff/objects.go @@ -6,6 +6,8 @@ import ( "os" "path/filepath" + "github.com/williamfzc/srctx/object" + "github.com/urfave/cli/v2" ) @@ -101,19 +103,18 @@ func NewOptionsFromJSONFile(fp string) (*Options, error) { return &opts, nil } -type FileVertex struct { - FileName string `csv:"fileName" json:"fileName"` - AffectedLinePercent float32 `csv:"affectedLinePercent" json:"affectedLinePercent"` - AffectedFunctionPercent float32 `csv:"affectedFunctionPercent" json:"affectedFunctionPercent"` - AffectedReferencePercent float32 `csv:"affectedReferencePercent" json:"affectedReferencePercent"` - - AffectedLines int `csv:"affectedLines" json:"affectedLines"` - TotalLines int `csv:"totalLines" json:"totalLines"` +type ImpactUnitWithFile struct { + *object.ImpactUnit - AffectedFunctions int `csv:"affectedFunctions" json:"affectedFunctions"` - TotalFunctions int `csv:"totalFunctions" json:"totalFunctions"` + // line level impact + AffectedLineCount int `csv:"affectedLineCount" json:"affectedLineCount"` + TotalLineCount int `csv:"totalLineCount" json:"totalLineCount"` +} - AffectedReferences int `csv:"affectedReferences" json:"affectedReferences"` - AffectedReferenceIds []string `csv:"-" json:"-"` - TotalReferences int `csv:"totalReferences" json:"totalReferences"` +func WrapImpactUnitWithFile(impactUnit *object.ImpactUnit) *ImpactUnitWithFile { + return &ImpactUnitWithFile{ + ImpactUnit: impactUnit, + AffectedLineCount: 0, + TotalLineCount: 0, + } } diff --git a/object/stat.go b/object/stat.go index 21a1637..da830b0 100644 --- a/object/stat.go +++ b/object/stat.go @@ -1,12 +1,11 @@ package object -type FileImpactPart struct { - // file level +type FileInfoPart struct { FileName string `csv:"fileName" json:"fileName"` - // line level impact - AffectedLineCount int `csv:"affectedLineCount" json:"affectedLineCount"` - TotalLineCount int `csv:"totalLineCount" json:"totalLineCount"` + // actually graph will not access the real file system + // so of course it knows nothing about the real files + // all the data we can access is from the indexing file } type UnitImpactPart struct { @@ -35,17 +34,15 @@ type ImpactDetails struct { } type ImpactUnit struct { - *FileImpactPart + *FileInfoPart *UnitImpactPart *ImpactDetails `json:"-" csv:"-"` } func NewImpactUnit() *ImpactUnit { return &ImpactUnit{ - FileImpactPart: &FileImpactPart{ - FileName: "", - AffectedLineCount: 0, - TotalLineCount: 0, + FileInfoPart: &FileInfoPart{ + FileName: "", }, UnitImpactPart: &UnitImpactPart{ UnitName: "",