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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
use apply::find_alive::*;
use backend::*;
use patch::*;
use rand;
use std::collections::HashSet;
use Result;
impl<'env, T: rand::Rng> MutTxn<'env, T> {
/// Deleted contexts are conflicts. Reconnect the graph by
/// inserting pseudo-edges alongside deleted edges.
pub(in apply) fn repair_deleted_contexts(
&mut self,
branch: &mut Branch,
patch: &Patch,
patch_id: PatchId,
) -> Result<()> {
let mut alive = Vec::new();
let mut files = Vec::new();
let mut find_alive = FindAlive::new();
// repair_missing_context adds all zombie edges needed.
for ch in patch.changes().iter() {
match *ch {
Change::NewEdges {
flag, ref edges, ..
} => {
if !flag.contains(EdgeFlags::DELETED_EDGE) {
self.repair_context_nondeleted(
branch,
patch_id,
edges,
flag,
&mut find_alive,
&mut alive,
&mut files,
)?
} else {
self.repair_context_deleted(
branch,
patch_id,
edges,
flag,
&mut find_alive,
&mut alive,
&mut files,
patch.dependencies(),
)?
}
}
Change::NewNodes {
ref up_context,
ref down_context,
flag,
..
} => {
debug!("repairing missing contexts for newnodes");
// If not all lines in `up_context` are alive, this
// is a conflict, repair.
for c in up_context {
let c = self.internal_key(c, patch_id);
// Is the up context deleted by another patch, and the
// deletion was not also confirmed by this patch?
let up_context_deleted = self.was_context_deleted(branch, patch_id, c);
debug!(
"up_context_deleted: patch_id = {:?} context = {:?} up_context_deleted = {:?}",
patch_id, c, up_context_deleted
);
if up_context_deleted {
self.repair_missing_up_context(
&mut find_alive,
branch,
c,
flag,
&mut alive,
&mut files,
&[patch_id],
)?
}
}
// If not all lines in `down_context` are alive,
// this is a conflict, repair.
for c in down_context {
let c = self.internal_key(c, patch_id);
let down_context_deleted = self.was_context_deleted(branch, patch_id, c);
debug!("down_context_deleted: {:?}", down_context_deleted);
if down_context_deleted {
self.repair_missing_down_context(
&mut find_alive,
branch,
c,
&mut alive,
&[patch_id],
)?
}
}
debug!("apply: newnodes, done");
}
}
}
Ok(())
}
/// This function handles the case where we're adding an alive
/// edge, and the origin or destination (or both) of this edge is
/// dead in the graph.
fn repair_context_nondeleted(
&mut self,
branch: &mut Branch,
patch_id: PatchId,
edges: &[NewEdge],
flag: EdgeFlags,
find_alive: &mut FindAlive,
alive: &mut Vec<Key<PatchId>>,
files: &mut Vec<(Key<PatchId>, Edge)>,
) -> Result<()> {
debug!("repairing missing contexts for non-deleted edges");
for e in edges {
let (up_context, down_context) = if flag.contains(EdgeFlags::PARENT_EDGE) {
(
self.internal_key(&e.to, patch_id),
self.internal_key(&e.from, patch_id),
)
} else {
(
self.internal_key(&e.from, patch_id),
self.internal_key(&e.to, patch_id),
)
};
if self.was_context_deleted(branch, patch_id, up_context) {
self.repair_missing_up_context(
find_alive,
branch,
up_context,
flag,
alive,
files,
&[patch_id],
)?
}
if self.was_context_deleted(branch, patch_id, down_context) {
self.repair_missing_down_context(
find_alive,
branch,
down_context,
alive,
&[patch_id],
)?
}
}
Ok(())
}
/// Handle the case where we're inserting a deleted edge, but the
/// source or target (or both) does not know about (at least one
/// of) its adjacent edges.
fn repair_context_deleted(
&mut self,
branch: &mut Branch,
patch_id: PatchId,
edges: &[NewEdge],
flag: EdgeFlags,
find_alive: &mut FindAlive,
alive: &mut Vec<Key<PatchId>>,
files: &mut Vec<(Key<PatchId>, Edge)>,
dependencies: &HashSet<Hash>,
) -> Result<()> {
debug!("repairing missing contexts for deleted edges");
debug_assert!(flag.contains(EdgeFlags::DELETED_EDGE));
for e in edges {
let dest = if flag.contains(EdgeFlags::PARENT_EDGE) {
self.internal_key(&e.from, patch_id)
} else {
self.internal_key(&e.to, patch_id)
};
debug!("dest = {:?}", dest);
// If there is at least one unknown child, repair the
// context.
let mut unknown_children = Vec::new();
for (k, v) in self.iter_nodes(branch, Some((dest, None))) {
if k != dest
|| v.flag | EdgeFlags::FOLDER_EDGE
> EdgeFlags::PSEUDO_EDGE | EdgeFlags::EPSILON_EDGE | EdgeFlags::FOLDER_EDGE
{
break;
}
if v.introduced_by != patch_id && {
let ext = self.external_hash(v.introduced_by).to_owned();
!dependencies.contains(&ext)
} {
unknown_children.push(v.introduced_by)
}
debug!(
"child is_unknown({}): {:?} {:?}",
line!(),
v,
unknown_children
);
}
if !unknown_children.is_empty() {
self.repair_missing_up_context(
find_alive,
branch,
dest,
flag,
alive,
files,
&unknown_children,
)?;
}
// If there is at least one alive parent we don't know
// about, repair.
let e = Edge::zero(EdgeFlags::PARENT_EDGE);
unknown_children.clear();
let mut unknown_parents = unknown_children;
for (k, v) in self.iter_nodes(branch, Some((dest, Some(e)))) {
if k != dest
|| v.flag | EdgeFlags::FOLDER_EDGE
!= EdgeFlags::PARENT_EDGE | EdgeFlags::FOLDER_EDGE
{
break;
}
if v.introduced_by != patch_id && {
let ext = self.external_hash(v.introduced_by).to_owned();
!dependencies.contains(&ext)
} {
unknown_parents.push(v.introduced_by)
}
debug!(
"parent is_unknown({}): {:?} {:?}",
line!(),
v,
unknown_parents
);
}
if !unknown_parents.is_empty() {
self.repair_missing_down_context(find_alive, branch, dest, alive, &unknown_parents)?
}
}
Ok(())
}
/// Was `context` deleted by patches other than `patch_id`, and
/// additionally not deleted by `patch_id`?
fn was_context_deleted(
&self,
branch: &Branch,
patch_id: PatchId,
context: Key<PatchId>,
) -> bool {
let mut context_deleted = false;
for v in self
.iter_adjacent(
branch,
context,
EdgeFlags::PARENT_EDGE | EdgeFlags::DELETED_EDGE,
EdgeFlags::all(),
)
.take_while(|v| {
v.flag
.contains(EdgeFlags::PARENT_EDGE | EdgeFlags::DELETED_EDGE)
})
{
debug!("was_context_deleted {:?}", v);
if v.introduced_by == patch_id {
return false;
} else {
context_deleted = true
}
}
context_deleted
}
/// Checks whether a line in the up context of a hunk is marked
/// deleted, and if so, reconnect the alive parts of the graph,
/// marking this situation as a conflict.
pub(crate) fn repair_missing_up_context(
&mut self,
find_alive: &mut FindAlive,
branch: &mut Branch,
context: Key<PatchId>,
flag: EdgeFlags,
alive: &mut Vec<Key<PatchId>>,
files: &mut Vec<(Key<PatchId>, Edge)>,
unknown_patches: &[PatchId],
) -> Result<()> {
// The up context needs a repair iff it's deleted.
// The up context was deleted, so the alive
// component of the graph might be disconnected, and needs
// a repair.
// Follow all paths upwards (in the direction of
// DELETED_EDGE|PARENT_EDGE) until finding an alive
// ancestor, and turn them all into zombie edges.
find_alive.clear();
find_alive.push(context);
alive.clear();
files.clear();
let mut first_file = None;
self.find_alive_ancestors(find_alive, branch, alive, &mut first_file, files);
debug!("files {:?} alive {:?}", files, alive);
if !flag.contains(EdgeFlags::FOLDER_EDGE) {
for ancestor in alive.drain(..).chain(first_file.into_iter()) {
let mut edge = Edge::zero(EdgeFlags::PSEUDO_EDGE | EdgeFlags::PARENT_EDGE);
if ancestor != context {
edge.dest = ancestor;
for patch_id in unknown_patches {
edge.introduced_by = patch_id.clone();
debug!("repairing up context: {:?} {:?}", context, edge);
self.put_edge_both_dirs(branch, context, edge)?;
}
}
}
}
for (key, mut edge) in files.drain(..) {
if !self.is_connected(branch, key, edge.dest) {
edge.flag =
EdgeFlags::PSEUDO_EDGE | EdgeFlags::PARENT_EDGE | EdgeFlags::FOLDER_EDGE;
for patch_id in unknown_patches {
edge.introduced_by = patch_id.clone();
debug!("file: repairing up context: {:?} {:?}", key, edge);
self.put_edge_both_dirs(branch, key, edge)?;
}
}
}
Ok(())
}
/// Checks whether a line in the down context of a hunk is marked
/// deleted, and if so, reconnect the alive parts of the graph,
/// marking this situation as a conflict.
fn repair_missing_down_context(
&mut self,
find_alive: &mut FindAlive,
branch: &mut Branch,
context: Key<PatchId>,
alive: &mut Vec<Key<PatchId>>,
unknown_patches: &[PatchId],
) -> Result<()> {
// Find all alive descendants, as well as the paths
// leading to them, and double these edges with
// pseudo-edges everywhere.
find_alive.clear();
alive.clear();
for v in self.iter_adjacent(
branch,
context,
EdgeFlags::DELETED_EDGE,
EdgeFlags::DELETED_EDGE,
) {
find_alive.push(v.dest)
}
self.find_alive_descendants(find_alive, &branch, alive);
debug!("down context relatives: {:?}", alive);
let mut edge = Edge::zero(EdgeFlags::PSEUDO_EDGE);
for key in alive.drain(..) {
if !self.is_connected(branch, key, edge.dest) && key != context {
edge.dest = key;
edge.flag = EdgeFlags::PSEUDO_EDGE | (edge.flag & EdgeFlags::FOLDER_EDGE);
for patch_id in unknown_patches {
edge.introduced_by = patch_id.clone();
debug!("repairing down context: {:?} {:?}", key, edge);
self.put_edge_both_dirs(branch, context, edge)?;
}
}
}
Ok(())
}
}