titor 0.2.0

A high-performance checkpointing library for time-travel through directory states
Documentation
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! Line-level diff computation for text files
//!
//! This module provides functionality to compute line-by-line differences
//! between text files, similar to git's unified diff format.
//!
//! ## Overview
//!
//! The diff algorithm uses a dynamic programming approach to find the
//! Longest Common Subsequence (LCS) between two files, then generates
//! a unified diff format with configurable context lines.
//!
//! ## Features
//!
//! - **LCS-based Algorithm**: Efficient O(mn) algorithm for finding differences
//! - **Unified Diff Format**: Compatible with standard diff tools
//! - **Binary Detection**: Automatically detects and handles binary files
//! - **Configurable Context**: Control how many unchanged lines to show
//! - **Whitespace Handling**: Optional whitespace-ignoring comparison
//! - **Memory Efficient**: Processes files line-by-line
//!
//! ## Examples
//!
//! ```rust,no_run
//! use titor::diff::{compute_line_diff, create_file_diff};
//! use titor::types::DiffOptions;
//! use std::path::Path;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Configure diff options
//! let options = DiffOptions {
//!     context_lines: 3,
//!     ignore_whitespace: false,
//!     show_line_numbers: true,
//!     max_file_size: 10 * 1024 * 1024, // 10MB
//! };
//!
//! // Compute diff between two text contents
//! let old_content = b"line1\nline2\nline3";
//! let new_content = b"line1\nmodified line2\nline3\nline4";
//!
//! let hunks = compute_line_diff(old_content, new_content, &options)?;
//!
//! // Or create a complete file diff
//! let file_diff = create_file_diff(
//!     Path::new("file.txt"),
//!     "old_hash",
//!     "new_hash", 
//!     old_content,
//!     new_content,
//!     &options
//! )?;
//!
//! println!("Lines added: {}, deleted: {}", 
//!     file_diff.lines_added, 
//!     file_diff.lines_deleted
//! );
//! # Ok(())
//! # }
//! ```

use crate::error::Result;
use crate::types::{DiffHunk, FileDiff, LineChange, DiffOptions};
use std::path::Path;

/// Compute line-level diff between two text contents
///
/// This function implements a simple line-based diff algorithm that:
/// 1. Splits both texts into lines
/// 2. Finds the longest common subsequence (LCS)
/// 3. Generates hunks with context lines
///
/// # Arguments
///
/// * `old_content` - Content from the source version
/// * `new_content` - Content from the target version
/// * `options` - Options controlling diff generation
///
/// # Returns
///
/// Returns a vector of `DiffHunk` representing the changes
pub fn compute_line_diff(
    old_content: &[u8],
    new_content: &[u8],
    options: &DiffOptions,
) -> Result<Vec<DiffHunk>> {
    // Convert to strings, handling potential UTF-8 errors
    let old_text = String::from_utf8_lossy(old_content);
    let new_text = String::from_utf8_lossy(new_content);
    
    // Split into lines
    let old_lines: Vec<&str> = old_text.lines().collect();
    let new_lines: Vec<&str> = new_text.lines().collect();
    
    // Handle empty files
    if old_lines.is_empty() && new_lines.is_empty() {
        return Ok(vec![]);
    }
    
    // Compute the diff
    let changes = compute_changes(&old_lines, &new_lines, options);
    
    // Group changes into hunks
    let hunks = create_hunks(changes, &old_lines, &new_lines, options.context_lines);
    
    Ok(hunks)
}

/// Represents a change operation in the diff
#[derive(Debug, Clone)]
enum ChangeOp {
    Keep(usize, usize),    // (old_line_idx, new_line_idx)
    Delete(usize),         // old_line_idx
    Insert(usize),         // new_line_idx
}

/// Compute the sequence of change operations
fn compute_changes(
    old_lines: &[&str],
    new_lines: &[&str],
    options: &DiffOptions,
) -> Vec<ChangeOp> {
    // Special cases
    if old_lines.is_empty() {
        return new_lines.iter().enumerate()
            .map(|(i, _)| ChangeOp::Insert(i))
            .collect();
    }
    if new_lines.is_empty() {
        return old_lines.iter().enumerate()
            .map(|(i, _)| ChangeOp::Delete(i))
            .collect();
    }
    
    // Use a simple dynamic programming approach for LCS
    let lcs = compute_lcs(old_lines, new_lines, options.ignore_whitespace);
    
    // Convert LCS to change operations
    lcs_to_changes(&lcs, old_lines.len(), new_lines.len())
}

/// Compute longest common subsequence using dynamic programming
fn compute_lcs(
    old_lines: &[&str],
    new_lines: &[&str],
    ignore_whitespace: bool,
) -> Vec<(usize, usize)> {
    let m = old_lines.len();
    let n = new_lines.len();
    
    // Build DP table
    let mut dp = vec![vec![0; n + 1]; m + 1];
    
    for i in 1..=m {
        for j in 1..=n {
            if lines_equal(old_lines[i-1], new_lines[j-1], ignore_whitespace) {
                dp[i][j] = dp[i-1][j-1] + 1;
            } else {
                dp[i][j] = dp[i-1][j].max(dp[i][j-1]);
            }
        }
    }
    
    // Backtrack to find LCS
    let mut lcs = Vec::new();
    let mut i = m;
    let mut j = n;
    
    while i > 0 && j > 0 {
        if lines_equal(old_lines[i-1], new_lines[j-1], ignore_whitespace) {
            lcs.push((i-1, j-1));
            i -= 1;
            j -= 1;
        } else if dp[i-1][j] > dp[i][j-1] {
            i -= 1;
        } else {
            j -= 1;
        }
    }
    
    lcs.reverse();
    lcs
}

/// Check if two lines are equal, optionally ignoring whitespace
fn lines_equal(a: &str, b: &str, ignore_whitespace: bool) -> bool {
    if ignore_whitespace {
        a.trim() == b.trim()
    } else {
        a == b
    }
}

/// Convert LCS to a sequence of change operations
fn lcs_to_changes(lcs: &[(usize, usize)], old_len: usize, new_len: usize) -> Vec<ChangeOp> {
    let mut changes = Vec::new();
    let mut old_idx = 0;
    let mut new_idx = 0;
    let mut lcs_idx = 0;
    
    while old_idx < old_len || new_idx < new_len {
        if lcs_idx < lcs.len() {
            let (lcs_old, lcs_new) = lcs[lcs_idx];
            
            // Process deletions before the next match
            while old_idx < lcs_old {
                changes.push(ChangeOp::Delete(old_idx));
                old_idx += 1;
            }
            
            // Process insertions before the next match
            while new_idx < lcs_new {
                changes.push(ChangeOp::Insert(new_idx));
                new_idx += 1;
            }
            
            // Keep the matching line
            if old_idx == lcs_old && new_idx == lcs_new {
                changes.push(ChangeOp::Keep(old_idx, new_idx));
                old_idx += 1;
                new_idx += 1;
                lcs_idx += 1;
            }
        } else {
            // No more matches, process remaining lines
            while old_idx < old_len {
                changes.push(ChangeOp::Delete(old_idx));
                old_idx += 1;
            }
            while new_idx < new_len {
                changes.push(ChangeOp::Insert(new_idx));
                new_idx += 1;
            }
        }
    }
    
    changes
}

/// Create diff hunks from change operations
fn create_hunks(
    changes: Vec<ChangeOp>,
    old_lines: &[&str],
    new_lines: &[&str],
    context_lines: usize,
) -> Vec<DiffHunk> {
    if changes.is_empty() {
        return vec![];
    }
    
    let mut hunks = Vec::new();
    let mut current_hunk: Option<HunkBuilder> = None;
    
    for (i, change) in changes.iter().enumerate() {
        match change {
            ChangeOp::Keep(old_idx, new_idx) => {
                // Check if this is within context of a change
                let near_change = is_near_change(&changes, i, context_lines);
                
                if near_change {
                    // Include as context
                    if let Some(ref mut hunk) = current_hunk {
                        hunk.add_context(*old_idx, *new_idx, old_lines[*old_idx]);
                    } else {
                        // Start new hunk with context
                        let mut hunk = HunkBuilder::new();
                        
                        // Add leading context
                        let start = i.saturating_sub(context_lines);
                        for j in start..i {
                            if let ChangeOp::Keep(o, n) = &changes[j] {
                                hunk.add_context(*o, *n, old_lines[*o]);
                            }
                        }
                        
                        hunk.add_context(*old_idx, *new_idx, old_lines[*old_idx]);
                        current_hunk = Some(hunk);
                    }
                } else if let Some(hunk) = current_hunk.take() {
                    // End current hunk
                    if let Some(built) = hunk.build() {
                        hunks.push(built);
                    }
                }
            }
            ChangeOp::Delete(old_idx) => {
                if current_hunk.is_none() {
                    let mut hunk = HunkBuilder::new();
                    
                    // Add leading context
                    let start = i.saturating_sub(context_lines);
                    for j in start..i {
                        if let ChangeOp::Keep(o, n) = &changes[j] {
                            hunk.add_context(*o, *n, old_lines[*o]);
                        }
                    }
                    
                    current_hunk = Some(hunk);
                }
                
                if let Some(ref mut hunk) = current_hunk {
                    hunk.add_deletion(*old_idx, old_lines[*old_idx]);
                }
            }
            ChangeOp::Insert(new_idx) => {
                if current_hunk.is_none() {
                    let mut hunk = HunkBuilder::new();
                    
                    // Add leading context
                    let start = i.saturating_sub(context_lines);
                    for j in start..i {
                        if let ChangeOp::Keep(o, n) = &changes[j] {
                            hunk.add_context(*o, *n, old_lines[*o]);
                        }
                    }
                    
                    current_hunk = Some(hunk);
                }
                
                if let Some(ref mut hunk) = current_hunk {
                    hunk.add_insertion(*new_idx, new_lines[*new_idx]);
                }
            }
        }
    }
    
    // Finish last hunk
    if let Some(hunk) = current_hunk {
        if let Some(built) = hunk.build() {
            hunks.push(built);
        }
    }
    
    hunks
}

/// Check if a position is near a change (within context_lines)
fn is_near_change(changes: &[ChangeOp], pos: usize, context_lines: usize) -> bool {
    let start = pos.saturating_sub(context_lines);
    let end = (pos + context_lines + 1).min(changes.len());
    
    for i in start..end {
        if i == pos {
            continue;
        }
        match &changes[i] {
            ChangeOp::Delete(_) | ChangeOp::Insert(_) => return true,
            ChangeOp::Keep(_, _) => continue,
        }
    }
    
    false
}

/// Helper for building diff hunks
struct HunkBuilder {
    from_start: Option<usize>,
    to_start: Option<usize>,
    from_count: usize,
    to_count: usize,
    changes: Vec<LineChange>,
}

impl HunkBuilder {
    fn new() -> Self {
        Self {
            from_start: None,
            to_start: None,
            from_count: 0,
            to_count: 0,
            changes: Vec::new(),
        }
    }
    
    fn add_context(&mut self, old_idx: usize, new_idx: usize, content: &str) {
        if self.from_start.is_none() {
            self.from_start = Some(old_idx + 1); // 1-based
            self.to_start = Some(new_idx + 1);
        }
        self.from_count += 1;
        self.to_count += 1;
        self.changes.push(LineChange::Context(old_idx + 1, content.to_string()));
    }
    
    fn add_deletion(&mut self, old_idx: usize, content: &str) {
        if self.from_start.is_none() {
            self.from_start = Some(old_idx + 1);
            self.to_start = Some(1); // Will be adjusted
        }
        self.from_count += 1;
        self.changes.push(LineChange::Deleted(old_idx + 1, content.to_string()));
    }
    
    fn add_insertion(&mut self, new_idx: usize, content: &str) {
        if self.to_start.is_none() {
            self.from_start = Some(1); // Will be adjusted
            self.to_start = Some(new_idx + 1);
        }
        self.to_count += 1;
        self.changes.push(LineChange::Added(new_idx + 1, content.to_string()));
    }
    
    fn build(self) -> Option<DiffHunk> {
        if self.changes.is_empty() {
            return None;
        }
        
        Some(DiffHunk {
            from_line: self.from_start.unwrap_or(1),
            from_count: self.from_count,
            to_line: self.to_start.unwrap_or(1),
            to_count: self.to_count,
            changes: self.changes,
        })
    }
}

/// Check if content appears to be binary
pub fn is_binary_content(content: &[u8]) -> bool {
    // Simple heuristic: check for null bytes in first 8KB
    let check_len = content.len().min(8192);
    content[..check_len].contains(&0)
}

/// Create a FileDiff from file contents
pub fn create_file_diff(
    path: &Path,
    from_hash: &str,
    to_hash: &str,
    old_content: &[u8],
    new_content: &[u8],
    options: &DiffOptions,
) -> Result<FileDiff> {
    // Check if binary
    let is_binary = is_binary_content(old_content) || is_binary_content(new_content);
    
    let (hunks, lines_added, lines_deleted) = if is_binary {
        (vec![], 0, 0)
    } else {
        let hunks = compute_line_diff(old_content, new_content, options)?;
        
        // Count line changes
        let mut added = 0;
        let mut deleted = 0;
        for hunk in &hunks {
            for change in &hunk.changes {
                match change {
                    LineChange::Added(_, _) => added += 1,
                    LineChange::Deleted(_, _) => deleted += 1,
                    LineChange::Context(_, _) => {}
                }
            }
        }
        
        (hunks, added, deleted)
    };
    
    Ok(FileDiff {
        path: path.to_path_buf(),
        from_hash: from_hash.to_string(),
        to_hash: to_hash.to_string(),
        is_binary,
        hunks,
        lines_added,
        lines_deleted,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_simple_diff() {
        let old = b"line1\nline2\nline3";
        let new = b"line1\nline2 modified\nline3\nline4";
        
        let options = DiffOptions::default();
        let hunks = compute_line_diff(old, new, &options).unwrap();
        
        assert_eq!(hunks.len(), 1);
        let hunk = &hunks[0];
        
        // Should have context, deletion, additions
        assert!(hunk.changes.iter().any(|c| matches!(c, LineChange::Deleted(_, _))));
        assert!(hunk.changes.iter().any(|c| matches!(c, LineChange::Added(_, _))));
    }
    
    #[test]
    fn test_empty_files() {
        let options = DiffOptions::default();
        
        // Both empty
        let hunks = compute_line_diff(b"", b"", &options).unwrap();
        assert_eq!(hunks.len(), 0);
        
        // Old empty
        let hunks = compute_line_diff(b"", b"new line", &options).unwrap();
        assert_eq!(hunks.len(), 1);
        assert!(hunks[0].changes.iter().all(|c| matches!(c, LineChange::Added(_, _))));
        
        // New empty
        let hunks = compute_line_diff(b"old line", b"", &options).unwrap();
        assert_eq!(hunks.len(), 1);
        assert!(hunks[0].changes.iter().all(|c| matches!(c, LineChange::Deleted(_, _))));
    }
    
    #[test]
    fn test_binary_detection() {
        assert!(is_binary_content(b"hello\x00world"));
        assert!(!is_binary_content(b"hello world"));
    }
    
    #[test]
    fn test_context_lines() {
        let old = b"1\n2\n3\n4\n5\n6\n7\n8\n9";
        let new = b"1\n2\n3\nMODIFIED\n5\n6\n7\n8\n9";
        
        let mut options = DiffOptions::default();
        options.context_lines = 2;
        
        let hunks = compute_line_diff(old, new, &options).unwrap();
        assert_eq!(hunks.len(), 1);
        
        // Check that we have context lines
        let context_count = hunks[0].changes.iter()
            .filter(|c| matches!(c, LineChange::Context(_, _)))
            .count();
        assert!(context_count >= 4); // At least 2 before and 2 after
    }
}