Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Diff within files #121

Merged
merged 4 commits into from
Oct 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ package cmd
import (
"errors"
"fmt"
"os"
"strings"
"sync"

"github.com/GoogleCloudPlatform/container-diff/differs"
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
"github.com/GoogleCloudPlatform/container-diff/util"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
"strings"
"sync"
)

var filename string

var diffCmd = &cobra.Command{
Use: "diff",
Short: "Compare two images: [image1] [image2]",
Expand All @@ -40,6 +42,9 @@ var diffCmd = &cobra.Command{
if err := checkIfValidAnalyzer(types); err != nil {
return err
}
if err := checkFilenameFlag(types); err != nil {
return err
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -57,6 +62,15 @@ func checkDiffArgNum(args []string) error {
return nil
}

func checkFilenameFlag(types string) error {
if filename != "" {
if !strings.Contains(types, "file") {
return errors.New("Please include --types=file with the --filename flag")
}
}
return nil
}

func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
diffTypes, err := differs.GetAnalyzers(diffArgs)
if err != nil {
Expand Down Expand Up @@ -109,14 +123,32 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
}
outputResults(diffs)

if filename != "" {
fmt.Fprintln(os.Stderr, "Computing filename diffs")
err := diffFile(imageMap[image1Arg], imageMap[image2Arg])
if err != nil {
return err
}
}

if save {
logrus.Infof("Images were saved at %s and %s", imageMap[image1Arg].FSPath,
imageMap[image2Arg].FSPath)
}
return nil
}

func diffFile(image1, image2 *pkgutil.Image) error {
diff, err := util.DiffFile(image1, image2, filename)
if err != nil {
return err
}
util.TemplateOutput(diff, "FilenameDiff")
return nil
}

func init() {
diffCmd.Flags().StringVarP(&filename, "filename", "f", "", "Set this flag to the path of a file in both containers to view the diff of the file. Must be used with --types=file flag.")
RootCmd.AddCommand(diffCmd)
addSharedFlags(diffCmd)
}
19 changes: 19 additions & 0 deletions pkg/util/fs_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ func GetSize(path string) int64 {
return stat.Size()
}

//GetFileContents returns the contents of a file at the specified path
func GetFileContents(path string) (*string, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, err
}

contents, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}

strContents := string(contents)
//If file is empty, return nil
if strContents == "" {
return nil, nil
}
return &strContents, nil
}

func getDirectorySize(path string) (int64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
Expand Down
64 changes: 64 additions & 0 deletions util/diff_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ type DirDiff struct {
Mods []EntryDiff
}

type FileNameDiff struct {
Filename string
Description string
Diff string
}

type EntryDiff struct {
Name string
Size1 int64
Expand Down Expand Up @@ -118,6 +124,64 @@ func DiffDirectory(d1, d2 pkgutil.Directory) (DirDiff, bool) {
return DirDiff{addedEntries, deletedEntries, modifiedEntries}, same
}

func DiffFile(image1, image2 *pkgutil.Image, filename string) (*FileNameDiff, error) {
//Join paths
image1FilePath := filepath.Join(image1.FSPath, filename)
image2FilePath := filepath.Join(image2.FSPath, filename)

//Get contents of files
image1FileContents, err := pkgutil.GetFileContents(image1FilePath)
if err != nil {
return nil, err
}

image2FileContents, err := pkgutil.GetFileContents(image2FilePath)
if err != nil {
return nil, err
}

description := ""
//Check if file contents are empty or if they are the same
if image1FileContents == nil && image2FileContents == nil {
description := "Both files are empty"
return &FileNameDiff{filename, description, ""}, nil
}

if image1FileContents == nil {
description := fmt.Sprintf("%s contains an empty file, the contents of %s are:", image1.Source, image2.Source)
return &FileNameDiff{filename, description, *image2FileContents}, nil
}

if image2FileContents == nil {
description := fmt.Sprintf("%s contains an empty file, the contents of %s are:", image2.Source, image1.Source)
return &FileNameDiff{filename, description, *image1FileContents}, nil
}

if *image1FileContents == *image2FileContents {
description := "Both files are the same, the contents are:"
return &FileNameDiff{filename, description, *image1FileContents}, nil
}

//Carry on with diffing, make string array for difflib requirements
image1Contents := []string{string(*image1FileContents)}
image2Contents := []string{string(*image2FileContents)}

//Run diff
diff := difflib.UnifiedDiff{
A: image1Contents,
B: image2Contents,
FromFile: image1.Source,
ToFile: image2.Source,
}

text, err := difflib.GetUnifiedDiffString(diff)

if err != nil {
return nil, err
}
return &FileNameDiff{filename, description, text}, nil
}

// Checks for content differences between files of the same name from different directories
func GetModifiedEntries(d1, d2 pkgutil.Directory) []string {
d1files := d1.Content
Expand Down
3 changes: 2 additions & 1 deletion util/format_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"bufio"
"encoding/json"
"errors"
"html/template"
"os"
"strings"
"text/tabwriter"
"text/template"

"github.com/sirupsen/logrus"
)
Expand All @@ -33,6 +33,7 @@ var templates = map[string]string{
"MultiVersionPackageDiff": MultiVersionDiffOutput,
"HistDiff": HistoryDiffOutput,
"DirDiff": FSDiffOutput,
"FilenameDiff": FilenameDiffOutput,
"ListAnalyze": ListAnalysisOutput,
"FileAnalyze": FileAnalysisOutput,
"MultiVersionPackageAnalyze": MultiVersionPackageOutput,
Expand Down
6 changes: 6 additions & 0 deletions util/template_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ Docker history lines found only in {{.Image1}}:{{if not .Diff.Adds}} None{{else}

Docker history lines found only in {{.Image2}}:{{if not .Diff.Dels}} None{{else}}{{block "list2" .Diff.Dels}}{{"\n"}}{{range .}}{{print "-" .}}{{"\n"}}{{end}}{{end}}{{end}}
`
const FilenameDiffOutput = `
-----Diff of {{.Filename}}-----
{{.Description}}

{{.Diff}}
`

const ListAnalysisOutput = `
-----{{.AnalyzeType}}-----
Expand Down