From 4092bce40d89eae1818fe8ac3768fef9952b2e28 Mon Sep 17 00:00:00 2001 From: Soham Arora Date: Tue, 13 Aug 2024 21:24:14 +0530 Subject: [PATCH] fixing output to show diffpatchmatch Signed-off-by: Soham Arora --- pkg/analyzer/analyzer.go | 80 ++++++++++++---------------------- pkg/analyzer/print_analysis.go | 56 +++++------------------- 2 files changed, 39 insertions(+), 97 deletions(-) diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go index 21e9189159..2c9c060cf4 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/analyzer/analyzer.go @@ -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 @@ -661,6 +660,7 @@ func compareNodes(dmp *diffmatchpatch.DiffMatchPatch, nodeOne, nodeTwo Node) (No switch nodeOne.NodeType { case "Package": + diffedNode.NodeType = "Package" nOne := nodeOne.Pkg @@ -816,6 +816,8 @@ func compareNodes(dmp *diffmatchpatch.DiffMatchPatch, nodeOne, nodeTwo Node) (No } } case "DependencyPackage": + + diffedNode.NodeType = "DependencyPackage" nOne := nodeOne.DepPkg nTwo := nodeTwo.DepPkg @@ -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) { @@ -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]) @@ -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 @@ -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 } diff --git a/pkg/analyzer/print_analysis.go b/pkg/analyzer/print_analysis.go index ca7c357dd2..59d3e90779 100644 --- a/pkg/analyzer/print_analysis.go +++ b/pkg/analyzer/print_analysis.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "sort" - "strings" "github.com/olekukonko/tablewriter" ) @@ -76,7 +75,6 @@ func GetNodeString(node Node) (string, error) { return message, nil } - return "", nil } @@ -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) @@ -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) @@ -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) @@ -211,9 +212,7 @@ 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) @@ -221,52 +220,17 @@ func PrintDiffedPathTable(diffs DiffResult) error { 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)