diffy 0.5.0

Tools for finding and manipulating differences between files
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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
//! Tests for patchset parsing.

use alloc::borrow::ToOwned;
use alloc::string::ToString;
use alloc::vec::Vec;

use super::FileOperation;
use super::ParseOptions;
use super::PatchSet;
use super::error::PatchSetParseErrorKind;

mod file_operation {
    use super::*;

    #[test]
    fn test_strip_prefix() {
        let op = FileOperation::Modify {
            original: "a/src/lib.rs".to_owned().into(),
            modified: "b/src/lib.rs".to_owned().into(),
        };
        let stripped = op.strip_prefix(1);
        assert_eq!(
            stripped,
            FileOperation::Modify {
                original: "src/lib.rs".to_owned().into(),
                modified: "src/lib.rs".to_owned().into(),
            }
        );
    }

    #[test]
    fn test_strip_prefix_no_slash() {
        let op = FileOperation::Create("file.rs".to_owned().into());
        let stripped = op.strip_prefix(1);
        assert_eq!(stripped, FileOperation::Create("file.rs".to_owned().into()));
    }
}

mod patchset_unidiff {
    use super::*;

    #[test]
    fn single_file() {
        let content = "\
--- a/file.rs
+++ b/file.rs
@@ -1,3 +1,4 @@
 line1
 line2
+line3
 line4
";
        let patches = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 1);
        assert!(patches[0].operation().is_modify());
    }

    #[test]
    fn multi_file() {
        let content = "\
--- a/file1.rs
+++ b/file1.rs
@@ -1 +1 @@
-old1
+new1
--- a/file2.rs
+++ b/file2.rs
@@ -1 +1 @@
-old2
+new2
";
        let patches: Vec<_> = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<_, _>>()
            .unwrap();
        assert_eq!(patches.len(), 2);
        assert!(patches[0].operation().is_modify());
        assert!(patches[1].operation().is_modify());
    }

    #[test]
    fn with_preamble() {
        let content = "\
This is a preamble
It should be ignored
--- a/file.rs
+++ b/file.rs
@@ -1 +1 @@
-old
+new
";
        let patches = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 1);
        assert!(patches[0].operation().is_modify());
    }

    #[test]
    fn plus_plus_content_in_hunk() {
        // A hunk that adds a line whose content is literally "++ foo" renders
        // in the diff as "+++ foo" (the leading "+" is the add marker).
        // The parser must not treat this as a patch header boundary.
        let content = "\
--- a/file1.rs
+++ b/file1.rs
@@ -1,2 +1,2 @@
 line1
-old
+++ foo
--- a/file2.rs
+++ b/file2.rs
@@ -1 +1 @@
-a
+b
";
        let patches = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 2);
    }

    #[test]
    fn false_positive_in_hunk() {
        // Line starting with "--- " inside hunk is not a patch boundary.
        let content = "\
--- a/file.rs
+++ b/file.rs
@@ -1,3 +1,3 @@
 line1
---- this is not a patch boundary
+--- this line starts with dashes
 line3
";
        let patches = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 1);
    }

    #[test]
    fn empty_content() {
        let err: Result<Vec<_>, _> = PatchSet::parse("", ParseOptions::unidiff()).collect();
        let err = err.unwrap_err();
        assert!(
            err.to_string().contains("no valid patches found"),
            "unexpected error: {}",
            err
        );
    }

    #[test]
    fn not_a_patch() {
        let content = "Some random text\nNo patches here\n";
        let err: Result<Vec<_>, _> = PatchSet::parse(content, ParseOptions::unidiff()).collect();
        let err = err.unwrap_err();
        assert!(
            err.to_string().contains("no valid patches found"),
            "unexpected error: {}",
            err
        );
    }

    #[test]
    fn incomplete_header() {
        // Has --- but no following +++ or @@.
        // parse_one treats it as a valid (header-only, no hunks) patch,
        // consistent with how GNU patch handles lone headers.
        let content = "\
--- a/file.rs
Some random text
No patches here
";
        let patches = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 1);
        assert!(patches[0].operation().is_modify());
    }

    #[test]
    fn create_file() {
        let content = "\
--- /dev/null
+++ b/new.rs
@@ -0,0 +1 @@
+content
";
        let patches = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 1);
        assert!(patches[0].operation().is_create());
        assert_eq!(
            patches[0].operation(),
            &FileOperation::Create("b/new.rs".to_owned().into())
        );
    }

    #[test]
    fn delete_file() {
        let content = "\
--- a/old.rs
+++ /dev/null
@@ -1 +0,0 @@
-content
";
        let patches = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 1);
        assert!(patches[0].operation().is_delete());
        assert_eq!(
            patches[0].operation(),
            &FileOperation::Delete("a/old.rs".to_owned().into())
        );
    }

    #[test]
    fn different_paths() {
        let content = "\
--- a/old.rs
+++ b/new.rs
@@ -1 +1 @@
-old
+new
";
        let patches = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 1);
        assert_eq!(
            patches[0].operation(),
            &FileOperation::Modify {
                original: "a/old.rs".to_owned().into(),
                modified: "b/new.rs".to_owned().into(),
            }
        );
    }

    #[test]
    fn both_dev_null_error() {
        let content = "\
--- /dev/null
+++ /dev/null
@@ -1 +1 @@
-old
+new
";
        let result: Result<Vec<_>, _> = PatchSet::parse(content, ParseOptions::unidiff()).collect();
        assert_eq!(
            result.unwrap_err().kind,
            PatchSetParseErrorKind::BothDevNull
        );
    }

    #[test]
    fn error_advances_past_bad_patch() {
        // Iterator advances past a malformed patch and continues
        // to yield subsequent valid patches (GNU patch behavior).
        let content = "\
--- /dev/null
+++ /dev/null
@@ -1 +1 @@
-old
+new
--- a/file.rs
+++ b/file.rs
@@ -1 +1 @@
-old
+new
";
        let items: Vec<_> = PatchSet::parse(content, ParseOptions::unidiff()).collect();
        assert_eq!(items.len(), 2);
        assert!(items[0].is_err(), "first item should be the error");
        assert!(items[1].is_ok(), "second item should be the valid patch");
    }

    #[test]
    fn diff_git_ignored_in_unidiff_mode() {
        // In UniDiff mode, `diff --git` is noise before `---` boundary.
        let content = "\
diff --git a/file1.rs b/file1.rs
--- a/file1.rs
+++ b/file1.rs
@@ -1 +1 @@
-old1
+new1
diff --git a/file2.rs b/file2.rs
--- a/file2.rs
+++ b/file2.rs
@@ -1 +1 @@
-old2
+new2
";
        let patches = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 2);
    }

    #[test]
    fn git_format_patch() {
        // Full git format-patch output with email headers and signature.
        let content = "\
From 1234567890abcdef1234567890abcdef12345678 Mon Sep 17 00:00:00 2001
From: Gandalf <gandalf@the.grey>
Date: Mon, 25 Mar 3019 00:00:00 +0000
Subject: [PATCH] fix!: destroy the one ring at mount doom

In a hole in the ground there lived a hobbit
---
 src/frodo.rs | 2 +-
 src/sam.rs   | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

--- a/src/frodo.rs
+++ b/src/frodo.rs
@@ -1 +1 @@
-finger
+peace
--- a/src/sam.rs
+++ b/src/sam.rs
@@ -1 +1,2 @@
 food
+more food
--
2.40.0
";
        let patches = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 2);
        assert!(patches[0].operation().is_modify());
        assert!(patches[1].operation().is_modify());
    }

    #[test]
    fn missing_modified_header() {
        // Only --- header, no +++ header.
        let content = "\
--- a/file.rs
@@ -1 +1 @@
-old
+new
";
        let patches = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 1);
        assert!(patches[0].operation().is_modify());
    }

    #[test]
    fn missing_original_header() {
        // Only +++ header, no --- header.
        let content = "\
+++ b/file.rs
@@ -1 +1 @@
-old
+new
";
        let patches = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 1);
        assert!(patches[0].operation().is_modify());
    }

    #[test]
    fn reversed_header_order() {
        // +++ before ---.
        let content = "\
+++ b/file.rs
--- a/file.rs
@@ -1 +1 @@
-old
+new
";
        let patches = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 1);
        assert!(patches[0].operation().is_modify());
    }

    #[test]
    fn multi_file_mixed_headers() {
        // Various combinations of missing headers.
        let content = "\
--- a/file1.rs
+++ b/file1.rs
@@ -1 +1 @@
-old1
+new1
--- a/file2.rs
@@ -1 +1 @@
-old2
+new2
+++ b/file3.rs
@@ -1 +1 @@
-old3
+new3
";
        let patches = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 3);
    }

    #[test]
    fn missing_modified_uses_original() {
        // When +++ is missing, original path is used for both.
        let content = "\
--- a/file.rs
@@ -1 +1 @@
-old
+new
";
        let patches = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(
            patches[0].operation(),
            &FileOperation::Modify {
                original: "a/file.rs".to_owned().into(),
                modified: "a/file.rs".to_owned().into(),
            }
        );
    }

    #[test]
    fn missing_original_uses_modified() {
        // When --- is missing, modified path is used for both.
        let content = "\
+++ b/file.rs
@@ -1 +1 @@
-old
+new
";
        let patches = PatchSet::parse(content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(
            patches[0].operation(),
            &FileOperation::Modify {
                original: "b/file.rs".to_owned().into(),
                modified: "b/file.rs".to_owned().into(),
            }
        );
    }

    #[test]
    fn hunk_only_no_headers() {
        // Only @@ header, no --- or +++ paths.
        // is_unidiff_boundary requires --- or +++ to identify patch start,
        // so this is not recognized as a patch at all.
        let content = "\
@@ -1 +1 @@
-old
+new
";
        let err: Result<Vec<_>, _> = PatchSet::parse(content, ParseOptions::unidiff()).collect();
        let err = err.unwrap_err();
        assert!(
            err.to_string().contains("no valid patches found"),
            "unexpected error: {}",
            err
        );
    }
}

mod patchset_gitdiff {
    use super::*;
    fn parse_gitdiff(input: &str) -> Vec<super::super::FilePatch<'_, str>> {
        PatchSet::parse(input, ParseOptions::gitdiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap()
    }

    /// Paths sharing trailing UTF-8 continuation bytes
    /// but belonging to different codepoints must not panic
    #[test]
    fn multibyte_char_boundary_in_diff_git_path() {
        // U+00BF INVERTED QUESTION MARK (¿) = 0xC2 0xBF
        // U+00FF LATIN SMALL LETTER Y WITH DIAERESIS (ÿ) = 0xC3 0xBF
        // Both share trailing byte 0xBF.
        let input = "diff --git a/\u{bf} b/\u{ff}\n";
        let result: Result<Vec<_>, _> = PatchSet::parse(input, ParseOptions::gitdiff()).collect();
        assert_eq!(
            result.unwrap_err().kind,
            PatchSetParseErrorKind::InvalidDiffGitPath
        );
    }

    /// `parse_one` must stop at `diff --git` boundaries so that
    /// back-to-back patches are split correctly.
    /// Without this, the second patch's `diff --git` line would be
    /// swallowed as trailing junk by the first patch's hunk parser.
    #[test]
    fn multi_file_stops_at_diff_git_boundary() {
        let input = "\
diff --git a/foo b/foo
--- a/foo
+++ b/foo
@@ -1 +1 @@
-old foo
+new foo
diff --git a/bar b/bar
--- a/bar
+++ b/bar
@@ -1 +1 @@
-old bar
+new bar
";
        let patches = parse_gitdiff(input);
        assert_eq!(patches.len(), 2);
    }

    #[test]
    fn pure_rename() {
        let input = "\
diff --git a/old.rs b/new.rs
similarity index 100%
rename from old.rs
rename to new.rs
";
        let patches = parse_gitdiff(input);
        assert_eq!(patches.len(), 1);
        assert_eq!(
            patches[0].operation(),
            &FileOperation::Rename {
                from: "old.rs".into(),
                to: "new.rs".into(),
            }
        );
    }

    /// Empty file creation has no ---/+++ headers, so the path comes
    /// from the `diff --git` line and retains the `b/` prefix.
    /// Callers use `strip_prefix(1)` to remove it.
    #[test]
    fn new_empty_file() {
        let input = "\
diff --git a/empty b/empty
new file mode 100644
index 0000000..e69de29
";
        let patches = parse_gitdiff(input);
        assert_eq!(patches.len(), 1);
        assert_eq!(
            patches[0].operation(),
            &FileOperation::Create("b/empty".into())
        );
        let p = patches[0].patch().as_text().unwrap();
        assert!(p.hunks().is_empty());
    }

    #[test]
    fn rename_then_modify() {
        // Rename with no hunks followed by a modify with hunks.
        // Tests that offset advances correctly across both.
        let input = "\
diff --git a/old.rs b/new.rs
similarity index 100%
rename from old.rs
rename to new.rs
diff --git a/foo b/foo
--- a/foo
+++ b/foo
@@ -1 +1 @@
-old
+new
";
        let patches = parse_gitdiff(input);
        assert_eq!(patches.len(), 2);
        assert!(matches!(
            patches[0].operation(),
            FileOperation::Rename { .. }
        ));
        assert!(matches!(
            patches[1].operation(),
            FileOperation::Modify { .. }
        ));
    }

    /// Quoted path containing an escaped quote (`\"`).
    /// Git produces this for filenames with literal double quotes.
    ///
    /// Observed with git 2.53.0:
    ///   $ printf 'x' > 'with"quote' && git add -A
    ///   $ git diff --cached | head -1
    ///   diff --git "a/with\"quote" "b/with\"quote"
    #[test]
    fn path_quoted_with_escaped_quote() {
        let input = "\
diff --git \"a/with\\\"quote\" \"b/with\\\"quote\"
--- \"a/with\\\"quote\"
+++ \"b/with\\\"quote\"
@@ -1 +1 @@
-old
+new
";
        let patches = parse_gitdiff(input);
        assert_eq!(patches.len(), 1);
        assert_eq!(
            patches[0].operation(),
            &FileOperation::Modify {
                original: "a/with\"quote".to_owned().into(),
                modified: "b/with\"quote".to_owned().into(),
            }
        );
    }

    /// Copy operation extracted from git extended headers.
    #[test]
    fn copy_operation() {
        let input = "\
diff --git a/original.rs b/copied.rs
similarity index 100%
copy from original.rs
copy to copied.rs
";
        let patches = parse_gitdiff(input);
        assert_eq!(patches.len(), 1);
        assert_eq!(
            patches[0].operation(),
            &FileOperation::Copy {
                from: "original.rs".into(),
                to: "copied.rs".into(),
            }
        );
    }

    /// Rename with both paths quoted (escapes in both).
    #[test]
    fn rename_both_quoted() {
        let input = "\
diff --git \"a/foo\\tbar.rs\" \"b/baz\\tqux.rs\"
similarity index 100%
rename from \"foo\\tbar.rs\"
rename to \"baz\\tqux.rs\"
";
        let patches = parse_gitdiff(input);
        assert_eq!(patches.len(), 1);
        assert_eq!(
            patches[0].operation(),
            &FileOperation::Rename {
                from: "foo\tbar.rs".into(),
                to: "baz\tqux.rs".into(),
            }
        );
    }

    /// Rename from quoted (has escape) to unquoted (plain).
    #[test]
    fn rename_quoted_to_unquoted() {
        let input = "\
diff --git \"a/foo\\tbar.rs\" b/normal.rs
similarity index 100%
rename from \"foo\\tbar.rs\"
rename to normal.rs
";
        let patches = parse_gitdiff(input);
        assert_eq!(patches.len(), 1);
        assert_eq!(
            patches[0].operation(),
            &FileOperation::Rename {
                from: "foo\tbar.rs".into(),
                to: "normal.rs".into(),
            }
        );
    }

    /// Rename from unquoted to quoted (has escape).
    #[test]
    fn rename_unquoted_to_quoted() {
        let input = "\
diff --git a/normal.rs \"b/foo\\tbar.rs\"
similarity index 100%
rename from normal.rs
rename to \"foo\\tbar.rs\"
";
        let patches = parse_gitdiff(input);
        assert_eq!(patches.len(), 1);
        assert_eq!(
            patches[0].operation(),
            &FileOperation::Rename {
                from: "normal.rs".into(),
                to: "foo\tbar.rs".into(),
            }
        );
    }

    /// Deleted file: `deleted file mode` header + /dev/null in +++.
    #[test]
    fn deleted_file_with_mode() {
        let input = "\
diff --git a/gone.rs b/gone.rs
deleted file mode 100644
index abc1234..0000000
--- a/gone.rs
+++ /dev/null
@@ -1 +0,0 @@
-content
";
        let patches = parse_gitdiff(input);
        assert_eq!(patches.len(), 1);
        assert!(patches[0].operation().is_delete());
        assert_eq!(
            patches[0].old_mode(),
            Some(&super::super::FileMode::Regular)
        );
    }

    /// Mode-only change: no hunks, no ---/+++ headers.
    /// File operation falls back to `diff --git` line paths.
    #[test]
    fn mode_only_change() {
        let input = "\
diff --git a/script.sh b/script.sh
old mode 100644
new mode 100755
";
        let patches = parse_gitdiff(input);
        assert_eq!(patches.len(), 1);
        assert!(patches[0].operation().is_modify());
        assert_eq!(
            patches[0].old_mode(),
            Some(&super::super::FileMode::Regular),
        );
        assert_eq!(
            patches[0].new_mode(),
            Some(&super::super::FileMode::Executable),
        );
        let p = patches[0].patch().as_text().unwrap();
        assert!(p.hunks().is_empty());
    }

    /// New file with content: `new file mode` header + /dev/null in ---.
    #[test]
    fn new_file_with_content() {
        let input = "\
diff --git a/new.rs b/new.rs
new file mode 100644
index 0000000..abc1234
--- /dev/null
+++ b/new.rs
@@ -0,0 +1 @@
+hello
";
        let patches = parse_gitdiff(input);
        assert_eq!(patches.len(), 1);
        assert!(patches[0].operation().is_create());
        assert_eq!(
            patches[0].new_mode(),
            Some(&super::super::FileMode::Regular),
        );
    }

    /// `diff --git` line with no-prefix paths (`git diff --no-prefix`).
    /// Fallback path parsing works when ---/+++ are absent.
    #[test]
    fn no_prefix_empty_file() {
        let input = "\
diff --git file.rs file.rs
new file mode 100644
index 0000000..e69de29
";
        let patches = parse_gitdiff(input);
        assert_eq!(patches.len(), 1);
        assert!(patches[0].operation().is_create());
    }

    #[test]
    fn binary_marker_kept_by_default() {
        // Default is Keep: binary marker is returned as BinaryPatch::Marker.
        let input = "\
diff --git a/img.png b/img.png
Binary files a/img.png and b/img.png differ
diff --git a/foo b/foo
--- a/foo
+++ b/foo
@@ -1 +1 @@
-old
+new
";
        let patches = parse_gitdiff(input);
        assert_eq!(patches.len(), 2);
        assert!(patches[0].patch().as_binary().is_some());
        assert!(patches[0].operation().is_modify());
        assert!(patches[1].patch().as_text().is_some());
    }
}

mod patchset_unidiff_bytes {
    use super::*;
    use crate::patch::Line;

    #[test]
    fn single_file_bytes() {
        let content = b"\
--- a/file.rs
+++ b/file.rs
@@ -1 +1 @@
-old
+new
";
        let patches = PatchSet::parse_bytes(content.as_slice(), ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 1);
        assert!(patches[0].operation().is_modify());
    }

    #[test]
    fn non_utf8_hunk_content() {
        // Simulate a patch where hunk content has non-UTF-8 bytes.
        // This is the primary use case for parse_bytes: git may produce
        // text-format hunks for files it misdetects as text (e.g. small
        // PNGs without NUL bytes).
        let mut content = Vec::new();
        content.extend_from_slice(b"--- a/icon.png\n");
        content.extend_from_slice(b"+++ b/icon.png\n");
        content.extend_from_slice(b"@@ -1 +1 @@\n");
        content.extend_from_slice(b"-old\x89PNG\n");
        content.extend_from_slice(b"+new\x89PNG\n");

        let patches = PatchSet::parse_bytes(&content, ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 1);

        let patch = patches[0].patch().as_text().unwrap();
        let lines = patch.hunks()[0].lines();
        assert_eq!(lines[0], Line::Delete(b"old\x89PNG\n".as_slice()));
        assert_eq!(lines[1], Line::Insert(b"new\x89PNG\n".as_slice()));
    }

    #[test]
    fn multi_file_bytes() {
        let content = b"\
--- a/file1.rs
+++ b/file1.rs
@@ -1 +1 @@
-old1
+new1
--- a/file2.rs
+++ b/file2.rs
@@ -1 +1 @@
-old2
+new2
";
        let patches = PatchSet::parse_bytes(content.as_slice(), ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 2);
    }

    #[test]
    fn create_file_bytes() {
        let content = b"\
--- /dev/null
+++ b/new.rs
@@ -0,0 +1 @@
+content
";
        let patches = PatchSet::parse_bytes(content.as_slice(), ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 1);
        assert!(patches[0].operation().is_create());
        assert_eq!(
            patches[0].operation(),
            &FileOperation::Create(b"b/new.rs".to_vec().into())
        );
    }

    #[test]
    fn delete_file_bytes() {
        let content = b"\
--- a/old.rs
+++ /dev/null
@@ -1 +0,0 @@
-content
";
        let patches = PatchSet::parse_bytes(content.as_slice(), ParseOptions::unidiff())
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(patches.len(), 1);
        assert!(patches[0].operation().is_delete());
        assert_eq!(
            patches[0].operation(),
            &FileOperation::Delete(b"a/old.rs".to_vec().into())
        );
    }
}