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

Commit 62ea1b2

Browse files
author
Priya Wadhwa
committed
Diff by filename
1 parent fe91244 commit 62ea1b2

File tree

7 files changed

+110
-7
lines changed

7 files changed

+110
-7
lines changed

cmd/analyze.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ var analyzeCmd = &cobra.Command{
3636
if err := validateArgs(args, checkAnalyzeArgNum); err != nil {
3737
return err
3838
}
39+
if err := checkAnalyzeFilenameFlag(); err != nil {
40+
return err
41+
}
3942
if err := checkIfValidAnalyzer(types); err != nil {
4043
return err
4144
}
@@ -56,6 +59,13 @@ func checkAnalyzeArgNum(args []string) error {
5659
return nil
5760
}
5861

62+
func checkAnalyzeFilenameFlag() error {
63+
if filename != "" {
64+
return errors.New("please remove --filename flag, incompatible with analyze")
65+
}
66+
return nil
67+
}
68+
5969
func analyzeImage(imageName string, analyzerArgs []string) error {
6070
analyzeTypes, err := differs.GetAnalyzers(analyzerArgs)
6171
if err != nil {

cmd/diff.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ package cmd
1919
import (
2020
"errors"
2121
"fmt"
22-
"os"
23-
"strings"
24-
"sync"
25-
2622
"github.com/GoogleCloudPlatform/container-diff/differs"
2723
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
24+
"github.com/GoogleCloudPlatform/container-diff/util"
2825
"github.com/golang/glog"
2926
"github.com/spf13/cobra"
27+
"os"
28+
"strings"
29+
"sync"
3030
)
3131

3232
var diffCmd = &cobra.Command{
@@ -43,7 +43,8 @@ var diffCmd = &cobra.Command{
4343
return nil
4444
},
4545
Run: func(cmd *cobra.Command, args []string) {
46-
if err := diffImages(args[0], args[1], strings.Split(types, ",")); err != nil {
46+
typesFlag := checkIfTypesFlagSet(cmd)
47+
if err := diffImages(args[0], args[1], strings.Split(types, ","), typesFlag); err != nil {
4748
glog.Error(err)
4849
os.Exit(1)
4950
}
@@ -57,7 +58,7 @@ func checkDiffArgNum(args []string) error {
5758
return nil
5859
}
5960

60-
func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
61+
func diffImages(image1Arg, image2Arg string, diffArgs []string, typesFlag bool) error {
6162
diffTypes, err := differs.GetAnalyzers(diffArgs)
6263
if err != nil {
6364
return err
@@ -106,6 +107,17 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
106107
if err != nil {
107108
return fmt.Errorf("Could not retrieve diff: %s", err)
108109
}
110+
111+
if filename != "" {
112+
err := diffFile(imageMap, image1Arg, image2Arg)
113+
if err != nil {
114+
return err
115+
}
116+
if !typesFlag {
117+
return nil
118+
}
119+
}
120+
109121
glog.Info("Retrieving diffs")
110122
outputResults(diffs)
111123

@@ -116,6 +128,20 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
116128
return nil
117129
}
118130

131+
func diffFile(imageMap map[string]*pkgutil.Image, image1Arg, image2Arg string) error {
132+
133+
image1FilePath := imageMap[image1Arg].FSPath + filename
134+
image2FilePath := imageMap[image2Arg].FSPath + filename
135+
136+
diff, err := util.DiffFile(image1FilePath, image2FilePath, image1Arg, image2Arg)
137+
if err != nil {
138+
return err
139+
}
140+
diff.Filename = filename
141+
util.TemplateOutput(diff, "FileNameDiff")
142+
return nil
143+
}
144+
119145
func init() {
120146
RootCmd.AddCommand(diffCmd)
121147
addSharedFlags(diffCmd)

cmd/root.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
var json bool
3636
var save bool
3737
var types string
38+
var filename string
3839

3940
type validatefxn func(args []string) error
4041

@@ -106,6 +107,11 @@ func checkIfValidAnalyzer(flagtypes string) error {
106107
return nil
107108
}
108109

110+
func checkIfTypesFlagSet(cmd *cobra.Command) bool {
111+
flag := cmd.Flags().Lookup("types")
112+
return flag.Changed
113+
}
114+
109115
func getPrepperForImage(image string) (pkgutil.Prepper, error) {
110116
cli, err := client.NewEnvClient()
111117
if err != nil {
@@ -139,4 +145,5 @@ func addSharedFlags(cmd *cobra.Command) {
139145
cmd.Flags().StringVarP(&types, "types", "t", "apt", "This flag sets the list of analyzer types to use. It expects a comma separated list of supported analyzers.")
140146
cmd.Flags().BoolVarP(&save, "save", "s", false, "Set this flag to save rather than remove the final image filesystems on exit.")
141147
cmd.Flags().BoolVarP(&util.SortSize, "order", "o", false, "Set this flag to sort any file/package results by descending size. Otherwise, they will be sorted by name.")
148+
cmd.Flags().StringVarP(&filename, "filename", "f", "", "View the diff of a file in both containers (can only be used with container-diff diff)")
142149
}

pkg/util/fs_utils.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package util
1818

1919
import (
2020
"bytes"
21+
"errors"
2122
"io/ioutil"
2223
"os"
2324
"path/filepath"
@@ -53,6 +54,24 @@ func GetSize(path string) int64 {
5354
return stat.Size()
5455
}
5556

57+
//GetFileContents returns the contents of a file at the specified path
58+
func GetFileContents(path string) ([]string, error) {
59+
stat, err := os.Stat(path)
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
if stat.IsDir() {
65+
return nil, errors.New("--filename is a directory, not a file")
66+
}
67+
contents, err := ioutil.ReadFile(path)
68+
69+
if err != nil {
70+
return nil, err
71+
}
72+
return []string{string(contents)}, nil
73+
}
74+
5675
func getDirectorySize(path string) (int64, error) {
5776
var size int64
5877
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {

util/diff_utils.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ type DirDiff struct {
3333
Mods []EntryDiff
3434
}
3535

36+
type FileNameDiff struct {
37+
Filename string
38+
Diff string
39+
}
40+
3641
type EntryDiff struct {
3742
Name string
3843
Size1 int64
@@ -117,6 +122,36 @@ func DiffDirectory(d1, d2 pkgutil.Directory) (DirDiff, bool) {
117122
return DirDiff{addedEntries, deletedEntries, modifiedEntries}, same
118123
}
119124

125+
//DiffFile diffs within a file
126+
func DiffFile(path1, path2, image1, image2 string) (FileNameDiff, error) {
127+
128+
var result FileNameDiff
129+
130+
image1Contents, err := pkgutil.GetFileContents(path1)
131+
if err != nil {
132+
return result, err
133+
}
134+
image2Contents, err := pkgutil.GetFileContents(path2)
135+
136+
if err != nil {
137+
return result, err
138+
}
139+
140+
diff := difflib.UnifiedDiff{
141+
A: image1Contents,
142+
B: image2Contents,
143+
FromFile: image1,
144+
ToFile: image2,
145+
}
146+
147+
text, err := difflib.GetUnifiedDiffString(diff)
148+
149+
if err != nil {
150+
return result, err
151+
}
152+
return FileNameDiff{Diff: text}, nil
153+
}
154+
120155
// Checks for content differences between files of the same name from different directories
121156
func GetModifiedEntries(d1, d2 pkgutil.Directory) []string {
122157
d1files := d1.Content

util/format_utils.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import (
2020
"bufio"
2121
"encoding/json"
2222
"errors"
23-
"html/template"
2423
"os"
2524
"strings"
2625
"text/tabwriter"
26+
"text/template"
2727

2828
"github.com/golang/glog"
2929
)
@@ -33,6 +33,7 @@ var templates = map[string]string{
3333
"MultiVersionPackageDiff": MultiVersionDiffOutput,
3434
"HistDiff": HistoryDiffOutput,
3535
"DirDiff": FSDiffOutput,
36+
"FileNameDiff": FilenameDiffOutput,
3637
"ListAnalyze": ListAnalysisOutput,
3738
"FileAnalyze": FileAnalysisOutput,
3839
"MultiVersionPackageAnalyze": MultiVersionPackageOutput,

util/template_utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ Docker history lines found only in {{.Image1}}:{{if not .Diff.Adds}} None{{else}
6565
6666
Docker history lines found only in {{.Image2}}:{{if not .Diff.Dels}} None{{else}}{{block "list2" .Diff.Dels}}{{"\n"}}{{range .}}{{print "-" .}}{{"\n"}}{{end}}{{end}}{{end}}
6767
`
68+
const FilenameDiffOutput = `
69+
-----Diff of {{.Filename}}-----
70+
71+
{{.Diff}}
72+
`
6873

6974
const ListAnalysisOutput = `
7075
-----{{.AnalyzeType}}-----

0 commit comments

Comments
 (0)