subversion 0.1.10

Rust bindings for Subversion
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
use crate::{svn_result, with_tmp_pool, Error};

/// Options for diff operations
#[derive(Debug, Clone, Copy, Default)]
pub struct DiffOptions {
    /// Ignore changes in whitespace
    pub ignore_whitespace: bool,
    /// Ignore changes in end-of-line style  
    pub ignore_eol_style: bool,
    /// Show context around changes
    pub show_c_function: bool,
}

impl DiffOptions {
    /// Creates new diff options with default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets whether to ignore whitespace changes.
    pub fn with_ignore_whitespace(mut self, ignore: bool) -> Self {
        self.ignore_whitespace = ignore;
        self
    }

    /// Sets whether to ignore end-of-line style differences.
    pub fn with_ignore_eol_style(mut self, ignore: bool) -> Self {
        self.ignore_eol_style = ignore;
        self
    }

    /// Sets whether to show C function names in context.
    pub fn with_show_c_function(mut self, show: bool) -> Self {
        self.show_c_function = show;
        self
    }
}

/// A diff hunk showing differences between files
pub struct DiffHunk {
    ptr: *mut subversion_sys::svn_diff_hunk_t,
}

impl DiffHunk {
    #[allow(dead_code)]
    unsafe fn from_raw(ptr: *mut subversion_sys::svn_diff_hunk_t) -> Self {
        Self { ptr }
    }

    /// Get the starting line number in the original file
    pub fn original_start(&self) -> u64 {
        unsafe { subversion_sys::svn_diff_hunk_get_original_start(self.ptr).into() }
    }

    /// Get the number of lines in the original file
    pub fn original_length(&self) -> u64 {
        unsafe { subversion_sys::svn_diff_hunk_get_original_length(self.ptr).into() }
    }

    /// Get the starting line number in the modified file
    pub fn modified_start(&self) -> u64 {
        unsafe { subversion_sys::svn_diff_hunk_get_modified_start(self.ptr).into() }
    }

    /// Get the number of lines in the modified file  
    pub fn modified_length(&self) -> u64 {
        unsafe { subversion_sys::svn_diff_hunk_get_modified_length(self.ptr).into() }
    }

    /// Get the leading context lines
    pub fn leading_context(&self) -> u64 {
        unsafe { subversion_sys::svn_diff_hunk_get_leading_context(self.ptr).into() }
    }

    /// Get the trailing context lines
    pub fn trailing_context(&self) -> u64 {
        unsafe { subversion_sys::svn_diff_hunk_get_trailing_context(self.ptr).into() }
    }
}

/// A diff between two files
pub struct Diff {
    ptr: *mut subversion_sys::svn_diff_t,
    _pool: apr::Pool<'static>,
}

impl Diff {
    unsafe fn from_raw(ptr: *mut subversion_sys::svn_diff_t, pool: apr::Pool<'static>) -> Self {
        Self { ptr, _pool: pool }
    }

    /// Check if the diff contains any changes
    pub fn contains_changes(&self) -> bool {
        unsafe { subversion_sys::svn_diff_contains_diffs(self.ptr) != 0 }
    }

    /// Check if the diff contains conflicts  
    pub fn contains_conflicts(&self) -> bool {
        unsafe { subversion_sys::svn_diff_contains_conflicts(self.ptr) != 0 }
    }

    /// Get the raw pointer for use with other SVN functions
    pub fn as_ptr(&self) -> *mut subversion_sys::svn_diff_t {
        self.ptr
    }
}

/// File options for diff operations
#[derive(Debug, Clone, Copy, Default)]
pub struct FileOptions {
    /// Ignore whitespace changes
    pub ignore_space: IgnoreSpace,
    /// Ignore end-of-line differences
    pub ignore_eol_style: bool,
    /// Show function context
    pub show_c_function: bool,
}

impl FileOptions {
    /// Creates new file options with default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets whether to ignore whitespace changes.
    pub fn with_ignore_whitespace(mut self, ignore: bool) -> Self {
        self.ignore_space = if ignore {
            IgnoreSpace::Change
        } else {
            IgnoreSpace::None
        };
        self
    }

    /// Sets whether to ignore end-of-line style differences.
    pub fn with_ignore_eol_style(mut self, ignore: bool) -> Self {
        self.ignore_eol_style = ignore;
        self
    }

    /// Sets whether to show C function names in context.
    pub fn with_show_c_function(mut self, show: bool) -> Self {
        self.show_c_function = show;
        self
    }
}

/// Types of whitespace ignoring
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum IgnoreSpace {
    #[default]
    /// Don't ignore any whitespace
    None,
    /// Ignore changes in whitespace
    Change,
    /// Ignore all whitespace
    All,
}

impl From<IgnoreSpace> for subversion_sys::svn_diff_file_ignore_space_t {
    fn from(ignore: IgnoreSpace) -> Self {
        match ignore {
            IgnoreSpace::None => {
                subversion_sys::svn_diff_file_ignore_space_t_svn_diff_file_ignore_space_none
            }
            IgnoreSpace::Change => {
                subversion_sys::svn_diff_file_ignore_space_t_svn_diff_file_ignore_space_change
            }
            IgnoreSpace::All => {
                subversion_sys::svn_diff_file_ignore_space_t_svn_diff_file_ignore_space_all
            }
        }
    }
}

/// Diff two files
pub fn file_diff(
    original: &std::path::Path,
    modified: &std::path::Path,
    options: FileOptions,
) -> Result<Diff, Error<'static>> {
    let original_cstr = std::ffi::CString::new(original.to_string_lossy().as_ref())?;
    let modified_cstr = std::ffi::CString::new(modified.to_string_lossy().as_ref())?;

    let pool = apr::Pool::new();
    let mut diff_ptr = std::ptr::null_mut();

    // Create diff options
    let diff_options = unsafe { subversion_sys::svn_diff_file_options_create(pool.as_mut_ptr()) };
    unsafe {
        (*diff_options).ignore_space = options.ignore_space.into();
        (*diff_options).ignore_eol_style = if options.ignore_eol_style { 1 } else { 0 };
        (*diff_options).show_c_function = if options.show_c_function { 1 } else { 0 };
    }

    let err = unsafe {
        subversion_sys::svn_diff_file_diff_2(
            &mut diff_ptr,
            original_cstr.as_ptr(),
            modified_cstr.as_ptr(),
            diff_options,
            pool.as_mut_ptr(),
        )
    };

    svn_result(err)?;
    Ok(unsafe { Diff::from_raw(diff_ptr, pool) })
}

/// Diff three files (three-way comparison)
pub fn file_diff3(
    original: &std::path::Path,
    modified: &std::path::Path,
    latest: &std::path::Path,
    options: FileOptions,
) -> Result<Diff, Error<'static>> {
    let original_cstr = std::ffi::CString::new(original.to_string_lossy().as_ref())?;
    let modified_cstr = std::ffi::CString::new(modified.to_string_lossy().as_ref())?;
    let latest_cstr = std::ffi::CString::new(latest.to_string_lossy().as_ref())?;

    let pool = apr::Pool::new();
    let mut diff_ptr = std::ptr::null_mut();

    // Create diff options
    let diff_options = unsafe { subversion_sys::svn_diff_file_options_create(pool.as_mut_ptr()) };
    unsafe {
        (*diff_options).ignore_space = options.ignore_space.into();
        (*diff_options).ignore_eol_style = if options.ignore_eol_style { 1 } else { 0 };
        (*diff_options).show_c_function = if options.show_c_function { 1 } else { 0 };
    }

    let err = unsafe {
        subversion_sys::svn_diff_file_diff3_2(
            &mut diff_ptr,
            original_cstr.as_ptr(),
            modified_cstr.as_ptr(),
            latest_cstr.as_ptr(),
            diff_options,
            pool.as_mut_ptr(),
        )
    };

    svn_result(err)?;
    Ok(unsafe { Diff::from_raw(diff_ptr, pool) })
}

/// Diff four files (four-way comparison with ancestor)
pub fn file_diff4(
    original: &std::path::Path,
    modified: &std::path::Path,
    latest: &std::path::Path,
    ancestor: &std::path::Path,
    options: FileOptions,
) -> Result<Diff, Error<'static>> {
    let original_cstr = std::ffi::CString::new(original.to_string_lossy().as_ref())?;
    let modified_cstr = std::ffi::CString::new(modified.to_string_lossy().as_ref())?;
    let latest_cstr = std::ffi::CString::new(latest.to_string_lossy().as_ref())?;
    let ancestor_cstr = std::ffi::CString::new(ancestor.to_string_lossy().as_ref())?;

    let pool = apr::Pool::new();
    let mut diff_ptr = std::ptr::null_mut();

    // Create diff options
    let diff_options = unsafe { subversion_sys::svn_diff_file_options_create(pool.as_mut_ptr()) };
    unsafe {
        (*diff_options).ignore_space = options.ignore_space.into();
        (*diff_options).ignore_eol_style = if options.ignore_eol_style { 1 } else { 0 };
        (*diff_options).show_c_function = if options.show_c_function { 1 } else { 0 };
    }

    let err = unsafe {
        subversion_sys::svn_diff_file_diff4_2(
            &mut diff_ptr,
            original_cstr.as_ptr(),
            modified_cstr.as_ptr(),
            latest_cstr.as_ptr(),
            ancestor_cstr.as_ptr(),
            diff_options,
            pool.as_mut_ptr(),
        )
    };

    svn_result(err)?;
    Ok(unsafe { Diff::from_raw(diff_ptr, pool) })
}

/// Output unified diff format
pub fn file_output_unified(
    output_stream: &mut crate::io::Stream,
    diff: &Diff,
    original_path: &std::path::Path,
    modified_path: &std::path::Path,
    original_header: Option<&str>,
    modified_header: Option<&str>,
    header_encoding: &str,
    context_size: i32,
) -> Result<(), Error<'static>> {
    let original_path_cstr = std::ffi::CString::new(original_path.to_string_lossy().as_ref())?;
    let modified_path_cstr = std::ffi::CString::new(modified_path.to_string_lossy().as_ref())?;
    let header_encoding_cstr = std::ffi::CString::new(header_encoding)?;

    let original_header_cstr = original_header.map(std::ffi::CString::new).transpose()?;
    let modified_header_cstr = modified_header.map(std::ffi::CString::new).transpose()?;

    with_tmp_pool(|scratch_pool| {
        let err = unsafe {
            subversion_sys::svn_diff_file_output_unified4(
                output_stream.as_mut_ptr(),
                diff.as_ptr(),
                original_path_cstr.as_ptr(),
                modified_path_cstr.as_ptr(),
                original_header_cstr
                    .as_ref()
                    .map_or(std::ptr::null(), |c| c.as_ptr()),
                modified_header_cstr
                    .as_ref()
                    .map_or(std::ptr::null(), |c| c.as_ptr()),
                header_encoding_cstr.as_ptr(),
                std::ptr::null(), // relative_to_dir
                1,                // show_c_function
                context_size,
                None,                 // cancel_func
                std::ptr::null_mut(), // cancel_baton
                scratch_pool.as_mut_ptr(),
            )
        };

        svn_result(err)
    })
}

/// Diff memory strings
pub fn mem_string_diff(
    original: &str,
    modified: &str,
    options: FileOptions,
) -> Result<Diff, Error<'static>> {
    let pool = apr::Pool::new();
    let mut diff_ptr = std::ptr::null_mut();

    // Create svn_string_t structures
    let original_svn_str = subversion_sys::svn_string_t {
        data: original.as_ptr() as *const std::os::raw::c_char,
        len: original.len(),
    };

    let modified_svn_str = subversion_sys::svn_string_t {
        data: modified.as_ptr() as *const std::os::raw::c_char,
        len: modified.len(),
    };

    // Create diff options
    let diff_options = unsafe { subversion_sys::svn_diff_file_options_create(pool.as_mut_ptr()) };
    unsafe {
        (*diff_options).ignore_space = options.ignore_space.into();
        (*diff_options).ignore_eol_style = if options.ignore_eol_style { 1 } else { 0 };
        (*diff_options).show_c_function = if options.show_c_function { 1 } else { 0 };
    }

    let err = unsafe {
        subversion_sys::svn_diff_mem_string_diff(
            &mut diff_ptr,
            &original_svn_str,
            &modified_svn_str,
            diff_options,
            pool.as_mut_ptr(),
        )
    };

    svn_result(err)?;
    Ok(unsafe { Diff::from_raw(diff_ptr, pool) })
}

/// Conflict display style for merge output
#[derive(Debug, Clone, Copy)]
pub enum ConflictDisplayStyle {
    /// Show modified and latest
    ModifiedLatest,
    /// Show resolved conflicts only
    ResolvedModifiedLatest,
    /// Show modified, original, and latest
    ModifiedOriginalLatest,
    /// Show only modified
    OnlyConflicts,
}

impl From<ConflictDisplayStyle> for subversion_sys::svn_diff_conflict_display_style_t {
    fn from(style: ConflictDisplayStyle) -> Self {
        match style {
            ConflictDisplayStyle::ModifiedLatest => {
                subversion_sys::svn_diff_conflict_display_style_t_svn_diff_conflict_display_modified_latest
            }
            ConflictDisplayStyle::ResolvedModifiedLatest => {
                subversion_sys::svn_diff_conflict_display_style_t_svn_diff_conflict_display_resolved_modified_latest
            }
            ConflictDisplayStyle::ModifiedOriginalLatest => {
                subversion_sys::svn_diff_conflict_display_style_t_svn_diff_conflict_display_modified_original_latest
            }
            ConflictDisplayStyle::OnlyConflicts => {
                subversion_sys::svn_diff_conflict_display_style_t_svn_diff_conflict_display_only_conflicts
            }
        }
    }
}

/// Output merge result with conflict markers
pub fn file_output_merge(
    output_stream: &mut crate::io::Stream,
    diff: &Diff,
    original_path: &std::path::Path,
    modified_path: &std::path::Path,
    latest_path: &std::path::Path,
    conflict_original: Option<&str>,
    conflict_modified: Option<&str>,
    conflict_latest: Option<&str>,
    conflict_separator: Option<&str>,
    conflict_style: ConflictDisplayStyle,
) -> Result<(), Error<'static>> {
    let original_path_cstr = std::ffi::CString::new(original_path.to_string_lossy().as_ref())?;
    let modified_path_cstr = std::ffi::CString::new(modified_path.to_string_lossy().as_ref())?;
    let latest_path_cstr = std::ffi::CString::new(latest_path.to_string_lossy().as_ref())?;

    let conflict_original_cstr = conflict_original.map(std::ffi::CString::new).transpose()?;
    let conflict_modified_cstr = conflict_modified.map(std::ffi::CString::new).transpose()?;
    let conflict_latest_cstr = conflict_latest.map(std::ffi::CString::new).transpose()?;
    let conflict_separator_cstr = conflict_separator.map(std::ffi::CString::new).transpose()?;

    with_tmp_pool(|scratch_pool| {
        let err = unsafe {
            subversion_sys::svn_diff_file_output_merge3(
                output_stream.as_mut_ptr(),
                diff.as_ptr(),
                original_path_cstr.as_ptr(),
                modified_path_cstr.as_ptr(),
                latest_path_cstr.as_ptr(),
                conflict_original_cstr
                    .as_ref()
                    .map_or(std::ptr::null(), |c| c.as_ptr()),
                conflict_modified_cstr
                    .as_ref()
                    .map_or(std::ptr::null(), |c| c.as_ptr()),
                conflict_latest_cstr
                    .as_ref()
                    .map_or(std::ptr::null(), |c| c.as_ptr()),
                conflict_separator_cstr
                    .as_ref()
                    .map_or(std::ptr::null(), |c| c.as_ptr()),
                conflict_style.into(),
                None,                 // cancel_func
                std::ptr::null_mut(), // cancel_baton
                scratch_pool.as_mut_ptr(),
            )
        };

        svn_result(err)
    })
}

/// Generic diff output using callback functions
///
/// # Safety
///
/// The caller must ensure that `output_baton` and `output_fns` are valid and compatible.
/// The function pointers in `output_fns` must be safe to call with the provided baton.
pub unsafe fn output(
    diff: &Diff,
    output_baton: *mut std::ffi::c_void,
    output_fns: &subversion_sys::svn_diff_output_fns_t,
) -> Result<(), Error<'static>> {
    let err = unsafe {
        subversion_sys::svn_diff_output2(
            diff.as_ptr(),
            output_baton,
            output_fns,
            None,                 // cancel_func
            std::ptr::null_mut(), // cancel_baton
        )
    };

    svn_result(err)
}

/// Output unified diff with more options
pub fn file_output_unified_with_options(
    output_stream: &mut crate::io::Stream,
    diff: &Diff,
    original_path: &std::path::Path,
    modified_path: &std::path::Path,
    original_header: Option<&str>,
    modified_header: Option<&str>,
    header_encoding: &str,
    relative_to_dir: Option<&std::path::Path>,
    show_c_function: bool,
    context_size: i32,
) -> Result<(), Error<'static>> {
    let original_path_cstr = std::ffi::CString::new(original_path.to_string_lossy().as_ref())?;
    let modified_path_cstr = std::ffi::CString::new(modified_path.to_string_lossy().as_ref())?;
    let header_encoding_cstr = std::ffi::CString::new(header_encoding)?;

    let original_header_cstr = original_header.map(std::ffi::CString::new).transpose()?;
    let modified_header_cstr = modified_header.map(std::ffi::CString::new).transpose()?;
    let relative_to_dir_cstr = relative_to_dir
        .map(|p| std::ffi::CString::new(p.to_string_lossy().as_ref()))
        .transpose()?;

    with_tmp_pool(|scratch_pool| {
        let err = unsafe {
            subversion_sys::svn_diff_file_output_unified4(
                output_stream.as_mut_ptr(),
                diff.as_ptr(),
                original_path_cstr.as_ptr(),
                modified_path_cstr.as_ptr(),
                original_header_cstr
                    .as_ref()
                    .map_or(std::ptr::null(), |c| c.as_ptr()),
                modified_header_cstr
                    .as_ref()
                    .map_or(std::ptr::null(), |c| c.as_ptr()),
                header_encoding_cstr.as_ptr(),
                relative_to_dir_cstr
                    .as_ref()
                    .map_or(std::ptr::null(), |c| c.as_ptr()),
                if show_c_function { 1 } else { 0 },
                context_size,
                None,                 // cancel_func
                std::ptr::null_mut(), // cancel_baton
                scratch_pool.as_mut_ptr(),
            )
        };

        svn_result(err)
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::tempdir;

    #[test]
    fn test_file_options() {
        let options = FileOptions::default()
            .with_ignore_whitespace(true)
            .with_ignore_eol_style(true)
            .with_show_c_function(false);

        assert_eq!(options.ignore_space, IgnoreSpace::Change);
        assert!(options.ignore_eol_style);
        assert!(!options.show_c_function);
    }

    #[test]
    fn test_mem_string_diff() {
        let original = "line 1\nline 2\nline 3\n";
        let modified = "line 1\nline 2 modified\nline 3\n";

        let options = FileOptions::default();
        let diff = mem_string_diff(original, modified, options).unwrap();
        assert!(diff.contains_changes());
    }

    #[test]
    fn test_file_diff() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;

        // Create test files
        let original_path = temp_dir.path().join("original.txt");
        let modified_path = temp_dir.path().join("modified.txt");

        let mut original_file = std::fs::File::create(&original_path)?;
        original_file.write_all(b"line 1\nline 2\nline 3\n")?;

        let mut modified_file = std::fs::File::create(&modified_path)?;
        modified_file.write_all(b"line 1\nline 2 modified\nline 3\n")?;

        let options = FileOptions::default();
        let diff = file_diff(&original_path, &modified_path, options)?;

        assert!(diff.contains_changes());
        assert!(!diff.contains_conflicts());

        Ok(())
    }

    #[test]
    fn test_diff_identical_files() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;

        // Create identical test files
        let original_path = temp_dir.path().join("original.txt");
        let modified_path = temp_dir.path().join("modified.txt");

        let content = b"line 1\nline 2\nline 3\n";
        std::fs::write(&original_path, content)?;
        std::fs::write(&modified_path, content)?;

        let options = FileOptions::default();
        let diff = file_diff(&original_path, &modified_path, options)?;

        assert!(!diff.contains_changes());

        Ok(())
    }

    #[test]
    fn test_file_output_unified() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;

        // Create test files
        let original_path = temp_dir.path().join("original.txt");
        let modified_path = temp_dir.path().join("modified.txt");

        std::fs::write(&original_path, b"line 1\nline 2\nline 3\n")?;
        std::fs::write(&modified_path, b"line 1\nline 2 modified\nline 3\nline 4\n")?;

        let options = FileOptions::default();
        let diff = file_diff(&original_path, &modified_path, options)?;

        // Create output stream using a string buffer
        let mut stringbuf = crate::io::StringBuf::new();
        let mut stream = crate::io::Stream::from_stringbuf(&mut stringbuf);

        // Generate unified diff output
        file_output_unified(
            &mut stream,
            &diff,
            &original_path,
            &modified_path,
            Some("Original File"),
            Some("Modified File"),
            "UTF-8",
            3,
        )?;

        // Verify output contains unified diff markers
        let output = stringbuf.to_string();
        assert!(output.contains("---"));
        assert!(output.contains("+++"));
        assert!(output.contains("@@"));

        Ok(())
    }

    #[test]
    fn test_file_output_merge() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;

        // Create test files for 3-way merge
        let original_path = temp_dir.path().join("original.txt");
        let modified_path = temp_dir.path().join("modified.txt");
        let latest_path = temp_dir.path().join("latest.txt");

        std::fs::write(&original_path, b"line 1\nline 2\nline 3\n")?;
        std::fs::write(&modified_path, b"line 1\nline 2 modified\nline 3\n")?;
        std::fs::write(&latest_path, b"line 1\nline 2 latest\nline 3\n")?;

        let options = FileOptions::default();
        let diff = file_diff3(&modified_path, &original_path, &latest_path, options)?;

        // Create output stream using a string buffer
        let mut stringbuf = crate::io::StringBuf::new();
        let mut stream = crate::io::Stream::from_stringbuf(&mut stringbuf);

        // Generate merge output with default conflict markers
        file_output_merge(
            &mut stream,
            &diff,
            &original_path,
            &modified_path,
            &latest_path,
            None,
            None,
            None,
            None,
            ConflictDisplayStyle::ModifiedLatest,
        )?;

        // Verify output was generated
        let output = stringbuf.to_string();
        assert!(!output.is_empty());

        Ok(())
    }

    #[test]
    fn test_file_output_merge_with_diff3_style() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;

        // Create test files with conflicting changes
        let original_path = temp_dir.path().join("original.txt");
        let modified_path = temp_dir.path().join("modified.txt");
        let latest_path = temp_dir.path().join("latest.txt");

        std::fs::write(&original_path, b"common line\noriginal line\ncommon end\n")?;
        std::fs::write(&modified_path, b"common line\nmodified line\ncommon end\n")?;
        std::fs::write(&latest_path, b"common line\nlatest line\ncommon end\n")?;

        let options = FileOptions::default();
        let diff = file_diff3(&modified_path, &original_path, &latest_path, options)?;

        // Create output stream using a string buffer
        let mut stringbuf = crate::io::StringBuf::new();
        let mut stream = crate::io::Stream::from_stringbuf(&mut stringbuf);

        // Generate merge output with diff3-style conflict markers
        file_output_merge(
            &mut stream,
            &diff,
            &original_path,
            &modified_path,
            &latest_path,
            Some("Modified"),
            Some("Original"),
            Some("Latest"),
            Some("======="),
            ConflictDisplayStyle::ModifiedOriginalLatest,
        )?;

        // Verify output was generated
        let output = stringbuf.to_string();
        assert!(!output.is_empty());

        Ok(())
    }

    #[test]
    fn test_file_output_unified_with_options() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;

        // Create test files
        let original_path = temp_dir.path().join("src/original.c");
        let modified_path = temp_dir.path().join("src/modified.c");

        // Create parent directory
        std::fs::create_dir_all(original_path.parent().unwrap())?;

        let original_content = b"void foo() {\n    int x = 1;\n}\n\nvoid bar() {\n    return;\n}\n";
        let modified_content = b"void foo() {\n    int x = 2;\n}\n\nvoid bar() {\n    return;\n}\n";

        std::fs::write(&original_path, original_content)?;
        std::fs::write(&modified_path, modified_content)?;

        let options = FileOptions::default();
        let diff = file_diff(&original_path, &modified_path, options)?;

        // Create output stream using a string buffer
        let mut stringbuf = crate::io::StringBuf::new();
        let mut stream = crate::io::Stream::from_stringbuf(&mut stringbuf);

        // Generate unified diff with options
        file_output_unified_with_options(
            &mut stream,
            &diff,
            &original_path,
            &modified_path,
            Some("Original Version"),
            Some("Modified Version"),
            "UTF-8",
            Some(&temp_dir.path()),
            true, // show_c_function
            5,    // context_size
        )?;

        // Verify output was generated
        let output = stringbuf.to_string();
        assert!(output.contains("---"));
        assert!(output.contains("+++"));

        Ok(())
    }

    #[test]
    fn test_conflict_display_style() {
        // Test the ConflictDisplayStyle enum conversions
        let style: subversion_sys::svn_diff_conflict_display_style_t =
            ConflictDisplayStyle::ModifiedLatest.into();
        assert_eq!(style, subversion_sys::svn_diff_conflict_display_style_t_svn_diff_conflict_display_modified_latest);

        let style: subversion_sys::svn_diff_conflict_display_style_t =
            ConflictDisplayStyle::ModifiedOriginalLatest.into();
        assert_eq!(style, subversion_sys::svn_diff_conflict_display_style_t_svn_diff_conflict_display_modified_original_latest);

        let style: subversion_sys::svn_diff_conflict_display_style_t =
            ConflictDisplayStyle::ResolvedModifiedLatest.into();
        assert_eq!(style, subversion_sys::svn_diff_conflict_display_style_t_svn_diff_conflict_display_resolved_modified_latest);

        let style: subversion_sys::svn_diff_conflict_display_style_t =
            ConflictDisplayStyle::OnlyConflicts.into();
        assert_eq!(style, subversion_sys::svn_diff_conflict_display_style_t_svn_diff_conflict_display_only_conflicts);
    }

    #[test]
    fn test_file_diff4() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;

        // Create test files for 4-way diff
        let original_path = temp_dir.path().join("original.txt");
        let modified_path = temp_dir.path().join("modified.txt");
        let latest_path = temp_dir.path().join("latest.txt");
        let ancestor_path = temp_dir.path().join("ancestor.txt");

        std::fs::write(&original_path, b"line 1\nline 2\nline 3\n")?;
        std::fs::write(&modified_path, b"line 1 modified\nline 2\nline 3\n")?;
        std::fs::write(&latest_path, b"line 1\nline 2 latest\nline 3\n")?;
        std::fs::write(&ancestor_path, b"line 1\nline 2\nline 3\n")?;

        let options = FileOptions::default();
        let diff = file_diff4(
            &original_path,
            &modified_path,
            &latest_path,
            &ancestor_path,
            options,
        )?;

        // Should have changes since modified and latest differ from original
        assert!(diff.contains_changes());

        Ok(())
    }
}