This repository was archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathdiff_utils.go
267 lines (222 loc) · 7.02 KB
/
diff_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
Copyright 2018 Google, Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
"fmt"
"os"
"path/filepath"
"sort"
pkgutil "github.com/GoogleContainerTools/container-diff/pkg/util"
"github.com/sirupsen/logrus"
"github.com/pmezard/go-difflib/difflib"
)
type DirDiff struct {
Adds []pkgutil.DirectoryEntry
Dels []pkgutil.DirectoryEntry
Mods []EntryDiff
}
type MultipleDirDiff struct {
DirDiffs []DirDiff
}
type FileNameDiff struct {
Filename string
Description string
Diff string
}
type EntryDiff struct {
Name string
Size1 int64
Size2 int64
}
// Modification of difflib's unified differ
func GetAdditions(a, b []string) []string {
matcher := difflib.NewMatcher(a, b)
differences := matcher.GetGroupedOpCodes(0)
adds := []string{}
for _, group := range differences {
for _, opCode := range group {
j1, j2 := opCode.J1, opCode.J2
if opCode.Tag == 'r' || opCode.Tag == 'i' {
adds = append(adds, b[j1:j2]...)
}
}
}
return adds
}
func GetDeletions(a, b []string) []string {
matcher := difflib.NewMatcher(a, b)
differences := matcher.GetGroupedOpCodes(0)
dels := []string{}
for _, group := range differences {
for _, opCode := range group {
i1, i2 := opCode.I1, opCode.I2
if opCode.Tag == 'r' || opCode.Tag == 'd' {
dels = append(dels, a[i1:i2]...)
}
}
}
return dels
}
func GetMatches(a, b []string) []string {
matcher := difflib.NewMatcher(a, b)
matchindexes := matcher.GetMatchingBlocks()
matches := []string{}
for i, match := range matchindexes {
if i != len(matchindexes)-1 {
start := match.A
end := match.A + match.Size
matches = append(matches, a[start:end]...)
}
}
return matches
}
// DiffDirectory takes the diff of two directories, assuming both are completely unpacked
func DiffDirectory(d1, d2 pkgutil.Directory) (DirDiff, bool) {
adds := GetAddedEntries(d1, d2)
sort.Strings(adds)
addedEntries := pkgutil.CreateDirectoryEntries(d2.Root, adds)
dels := GetDeletedEntries(d1, d2)
sort.Strings(dels)
deletedEntries := pkgutil.CreateDirectoryEntries(d1.Root, dels)
mods := GetModifiedEntries(d1, d2)
sort.Strings(mods)
modifiedEntries := createEntryDiffs(d1.Root, d2.Root, mods)
var same bool
if len(adds) == 0 && len(dels) == 0 && len(mods) == 0 {
same = true
} else {
same = false
}
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 := difflib.SplitLines(string(*image1FileContents))
image2Contents := difflib.SplitLines(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
d2files := d2.Content
filematches := GetMatches(d1files, d2files)
modified := []string{}
for _, f := range filematches {
f1path := fmt.Sprintf("%s%s", d1.Root, f)
f2path := fmt.Sprintf("%s%s", d2.Root, f)
f1stat, err := os.Lstat(f1path)
if err != nil {
logrus.Errorf("Error checking directory entry %s: %s\n", f, err)
continue
}
f2stat, err := os.Lstat(f2path)
if err != nil {
logrus.Errorf("Error checking directory entry %s: %s\n", f, err)
continue
}
// If the directory entry is a symlink, make sure the symlinks point to the same place
if f1stat.Mode()&os.ModeSymlink != 0 && f2stat.Mode()&os.ModeSymlink != 0 {
same, err := pkgutil.CheckSameSymlink(f1path, f2path)
if err != nil {
logrus.Errorf("Error determining if symlink %s and %s are equivalent: %s\n", f1path, f2path, err)
continue
}
if !same {
modified = append(modified, f)
}
continue
}
// If the directory entry in question is a tar, verify that the two have the same size
if pkgutil.IsTar(f1path) {
if f1stat.Size() != f2stat.Size() {
modified = append(modified, f)
}
continue
}
// If the directory entry is not a tar and not a directory, then it's a file so make sure the file contents are the same
// Note: We skip over directory entries because to compare directories, we compare their contents
if !f1stat.IsDir() {
same, err := pkgutil.CheckSameFile(f1path, f2path)
if err != nil {
logrus.Errorf("Error diffing contents of %s and %s: %s\n", f1path, f2path, err)
continue
}
if !same {
modified = append(modified, f)
}
}
}
return modified
}
func GetAddedEntries(d1, d2 pkgutil.Directory) []string {
return GetAdditions(d1.Content, d2.Content)
}
func GetDeletedEntries(d1, d2 pkgutil.Directory) []string {
return GetDeletions(d1.Content, d2.Content)
}
func createEntryDiffs(root1, root2 string, entryNames []string) (entries []EntryDiff) {
for _, name := range entryNames {
entryPath1 := filepath.Join(root1, name)
size1 := pkgutil.GetSize(entryPath1)
entryPath2 := filepath.Join(root2, name)
size2 := pkgutil.GetSize(entryPath2)
entry := EntryDiff{
Name: name,
Size1: size1,
Size2: size2,
}
entries = append(entries, entry)
}
return entries
}