This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
/
diff_utils.go
191 lines (161 loc) · 4.76 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
/*
Copyright 2017 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/GoogleCloudPlatform/container-diff/pkg/util"
"github.com/golang/glog"
"github.com/pmezard/go-difflib/difflib"
)
type DirDiff struct {
Adds []pkgutil.DirectoryEntry
Dels []pkgutil.DirectoryEntry
Mods []EntryDiff
}
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' {
for _, line := range b[j1:j2] {
adds = append(adds, line)
}
}
}
}
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' {
for _, line := range a[i1:i2] {
dels = append(dels, line)
}
}
}
}
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
for _, line := range a[start:end] {
matches = append(matches, line)
}
}
}
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
}
// 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.Stat(f1path)
if err != nil {
glog.Errorf("Error checking directory entry %s: %s\n", f, err)
continue
}
f2stat, err := os.Stat(f2path)
if err != nil {
glog.Errorf("Error checking directory entry %s: %s\n", f, err)
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 {
glog.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
}