@@ -1335,6 +1335,19 @@ mod tests {
13351335 }
13361336 }
13371337
1338+ fn large_diff_fixture ( label : & str ) -> String {
1339+ ( 0 ..2_105 )
1340+ . map ( |index| {
1341+ let marker = if index < 3 || index >= 2_102 {
1342+ "context"
1343+ } else {
1344+ label
1345+ } ;
1346+ format ! ( "{marker}-{index:04}-{}\n " , "x" . repeat( 240 ) )
1347+ } )
1348+ . collect ( )
1349+ }
1350+
13381351 #[ test]
13391352 fn test_commit_navigation_respects_order_and_reset ( ) {
13401353 let test_repo = TestRepo :: new ( ) ;
@@ -1838,4 +1851,114 @@ mod tests {
18381851 let diff = now. signed_duration_since ( result. date ) ;
18391852 assert ! ( diff. num_seconds( ) < 60 ) ;
18401853 }
1854+
1855+ #[ test]
1856+ fn test_get_commit_omits_oversized_blob_contents_and_keeps_context_lines ( ) {
1857+ let test_repo = TestRepo :: new ( ) ;
1858+ let original = large_diff_fixture ( "before" ) ;
1859+ let updated = large_diff_fixture ( "after" ) ;
1860+ assert ! ( original. len( ) > MAX_BLOB_SIZE ) ;
1861+ assert ! ( updated. len( ) > MAX_BLOB_SIZE ) ;
1862+
1863+ test_repo. commit_file (
1864+ "large.txt" ,
1865+ & original,
1866+ "Alice Example" ,
1867+ 1868+ 1_700_004_000 ,
1869+ "large base" ,
1870+ ) ;
1871+ let updated_hash = test_repo. commit_file (
1872+ "large.txt" ,
1873+ & updated,
1874+ "Bob Example" ,
1875+ 1876+ 1_700_004_060 ,
1877+ "large update" ,
1878+ ) ;
1879+
1880+ let repo = GitRepository :: open ( & test_repo. path ) . unwrap ( ) ;
1881+ let metadata = repo. get_commit ( & updated_hash) . unwrap ( ) ;
1882+ let change = & metadata. changes [ 0 ] ;
1883+ let context_lines = change
1884+ . hunks
1885+ . iter ( )
1886+ . flat_map ( |hunk| hunk. lines . iter ( ) )
1887+ . filter ( |line| matches ! ( line. change_type, LineChangeType :: Context ) )
1888+ . count ( ) ;
1889+
1890+ assert_eq ! ( change. path, "large.txt" ) ;
1891+ assert ! ( change. old_content. is_none( ) ) ;
1892+ assert ! ( change. new_content. is_none( ) ) ;
1893+ assert ! ( change. is_excluded) ;
1894+ assert ! ( change
1895+ . exclusion_reason
1896+ . as_deref( )
1897+ . is_some_and( |reason| reason. contains( "too many changes" ) ) ) ;
1898+ assert ! ( context_lines >= 6 ) ;
1899+ }
1900+
1901+ #[ test]
1902+ fn test_working_tree_diff_omits_oversized_index_and_workdir_contents ( ) {
1903+ let test_repo = TestRepo :: new ( ) ;
1904+ let large = large_diff_fixture ( "staged" ) ;
1905+ let updated = large_diff_fixture ( "workdir" ) ;
1906+ let file = test_repo. path . join ( "large.txt" ) ;
1907+
1908+ test_repo. commit_file (
1909+ "large.txt" ,
1910+ "small\n " ,
1911+ "Alice Example" ,
1912+ 1913+ 1_700_005_000 ,
1914+ "small base" ,
1915+ ) ;
1916+
1917+ std:: fs:: write ( & file, & large) . unwrap ( ) ;
1918+ let mut index = test_repo. repo . index ( ) . unwrap ( ) ;
1919+ index. add_path ( std:: path:: Path :: new ( "large.txt" ) ) . unwrap ( ) ;
1920+ index. write ( ) . unwrap ( ) ;
1921+
1922+ let repo = GitRepository :: open ( & test_repo. path ) . unwrap ( ) ;
1923+ let staged = repo. get_working_tree_diff ( DiffMode :: Staged ) . unwrap ( ) ;
1924+ let staged_change = & staged. changes [ 0 ] ;
1925+ assert_eq ! ( staged_change. old_content. as_deref( ) , Some ( "small\n " ) ) ;
1926+ assert ! ( staged_change. new_content. is_none( ) ) ;
1927+ assert ! ( staged_change. is_excluded) ;
1928+
1929+ test_repo. commit_file (
1930+ "large.txt" ,
1931+ & large,
1932+ "Bob Example" ,
1933+ 1934+ 1_700_005_060 ,
1935+ "large base" ,
1936+ ) ;
1937+
1938+ std:: fs:: write ( & file, "small again\n " ) . unwrap ( ) ;
1939+ let mut index = test_repo. repo . index ( ) . unwrap ( ) ;
1940+ index. add_path ( std:: path:: Path :: new ( "large.txt" ) ) . unwrap ( ) ;
1941+ index. write ( ) . unwrap ( ) ;
1942+
1943+ let repo = GitRepository :: open ( & test_repo. path ) . unwrap ( ) ;
1944+ let staged_from_large = repo. get_working_tree_diff ( DiffMode :: Staged ) . unwrap ( ) ;
1945+ let staged_from_large_change = & staged_from_large. changes [ 0 ] ;
1946+ assert ! ( staged_from_large_change. old_content. is_none( ) ) ;
1947+ assert_eq ! (
1948+ staged_from_large_change. new_content. as_deref( ) ,
1949+ Some ( "small again\n " )
1950+ ) ;
1951+
1952+ std:: fs:: write ( & file, & updated) . unwrap ( ) ;
1953+
1954+ let repo = GitRepository :: open ( & test_repo. path ) . unwrap ( ) ;
1955+ let unstaged = repo. get_working_tree_diff ( DiffMode :: Unstaged ) . unwrap ( ) ;
1956+ let unstaged_change = & unstaged. changes [ 0 ] ;
1957+ assert_eq ! (
1958+ unstaged_change. old_content. as_deref( ) ,
1959+ Some ( "small again\n " )
1960+ ) ;
1961+ assert ! ( unstaged_change. new_content. is_none( ) ) ;
1962+ assert ! ( unstaged_change. is_excluded) ;
1963+ }
18411964}
0 commit comments