@@ -951,6 +951,61 @@ impl<'diff> DoubleEndedIterator for Deltas<'diff> {
951
951
}
952
952
impl < ' diff > ExactSizeIterator for Deltas < ' diff > { }
953
953
954
+ /// Line origin constants.
955
+ #[ derive( Copy , Clone , Debug , PartialEq ) ]
956
+ pub enum DiffLineType {
957
+ /// These values will be sent to `git_diff_line_cb` along with the line
958
+ Context ,
959
+ ///
960
+ Addition ,
961
+ ///
962
+ Deletion ,
963
+ /// Both files have no LF at end
964
+ ContextEOFNL ,
965
+ /// Old has no LF at end, new does
966
+ AddEOFNL ,
967
+ /// Old has LF at end, new does not
968
+ DeleteEOFNL ,
969
+ /// The following values will only be sent to a `git_diff_line_cb` when
970
+ /// the content of a diff is being formatted through `git_diff_print`.
971
+ FileHeader ,
972
+ ///
973
+ HunkHeader ,
974
+ /// For "Binary files x and y differ"
975
+ Binary ,
976
+ }
977
+
978
+ impl Binding for DiffLineType {
979
+ type Raw = raw:: git_diff_line_t ;
980
+ unsafe fn from_raw ( raw : raw:: git_diff_line_t ) -> Self {
981
+ match raw {
982
+ raw:: GIT_DIFF_LINE_CONTEXT => DiffLineType :: Context ,
983
+ raw:: GIT_DIFF_LINE_ADDITION => DiffLineType :: Addition ,
984
+ raw:: GIT_DIFF_LINE_DELETION => DiffLineType :: Deletion ,
985
+ raw:: GIT_DIFF_LINE_CONTEXT_EOFNL => DiffLineType :: ContextEOFNL ,
986
+ raw:: GIT_DIFF_LINE_ADD_EOFNL => DiffLineType :: AddEOFNL ,
987
+ raw:: GIT_DIFF_LINE_DEL_EOFNL => DiffLineType :: DeleteEOFNL ,
988
+ raw:: GIT_DIFF_LINE_FILE_HDR => DiffLineType :: FileHeader ,
989
+ raw:: GIT_DIFF_LINE_HUNK_HDR => DiffLineType :: HunkHeader ,
990
+ raw:: GIT_DIFF_LINE_BINARY => DiffLineType :: Binary ,
991
+ _ => panic ! ( "Unknown git diff line type" ) ,
992
+ }
993
+ }
994
+ fn raw ( & self ) -> raw:: git_diff_line_t {
995
+ match * self {
996
+ DiffLineType :: Context => raw:: GIT_DIFF_LINE_CONTEXT ,
997
+ DiffLineType :: Addition => raw:: GIT_DIFF_LINE_ADDITION ,
998
+ DiffLineType :: Deletion => raw:: GIT_DIFF_LINE_DELETION ,
999
+ DiffLineType :: ContextEOFNL => raw:: GIT_DIFF_LINE_CONTEXT_EOFNL ,
1000
+ DiffLineType :: AddEOFNL => raw:: GIT_DIFF_LINE_ADD_EOFNL ,
1001
+ DiffLineType :: DeleteEOFNL => raw:: GIT_DIFF_LINE_DEL_EOFNL ,
1002
+ DiffLineType :: FileHeader => raw:: GIT_DIFF_LINE_FILE_HDR ,
1003
+ DiffLineType :: HunkHeader => raw:: GIT_DIFF_LINE_HUNK_HDR ,
1004
+ DiffLineType :: Binary => raw:: GIT_DIFF_LINE_BINARY ,
1005
+ }
1006
+ }
1007
+ }
1008
+
954
1009
impl < ' a > DiffLine < ' a > {
955
1010
/// Line number in old file or `None` for added line
956
1011
pub fn old_lineno ( & self ) -> Option < u32 > {
@@ -988,6 +1043,12 @@ impl<'a> DiffLine<'a> {
988
1043
}
989
1044
}
990
1045
1046
+ /// origin of this `DiffLine`.
1047
+ ///
1048
+ pub fn origin_value ( & self ) -> DiffLineType {
1049
+ unsafe { Binding :: from_raw ( ( * self . raw ) . origin as raw:: git_diff_line_t ) }
1050
+ }
1051
+
991
1052
/// Sigil showing the origin of this `DiffLine`.
992
1053
///
993
1054
/// * ` ` - Line context
@@ -1475,7 +1536,7 @@ impl DiffPatchidOptions {
1475
1536
1476
1537
#[ cfg( test) ]
1477
1538
mod tests {
1478
- use crate :: { DiffOptions , Oid , Signature , Time } ;
1539
+ use crate :: { DiffLineType , DiffOptions , Oid , Signature , Time } ;
1479
1540
use std:: borrow:: Borrow ;
1480
1541
use std:: fs:: File ;
1481
1542
use std:: io:: Write ;
@@ -1726,4 +1787,28 @@ mod tests {
1726
1787
assert_eq ! ( line. trim( ) , "" )
1727
1788
}
1728
1789
}
1790
+
1791
+ #[ test]
1792
+ fn foreach_diff_line_origin_value ( ) {
1793
+ let foo_path = Path :: new ( "foo" ) ;
1794
+ let ( td, repo) = crate :: test:: repo_init ( ) ;
1795
+ t ! ( t!( File :: create( & td. path( ) . join( foo_path) ) ) . write_all( b"bar\n " ) ) ;
1796
+ let mut index = t ! ( repo. index( ) ) ;
1797
+ t ! ( index. add_path( foo_path) ) ;
1798
+ let mut opts = DiffOptions :: new ( ) ;
1799
+ opts. include_untracked ( true ) ;
1800
+ let diff = t ! ( repo. diff_tree_to_index( None , Some ( & index) , Some ( & mut opts) ) ) ;
1801
+ let mut origin_values: Vec < DiffLineType > = Vec :: new ( ) ;
1802
+ t ! ( diff. foreach(
1803
+ & mut |_file, _progress| { true } ,
1804
+ None ,
1805
+ None ,
1806
+ Some ( & mut |_file, _hunk, line| {
1807
+ origin_values. push( line. origin_value( ) ) ;
1808
+ true
1809
+ } )
1810
+ ) ) ;
1811
+ assert_eq ! ( origin_values. len( ) , 1 ) ;
1812
+ assert_eq ! ( origin_values[ 0 ] , DiffLineType :: Addition ) ;
1813
+ }
1729
1814
}
0 commit comments