forked from jj-vcs/jj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.rs
444 lines (420 loc) · 16.2 KB
/
files.rs
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// Copyright 2020 Google LLC
//
// 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
//
// https://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.
use std::collections::{HashSet, VecDeque};
use std::fmt::{Debug, Error, Formatter};
use std::ops::Range;
use itertools::Itertools;
use crate::diff;
use crate::diff::{Diff, DiffHunk};
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct DiffLine<'a> {
pub left_line_number: u32,
pub right_line_number: u32,
pub has_left_content: bool,
pub has_right_content: bool,
pub hunks: Vec<DiffHunk<'a>>,
}
impl DiffLine<'_> {
fn reset_line(&mut self) {
self.has_left_content = false;
self.has_right_content = false;
self.hunks.clear();
}
pub fn is_unmodified(&self) -> bool {
self.hunks
.iter()
.all(|hunk| matches!(hunk, DiffHunk::Matching(_)))
}
}
pub fn diff<'a>(left: &'a [u8], right: &'a [u8]) -> DiffLineIterator<'a> {
let diff_hunks = diff::diff(left, right);
DiffLineIterator::new(diff_hunks)
}
pub struct DiffLineIterator<'a> {
diff_hunks: Vec<DiffHunk<'a>>,
current_pos: usize,
current_line: DiffLine<'a>,
queued_lines: VecDeque<DiffLine<'a>>,
}
impl<'a> DiffLineIterator<'a> {
fn new(diff_hunks: Vec<DiffHunk<'a>>) -> Self {
let current_line = DiffLine {
left_line_number: 1,
right_line_number: 1,
has_left_content: false,
has_right_content: false,
hunks: vec![],
};
DiffLineIterator {
diff_hunks,
current_pos: 0,
current_line,
queued_lines: VecDeque::new(),
}
}
}
impl<'a> Iterator for DiffLineIterator<'a> {
type Item = DiffLine<'a>;
fn next(&mut self) -> Option<Self::Item> {
// TODO: Should we attempt to interpret as utf-8 and otherwise break only at
// newlines?
while self.current_pos < self.diff_hunks.len() && self.queued_lines.is_empty() {
let hunk = &self.diff_hunks[self.current_pos];
self.current_pos += 1;
match hunk {
diff::DiffHunk::Matching(text) => {
let lines = text.split_inclusive(|b| *b == b'\n');
for line in lines {
self.current_line.has_left_content = true;
self.current_line.has_right_content = true;
self.current_line.hunks.push(DiffHunk::Matching(line));
if line.ends_with(b"\n") {
self.queued_lines.push_back(self.current_line.clone());
self.current_line.left_line_number += 1;
self.current_line.right_line_number += 1;
self.current_line.reset_line();
}
}
}
diff::DiffHunk::Different(contents) => {
let left_lines = contents[0].split_inclusive(|b| *b == b'\n');
for left_line in left_lines {
self.current_line.has_left_content = true;
self.current_line
.hunks
.push(DiffHunk::Different(vec![left_line, b""]));
if left_line.ends_with(b"\n") {
self.queued_lines.push_back(self.current_line.clone());
self.current_line.left_line_number += 1;
self.current_line.reset_line();
}
}
let right_lines = contents[1].split_inclusive(|b| *b == b'\n');
for right_line in right_lines {
self.current_line.has_right_content = true;
self.current_line
.hunks
.push(DiffHunk::Different(vec![b"", right_line]));
if right_line.ends_with(b"\n") {
self.queued_lines.push_back(self.current_line.clone());
self.current_line.right_line_number += 1;
self.current_line.reset_line();
}
}
}
}
}
if let Some(line) = self.queued_lines.pop_front() {
return Some(line);
}
if !self.current_line.hunks.is_empty() {
let line = self.current_line.clone();
self.current_line.reset_line();
return Some(line);
}
None
}
}
#[derive(PartialEq, Eq, Clone)]
pub enum MergeHunk {
Resolved(Vec<u8>),
Conflict {
removes: Vec<Vec<u8>>,
adds: Vec<Vec<u8>>,
},
}
impl Debug for MergeHunk {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
match self {
MergeHunk::Resolved(data) => f
.debug_tuple("Resolved")
.field(&String::from_utf8_lossy(data))
.finish(),
MergeHunk::Conflict { removes, adds } => f
.debug_struct("Conflict")
.field(
"removes",
&removes
.iter()
.map(|part| String::from_utf8_lossy(part))
.collect_vec(),
)
.field(
"adds",
&adds
.iter()
.map(|part| String::from_utf8_lossy(part))
.collect_vec(),
)
.finish(),
}
}
}
#[derive(PartialEq, Eq, Clone)]
pub enum MergeResult {
Resolved(Vec<u8>),
Conflict(Vec<MergeHunk>),
}
impl Debug for MergeResult {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
match self {
MergeResult::Resolved(data) => f
.debug_tuple("Resolved")
.field(&String::from_utf8_lossy(data))
.finish(),
MergeResult::Conflict(hunks) => f.debug_tuple("Conflict").field(hunks).finish(),
}
}
}
/// A region where the base and two sides match.
#[derive(Debug, PartialEq, Eq, Clone)]
struct SyncRegion {
base: Range<usize>,
left: Range<usize>,
right: Range<usize>,
}
// TODO: Should we require `add.len() == removes.len() + 1`? If that condition
// is false, it effectively means that we should pretend that there are empty
// strings in `removes` or `adds` to make it true. Maybe we should have to
// caller make it explicitly that way.
pub fn merge(removes: &[&[u8]], adds: &[&[u8]]) -> MergeResult {
let num_removes = removes.len();
// TODO: Using the first remove as base (first in the inputs) is how it's
// usually done for 3-way conflicts. Are there better heuristics when there are
// more than 3 parts?
let mut diff_inputs = removes.to_vec();
diff_inputs.extend(adds);
let diff = Diff::for_tokenizer(&diff_inputs, &diff::find_line_ranges);
let mut resolved_hunk: Vec<u8> = vec![];
let mut merge_hunks: Vec<MergeHunk> = vec![];
for diff_hunk in diff.hunks() {
match diff_hunk {
DiffHunk::Matching(content) => {
if adds.len() > removes.len() {
resolved_hunk.extend(content);
}
}
DiffHunk::Different(parts) => {
let mut removed_parts = parts[..num_removes].to_vec();
let mut added_parts = parts[num_removes..].to_vec();
// Remove pairs of parts that match in the removes and adds.
let mut added_index = 0;
while added_index < added_parts.len() {
let added_part = added_parts[added_index];
added_index += 1;
for (removed_index, removed_part) in removed_parts.iter().enumerate() {
if *removed_part == added_part {
added_index -= 1;
added_parts.remove(added_index);
removed_parts.remove(removed_index);
break;
}
}
}
let distinct_removes: HashSet<&[u8]> = removed_parts.iter().copied().collect();
let distinct_adds: HashSet<&[u8]> = added_parts.iter().copied().collect();
if removed_parts.is_empty() && added_parts.is_empty() {
// The same content was added and removed, so there's
// nothing left.
} else if distinct_removes.is_empty() && distinct_adds.len() == 1 {
// All sides added the same content
resolved_hunk.extend(added_parts[0]);
} else if distinct_removes.len() == 1 && distinct_adds.is_empty() {
// All sides removed the same content
} else if distinct_removes.len() == 1
&& distinct_adds.len() == 1
&& added_parts.len() == removed_parts.len() + 1
{
// All sides made the same change, and there's a matching extra base to apply it
// to
resolved_hunk.extend(added_parts[0]);
} else {
if !resolved_hunk.is_empty() {
merge_hunks.push(MergeHunk::Resolved(resolved_hunk));
resolved_hunk = vec![];
}
// Include the unfiltered lists of removed and added here, so the caller
// knows which part corresponds to which input.
merge_hunks.push(MergeHunk::Conflict {
removes: parts[..num_removes]
.iter()
.map(|part| part.to_vec())
.collect_vec(),
adds: parts[num_removes..]
.iter()
.map(|part| part.to_vec())
.collect_vec(),
});
}
}
}
}
if merge_hunks.is_empty() {
MergeResult::Resolved(resolved_hunk)
} else {
if !resolved_hunk.is_empty() {
merge_hunks.push(MergeHunk::Resolved(resolved_hunk));
}
MergeResult::Conflict(merge_hunks)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_merge() {
// Unchanged and empty on all sides
assert_eq!(
merge(&[b""], &[b"", b""]),
MergeResult::Resolved(b"".to_vec())
);
// Unchanged on all sides
assert_eq!(
merge(&[b"a"], &[b"a", b"a"]),
MergeResult::Resolved(b"a".to_vec())
);
// One side removed, one side unchanged
assert_eq!(
merge(&[b"a\n"], &[b"", b"a\n"]),
MergeResult::Resolved(b"".to_vec())
);
// One side unchanged, one side removed
assert_eq!(
merge(&[b"a\n"], &[b"a\n", b""]),
MergeResult::Resolved(b"".to_vec())
);
// Both sides removed same line
assert_eq!(
merge(&[b"a\n"], &[b"", b""]),
MergeResult::Resolved(b"".to_vec())
);
// One side modified, one side unchanged
assert_eq!(
merge(&[b"a"], &[b"a b", b"a"]),
MergeResult::Resolved(b"a b".to_vec())
);
// One side unchanged, one side modified
assert_eq!(
merge(&[b"a"], &[b"a", b"a b"]),
MergeResult::Resolved(b"a b".to_vec())
);
// All sides added same content
assert_eq!(
merge(&[], &[b"a\n", b"a\n", b"a\n"]),
MergeResult::Resolved(b"a\n".to_vec())
);
// One side modified, two sides added
assert_eq!(
merge(&[b"a"], &[b"b", b"b", b"b"]),
MergeResult::Conflict(vec![MergeHunk::Conflict {
removes: vec![b"a".to_vec()],
adds: vec![b"b".to_vec(), b"b".to_vec(), b"b".to_vec()]
}])
);
// All sides removed same content
assert_eq!(
merge(&[b"a\n", b"a\n", b"a\n"], &[]),
MergeResult::Resolved(b"".to_vec())
);
// One side modified, two sides removed
assert_eq!(
merge(&[b"a\n", b"a\n", b"a\n"], &[b""]),
MergeResult::Conflict(vec![MergeHunk::Conflict {
removes: vec![b"a\n".to_vec(), b"a\n".to_vec(), b"a\n".to_vec()],
adds: vec![b"".to_vec()]
}])
);
// Three sides made the same change
assert_eq!(
merge(&[b"a", b"a"], &[b"b", b"b", b"b"]),
MergeResult::Resolved(b"b".to_vec())
);
// One side unchanged, one side added
assert_eq!(
merge(&[b"a\n"], &[b"a\nb\n"]),
MergeResult::Conflict(vec![MergeHunk::Conflict {
removes: vec![b"".to_vec()],
adds: vec![b"b\n".to_vec()]
}])
);
// Two sides left one line unchanged, and added conflicting additional lines
assert_eq!(
merge(&[b"a\n"], &[b"a\nb\n", b"a\nc\n"]),
MergeResult::Conflict(vec![
MergeHunk::Resolved(b"a\n".to_vec()),
MergeHunk::Conflict {
removes: vec![b"".to_vec()],
adds: vec![b"b\n".to_vec(), b"c\n".to_vec()]
}
])
);
// One side removed, one side modified
assert_eq!(
merge(&[b"a\n"], &[b"", b"b\n"]),
MergeResult::Conflict(vec![MergeHunk::Conflict {
removes: vec![b"a\n".to_vec()],
adds: vec![b"".to_vec(), b"b\n".to_vec()]
}])
);
// One side modified, one side removed
assert_eq!(
merge(&[b"a\n"], &[b"b\n", b""]),
MergeResult::Conflict(vec![MergeHunk::Conflict {
removes: vec![b"a\n".to_vec()],
adds: vec![b"b\n".to_vec(), b"".to_vec()]
}])
);
// Two sides modified in different ways
assert_eq!(
merge(&[b"a"], &[b"b", b"c"]),
MergeResult::Conflict(vec![MergeHunk::Conflict {
removes: vec![b"a".to_vec()],
adds: vec![b"b".to_vec(), b"c".to_vec()]
}])
);
// Two of three sides don't change, third side changes
assert_eq!(
merge(&[b"a", b"a"], &[b"a", b"", b"a"]),
MergeResult::Resolved(b"".to_vec())
);
// One side unchanged, two other sides make the same change
assert_eq!(
merge(&[b"a", b"a"], &[b"", b"a", b""]),
MergeResult::Resolved(b"".to_vec())
);
// One side unchanged, two other sides make the different change
assert_eq!(
merge(&[b"a", b"a"], &[b"b", b"a", b"c"]),
MergeResult::Conflict(vec![MergeHunk::Conflict {
removes: vec![b"a".to_vec(), b"a".to_vec()],
adds: vec![b"b".to_vec(), b"a".to_vec(), b"c".to_vec()]
}])
);
// Merge of an unresolved conflict and another branch, where the other branch
// undid the change from one of the inputs to the unresolved conflict in the
// first.
assert_eq!(
merge(&[b"a", b"b"], &[b"b", b"a", b"c"]),
MergeResult::Resolved(b"c".to_vec())
);
// Merge of an unresolved conflict and another branch.
assert_eq!(
merge(&[b"a", b"b"], &[b"c", b"d", b"e"]),
MergeResult::Conflict(vec![MergeHunk::Conflict {
removes: vec![b"a".to_vec(), b"b".to_vec()],
adds: vec![b"c".to_vec(), b"d".to_vec(), b"e".to_vec()]
}])
);
}
}