branchdiff 0.63.8

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
use std::collections::HashMap;

use super::{DiffLine, LineSource};

/// Determine where a base line was deleted (in commit, staging, or working)
pub(super) fn determine_deletion_source(
    base_idx: usize,
    _base_lines: &[&str],
    _head_lines: &[&str],
    _index_lines: &[&str],
    head_from_base: &[Option<usize>],
    index_from_head: &[Option<usize>],
) -> LineSource {
    // Check if base line still exists in head (by provenance, not content)
    // A base line exists in head if some head line traces back to this base line
    let in_head = head_from_base.contains(&Some(base_idx));

    if !in_head {
        return LineSource::DeletedBase;  // Deleted in commit
    }

    // Find which head line came from this base line
    let head_idx = head_from_base.iter().position(|&opt| opt == Some(base_idx));
    if let Some(head_idx) = head_idx {
        // Check if this head line still exists in index
        let in_index = index_from_head.contains(&Some(head_idx));

        if !in_index {
            return LineSource::DeletedCommitted;  // Deleted in staging
        }
    }

    LineSource::DeletedStaged  // Deleted in working tree
}

/// Build the output line for a working line, handling modifications
pub(super) fn build_working_line_output<F1, F2>(
    working_idx: usize,
    working_content: &str,
    source: LineSource,
    line_num: usize,
    path: &str,
    working_from_index: &[Option<usize>],
    index_from_head: &[Option<usize>],
    _head_from_base: &[Option<usize>],
    index_working_mods: &HashMap<usize, (usize, &str)>,
    base_head_mods: &HashMap<usize, (usize, &str)>,
    head_index_mods: &HashMap<usize, (usize, &str)>,
    _index_lines: &[&str],
    _head_lines: &[&str],
    trace_index_source: &F1,
    trace_head_source: &F2,
) -> DiffLine
where
    F1: Fn(usize) -> LineSource,
    F2: Fn(usize) -> LineSource,
{
    let content = working_content.to_string();

    // Default output: simple line with source
    let default_line = || {
        let prefix = if source == LineSource::Base { ' ' } else { '+' };
        DiffLine::new(source, content.clone(), prefix, Some(line_num)).with_file_path(path)
    };

    match source {
        LineSource::Unstaged => {
            if let Some((index_idx, old_content)) = index_working_mods.get(&working_idx) {
                let original_source = trace_index_source(*index_idx);
                return DiffLine::new(original_source, content, ' ', Some(line_num))
                    .with_file_path(path)
                    .with_old_content(old_content)
                    .with_change_source(LineSource::Unstaged);
            }
            default_line()
        }

        LineSource::Committed => {
            if let Some(index_idx) = working_from_index.get(working_idx).copied().flatten()
                && let Some(head_idx) = index_from_head.get(index_idx).copied().flatten()
                && let Some((_base_idx, old_content)) = base_head_mods.get(&head_idx)
            {
                return DiffLine::new(LineSource::Base, content, ' ', Some(line_num))
                    .with_file_path(path)
                    .with_old_content(old_content)
                    .with_change_source(LineSource::Committed);
            }
            default_line()
        }

        LineSource::Staged => {
            if let Some(index_idx) = working_from_index.get(working_idx).copied().flatten()
                && let Some((_head_idx, old_content)) = head_index_mods.get(&index_idx)
            {
                let original_source = if let Some(head_idx) = index_from_head.get(index_idx).copied().flatten() {
                    trace_head_source(head_idx)
                } else {
                    LineSource::Base
                };

                return DiffLine::new(original_source, content, ' ', Some(line_num))
                    .with_file_path(path)
                    .with_old_content(old_content)
                    .with_change_source(LineSource::Staged);
            }
            default_line()
        }

        LineSource::Base => default_line(),

        _ => default_line(),
    }
}

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

    // === Tests for determine_deletion_source ===

    #[test]
    fn test_deletion_source_deleted_in_commit() {
        // Base line doesn't exist in head (deleted during commit)
        let base_lines = &["line0", "line1", "line2"];
        let head_lines = &["line0", "line2"]; // line1 deleted
        let index_lines = &["line0", "line2"];

        // head_from_base: head[0]=base[0], head[1]=base[2] (line1 not in head)
        let head_from_base = vec![Some(0), Some(2)];
        let index_from_head = vec![Some(0), Some(1)];

        let source = determine_deletion_source(
            1, // base_idx for "line1"
            base_lines,
            head_lines,
            index_lines,
            &head_from_base,
            &index_from_head,
        );

        assert_eq!(source, LineSource::DeletedBase);
    }

    #[test]
    fn test_deletion_source_deleted_in_staging() {
        // Base line exists in head but not in index (deleted during staging)
        let base_lines = &["line0", "line1", "line2"];
        let head_lines = &["line0", "line1", "line2"];
        let index_lines = &["line0", "line2"]; // line1 deleted from index

        // head_from_base: all lines map 1:1
        let head_from_base = vec![Some(0), Some(1), Some(2)];
        // index_from_head: index[0]=head[0], index[1]=head[2] (head[1] not in index)
        let index_from_head = vec![Some(0), Some(2)];

        let source = determine_deletion_source(
            1, // base_idx for "line1"
            base_lines,
            head_lines,
            index_lines,
            &head_from_base,
            &index_from_head,
        );

        assert_eq!(source, LineSource::DeletedCommitted);
    }

    #[test]
    fn test_deletion_source_deleted_in_working() {
        // Base line exists in head and index but not in working (deleted in working tree)
        let base_lines = &["line0", "line1", "line2"];
        let head_lines = &["line0", "line1", "line2"];
        let index_lines = &["line0", "line1", "line2"];

        // All lines map 1:1 through head and index
        let head_from_base = vec![Some(0), Some(1), Some(2)];
        let index_from_head = vec![Some(0), Some(1), Some(2)];

        let source = determine_deletion_source(
            1, // base_idx for "line1" - exists in head and index
            base_lines,
            head_lines,
            index_lines,
            &head_from_base,
            &index_from_head,
        );

        assert_eq!(source, LineSource::DeletedStaged);
    }

    #[test]
    fn test_deletion_source_first_line() {
        // Test deletion of first line
        let base_lines = &["first", "second"];
        let head_lines = &["second"]; // first deleted

        let head_from_base = vec![Some(1)]; // only "second" remains
        let index_from_head = vec![Some(0)];

        let source = determine_deletion_source(
            0, // first line deleted
            base_lines,
            head_lines,
            &["second"],
            &head_from_base,
            &index_from_head,
        );

        assert_eq!(source, LineSource::DeletedBase);
    }

    #[test]
    fn test_deletion_source_last_line() {
        // Test deletion of last line
        let base_lines = &["first", "last"];
        let head_lines = &["first"]; // last deleted

        let head_from_base = vec![Some(0)]; // only "first" remains
        let index_from_head = vec![Some(0)];

        let source = determine_deletion_source(
            1, // last line deleted
            base_lines,
            head_lines,
            &["first"],
            &head_from_base,
            &index_from_head,
        );

        assert_eq!(source, LineSource::DeletedBase);
    }

    // === Tests for build_working_line_output ===

    #[test]
    fn test_build_output_base_line() {
        let working_from_index = vec![Some(0)];
        let index_from_head = vec![Some(0)];
        let head_from_base = vec![Some(0)];
        let index_working_mods = HashMap::new();
        let base_head_mods = HashMap::new();
        let head_index_mods = HashMap::new();

        let line = build_working_line_output(
            0,
            "content",
            LineSource::Base,
            1,
            "test.rs",
            &working_from_index,
            &index_from_head,
            &head_from_base,
            &index_working_mods,
            &base_head_mods,
            &head_index_mods,
            &["content"],
            &["content"],
            &|_| LineSource::Base,
            &|_| LineSource::Base,
        );

        assert_eq!(line.source, LineSource::Base);
        assert_eq!(line.content, "content");
        assert_eq!(line.prefix, ' ');
        assert_eq!(line.line_number, Some(1));
        assert_eq!(line.file_path, Some("test.rs".to_string()));
    }

    #[test]
    fn test_build_output_unstaged_addition() {
        let working_from_index: Vec<Option<usize>> = vec![None]; // not from index
        let index_from_head = vec![];
        let head_from_base = vec![];
        let index_working_mods = HashMap::new();
        let base_head_mods = HashMap::new();
        let head_index_mods = HashMap::new();

        let line = build_working_line_output(
            0,
            "new line",
            LineSource::Unstaged,
            5,
            "test.rs",
            &working_from_index,
            &index_from_head,
            &head_from_base,
            &index_working_mods,
            &base_head_mods,
            &head_index_mods,
            &[],
            &[],
            &|_| LineSource::Base,
            &|_| LineSource::Base,
        );

        assert_eq!(line.source, LineSource::Unstaged);
        assert_eq!(line.content, "new line");
        assert_eq!(line.prefix, '+');
        assert_eq!(line.line_number, Some(5));
    }

    #[test]
    fn test_build_output_unstaged_modification() {
        let working_from_index = vec![Some(0)];
        let index_from_head = vec![Some(0)];
        let head_from_base = vec![Some(0)];

        // Working line 0 is a modification of index line 0
        let mut index_working_mods = HashMap::new();
        index_working_mods.insert(0usize, (0usize, "old content"));

        let base_head_mods = HashMap::new();
        let head_index_mods = HashMap::new();

        let line = build_working_line_output(
            0,
            "new content",
            LineSource::Unstaged,
            1,
            "test.rs",
            &working_from_index,
            &index_from_head,
            &head_from_base,
            &index_working_mods,
            &base_head_mods,
            &head_index_mods,
            &["old content"],
            &["old content"],
            &|_| LineSource::Base,
            &|_| LineSource::Base,
        );

        // Modification should have Base source with old_content and change_source
        assert_eq!(line.source, LineSource::Base);
        assert_eq!(line.content, "new content");
        assert_eq!(line.prefix, ' ');
        assert_eq!(line.old_content, Some("old content".to_string()));
        assert_eq!(line.change_source, Some(LineSource::Unstaged));
    }

    #[test]
    fn test_build_output_committed_addition() {
        let working_from_index = vec![Some(0)];
        let index_from_head = vec![Some(0)];
        let head_from_base: Vec<Option<usize>> = vec![None]; // not from base

        let index_working_mods = HashMap::new();
        let base_head_mods = HashMap::new();
        let head_index_mods = HashMap::new();

        let line = build_working_line_output(
            0,
            "committed line",
            LineSource::Committed,
            1,
            "test.rs",
            &working_from_index,
            &index_from_head,
            &head_from_base,
            &index_working_mods,
            &base_head_mods,
            &head_index_mods,
            &["committed line"],
            &["committed line"],
            &|_| LineSource::Committed,
            &|_| LineSource::Committed,
        );

        assert_eq!(line.source, LineSource::Committed);
        assert_eq!(line.content, "committed line");
        assert_eq!(line.prefix, '+');
    }

    #[test]
    fn test_build_output_committed_modification() {
        let working_from_index = vec![Some(0)];
        let index_from_head = vec![Some(0)];
        let head_from_base = vec![Some(0)];

        let index_working_mods = HashMap::new();
        // Head line 0 is a modification of base line 0
        let mut base_head_mods = HashMap::new();
        base_head_mods.insert(0usize, (0usize, "base content"));
        let head_index_mods = HashMap::new();

        let line = build_working_line_output(
            0,
            "modified in commit",
            LineSource::Committed,
            1,
            "test.rs",
            &working_from_index,
            &index_from_head,
            &head_from_base,
            &index_working_mods,
            &base_head_mods,
            &head_index_mods,
            &["modified in commit"],
            &["modified in commit"],
            &|_| LineSource::Base,
            &|_| LineSource::Base,
        );

        assert_eq!(line.source, LineSource::Base);
        assert_eq!(line.old_content, Some("base content".to_string()));
        assert_eq!(line.change_source, Some(LineSource::Committed));
    }

    #[test]
    fn test_build_output_staged_addition() {
        let working_from_index = vec![Some(0)];
        let index_from_head: Vec<Option<usize>> = vec![None]; // not from head
        let head_from_base = vec![];

        let index_working_mods = HashMap::new();
        let base_head_mods = HashMap::new();
        let head_index_mods = HashMap::new();

        let line = build_working_line_output(
            0,
            "staged line",
            LineSource::Staged,
            1,
            "test.rs",
            &working_from_index,
            &index_from_head,
            &head_from_base,
            &index_working_mods,
            &base_head_mods,
            &head_index_mods,
            &["staged line"],
            &[],
            &|_| LineSource::Staged,
            &|_| LineSource::Base,
        );

        assert_eq!(line.source, LineSource::Staged);
        assert_eq!(line.prefix, '+');
    }

    #[test]
    fn test_build_output_staged_modification() {
        let working_from_index = vec![Some(0)];
        let index_from_head = vec![Some(0)];
        let head_from_base = vec![Some(0)];

        let index_working_mods = HashMap::new();
        let base_head_mods = HashMap::new();
        // Index line 0 is a modification of head line 0
        let mut head_index_mods = HashMap::new();
        head_index_mods.insert(0usize, (0usize, "head content"));

        let line = build_working_line_output(
            0,
            "modified in staging",
            LineSource::Staged,
            1,
            "test.rs",
            &working_from_index,
            &index_from_head,
            &head_from_base,
            &index_working_mods,
            &base_head_mods,
            &head_index_mods,
            &["modified in staging"],
            &["head content"],
            &|_| LineSource::Committed,
            &|idx| if idx == 0 { LineSource::Base } else { LineSource::Committed },
        );

        assert_eq!(line.source, LineSource::Base);
        assert_eq!(line.old_content, Some("head content".to_string()));
        assert_eq!(line.change_source, Some(LineSource::Staged));
    }
}