branchdiff 0.63.2

Terminal UI showing unified diff of current branch vs its base
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
531
532
533
534
535
use similar::{ChangeTag, TextDiff};

use super::LineSource;

/// Result of computing inline diff - indicates whether the diff is meaningful
/// for single-line merged display
#[derive(Debug)]
pub struct InlineDiffResult {
    /// The spans for rendering (only used if is_meaningful is true)
    pub spans: Vec<InlineSpan>,
    /// Whether this diff has meaningful inline changes (some unchanged portions)
    /// If false, the lines are too different and should use -/+ pair display
    pub is_meaningful: bool,
}

/// Represents a span within a line for inline diff highlighting
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InlineSpan {
    /// The text content of this span
    pub text: String,
    /// The source/style for this span (None = unchanged from base, Some = this specific source)
    pub source: Option<LineSource>,
    /// Whether this span represents deleted text (should be rendered but isn't part of actual line content)
    /// Deleted spans appear visually before their corresponding insertion at the same position
    pub is_deletion: bool,
}

/// Compute inline diff spans showing BOTH deleted and inserted text
///
/// The key insight: we show deleted text (in red/deletion color) immediately before
/// the corresponding inserted text (in the change_source color). Unchanged text
/// uses source=None (gray).
///
/// Example: "commercial_renewal.name" -> "bond.name"
/// Produces spans: [
///   { text: "commercial_renewal", source: DeletedBase, is_deletion: true },
///   { text: "bond", source: Committed, is_deletion: false },
///   { text: ".name", source: None, is_deletion: false },
/// ]
///
/// The deletion_source parameter specifies what color to use for deleted text
/// (e.g., DeletedBase for committed changes, DeletedCommitted for staged, etc.)
pub fn compute_inline_diff_merged(
    old_line: &str,
    new_line: &str,
    change_source: LineSource,
) -> InlineDiffResult {
    let deletion_source = match change_source {
        LineSource::Committed => LineSource::DeletedBase,
        LineSource::Staged => LineSource::DeletedCommitted,
        LineSource::Unstaged => LineSource::DeletedStaged,
        _ => LineSource::DeletedBase,
    };

    let diff = TextDiff::from_chars(old_line, new_line);
    let mut spans = Vec::new();
    let mut max_unchanged_segment = 0usize;
    let mut num_unchanged_segments = 0usize;
    // Track whether we've seen any non-whitespace content yet.
    // Leading whitespace shouldn't count toward "meaningful" unchanged segments
    // because indentation matching creates false positives (e.g., `}` vs `.map(...)`
    // both have 12 spaces of indentation but are completely different code).
    let mut seen_non_whitespace = false;

    let mut pending_unchanged = String::new();
    let mut pending_deleted = String::new();
    let mut pending_inserted = String::new();

    // Helper to flush pending unchanged content, updating metrics
    let flush_unchanged = |pending: &mut String,
                               spans: &mut Vec<InlineSpan>,
                               seen_non_whitespace: &mut bool,
                               max_unchanged_segment: &mut usize,
                               num_unchanged_segments: &mut usize| {
        if pending.is_empty() {
            return;
        }
        let has_non_ws = pending.chars().any(|c| !c.is_whitespace());
        if *seen_non_whitespace || has_non_ws {
            let segment_len = pending.chars().count();
            *max_unchanged_segment = (*max_unchanged_segment).max(segment_len);
            *num_unchanged_segments += 1;
        }
        if has_non_ws {
            *seen_non_whitespace = true;
        }
        spans.push(InlineSpan {
            text: std::mem::take(pending),
            source: None,
            is_deletion: false,
        });
    };

    // Helper to flush pending changed content (deleted or inserted)
    let flush_changed = |pending: &mut String,
                         spans: &mut Vec<InlineSpan>,
                         seen_non_whitespace: &mut bool,
                         source: LineSource,
                         is_deletion: bool| {
        if pending.is_empty() {
            return;
        }
        if pending.chars().any(|c| !c.is_whitespace()) {
            *seen_non_whitespace = true;
        }
        spans.push(InlineSpan {
            text: std::mem::take(pending),
            source: Some(source),
            is_deletion,
        });
    };

    for change in diff.iter_all_changes() {
        let text = change.value();
        match change.tag() {
            ChangeTag::Equal => {
                if !pending_deleted.is_empty() || !pending_inserted.is_empty() {
                    flush_unchanged(
                        &mut pending_unchanged,
                        &mut spans,
                        &mut seen_non_whitespace,
                        &mut max_unchanged_segment,
                        &mut num_unchanged_segments,
                    );
                    flush_changed(
                        &mut pending_deleted,
                        &mut spans,
                        &mut seen_non_whitespace,
                        deletion_source,
                        true,
                    );
                    flush_changed(
                        &mut pending_inserted,
                        &mut spans,
                        &mut seen_non_whitespace,
                        change_source,
                        false,
                    );
                }
                pending_unchanged.push_str(text);
            }
            ChangeTag::Delete => {
                pending_deleted.push_str(text);
            }
            ChangeTag::Insert => {
                pending_inserted.push_str(text);
            }
        }
    }

    // Flush any remaining content
    flush_unchanged(
        &mut pending_unchanged,
        &mut spans,
        &mut seen_non_whitespace,
        &mut max_unchanged_segment,
        &mut num_unchanged_segments,
    );
    flush_changed(
        &mut pending_deleted,
        &mut spans,
        &mut seen_non_whitespace,
        deletion_source,
        true,
    );
    flush_changed(
        &mut pending_inserted,
        &mut spans,
        &mut seen_non_whitespace,
        change_source,
        false,
    );

    // Determine if inline diff is meaningful:
    // We need:
    // 1. A contiguous unchanged segment of meaningful length (>= 5 chars)
    // 2. Not too many scattered unchanged segments (fragmentation)
    //
    // The fragmentation check catches cases like a for-loop becoming a comment,
    // where scattered single-char or word matches (like "the", "span", " ") create
    // many small unchanged segments but the lines are structurally completely different.
    //
    // A diff with 1-3 unchanged segments is likely a real modification (prefix, middle, suffix)
    // A diff with 5+ unchanged segments is likely coincidental character matches
    //
    // Examples:
    // - "do_thing(data)" -> "do_thing(data, params)": 2 segments (prefix + suffix) ✓
    // - "end" -> "end # comment": 1 segment (prefix) ✓
    // - "hello world" -> "hello earth": 2 segments ✓
    // - "for i in (x..y).rev() {" -> "// comment text": many scattered segments ✗
    let has_meaningful_segment = max_unchanged_segment >= 5;
    let not_too_fragmented = num_unchanged_segments <= 4;

    let is_meaningful = has_meaningful_segment && not_too_fragmented;

    InlineDiffResult { spans, is_meaningful }
}

#[cfg(test)]
mod tests {
    use super::*;

    // === InlineSpan creation and properties ===

    #[test]
    fn test_inline_span_unchanged() {
        let span = InlineSpan {
            text: "unchanged".to_string(),
            source: None,
            is_deletion: false,
        };
        assert_eq!(span.text, "unchanged");
        assert!(span.source.is_none());
        assert!(!span.is_deletion);
    }

    #[test]
    fn test_inline_span_deletion() {
        let span = InlineSpan {
            text: "deleted".to_string(),
            source: Some(LineSource::DeletedBase),
            is_deletion: true,
        };
        assert_eq!(span.text, "deleted");
        assert_eq!(span.source, Some(LineSource::DeletedBase));
        assert!(span.is_deletion);
    }

    #[test]
    fn test_inline_span_insertion() {
        let span = InlineSpan {
            text: "inserted".to_string(),
            source: Some(LineSource::Committed),
            is_deletion: false,
        };
        assert_eq!(span.text, "inserted");
        assert_eq!(span.source, Some(LineSource::Committed));
        assert!(!span.is_deletion);
    }

    // === Deletion source mapping ===

    #[test]
    fn test_deletion_source_for_committed_change() {
        let result = compute_inline_diff_merged("old content", "new content", LineSource::Committed);

        // Deleted spans should have DeletedBase source
        let deletion_spans: Vec<_> = result.spans.iter()
            .filter(|s| s.is_deletion)
            .collect();
        for span in deletion_spans {
            assert_eq!(span.source, Some(LineSource::DeletedBase));
        }
    }

    #[test]
    fn test_deletion_source_for_staged_change() {
        let result = compute_inline_diff_merged("old content", "new content", LineSource::Staged);

        let deletion_spans: Vec<_> = result.spans.iter()
            .filter(|s| s.is_deletion)
            .collect();
        for span in deletion_spans {
            assert_eq!(span.source, Some(LineSource::DeletedCommitted));
        }
    }

    #[test]
    fn test_deletion_source_for_unstaged_change() {
        let result = compute_inline_diff_merged("old content", "new content", LineSource::Unstaged);

        let deletion_spans: Vec<_> = result.spans.iter()
            .filter(|s| s.is_deletion)
            .collect();
        for span in deletion_spans {
            assert_eq!(span.source, Some(LineSource::DeletedStaged));
        }
    }

    // === is_meaningful determination ===

    #[test]
    fn test_meaningful_when_long_unchanged_segment() {
        // "hello world" -> "hello earth" shares "hello " (6 chars) which is >= 5
        let result = compute_inline_diff_merged("hello world", "hello earth", LineSource::Committed);
        assert!(result.is_meaningful, "Should be meaningful with 6-char unchanged prefix");
    }

    #[test]
    fn test_meaningful_suffix_preservation() {
        // "do_thing(data)" -> "do_thing(data, params)" shares "do_thing(data" + ")"
        let result = compute_inline_diff_merged(
            "do_thing(data)",
            "do_thing(data, params)",
            LineSource::Committed,
        );
        assert!(result.is_meaningful, "Should be meaningful - prefix/suffix unchanged");
    }

    #[test]
    fn test_not_meaningful_too_short_unchanged() {
        // "abc" -> "xyz" - no meaningful unchanged segment
        let result = compute_inline_diff_merged("abc", "xyz", LineSource::Committed);
        assert!(!result.is_meaningful, "Should not be meaningful - no shared content");
    }

    #[test]
    fn test_not_meaningful_only_short_matches() {
        // Lines that share only small fragments shouldn't be meaningful
        let result = compute_inline_diff_merged("end", "let", LineSource::Committed);
        assert!(!result.is_meaningful, "Should not be meaningful - only 1 char match");
    }

    #[test]
    fn test_not_meaningful_too_fragmented() {
        // Many scattered unchanged segments indicate coincidental matches
        let result = compute_inline_diff_merged(
            "for i in (x..y).rev() {",
            "// the range span note",
            LineSource::Committed,
        );
        // This creates scattered matches like "r", " ", etc.
        assert!(!result.is_meaningful, "Should not be meaningful - too fragmented");
    }

    #[test]
    fn test_leading_whitespace_not_counted_for_meaningfulness() {
        // Two lines that only share leading whitespace shouldn't be meaningful
        let result = compute_inline_diff_merged(
            "            }",
            "            .map(|x| x)",
            LineSource::Committed,
        );
        // The 12 spaces of indentation shouldn't make this meaningful
        assert!(!result.is_meaningful, "Leading whitespace alone shouldn't make diff meaningful");
    }

    // === Span structure verification ===

    #[test]
    fn test_identical_lines_single_unchanged_span() {
        let result = compute_inline_diff_merged("same line", "same line", LineSource::Committed);

        assert_eq!(result.spans.len(), 1);
        assert_eq!(result.spans[0].text, "same line");
        assert!(result.spans[0].source.is_none());
        assert!(!result.spans[0].is_deletion);
    }

    #[test]
    fn test_empty_lines() {
        let result = compute_inline_diff_merged("", "", LineSource::Committed);

        // Empty lines should produce no spans
        assert!(result.spans.is_empty());
        assert!(!result.is_meaningful);
    }

    #[test]
    fn test_simple_replacement_structure() {
        // "commercial_renewal" -> "bond" with ".name" preserved
        let result = compute_inline_diff_merged(
            "commercial_renewal.name",
            "bond.name",
            LineSource::Committed,
        );

        // Should have deletion spans, insertion spans, and unchanged spans
        let has_deletions = result.spans.iter().any(|s| s.is_deletion);
        let has_insertions = result.spans.iter().any(|s| s.source == Some(LineSource::Committed));
        let has_unchanged = result.spans.iter().any(|s| s.source.is_none());

        assert!(has_deletions, "Should have deletion spans");
        assert!(has_insertions, "Should have insertion spans");
        assert!(has_unchanged, "Should have unchanged spans");

        // The unchanged portion should contain ".name"
        let unchanged: String = result.spans.iter()
            .filter(|s| s.source.is_none())
            .map(|s| s.text.as_str())
            .collect();
        assert!(unchanged.contains(".name"), "Should preserve '.name' as unchanged");

        // Deleted content should include material from "commercial_renewal"
        let deleted: String = result.spans.iter()
            .filter(|s| s.is_deletion)
            .map(|s| s.text.as_str())
            .collect();
        assert!(!deleted.is_empty(), "Should have deleted content");
    }

    #[test]
    fn test_prefix_change() {
        // Change only the prefix
        let result = compute_inline_diff_merged(
            "old_function_name()",
            "new_function_name()",
            LineSource::Committed,
        );

        // "old" -> "new" deletion/insertion, then "_function_name()" unchanged
        assert!(result.is_meaningful, "Should be meaningful - long unchanged suffix");

        let unchanged: String = result.spans.iter()
            .filter(|s| s.source.is_none())
            .map(|s| s.text.as_str())
            .collect();
        assert!(unchanged.contains("_function_name()"));
    }

    #[test]
    fn test_suffix_change() {
        // Change only the suffix
        let result = compute_inline_diff_merged(
            "function_name_old()",
            "function_name_new()",
            LineSource::Committed,
        );

        assert!(result.is_meaningful, "Should be meaningful - long unchanged prefix");

        let unchanged: String = result.spans.iter()
            .filter(|s| s.source.is_none())
            .map(|s| s.text.as_str())
            .collect();
        assert!(unchanged.contains("function_name_"));
    }

    #[test]
    fn test_middle_insertion() {
        // Insert in the middle
        let result = compute_inline_diff_merged(
            "hello world",
            "hello beautiful world",
            LineSource::Staged,
        );

        // Should have "hello ", insertion "beautiful ", "world"
        let unchanged_texts: Vec<_> = result.spans.iter()
            .filter(|s| s.source.is_none())
            .map(|s| s.text.as_str())
            .collect();

        let inserted_texts: Vec<_> = result.spans.iter()
            .filter(|s| s.source == Some(LineSource::Staged))
            .map(|s| s.text.as_str())
            .collect();

        assert!(unchanged_texts.contains(&"hello "));
        assert!(unchanged_texts.contains(&"world"));
        assert!(inserted_texts.iter().any(|t| t.contains("beautiful")));
    }

    #[test]
    fn test_complete_replacement_not_meaningful() {
        // Completely different lines
        let result = compute_inline_diff_merged(
            "func foo() { return 42; }",
            "struct Bar { x: i32, y: i32 }",
            LineSource::Committed,
        );

        assert!(!result.is_meaningful, "Completely different lines should not be meaningful");
    }

    // === Edge cases ===

    #[test]
    fn test_single_char_difference() {
        let result = compute_inline_diff_merged("test_a", "test_b", LineSource::Committed);

        // "test_" unchanged (5 chars - exactly at threshold)
        assert!(result.is_meaningful, "5 char unchanged segment should be meaningful");
    }

    #[test]
    fn test_whitespace_only_change() {
        let result = compute_inline_diff_merged(
            "let x = 1;",
            "let  x  =  1;",
            LineSource::Unstaged,
        );

        // Should show whitespace changes
        let has_insertion = result.spans.iter().any(|s| s.source.is_some() && !s.is_deletion);
        assert!(has_insertion, "Should detect whitespace insertions");
    }

    #[test]
    fn test_unicode_content() {
        let result = compute_inline_diff_merged(
            "hello こんにちは world",
            "hello 你好 world",
            LineSource::Committed,
        );

        // Should handle unicode properly
        let unchanged: String = result.spans.iter()
            .filter(|s| s.source.is_none())
            .map(|s| s.text.as_str())
            .collect();
        assert!(unchanged.contains("hello "));
        assert!(unchanged.contains(" world"));
    }

    #[test]
    fn test_deletion_before_insertion_ordering() {
        // Verify deleted content appears before inserted content in span order
        let result = compute_inline_diff_merged("old", "new", LineSource::Committed);

        // Find positions
        let deletion_pos = result.spans.iter().position(|s| s.is_deletion);
        let insertion_pos = result.spans.iter().position(|s| !s.is_deletion && s.source.is_some());

        if let (Some(del), Some(ins)) = (deletion_pos, insertion_pos) {
            assert!(del < ins, "Deletion should come before insertion");
        }
    }

    #[test]
    fn test_multiple_changes_in_line() {
        let result = compute_inline_diff_merged(
            "let x = foo(a, b);",
            "let y = bar(c, d);",
            LineSource::Committed,
        );

        // Multiple changes: x->y, foo->bar, a,b->c,d
        // But shared: "let ", " = ", "(", ", ", ");"
        let unchanged_count = result.spans.iter()
            .filter(|s| s.source.is_none())
            .count();
        assert!(unchanged_count >= 2, "Should have multiple unchanged segments");
    }
}