Skip to content

Commit

Permalink
fixing output to show diffpatchmatch
Browse files Browse the repository at this point in the history
Signed-off-by: Soham Arora <[email protected]>
  • Loading branch information
arorasoham9 committed Aug 13, 2024
1 parent a01cd3e commit 4092bce
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 97 deletions.
80 changes: 29 additions & 51 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ func MakeGraph(hasSBOM model.HasSBOMsHasSBOM, metadata, inclSoft, inclDeps, incl
node.Attributes["Digest"] = hasSBOM.Digest
node.Attributes["Uri"] = hasSBOM.Uri
}
//TODO: inclSoft and inclOccur

if inclDeps || compareAll {
//add included dependencies
Expand Down Expand Up @@ -661,6 +660,7 @@ func compareNodes(dmp *diffmatchpatch.DiffMatchPatch, nodeOne, nodeTwo Node) (No
switch nodeOne.NodeType {

case "Package":
diffedNode.NodeType = "Package"

nOne := nodeOne.Pkg

Expand Down Expand Up @@ -816,6 +816,8 @@ func compareNodes(dmp *diffmatchpatch.DiffMatchPatch, nodeOne, nodeTwo Node) (No
}
}
case "DependencyPackage":

diffedNode.NodeType = "DependencyPackage"
nOne := nodeOne.DepPkg

nTwo := nodeTwo.DepPkg
Expand Down Expand Up @@ -965,7 +967,7 @@ func compareNodes(dmp *diffmatchpatch.DiffMatchPatch, nodeOne, nodeTwo Node) (No
}
}
}
}
}
return diffedNode, diffs, nil
}
func CompareTwoPaths(dmp *diffmatchpatch.DiffMatchPatch, analysisListOne, analysisListTwo []*Node) ([]Node, [][]string, int, error) {
Expand Down Expand Up @@ -999,11 +1001,13 @@ func CompareTwoPaths(dmp *diffmatchpatch.DiffMatchPatch, analysisListOne, analys
if i >= len(shorterPath) {
dumnode := &Node{}
if node.NodeType == "Package" {
dumnode.NodeType = "Package"
dumnode.Pkg = model.AllIsDependencyTreePackage{}
} else if node.NodeType == "DependencyPackage" {
dumnode.NodeType = "DependencyPackage"
dumnode.DepPkg = model.AllIsDependencyTreeDependencyPackage{}
}

diffNode, diffs, err = compareNodes(dmp, *node, *dumnode)
} else {
diffNode, diffs, err = compareNodes(dmp, *node, *shorterPath[i])
Expand Down Expand Up @@ -1033,52 +1037,6 @@ func CompareTwoPaths(dmp *diffmatchpatch.DiffMatchPatch, analysisListOne, analys
return nodesDiff, pathDiff, diffCount, nil
}

// func CompareTwoPaths(dmp *diffmatchpatch.DiffMatchPatch, analysisListOne, analysisListTwo []*Node) ([]Node, [][]string, int, error) {
// var longerPath, shorterPath []*Node

// if len(analysisListOne) > len(analysisListTwo) {
// longerPath = analysisListOne
// shorterPath = analysisListTwo
// } else {
// longerPath = analysisListTwo
// shorterPath = analysisListOne
// }

// pathDiff := make( [][]string, len(longerPath))
// nodesDiff := make([]Node, len(longerPath))
// var diffCount int

// for i, node := range longerPath {
// if i >= len(shorterPath) {
// dumnode := &Node{}
// if node.NodeType == "Package" {
// dumnode.Pkg = model.AllIsDependencyTreePackage{}
// } else if node.NodeType == "DependencyPackage" {
// dumnode.DepPkg = model.AllIsDependencyTreeDependencyPackage{}
// }

// diffNode, diffs, err := compareNodes(dmp, *node, *dumnode)
// if err != nil {
// return nodesDiff, pathDiff, 0, fmt.Errorf(err.Error())
// }

// pathDiff[i] = diffs
// nodesDiff[i] = diffNode
// diffCount += len(diffs)
// } else {
// diffNode, diffs, err := compareNodes(dmp, *node, *shorterPath[i])
// if err != nil {
// return nodesDiff, pathDiff, 0, fmt.Errorf(err.Error())
// }
// pathDiff[i] = diffs
// nodesDiff[i] = diffNode
// diffCount += len(diffs)
// }
// }

// return nodesDiff, pathDiff, diffCount, nil
// }

func CompareAllPaths(listOne, listTwo [][]*Node) (DiffResult, error) {

var small, big [][]*Node
Expand Down Expand Up @@ -1170,9 +1128,29 @@ func CompareAllPaths(listOne, listTwo [][]*Node) (DiffResult, error) {
for i, val := range big {
_, ok := used[i]
if !ok {
pathResults = append(pathResults, DiffedPath{PathOne: val})


//diff each missing path and append to result
var missingPath []Node
for _, node := range val {
dumnode := &Node{}
if node.NodeType == "Package" {
dumnode.NodeType = "Package"
dumnode.Pkg = model.AllIsDependencyTreePackage{}
} else if node.NodeType == "DependencyPackage" {
dumnode.NodeType = "DependencyPackage"
dumnode.DepPkg = model.AllIsDependencyTreeDependencyPackage{}
}
dmp := diffmatchpatch.New()
diffNode, _, err := compareNodes(dmp, *node, *dumnode)
if err != nil {
return DiffResult{}, fmt.Errorf(err.Error())
}
missingPath = append(missingPath, diffNode)

}
pathResults = append(pathResults, DiffedPath{PathOne: val, NodeDiffs: missingPath})
}
}

return DiffResult{Paths: pathResults, Nodes: nodeResults}, nil
}
56 changes: 10 additions & 46 deletions pkg/analyzer/print_analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"sort"
"strings"

"github.com/olekukonko/tablewriter"
)
Expand Down Expand Up @@ -76,7 +75,6 @@ func GetNodeString(node Node) (string, error) {
return message, nil

}

return "", nil
}

Expand Down Expand Up @@ -151,7 +149,9 @@ func PrintPathTable(header string, analysisOne, analysisTwo [][]*Node) error {
}

func PrintDiffedNodeTable(diffs DiffResult) error {

if len(diffs.Nodes) == 0 {
return nil
}
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)

Expand Down Expand Up @@ -184,10 +184,8 @@ func PrintDiffedNodeTable(diffs DiffResult) error {
}
row = append(row, s)
table.Append(row)

table.Append([]string{"================================="})
table.Append([]string{fmt.Sprintf("Node pair causing %v paths to differ", diff.Count)})
table.Append([]string{"================================="})
table.Append([]string{"+------------------------------------------------------------------+"})
}

table.SetAlignment(tablewriter.ALIGN_LEFT)
Expand All @@ -196,6 +194,9 @@ func PrintDiffedNodeTable(diffs DiffResult) error {
}

func PrintDiffedPathTable(diffs DiffResult) error {
if len(diffs.Paths) == 0 {
return nil
}

table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
Expand All @@ -211,62 +212,25 @@ func PrintDiffedPathTable(diffs DiffResult) error {

for _, diff := range diffs.Paths {
var row []string

for i, nodeOne := range diff.PathOne {

for i, node := range diff.NodeDiffs {
if len(row) != 0 {
row = append(row, "--->")
table.SetColMinWidth(i+1, colMinWidth)
} else {
table.SetColMinWidth(i, colMinWidth)
}

s, err := GetNodeString(*nodeOne)
if err != nil {
return fmt.Errorf("unable to print diffs: %v", err)
}
row = append(row, s)

}

table.Append(row)
row = []string{}

for i, nodeOne := range diff.PathTwo {

if len(row) != 0 {
row = append(row, "--->")
table.SetColMinWidth(i+1, colMinWidth)
} else {
table.SetColMinWidth(i, colMinWidth)
}
s, err := GetNodeString(node)

s, err := GetNodeString(*nodeOne)
if err != nil {
return fmt.Errorf("unable to print diffs: %v", err)
}
row = append(row, s)

}
table.Append(row)

table.Append([]string{"================================="})

row = []string{}
for i, diff := range diff.Diffs {

if len(row) != 0 {
row = append(row, " ")
table.SetColMinWidth(i+1, colMinWidth)
} else {
table.SetColMinWidth(i, colMinWidth)
}

row = append(row, strings.Join(diff, "\n"))
}
table.Append(row)

table.Append([]string{"================================="})
table.Append([]string{"+------------------------------------------------------------------+"})
}

table.SetAlignment(tablewriter.ALIGN_LEFT)
Expand Down

0 comments on commit 4092bce

Please sign in to comment.