Skip to content

Commit

Permalink
chore: rename affected -> impact
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfzc committed Jul 21, 2023
1 parent 4a587fb commit 45074d0
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 42 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,18 @@

# Go workspace file
go.work

# idea
.idea

# macos
.Ds_Store

# temp data
cmd/srctx/*.json
cmd/srctx/*.csv
cmd/srctx/*.html
cmd/srctx/*.dot
/*.lsif
/src_darwin_amd64
/srctx
6 changes: 3 additions & 3 deletions cmd/srctx/diff/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func createIndexFile(opts *Options) error {
return nil
}

func collectLineMap(opts *Options) (diff.AffectedLineMap, error) {
func collectLineMap(opts *Options) (diff.ImpactLineMap, error) {
if !opts.NoDiff {
lineMap, err := diff.GitDiff(opts.Src, opts.Before, opts.After)
if err != nil {
Expand All @@ -83,10 +83,10 @@ func collectLineMap(opts *Options) (diff.AffectedLineMap, error) {
return lineMap, nil
}
log.Infof("noDiff enabled")
return make(diff.AffectedLineMap), nil
return make(diff.ImpactLineMap), nil
}

func collectTotalLineCountMap(opts *Options, src string, lineMap diff.AffectedLineMap) (map[string]int, error) {
func collectTotalLineCountMap(opts *Options, src string, lineMap diff.ImpactLineMap) (map[string]int, error) {
totalLineCountMap := make(map[string]int)
var err error

Expand Down
6 changes: 3 additions & 3 deletions cmd/srctx/diff/core_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/williamfzc/srctx/graph/file"
)

func fileLevelMain(opts *Options, lineMap diff.AffectedLineMap, totalLineCountMap map[string]int) error {
func fileLevelMain(opts *Options, lineMap diff.ImpactLineMap, totalLineCountMap map[string]int) error {
log.Infof("file level main entry")
fileGraph, err := createFileGraph(opts)
if err != nil {
Expand All @@ -35,8 +35,8 @@ func fileLevelMain(opts *Options, lineMap diff.AffectedLineMap, totalLineCountMa
if totalLineCount, ok := totalLineCountMap[eachStat.FileName]; ok {
wrappedStat.TotalLineCount = totalLineCount
}
if affectedLineCount, ok := lineMap[eachStat.FileName]; ok {
wrappedStat.AffectedLineCount = len(affectedLineCount)
if impactLineCount, ok := lineMap[eachStat.FileName]; ok {
wrappedStat.ImpactLineCount = len(impactLineCount)
}
stats = append(stats, wrappedStat)
log.Infof("start point: %v, refed: %d, ref: %d", eachPtr.Id(), len(eachStat.ReferencedIds), len(eachStat.ReferenceIds))
Expand Down
8 changes: 4 additions & 4 deletions cmd/srctx/diff/core_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/williamfzc/srctx/graph/visual/g6"
)

func funcLevelMain(opts *Options, lineMap diff.AffectedLineMap, totalLineCountMap map[string]int) error {
func funcLevelMain(opts *Options, lineMap diff.ImpactLineMap, totalLineCountMap map[string]int) error {
log.Infof("func level main entry")
// metadata
funcGraph, err := createFuncGraph(opts)
Expand All @@ -39,17 +39,17 @@ func funcLevelMain(opts *Options, lineMap diff.AffectedLineMap, totalLineCountMa
wrappedStat := WrapImpactUnitWithFile(eachStat)

totalLineCount := len(eachPtr.GetSpan().Lines())
affectedLineCount := 0
impactLineCount := 0

if lines, ok := lineMap[eachPtr.Path]; ok {
for _, eachLine := range lines {
if eachPtr.GetSpan().ContainLine(eachLine) {
affectedLineCount++
impactLineCount++
}
}
}
wrappedStat.TotalLineCount = totalLineCount
wrappedStat.AffectedLineCount = affectedLineCount
wrappedStat.ImpactLineCount = impactLineCount

stats = append(stats, wrappedStat)
log.Infof("start point: %v, refed: %d, ref: %d", eachPtr.Id(), len(eachStat.ReferencedIds), len(eachStat.ReferenceIds))
Expand Down
10 changes: 5 additions & 5 deletions cmd/srctx/diff/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ type ImpactUnitWithFile struct {
*object.ImpactUnit

// line level impact
AffectedLineCount int `csv:"affectedLineCount" json:"affectedLineCount"`
TotalLineCount int `csv:"totalLineCount" json:"totalLineCount"`
ImpactLineCount int `csv:"impactLineCount" json:"impactLineCount"`
TotalLineCount int `csv:"totalLineCount" json:"totalLineCount"`
}

func WrapImpactUnitWithFile(impactUnit *object.ImpactUnit) *ImpactUnitWithFile {
return &ImpactUnitWithFile{
ImpactUnit: impactUnit,
AffectedLineCount: 0,
TotalLineCount: 0,
ImpactUnit: impactUnit,
ImpactLineCount: 0,
TotalLineCount: 0,
}
}
22 changes: 11 additions & 11 deletions diff/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
log "github.com/sirupsen/logrus"
)

// file name -> lines
type AffectedLineMap = map[string][]int
// ImpactLineMap file name -> lines
type ImpactLineMap = map[string][]int

func GitDiff(rootDir string, before string, after string) (AffectedLineMap, error) {
// about why I use cmd rather than some libs
func GitDiff(rootDir string, before string, after string) (ImpactLineMap, error) {
// about why, I use cmd rather than some libs
// because go-git 's patch has some bugs ...
gitDiffCmd := exec.Command("git", "diff", before, after)
gitDiffCmd.Dir = rootDir
Expand All @@ -23,14 +23,14 @@ func GitDiff(rootDir string, before string, after string) (AffectedLineMap, erro
return nil, err
}

affected, err := Unified2Affected(data)
affected, err := Unified2Impact(data)
if err != nil {
return nil, err
}
return affected, nil
}

func PathOffset(repoRoot string, srcRoot string, origin AffectedLineMap) (AffectedLineMap, error) {
func PathOffset(repoRoot string, srcRoot string, origin ImpactLineMap) (ImpactLineMap, error) {
modifiedLineMap := make(map[string][]int)
for file, lines := range origin {
afterPath, err := PathOffsetOne(repoRoot, srcRoot, file)
Expand All @@ -47,28 +47,28 @@ func PathOffsetOne(repoRoot string, srcRoot string, target string) (string, erro
return filepath.Rel(srcRoot, absFile)
}

func Unified2Affected(patch []byte) (AffectedLineMap, error) {
func Unified2Impact(patch []byte) (ImpactLineMap, error) {
parsed, _, err := gitdiff.Parse(bytes.NewReader(patch))
if err != nil {
return nil, err
}

affectedMap := make(map[string][]int)
impactLineMap := make(ImpactLineMap)
for _, each := range parsed {
if each.IsBinary || each.IsDelete {
continue
}
affectedMap[each.NewName] = make([]int, 0)
impactLineMap[each.NewName] = make([]int, 0)
fragments := each.TextFragments
for _, eachF := range fragments {
left := int(eachF.NewPosition)

for i, eachLine := range eachF.Lines {
if eachLine.New() && eachLine.Op == gitdiff.OpAdd {
affectedMap[each.NewName] = append(affectedMap[each.NewName], left+i-1)
impactLineMap[each.NewName] = append(impactLineMap[each.NewName], left+i-1)
}
}
}
}
return affectedMap, nil
return impactLineMap, nil
}
6 changes: 3 additions & 3 deletions graph/file/api_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ func (fg *Graph) Stat(f *Vertex) *object.ImpactUnit {
impactUnit.FileName = f.Path
impactUnit.UnitName = f.Id()

impactUnit.DirectConnectCount = len(referencedIds) + len(referenceIds)
impactUnit.InDirectConnectCount = len(transitiveReferenceIds) + len(transitiveReferencedIds)
impactUnit.ImpactCount = len(referencedIds) + len(referenceIds)
impactUnit.TransImpactCount = len(transitiveReferenceIds) + len(transitiveReferencedIds)
impactUnit.TotalUnitCount = len(fg.IdCache)

impactUnit.AffectedEntries = len(fg.EntryIds(f))
impactUnit.ImpactEntries = len(fg.EntryIds(f))
impactUnit.TotalEntriesCount = len(fg.ListEntries())

// details
Expand Down
9 changes: 6 additions & 3 deletions graph/function/api_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ func (fg *Graph) Stat(f *Vertex) *object.ImpactUnit {
impactUnit.FileName = f.Path
impactUnit.UnitName = f.Id()

impactUnit.DirectConnectCount = len(referencedIds) + len(referenceIds)
impactUnit.InDirectConnectCount = len(transitiveReferenceIds) + len(transitiveReferencedIds)
impactUnit.ImpactCount = len(referencedIds) + len(referenceIds)
impactUnit.TransImpactCount = len(transitiveReferenceIds) + len(transitiveReferencedIds)
impactUnit.TotalUnitCount = len(fg.IdCache)

impactUnit.AffectedEntries = len(fg.EntryIds(f))
impactUnit.ImpactEntries = len(fg.EntryIds(f))
impactUnit.TotalEntriesCount = len(fg.ListEntries())

// details
Expand All @@ -29,3 +29,6 @@ func (fg *Graph) Stat(f *Vertex) *object.ImpactUnit {

return impactUnit
}

func (fg *Graph) GlobalStat() {
}
20 changes: 10 additions & 10 deletions object/stat.go → object/impact.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ type UnitImpactPart struct {
UnitName string `csv:"unitName" json:"unitName"`

// Heat
DirectConnectCount int `csv:"directConnectCount" json:"directConnectCount"`
InDirectConnectCount int `csv:"inDirectConnectCount" json:"inDirectConnectCount"`
TotalUnitCount int `csv:"totalUnitCount" json:"totalUnitCount"`
ImpactCount int `csv:"impactCount" json:"impactCount"`
TransImpactCount int `csv:"transImpactCount" json:"transImpactCount"`
TotalUnitCount int `csv:"totalUnitCount" json:"totalUnitCount"`

// entries
AffectedEntries int `csv:"affectedEntries" json:"affectedEntries"`
ImpactEntries int `csv:"impactEntries" json:"impactEntries"`
TotalEntriesCount int `csv:"totalEntriesCount" json:"totalEntriesCount"`
}

Expand All @@ -45,12 +45,12 @@ func NewImpactUnit() *ImpactUnit {
FileName: "",
},
UnitImpactPart: &UnitImpactPart{
UnitName: "",
DirectConnectCount: 0,
InDirectConnectCount: 0,
TotalUnitCount: 0,
AffectedEntries: 0,
TotalEntriesCount: 0,
UnitName: "",
ImpactCount: 0,
TransImpactCount: 0,
TotalUnitCount: 0,
ImpactEntries: 0,
TotalEntriesCount: 0,
},
ImpactDetails: &ImpactDetails{
ReferencedIds: make([]string, 0),
Expand Down

0 comments on commit 45074d0

Please sign in to comment.