-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.go
261 lines (227 loc) · 7.9 KB
/
diff.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
// Copyright 2017 The LUCI Authors.
//
// 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 srcman
import (
"reflect"
"strings"
)
// zeroCmpTwo compares two objects of the same type where the objects could be
// pointers, slices or strings.
//
// It generates a Stat value of:
// * EQUAL if they're both nil/empty/zero
// * ADDED if a is nil and b is not
// * REMOVED if b is nil and a is not
// * For strings:
// * EQUAL/MODIFIED depending on string equality
// * For slices:
// * MODIFIED if they have differing lengths
// * Whatever the callback method returns
// * For others
// * Whatever the callback method returns
//
// The callback method is required for non-string types.
func zeroCmpTwo(a, b interface{}, modCb func() ManifestDiff_Stat) ManifestDiff_Stat {
av, bv := reflect.ValueOf(a), reflect.ValueOf(b)
t := av.Type()
if t != bv.Type() {
panic("inconsistent types")
}
var az, bz bool
if t.Kind() == reflect.Slice || t.Kind() == reflect.String {
az, bz = av.Len() == 0, bv.Len() == 0
} else {
z := reflect.Zero(t)
az, bz = av == z, bv == z
}
switch {
case az && bz:
return ManifestDiff_EQUAL
case az && !bz:
return ManifestDiff_ADDED
case !az && bz:
return ManifestDiff_REMOVED
default:
switch t.Kind() {
case reflect.String:
if av.String() == bv.String() {
return ManifestDiff_EQUAL
}
return ManifestDiff_MODIFIED
case reflect.Slice:
if av.Len() != bv.Len() {
return ManifestDiff_MODIFIED
}
}
return modCb()
}
}
// modifiedTracker is a simplistic structure. It:
// * tracks the status of anything you feed to add(). If you feed a status
// other than EQUAL, it flips to true.
// * returns MODIFIED from status() if its bool value is true, otherwise EQUAL.
//
// It's used for semi-transparently computing `Overall` Stat values for diff
// entries containing many interesting fields.
type modifiedTracker bool
// add incorporates `st` into this modifiedTracker's state.
func (c *modifiedTracker) add(st ManifestDiff_Stat) ManifestDiff_Stat {
*c = *c || st != ManifestDiff_EQUAL
return st
}
// status returns MODIFIED or EQUAL, depending on if the tracked state is true
// or false.
func (c modifiedTracker) status() ManifestDiff_Stat {
if c {
return ManifestDiff_MODIFIED
}
return ManifestDiff_EQUAL
}
func parseChangeRef(ref string) (cl string) {
if !strings.HasPrefix(ref, "refs/changes/") {
return ""
}
parts := strings.Split(ref, "/")[2:] // remove refs/changes
// should be NN/YYYYYNN/ZZ
if len(parts) != 3 {
return ""
}
return parts[1]
}
// Diff generates a Stat reflecting the difference between the `old`
// GitCheckout and the `new` one.
//
// This will generate a Stat of `DIFF` if the two GitCheckout's are non-nil and
// share the same RepoUrl.
//
// This only calculates the pure-data differences. Notably, this will not reach
// out to any remote services to populate the git_history field.
func (old *Manifest_GitCheckout) Diff(new *Manifest_GitCheckout) *ManifestDiff_GitCheckout {
if old == nil && new == nil {
return nil
}
ret := &ManifestDiff_GitCheckout{}
ret.Overall = zeroCmpTwo(old, new, func() ManifestDiff_Stat {
if old.RepoUrl == new.RepoUrl {
// For now, the canonical 'diff' URL is always the new URL. If we add
// support for source-of-truth migrations, this could change.
ret.RepoUrl = new.RepoUrl
// FetchRef doesn't matter for comparison purposes for now.
ret.Revision = zeroCmpTwo(old.Revision, new.Revision, nil)
if ret.Revision == ManifestDiff_MODIFIED {
ret.Revision = ManifestDiff_DIFF
}
// We calculate DIFF for PatchRevision iff the two checkouts both include
// patches from the same CL.
ret.PatchRevision = zeroCmpTwo(old.PatchRevision, new.PatchRevision, nil)
if ret.PatchRevision == ManifestDiff_MODIFIED {
oldCL, newCL := parseChangeRef(old.PatchFetchRef), parseChangeRef(new.PatchFetchRef)
if oldCL != "" && newCL != "" && oldCL == newCL {
ret.PatchRevision = ManifestDiff_DIFF
}
}
// If all the revisions are the same and the repo url is the same, that's
// good enough for the whole checkout to be equal.
if ret.Revision == ManifestDiff_EQUAL && ret.PatchRevision == ManifestDiff_EQUAL {
return ManifestDiff_EQUAL
}
}
return ManifestDiff_MODIFIED
})
return ret
}
// Diff generates a ManifestDiff_Stat reflecting the difference between the `old`
// CIPDPackage and the `new` one.
func (old *Manifest_CIPDPackage) Diff(new *Manifest_CIPDPackage) ManifestDiff_Stat {
return zeroCmpTwo(old, new, func() ManifestDiff_Stat {
// Version and PackagePattern don't matter for diff purposes.
return zeroCmpTwo(old.InstanceId, new.InstanceId, nil)
})
}
// IsolatedDiff produces a ManifestDiff_Stat reflecting the difference between
// two lists of Manifest_Isolated's.
func IsolatedDiff(oldisos, newisos []*Manifest_Isolated) ManifestDiff_Stat {
return zeroCmpTwo(oldisos, newisos, func() ManifestDiff_Stat {
// we know they're non-zero and also have the same length at this point
for i, old := range oldisos {
new := newisos[i]
if old.Namespace != new.Namespace {
return ManifestDiff_MODIFIED
}
if old.Hash != new.Hash {
return ManifestDiff_MODIFIED
}
}
return ManifestDiff_EQUAL
})
}
// Diff generates a ManifestDiff_Directory object which shows what changed
// between the `old` manifest directory and the `new` manifest directory.
func (old *Manifest_Directory) Diff(new *Manifest_Directory) *ManifestDiff_Directory {
ret := &ManifestDiff_Directory{}
ret.Overall = zeroCmpTwo(old, new, func() ManifestDiff_Stat {
var dirChanged modifiedTracker
if ret.GitCheckout = old.GitCheckout.Diff(new.GitCheckout); ret.GitCheckout != nil {
dirChanged.add(ret.GitCheckout.Overall)
}
ret.CipdServerHost = dirChanged.add(zeroCmpTwo(old.CipdServerHost, new.CipdServerHost, nil))
if ret.CipdServerHost == ManifestDiff_EQUAL && new.CipdServerHost != "" {
cipdPackages := map[string]ManifestDiff_Stat{}
for name, pkg := range old.CipdPackage {
cipdPackages[name] = dirChanged.add(pkg.Diff(new.CipdPackage[name]))
}
for name, pkg := range new.CipdPackage {
if _, ok := cipdPackages[name]; !ok {
cipdPackages[name] = dirChanged.add(old.CipdPackage[name].Diff(pkg))
}
}
if len(cipdPackages) > 0 {
ret.CipdPackage = cipdPackages
}
}
ret.IsolatedServerHost = dirChanged.add(zeroCmpTwo(old.IsolatedServerHost, new.IsolatedServerHost, nil))
ret.Isolated = dirChanged.add(IsolatedDiff(old.Isolated, new.Isolated))
return dirChanged.status()
})
return ret
}
// Diff generates a ManifestDiff object which shows what changed between the
// `old` manifest and the `new` manifest.
//
// This only calculates the pure-data differences. Notably, this will not reach
// out to any remote services to populate the git_history field.
func (old *Manifest) Diff(new *Manifest) *ManifestDiff {
ret := &ManifestDiff{
Old: old,
New: new,
Directories: map[string]*ManifestDiff_Directory{},
}
ret.Overall = zeroCmpTwo(old, new, func() ManifestDiff_Stat {
var anyChanged modifiedTracker
for path, olddir := range old.Directories {
dir := olddir.Diff(new.Directories[path])
ret.Directories[path] = dir
anyChanged.add(dir.Overall)
}
for path, newdir := range new.Directories {
if _, ok := ret.Directories[path]; !ok {
dir := old.Directories[path].Diff(newdir)
ret.Directories[path] = dir
anyChanged.add(dir.Overall)
}
}
return anyChanged.status()
})
return ret
}